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

Simulating a Halloween Night 1. The File TreatHouse .java contains a partial def

ID: 3758899 • Letter: S

Question

Simulating a Halloween Night

1.               The File TreatHouse.java contains a partial definition for a class representing a house giving out candy. Save it to your directory and study it to see what methods it contains. Then complete the TreatHouse class as described below.

Fill in the code for constructor, which should initialize the instance variables. Ensure that the amount of candy is always greater than zero. Print a message “We can't give out candy if we don't have any. I think we have some from last year. Yep, we have 100 pieces of candy to give out.” The constructor should also initialize the instance variable currentPot as specified by the parameter candyPot if candyPot is 1 or 2. Otherwise prints a message "Invalid input, we will use candy pot 1 first."

Fill in the code for method passOutCandy which decrease the amount of candy in the correct pot. If there are enough treats per treater for the given amount per treater, pass out candy from the current pot and switch to the other one. Else display a message that the treaters have been tricked... (no candy for them.) But do not change the current pot.

Fill in the other methods for the getters and setters.

2.               File Halloween.java contains a driver program that uses the TreatHouse class above. Save it to your directory and study it to see what it does. Then compile and run it to test the TreatHouse class.

import java.util.Random;

public class TreatHouse {

                  int candyPot1; //amount of candy in pot 1

                  int candyPot2; //amount of candy in pot 2

                  int currentPot; //1 or 2

                  int totalCandy;

                  int currentTreaters;

                  int treatsPerTreater;

                  public TreatHouse(int candyPot, int totalCandy) {

                                    //Add code here, be sure to split the candy between the pots.

                                   

                  }

                  public int getCandyCount() {

                                    return candyPot1 + candyPot2;

                  }

                  public void passOutCandy() {

                                    //If there are enough treats per treater for the given amount per treater, pass out candy

                                    //from the current pot and switch to the other one.

                                    //Else display a message that the treaters have been tricked... (no candy for them.)

                                    // but do not change the current pot

                                   

                  }

                 

                  //Sets the number of trick or treaters.

                  public void knockKnock() {

                                    Random gen = new Random(System.currentTimeMillis());

                                    this.currentTreaters = gen.nextInt(13) + 1; //1 to 13 treaters.

                  }

                 

                  //Displays how much candy in each pot, total candy left

                  public void getCandyStatus() {

                                    //add in code here

                                   

                  }

                  //returns the pot number for which candy was last given.

                  public int getLastPot() {

                                    //add code here

                  }

                  public void setTreatsPerTreater(int treatsPerTreater) {

                                    //add code here

                  }

}

import java.util.Scanner;

public class Halloween {

                  public static void main(String[] args) {

                                    Scanner scan = new Scanner(System.in);

                                    System.out.println("Which candy should we give out first? Candy in pot 1 or candy in pot 2?");

                                    int candyPot = scan.nextInt();

                                    System.out.println("How much candy did we buy?");

                                    int totalCandy = scan.nextInt();

                                    TreatHouse ourHouse = new TreatHouse(candyPot, totalCandy);

                                    while (ourHouse.getCandyCount() > 0) {

                                                      ourHouse.getCandyStatus(); //tells how much candy is left & other stuff

                                                      System.out.println("How much candy per treater should we give out?");

                                                      int treatsPerTreater = scan.nextInt();

                                                      ourHouse.setTreatsPerTreater(treatsPerTreater);

                                                                                                           

                                                      System.out.println("Knock, knock...." + "Trick or treat!");

                                                      ourHouse.knockKnock();

                                                      ourHouse.passOutCandy();

                                    }

                                    System.out.println("Time to turn off the lights and go to bed!");

                                    System.out.println("The last candy came from pot number"+ ourHouse.getLastPot());

                                    System.out.println("Happy Halloween!");

                                    scan.close();

                  }

}

Explanation / Answer

Complete Program:

File: TreatHouse.java

// TreatHouse class implementation
import java.util.Random;
public class TreatHouse
{  
   int candyPot1; //amount of candy in pot 1
   int candyPot2; //amount of candy in pot 2
   int currentPot; //1 or 2
   int totalCandy;
   int currentTreaters;
   int treatsPerTreater;
  
   public TreatHouse(int candyPot, int totalCandy)
   {
       //Add code here, be sure to split the candy between the pots.
       if(totalCandy > 0)
       {
           this.totalCandy = totalCandy;
           candyPot1 = this.totalCandy / 2;
           candyPot2 = this.totalCandy - candyPot1;
       }
       else
       {
           System.out.println("We can't give out candy if we don't have any. I think we have some from last year. Yep, we have 100 pieces of candy to give out.");
           this.totalCandy = 100;
           candyPot1 = this.totalCandy / 2;
           candyPot2 = this.totalCandy - candyPot1;
       }
      
       if(candyPot == 1 || candyPot == 2)
           currentPot = candyPot;
       else
       {
           System.out.println("Invalid input, we will use candy pot 1 first.");
           currentPot = 1;
       }
   }
  
   public int getCandyCount()
   {
       return this.totalCandy;
   }
  
   public void passOutCandy()
   {
       //If there are enough treats per treater for the given amount per treater, pass out candy
       //from the current pot and switch to the other one.      
       //Else display a message that the treaters have been tricked... (no candy for them.)
       // but do not change the current pot
      
        int treats = (treatsPerTreater * currentTreaters);
            
        if(currentPot == 1 && candyPot1 >= treats)
        {
           candyPot1 = candyPot1 - treats;          
           currentPot = 2;          
        }
        else if(currentPot == 2 && candyPot2 >= treats)
        {
           candyPot2 = candyPot2 - treats;          
           currentPot = 1;          
        }
        else
           System.out.println("The treaters have been tricked... (no candy for them.)");
      
        this.totalCandy = candyPot1 + candyPot2;
   }
  
   //Sets the number of trick or treaters.
   public void knockKnock()
   {
       Random gen = new Random(System.currentTimeMillis());
       this.currentTreaters = gen.nextInt(13) + 1; //1 to 13 treaters.
   }
   
   //Displays how much candy in each pot, total candy left
   public void getCandyStatus()
   {
       //add in code here
       System.out.println("Candies in pot 1: " + candyPot1);
       System.out.println("Candies in pot 2: " + candyPot2);
       System.out.println("Total candies:    " + totalCandy);
   }

   //returns the pot number for which candy was last given.
   public int getLastPot()
   {
       //add code here
       return currentPot;
   }

   public void setTreatsPerTreater(int treatsPerTreater)
   {
       //add code here
       this.treatsPerTreater = treatsPerTreater;
   }
} // end of TreatHouse class


File: Halloween.java

// Halloween class implementation
import java.util.Scanner;
public class Halloween
{
   public static void main(String[] args)
   {
       Scanner scan = new Scanner(System.in);
      
       System.out.println("Which candy should we give out first? Candy in pot 1 or candy in pot 2?");
       int candyPot = scan.nextInt();
      
       System.out.println("How much candy did we buy?");
       int totalCandy = scan.nextInt();
      
       TreatHouse ourHouse = new TreatHouse(candyPot, totalCandy);
      
       while(ourHouse.getCandyCount() > 0)
       {
           ourHouse.getCandyStatus(); // tells how much candy is left & other stuff
          
           System.out.println("How much candy per treater should we give out?");
           int treatsPerTreater = scan.nextInt();
          
           ourHouse.setTreatsPerTreater(treatsPerTreater);
          
           System.out.println("Knock, knock...." + "Trick or treat!");
          
           ourHouse.knockKnock();
           ourHouse.passOutCandy();
       }
      
       System.out.println("Time to turn off the lights and go to bed!");
       System.out.println("The last candy came from pot number" + ourHouse.getLastPot());
       System.out.println("Happy Halloween!");
       scan.close();
   }
} // end of Halloween class