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

Halloween Again.... This year Halloween comes twice! We are going to modify the

ID: 3761458 • Letter: H

Question

Halloween Again....

This year Halloween comes twice! We are going to modify the Halloween lab to have additional functionality. This week we covered static methods, static variables, and the this keyword. We will modify the TreatingHouse.java to include these new topics. The driver class has been severely modified to allow for multiple houses. Take note of the new ArrayList and the advanced for loop structure.

Add a variable that counts each house that is participating in the holiday. This variable, participatingHouses, should count each time a TreatingHouse object is created.

We will also include a variable to count all of the candy given out so far, totalCandyPassedOut.

We will also count the number of tricks given and store this in a variable totalTricksGiven . The tricks will be per person. For example, 7 kids get tricked there are seven tricks.

We will add a variable to store an ID for each house. This ID should be unique and can be any integer number. Starting at 1 can be helpful.

Add a method called getHalloweenStatus that will display the new variables to Halloween.java. An instance variable will be needed in the TreatingHouse.java file to accomplish this implementation.

1 package Lab08;
2
3 import java.util.ArrayList;
4 import java.util.Random;
5 import java.util.Scanner;
6
7 public class Halloween {
8
9 public static void main(String[] args) {
10 Scanner scan = new Scanner(System.in);
11 ArrayList<TreatHouse> houses = new ArrayList<TreatHouse>();
12
13 Random gen = new Random(System.currentTimeMillis());
14 int numHouse = gen.nextInt(10) + 1;
15
16 // create houses
17 for (int i = 0; i < numHouse; i++) {
18 System.out.println("House " + i );
19 System.out.println("Which candy should we give out first? Candy in pot 1 or candy in pot 2?");
20 int candyPot = scan.nextInt();
21
22 System.out.println("How much candy did we buy?");
23 int totalCandy = scan.nextInt();
24
25 houses.add( new TreatHouse(candyPot, totalCandy));
26 }
27
28 boolean allDone = false;
29 while(!allDone){
30 //Each house will pass out candy for a bit.
31 for (TreatHouse house : houses) {
32 house.passTime();
33 }
34
35 //Check to see if all the house are done. If so set sentinel flag to true and exit while loop
36 allDone = false;
37 for(TreatHouse house: houses){
38 //If a single house is not done then all are not done
39 if(!house.isDone()){
40 allDone = false;
41 break;
42 }
43 else
44 allDone = true;
45 }
46
47 //Call static method to display some statistics about the holiday so far
48
49
50 }
51 System.out.println("All houses are done");
52 scan.close();
53 }
54 }

  1 package Lab08;
2
3 import java.util.Random;
4 import java.util.Scanner;
5
6 public class TreatHouse {
7 private int candyPot1; // amount of candy in pot 1 for a single TreatHouse
8 private int candyPot2; // amount of candy in pot 2 for a single TreatHouse
9 private int currentPot; // (1 or 2) current Pot for a single TreatHouse
10 private int totalCandy; // total candy for a single TreatHouse
11 private int currentTreaters; // current Treaters for a single TreatHouse
12 private boolean isDone = false; // is this house done for the night?
13 private int treatsPerTreater; //How much candy to give out to each treater at a single TreatHouse
14
15 //Static variables
16 // participatingHouses //This contains the total number of houses participating in this year's holiday
17 // totalCandyPassedOut //This contains the total number of candy passed out for all the houses.
18 // totalTricksGiven //This contains the total number of tricks given out. Remember that each kid gets tricked.
19 // houseID //This is the number for the house which is unique.
20
21 //Method to display the statistic variables
22 // getHalloweenStatus(){
23 //
24 // }
25
26
27
28 public TreatHouse(int candyPot, int totalCandy) {
29 // Error Check and split candy
30 if (totalCandy > 0) {
31 candyPot1 = totalCandy / 2;
32 candyPot2 = totalCandy / 2 + totalCandy % 2;
33 } else {
34 System.out.println("We can't give out candy if we don't have any."
35 + " I think we have some from last year. Yep, we have 100 " + "" + "pieces of candy to give out.");
36 totalCandy = 100;
37 candyPot1 = totalCandy / 2;
38 candyPot2 = totalCandy / 2 + totalCandy % 2;
39 }
40
41 // currentPot Check
42 if (candyPot == 1) {
43 currentPot = 1;
44 } else if (candyPot == 2) {
45 currentPot = 2;
46 } else {
47 System.out.println("Invalid choice for pot. Only 1 or 2. Using pot 1");
48 currentPot = 1;
49 }
50
51 //Adjust Static variables as needed here:
52
53
54 }
55
56 public void passTime() {
57 Scanner scan = new Scanner(System.in);
58
59 //before
60 this.getCandyStatus();
61
62 System.out.println("How much candy per treater should we give out?");
63 this.setTreatsPerTreater(scan.nextInt());
64
65 System.out.println("Knock, knock...." + "Trick or treat!");
66 this.knockKnock();
67 this.passOutCandy();
68
69 //After
70 this.getCandyStatus();
71
72 //Determines if the house if done for the night.
73 if(totalCandy == 0){
74 System.out.println("Time to turn off the lights and go to bed!");
75 System.out.println("The last candy came from pot number" + this.getLastPot());
76 System.out.println("Happy Halloween!");
77 isDone = true;
78 }
79 }
80
81 private void printCurrentPot() {
82 System.out.println("The current Pot is: " + currentPot);
83 }
84
85
86
87 public int getCandyCount() {
88 return candyPot1 + candyPot2;
89 }
90
91 public void passOutCandy() {
92 // If there are enough treats per treater for the given amount per
93 // treater, pass out candy from the current pot and switch to the other one.
94 // Else display a message that the treaters have been tricked... (no
95 // candy for them.) but do not change the current pot
96
97 if ((currentPot == 1 && candyPot1 == 0) || (currentPot == 2 && candyPot2 == 0)) {
98 // Switch pots and print message
99 System.out.println("There is no candy in the currentPot Switching pots.");
100 switchPots();
101 }
102
103 {// There is some candy to give out.
104 int currentPotAmmount = getCurrentPotAmount();
105 if (treatsPerTreater * currentTreaters <= currentPotAmmount) {
106 //Passing out Candy
107 if (currentPot == 1) {
108 candyPot1 -= treatsPerTreater * currentTreaters;
109 totalCandy -= treatsPerTreater * currentTreaters;
110 } else {
111 candyPot2 -= treatsPerTreater * currentTreaters;
112 totalCandy -= treatsPerTreater * currentTreaters;
113 }
114 switchPots();
115 } else {
116 // trick
117 System.out.println("You have been tricked!");
118 }
119
120 }
121
122 }
123
124 private int getCurrentPotAmount() {
125 if (currentPot == 1)
126 return candyPot1;
127 else
128 return candyPot2;
129 }
130
131 private void switchPots() {
132 if (currentPot == 1) {
133 currentPot = 2;
134 } else
135 currentPot = 1;
136
137 }
138
139 // Sets the number of trick or treaters.
140 public void knockKnock() {
141 Random gen = new Random(System.currentTimeMillis());
142 this.currentTreaters = gen.nextInt(13) + 1; // 1 to 13 treaters.
143 System.out.println("There are " + currentTreaters + " treaters.");
144 }
145
146 // Displays how much candy in each pot, total candy left
147
148 public void getCandyStatus() {
149 this.printCurrentPot();
150 System.out.println("Candy in Pot 1: " + candyPot1);
151 System.out.println("Candy in Pot 2: " + candyPot2);
152
153 }
154
155 // returns the pot number for which candy was last given.
156 public int getLastPot() {
157 if (currentPot == 1)
158 return 2;
159 else {
160 return 1;
161 }
162 }
163
164 public void setTreatsPerTreater(int treatsPerTreater) {
165 this.treatsPerTreater = treatsPerTreater;
166 }
167
168 public boolean isDone() {
169
170 return isDone;
171 }
172 }

PLEASE MAKE SURE ALL POINTS ARE COVERED!! THANKS!!

Explanation / Answer

import java.util.Random;
import java.util.Scanner;
public class TreatHouse {
   private int candyPot1; // amount of candy in pot 1 for a single TreatHouse
   private int candyPot2; // amount of candy in pot 2 for a single TreatHouse
   private int currentPot; // (1 or 2) current Pot for a single TreatHouse
   private int totalCandy; // total candy for a single TreatHouse
   private int currentTreaters; // current Treaters for a single TreatHouse
   private boolean isDone = false; // is this house done for the night?
   private int treatsPerTreater; //How much candy to give out to each treater at a single TreatHouse

   //Static variables
   // participatingHouses //This contains the total number of houses participating in this year's holiday
   // totalCandyPassedOut //This contains the total number of candy passed out for all the houses.
   // totalTricksGiven //This contains the total number of tricks given out. Remember that each kid gets tricked.
   // houseID //This is the number for the house which is unique.
  
   static int participatingHouses = 0;
   static int totalCandyPassedOut = 0;
   static int totalTricksGiven = 0;
   static int houseID = 1;
   int hid;

   //Method to display the statistic variables
   void getHalloweenStatus(){
       System.out.println("Participating Houses: " + participatingHouses);
       System.out.println("Total Candy Passed Out: " + totalCandyPassedOut);
       System.out.println("Total Tricks Given: " + totalTricksGiven);
   }

   public TreatHouse(int candyPot, int totalCandy) {
   // Error Check and split candy
   if (totalCandy > 0) {
       candyPot1 = totalCandy / 2;
       candyPot2 = totalCandy / 2 + totalCandy % 2;
   } 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.");
       totalCandy = 100;
       candyPot1 = totalCandy / 2;
       candyPot2 = totalCandy / 2 + totalCandy % 2;
   }

   // currentPot Check
   if (candyPot == 1) {
       currentPot = 1;
   } else if (candyPot == 2) {
       currentPot = 2;
   } else {
       System.out.println("Invalid choice for pot. Only 1 or 2. Using pot 1");
       currentPot = 1;
   }
  
   //Adjust Static variables as needed here:
       hid = houseID++;
       participatingHouses++;
     
  
   }

   public void passTime() {
   Scanner scan = new Scanner(System.in);

   //before
   this.getCandyStatus();
     
       System.out.println("How much candy per treater should we give out?");
       this.setTreatsPerTreater(scan.nextInt());
       System.out.println("Knock, knock...." + "Trick or treat!");
       this.knockKnock();
       this.passOutCandy();
     
       //After
       this.getCandyStatus();

       //Determines if the house if done for the night.
       if(totalCandy == 0){
       System.out.println("Time to turn off the lights and go to bed!");
       System.out.println("The last candy came from pot number" + this.getLastPot());
       System.out.println("Happy Halloween!");
       isDone = true;
       }
   }

   private void printCurrentPot() {
       System.out.println("The current Pot is: " + currentPot);   
   }



   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

       if ((currentPot == 1 && candyPot1 == 0) || (currentPot == 2 && candyPot2 == 0)) {
       // Switch pots and print message
       System.out.println("There is no candy in the currentPot Switching pots.");
       switchPots();
   }

   {// There is some candy to give out.
       int currentPotAmmount = getCurrentPotAmount();
       if (treatsPerTreater * currentTreaters <= currentPotAmmount) {
           //Passing out Candy
           if (currentPot == 1) {
               candyPot1 -= treatsPerTreater * currentTreaters;
               totalCandy -= treatsPerTreater * currentTreaters;
           } else {
               candyPot2 -= treatsPerTreater * currentTreaters;
               totalCandy -= treatsPerTreater * currentTreaters;
           }
           switchPots();
           totalCandyPassedOut++;
       } else {
           // trick
           System.out.println("You have been tricked!");
           totalTricksGiven++;
       }

   }

   }

   private int getCurrentPotAmount() {
   if (currentPot == 1)
       return candyPot1;
   else
       return candyPot2;
   }

   private void switchPots() {
   if (currentPot == 1) {
       currentPot = 2;
   } else
       currentPot = 1;

   }

   // 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.
   System.out.println("There are " + currentTreaters + " treaters.");
   }

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

   public void getCandyStatus() {
   this.printCurrentPot();
   System.out.println("Candy in Pot 1: " + candyPot1);
   System.out.println("Candy in Pot 2: " + candyPot2);

   }

   // returns the pot number for which candy was last given.
   public int getLastPot() {
   if (currentPot == 1)
       return 2;
   else {
       return 1;
   }
   }

   public void setTreatsPerTreater(int treatsPerTreater) {
   this.treatsPerTreater = treatsPerTreater;
   }

   public boolean isDone() {
          
   return isDone;
   }
}