Objects Implementation (Phase 2) Student.java contains the definition of a class
ID: 3692099 • 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
I had not implemented for courses.
package student;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.exit;
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
String firstName,lastName;
int id,gradYear;
Scanner sc1=new Scanner(System.in);
public Student(String firstName, String lastName, int id, int gradYear){
this.firstName=firstName;
this.lastName=lastName;
if(id >=100000000 && id <= 999999999)
this.id=id;
else
System.out.println(""id" must be in the range 100000000-999999999(both include)");
if(gradYear >=1900 && gradYear <= 2050)
this.gradYear=gradYear;
else
System.out.println(""year" must be in the range 1900-2050 (both includes)");
}
public String getFirstName(){
return firstName;
} //returns the student’s first name
public String getLastName(){
return lastName;
} //returns the student’s last name
public int getId(){
return id;
} //returns the student’s ID
public int getGradYear(){
return gradYear;
} //returns the student’s graduation year
public String[] getSchedule(){
String a[]=new String[6];
for(int k=0;k<a.length;k++){
System.out.println("Enter course "+k);
a[k]=sc1.nextLine();
}
return a;
}
@Override
public String toString(){
return (Integer.toString(this.id)+" :"+ this.lastName+","+this.firstName +"-" +Integer.toString(this.gradYear));
}
public static void main(String[] args) throws IOException {
int i;
Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Student s = null;
Student std[]=new Student[6];
String fn,ln;
int id,gy;
System.out.println("How many students are ther in the class :");
i=sc.nextInt();
for(int j=1;j <= i;j++)
{
System.out.print("enter student["+j+"] first name :");
fn=br.readLine();
System.out.print(" enter student ["+j+"] last name :");
ln=br.readLine();
System.out.print(" enter student ["+j+"] id :");
id=sc.nextInt();
System.out.print(" enter student ["+j+"] Graduation year :");
gy=sc.nextInt();
// std.getSchedule();
if((id >=100000000 && id <= 999999999)&&(gy >=1900 && gy <= 2050)){
std[j-1]=new Student(fn,ln,id,gy);
//System.out.println(std.toString());
}
else{
System.out.println(""id" must be in the range 100000000-999999999(both include) AND");
System.out.println(""year" must be in the range 1900-2050 (both includes)");
exit(0);
}
}
for(int k=0;k<i;k++){
System.out.println(std[k].toString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.