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

1. Suppose we want to add a flip() method to the Chance class. This method shoul

ID: 3682162 • Letter: 1

Question

1. Suppose we want to add a flip() method to the Chance class. This method should return, at random, either a 0 or a 1, where 0 stands for tails and 1 stands for heads. In the box provided below, code this method. Be sure to take advantage of the fact that Chance extends Random.

Hint: what Random class method call will produce values of either 0 or 1?

import java.util.Random;

public class Chance extends Random {

  public int flip() {

2. 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() {

Explanation / Answer

Question 1 :

Answer

public int flip() {
       int choice = super.nextInt(2);     
       return choice;
}

Question 2:

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;
   }
}