Define a Java class named Student in a package named STUDENT
Define a Java class named “Student” in a package named “STUDENT”. This class has three attributes:
Student ID (an integer of 10 digits)
Full name (first last - of String type)
Major (a string)
Class Student definition provides a default constructor, get-method and set-method for each attribute, and a public method to print out all information of the student in one line.
PART II:
Write a Java program that can store data of three students in an array and display their data, each student in one line. The user enters all three student’s data from the console. It is assumed that the user does not make any mistake while entering students’ data.
The Java program should be another Java class named “StudentDisplayer” in the same package, i.e. STUDENT. The program creates an array of three elements of class Student type. After reading every piece of data of each student from the console, the program assigns the input to the corresponding element of the array. When all the data of the three students have been entered and correctly stored into the array, the program displays their data, each in one line, by invoking a public method of class Student.
Solution
StudentDisplayer.java
import java.util.Scanner;
public class StudentDisplayer {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Student stu[] = new Student[3];
for(int i=0; i<stu.length; i++){
System.out.print(\"Enter Student ID: \");
int id = scan.nextInt();
scan.nextLine();
System.out.print(\"Enter Stundet Full Name: \");
String name = scan.nextLine();
System.out.print(\"Enter Student Major: \");
String major = scan.next();
scan.nextLine();
Student s = new Student();
s.setFullName(name);
s.setID(id);
s.setMajor(major);
stu[i] = s;
}
System.out.println(\"Student details are: \");
for(int i=0; i<stu.length; i++){
System.out.println(stu[i].toString());
}
}
}
Student.java
public class Student {
private int ID;
private String fullName;
private String major;
public Student(){
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String toString(){
return \"Student ID: \"+getID()+\" Full Name: \"+getFullName()+\" Major: \"+getMajor();
}
}
Output:
Enter Student ID: 1111
Enter Stundet Full Name: Suresh Murapaka
Enter Student Major: Computer Science
Enter Student ID: 2222
Enter Stundet Full Name: Sekhar Murapaka
Enter Student Major: Physics
Enter Student ID: 3333
Enter Stundet Full Name: Anshu Murapaka
Enter Student Major: Chemistry
Student details are:
Student ID: 1111 Full Name: Suresh Murapaka Major: Computer
Student ID: 2222 Full Name: Sekhar Murapaka Major: Physics
Student ID: 3333 Full Name: Anshu Murapaka Major: Chemistry

