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

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote