In the box provided below, add a toString method to this class. The method shoul
ID: 3566334 • Letter: I
Question
In the box provided below, add a toString method to this class. The method should return a String, and should report the Infant's name and age. For example, These lines of code
should print:
Infant name: jake age: 3
public class Infant {
private String name;
private int age; // in months
public Infant(String who, int months) {
name = who;
age = months;
}
public String getName() { return name;}
public int getAge() { return age;}
public void anotherMonth() { age = age + 1;}
public String toString() {
CODE GOES HERE
} //end method
} //end class
Explanation / Answer
public class Infant {
private String name;
private int age; // in months
public Infant(String who, int months) {
name = who;
age = months;
}
public String getName() { return name;}
public int getAge() { return age;}
public void anotherMonth() { age = age + 1;}
public String toString() {
return "Infant name: "+name+" age: "+age;
} //end method
} //end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.