Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objects Implementation (Phase 2) Student.java contains the definition of a class

ID: 3689913 • Letter: O

Question

Objects Implementation (Phase 2)

Student.java contains the definition of a class called Student. A Student object represents an entry for a student in a class. Each Student object keeps track of the student’s first name, last name, student id, and graduation year. The Student object also keeps track of the student’s schedule – a list of classes the student is currently enrolled in.

*****Implement Student.java.*****

Make sure all of your fields are private and your method signatures are written exactly as defined below.

*You can assume that all parameters being passed into the constructors will be in their valid ranges.*

*You can also assume that the objects passed into the ‘equals’ or ‘compareTo’ methods will NOT be null.”

Student.java :

public Student(String firstName, String lastName, int id, int gradYear)

id must be 9 digits (valid range: 100000000-999999999)

graduation year ranges from 1900-2050

each student maintains a list (array) of courses – maximum number of courses for a student is 6

getter methods for all of the variables passed into the constructor:

public String getFirstName() //returns the student’s first name

public String getLastName() //returns the student’s last name

public int getId() //returns the student’s ID

public int getGradYear() //returns the student’s graduation year

public Course[] getSchedule() //returns the student’s schedule – list of courses a student is taking

public String toString()return the student in the following format: “id: lastName, firstName - gradYear

ex: 123456789: Smith, Joe - 2020

public boolean equals(Student other)

two students are considered equal if their IDs are the same

Explanation / Answer

Student.java


public class Student {
   private String firstName;
   private String lastName;
   private int id;
   private int gradYear;
   Course courses[] = new Course[6];
   public Student(String firstName, String lastName, int id, int gradYear){
       this.firstName = firstName;
       this.lastName = lastName;
       this.id = id;
       this.gradYear = gradYear;
   }
   public String getFirstName() {
       return firstName;
   }
   public String getLastName() {
       return lastName;
   }
   public int getId() {
       return id;
   }
   public int getGradYear() {
       return gradYear;
   }
   public Course[] getSchedule() {
       return courses;
   }
   public void setSchedule(Course[] courses) {
       this.courses = courses;
   }
   public String toString() {
       return id+": "+firstName+", "+lastName+" - "+gradYear;
   }
   public boolean equals(Student other) {
       if(this.id == other.id){
           return true;  
       }
       else{
           return false;
       }
   }
}

class Course {
   String courseName;
   public String getCourseName() {
       return courseName;
   }
   public void setCourseName(String courseName) {
       this.courseName = courseName;
   }
   public Course(String courseName){
       this.courseName = courseName;
   }
}

StudentTest.java


import java.util.Scanner;

public class StudentTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner scanner = new Scanner(System.in);
       System.out.println("Please enter first name");
       String firstName1 = scanner.next();
       System.out.println("Please enter last name");
       String lastName1 = scanner.next();
       System.out.println("Please enter id");
       int id1 = scanner.nextInt();
       System.out.println("Please enter grade year");
       int gradYear1 = scanner.nextInt();
       Course courses[] = new Course[6];
       System.out.println("Enter 6 Courses");
       for(int i=0; i<6; i++){
           courses[i] = new Course(scanner.next());
       }
       Student s1 = new Student(firstName1, lastName1, id1, gradYear1);
       s1.setSchedule(courses);
       System.out.println(s1.toString());
       Course[] courses1 = s1.getSchedule();
       for(int i=0; i<courses1.length; i++){
           System.out.println(courses1[i].getCourseName());
       }       System.out.println("Please enter first name");
       String firstName2 = scanner.next();
       System.out.println("Please enter last name");
       String lastName2 = scanner.next();
       System.out.println("Please enter id");
       int id2 = scanner.nextInt();
       System.out.println("Please enter grade year");
       int gradYear2 = scanner.nextInt();
       for(int i=0; i<6; i++){
           System.out.println("Enter 6 Courses");
           courses[i] = new Course(scanner.next());
       }
       Student s2 = new Student(firstName2, lastName2, id2, gradYear2);
       s2.setSchedule(courses);
       System.out.println(s2.toString());
       Course[] courses2 = s2.getSchedule();
       for(int i=0; i<courses2.length; i++){
           System.out.println(courses2[i].getCourseName());
       }      
       System.out.println("Both Students IDs Equal: "+s1.equals(s2));
   }

}

Output:

Please enter first name
Smith
Please enter last name
Joe
Please enter id
123456789
Please enter grade year
2020
Enter 6 Courses
Maths
Physics
Chemistry
Biology
English
Social
123456789: Smith, Joe - 2020
Maths
Physics
Chemistry
Biology
English
Social
Please enter first name
John
Please enter last name
Dae
Please enter id
123456790
Please enter grade year
2021
Enter 6 Courses
Maths
Enter 6 Courses
English
Enter 6 Courses
Science
Enter 6 Courses
Biology
Enter 6 Courses
Social
Enter 6 Courses
Accounts
123456790: John, Dae - 2021
Maths
English
Science
Biology
Social
Accounts
Both Students IDs Equal: false