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

Here is a link to the Infant class. While Comparable is supposed to capture the

ID: 3627474 • Letter: H

Question

Here is a link to the Infant class.


While Comparable is supposed to capture the natural ordering for a class, it's possible to imagine unnatural orderings as well. So: in the box provided below, reimplement the compareTo() method so that one Infant is less than another, if the length of his or her name is less than the other.

public class Infant implements Comparable {

  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 int compareTo(Object other) {

put your code here

  } //end method
} //end class

Explanation / Answer

public int compareTo(Object other){ String firstName = ((Infant)other).getName(); String secondName = this.name; return(fristName.length-secondName.length); }

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