A single Student class that has i. Person instance variable, read-only, cannot b
ID: 3642957 • Letter: A
Question
A single Student class that hasi. Person instance variable, read-only, cannot be null
**uses Person class below
ii. String student Id variable, read only, cannot be null or 0-length
iii. Int gradtuationYear variable, read-write, must be greater than 2000
iv. Public constructor taking as input a person, string and int
v. no other public constructors
public class Person {
private String firstName;
private String lastName;
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName (String lastNameChange){
if (lastName != null && lastName.length() > 0) {
this.lastName = lastNameChange;
}
}
public Person (String firstName, String lastName) throws Exception{
if (firstName == null || firstName.length() == 0 || lastName == null || lastName.length() == 0) {
throw new Exception("Bad entry");
}
}
private Person(){
}
}
Explanation / Answer
Hi,
Please find below the Student class as per your requirement. Student takes a 3 arg constructor, check for person not to be null object, Id not to be zero length or null and year not less than 2000. Id and Person are read only.
package introtoprg;
public class Student {
Person person;
String studentId;
Integer graduationYear;
public Student(Person p, String id, int year) throws Exception {
if (p == null || id == null || id == "" || year < 2000)
throw new Exception("Bad Entry");
this.person = p;
this.studentId = id;
this.graduationYear = year;
}
public Integer getGraduationYear() {
return graduationYear;
}
public Person getPerson() {
return person;
}
public String getStudentId() {
return studentId;
}
public void setGraduationYear(Integer graduationYear) {
this.graduationYear = graduationYear;
}
}
Hope it helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.