In: Computer Science
In this assignment you are going to create a Student class to demonstrate a Student object. Then create a StudentTest class to place the main method in, which demonstrates the Student object.
Student class should have the following instance
variables:
Student class should have the following
methods:
Design the application in main that you create 10 student
objects in main. You don’t need to get the input from the user;
just provide some values you prefer. Also, create an ArrayList of
type Student in main.
Once you create 10 student objects, add these objects into the ArrayList. Using enhanced for loop (which we sometimes call foreach loop), display the information of every student in your ArrayList
//file Student.java
public class Student{
private String firstName;
private String middleName;
private String lastName;
private int id;
private int grade;
public Student(String fname_input,String mname_input,
String lname_input,int id_input,int grade_input){
setFirstName(fname_input);
setLastName(lname_input);
setMiddleName(mname_input);
setGrade(grade_input);
setId(id_input);
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getMiddleName(){
return middleName;
}
public int getId(){
return id;
}
public int getGrade(){
return grade;
}
public void setFirstName(String fname_input){
firstName=fname_input;
}
public void setLastName(String lname_input){
lastName=lname_input;
}
public void setMiddleName(String mname_input){
middleName=mname_input;
}
public void setId(int id_input){
id=id_input;
}
public void setGrade(int grade_input){
grade=grade_input;
}
}
//file StudentTest.java
import java.util.ArrayList;
public class StudentTest{
public static void main(String[] args) {
ArrayList<Student> studentsList = new ArrayList<Student>();
studentsList.add(new Student("Jon","","Doe",32,5));
studentsList.add(new Student("Angus","Alec","Homer",55,3));
studentsList.add(new Student("Otis","","Milburn",5,7));
studentsList.add(new Student("Jon","","Snow",69,1));
studentsList.add(new Student("Bran","","Stark",56,6));
studentsList.add(new Student("Khal","","Drogo",43,5));
studentsList.add(new Student("Broon","","",5,4));
studentsList.add(new Student("Maeve","","Wiley",22,3));
studentsList.add(new Student("Aimee","","Gibbs",44,3));
studentsList.add(new Student("Ruby","","",31,1));
for (Student s : studentsList)
{
System.out.println("Name: " + s.getFirstName()+" "+s.getMiddleName()+" "+s.getLastName());
System.out.println("Id: " + s.getId());
System.out.println("Grade: " + s.getGrade()+"\n");
}
}
}
Add the codes to Student.java and StudentTest.java files respectively.
Commands to run the code:
javac StudentTest.java Student.java
java StudentTest
The 1st command compiles the code files and the 2nd commands runs it.