Complete the following code sections. //all methods that are needed are already
ID: 3642329 • Letter: C
Question
Complete the following code sections.//all methods that are needed are already here, do not add any methods just complete what is here
public class Person {
private String firstName; //ensure this can never be null or zero length
private String lastName; //ensure this can never be null or zero length
public String getFirstName(){
//complete this section
}
public String getLastName(){
//complete this section
}
public void setLastName (String lastNameChange){
//complete this section with validation
}
public Person (String firstName, String lastName) throws Exception{
//complete this section with validation
}
private Person(){ //do not alter
}
]
Explanation / Answer
For the get methods, simply return what you have public String getX() { return x; } Set methods need a little work, check that strings are not null or zero length then set public void setX(String x) { if (x != null && x.length > 0) { this.x = x; // Needs this.x, x will refer to the input, not the field! } } For the constructor, throw exception if any of the input is null or zero length public Person(String firstName, String lastName) { if (firstName == null || firstName.length == 0) { throw new Exception("bad first name"); } this.firstName = firstName; // same thing for last name }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.