Question

Step 1: Create a new Java project in NetBeans called “PartnerLab1” Create a new Java Class...

Step 1:

  • Create a new Java project in NetBeans called “PartnerLab1”
  • Create a new Java Class in that project called "BouncyHouse"
  • Create a new Java Class in that project called “Person”

Step 2:

Implement a properly encapsulated "BouncyHouse" class that has the following attributes:

  • Weight limit
  • Total current weight of all occupants in the bouncy house

(Note: all weights in this assignment can be represented as whole numbers)

…and methods to perform the following tasks:

  • Set the weight limit
  • Set the total current weight
  • Return all info about the bouncy house (you may want to call this method getInfo())
  • ‘Add a person’ to the bouncy house by
    1. Testing if their weight would cause the bouncy house to exceed its weight limit if they entered.
    2. Returning true and updating the total current weight of the bouncy house to reflect the addition of their weight (if they are able to enter).
    3. Returning false if they are unable to enter.

Step 3:

  • Create a no argument constructor for the BouncyHouse class that sets both the weight limit and the total weight to a default value of 0.

  Step 4:

Implement a properly encapsulated "Person" class that has the following attributes:

  • Name
  • Weight in pounds

…and methods to perform the following tasks:

  • Get the name
  • Get the weight
  • Return all info about the person (you may want to call this method getInfo())

Step 5:

  • Create a two argument constructor for the Person class that takes in the name and weight and sets both attributes accordingly

Step 6:  <-- spoiler alert: this is the hard part...

  • Modify your class structure such that:
    • Your design supports the creation of multiple bounce houses
    • You can store and access the names of each person in each bounce house.
  • Note that there are a few valid ways to accomplish this. Discuss the upsides and downsides of each approach with your partner. Hints will be given in class.

Step 7:

In your main method:

  • Create an instance of your Bouncy House class using your constructor. Use your setter to set the weight limit of the bouncy house. There is no need to modify the total weight at this point since nobody is in the bouncy house to start.
  • Repeatedly prompt the user to add a person (including name and weight), or type "q" when done.
    • Attempt to add each person to the bouncy house.
  • Display all of the info for the bouncy house and each of the people inside it.

Note: Although your class structure will support multiple bounce houses, your main method only needs to create one.

Homework Answers

Answer #1

///////////////////////////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]

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of type String, custName of type String and checkingBalance of type double and savingBalance of type double 2)Write the constructor for CustomerAccount class. The constructor takes parameters to initialize custID, custName, checkingBalance and savingBalance. 3)Write the mutator method for all attributes of the class CustomerAccount. then Write the toString method for class CustomerAccount.
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class...
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class ( super class) 2,3. Create two sub classes ( car, bus , truck train or any) for vehicle class 4 Create a subclass (sportscar or schoolbus or Goodstrain or any) for car or bus You need to use the following atleast for one class. Use a protected variable Add constructor for superclass and subclass Override a method which displays a description about the vehicle...
in JAVA Write a class Store which includes the attributes: store name and sales tax rate....
in JAVA Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Yarn Store, which inherits from Store. A Yarn Store has the following additional attributes: how many skeins of yarn are sold every year and the average price per skein. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Yarn Store; In the Yarn Store class, also code a method returning the...
Create a java class called Timer which contains a method called Display that prints a counter...
Create a java class called Timer which contains a method called Display that prints a counter beginning at 00:00 and increments second and minute by minute until it stops at a total of 60 minutes.
JAVA Rational Numbers Create a Rational number class in the same style as the Complex number...
JAVA Rational Numbers Create a Rational number class in the same style as the Complex number class created in class. That is, implement the following methods: constructor add sub mul div toString You must also provide a Main class and main method to fully test your Rational number class.
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. // code from Q1: interface Vehicle { void Start();...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to get 4 integers from the user and store them in variables. The program should calculate the average of the 6 numbers as a floating point. Output all of the original input and the calculated average in the Command window. The code is expected to be commented and user-friendly.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT