Step 1:
Step 2:
Implement a properly encapsulated "BouncyHouse" class that has the following attributes:
(Note: all weights in this assignment can be represented as whole numbers)
…and methods to perform the following tasks:
Step 3:
Step 4:
Implement a properly encapsulated "Person" class that has the following attributes:
…and methods to perform the following tasks:
Step 5:
Step 6: <-- spoiler alert: this is the hard part...
Step 7:
In your main method:
Note: Although your class structure will support multiple bounce houses, your main method only needs to create one.
///////////////////////////TestHouse.java/////////////////////////
package PartnerLab1;
import java.util.Scanner;
public class TestHouse {
public static void main(String args[]){
Scanner sc = new
Scanner(System.in);
BouncyHouse bh1 = new
BouncyHouse();
bh1.setWeightLimit(1000);
boolean takeData = true;
do{
System.out.println("Add a person?Press 'y' to ENTER , 'q' to
QUIT");
String entry =
sc.nextLine();
if(entry.equals("q")){
takeData = false;
}else
if(entry.equals("y")){
System.out.println("name:");
String name = sc.nextLine();
System.out.println("Weight in pounds:");
int weight =
Integer.parseInt(sc.nextLine());
Person p = new Person(name,weight);
bh1.addPerson(p);
}else{
System.out.println("Invalid entry!");
}
}while(takeData);
sc.close();
//UNCOMMENT THE BELOW COMMENTED
CODE (in bold) IF YOU NEED TO ADD ANOTHER BOUNCY
HOUSE
/*System.out.println("Creating 2nd bouncy house");
BouncyHouse bh2 = new
BouncyHouse();
bh2.setWeightLimit(200);
bh2.addPerson(new
Person("Mike",98));
bh2.addPerson(new
Person("Will",76));
bh1.setNextHouse(bh2);*/
//Print bouncy house info as long
as there is a next bouncy house
BouncyHouse bh = bh1;
int counter = 1;
do{
System.out.println();
System.out.println("Bouncy house# "+ counter++);
System.out.println(bh.getInfo());
if(bh.hasNextHouse()){//will check if there is any next bouncy
house
bh = bh.nextBouncyHouse;
}else{//break
out of the loop
break;
}
}while(true);
}
}
///////////////////////////Person.java/////////////////////////////////////
package PartnerLab1;
//class: Person
public class Person {
private String name;
private int weightInPounds;
//constructor
public Person(String name, int weight){
this.name = name;
this.weightInPounds = weight;
}
//getters
public String getName(){
return name;
}
public int getWeightInPounds(){
return weightInPounds;
}
//method: to get person info
public String getInfo(){
String info = "[Name: "+name+" ,
"+"Weight in Pounds: "+weightInPounds+"]";
return info;
}
}
//////////////////////////////BouncyHouse.java///////////////////////////////////
package PartnerLab1;
import java.util.ArrayList;
import java.util.List;
//class: Bouncy House
public class BouncyHouse {
private int weightLimit;
private int totalCurrentWeight;
private List<Person> persons; //to add persons
in this bouncy house
BouncyHouse nextBouncyHouse = null; //to take multiply
bouncy houses as singly linked list
//no arg constructor
public BouncyHouse(){
this.weightLimit = 0 ;
this.totalCurrentWeight = 0;
}
//method to set next bouncy house for the current
bouncy house
public void setNextHouse(BouncyHouse bh){
this.nextBouncyHouse = bh;
}
//method to check if there exist any next bouncy
house
public boolean hasNextHouse(){
return (this.nextBouncyHouse!=null?
true:false);
}
//setters
public void setWeightLimit(int weightLimit) {
this.weightLimit =
weightLimit;
}
public void setTotalCurrentWeight(int
totalCurrentWeight) {
this.totalCurrentWeight =
totalCurrentWeight;
}
//add person in the current bouncy house
public boolean addPerson(Person p){
if(totalCurrentWeight +
p.getWeightInPounds() > weightLimit){
System.out.println("Person "+p.getInfo()+" is unable to
enter.");
return
false;
}else{
if(persons ==
null){
persons = new ArrayList<Person>();//create
a new list
}
persons.add(p);//add into list
//update total
weight of this bouncy house
totalCurrentWeight = totalCurrentWeight +
p.getWeightInPounds();
System.out.println("Person "+p.getInfo()+" is able to
enter.");
return
true;
}
}
//method to get info of this bouncy house
public String getInfo(){
StringBuilder sb = new
StringBuilder();
sb.append("Weight Limit:
"+weightLimit);
sb.append("\nTotal Current Weight
Of all Occupants: "+totalCurrentWeight);
if(persons!=null){
sb.append("\nPersons in the this bouncy house are:");
for(Person p :
persons){
sb.append("\n"+p.getInfo());
}
}
return sb.toString();
}
}
==========================================
OUTPUT
==========================================
Add a person?Press 'y' to ENTER , 'q' to QUIT
y
name:
harry
Weight in pounds:
191
Person [Name: harry , Weight in Pounds: 191] is able to
enter.
Add a person?Press 'y' to ENTER , 'q' to QUIT
p
Invalid entry!
Add a person?Press 'y' to ENTER , 'q' to QUIT
y
name:
gomes
Weight in pounds:
187
Person [Name: gomes , Weight in Pounds: 187] is able to
enter.
Add a person?Press 'y' to ENTER , 'q' to QUIT
y
name:
peter
Weight in pounds:
177
Person [Name: peter , Weight in Pounds: 177] is able to
enter.
Add a person?Press 'y' to ENTER , 'q' to QUIT
y
name:
larry
Weight in pounds:
167
Person [Name: larry , Weight in Pounds: 167] is able to
enter.
Add a person?Press 'y' to ENTER , 'q' to QUIT
y
name:
ronty
Weight in pounds:
178
Person [Name: ronty , Weight in Pounds: 178] is able to
enter.
Add a person?Press 'y' to ENTER , 'q' to QUIT
y
name:
dsouza
Weight in pounds:
171
Person [Name: dsouza , Weight in Pounds: 171] is unable to
enter.
Add a person?Press 'y' to ENTER , 'q' to QUIT
q
Bouncy house# 1
Weight Limit: 1000
Total Current Weight Of all Occupants: 900
Persons in the this bouncy house are:
[Name: harry , Weight in Pounds: 191]
[Name: gomes , Weight in Pounds: 187]
[Name: peter , Weight in Pounds: 177]
[Name: larry , Weight in Pounds: 167]
[Name: ronty , Weight in Pounds: 178]
Get Answers For Free
Most questions answered within 1 hours.