Question

Program Description A local company has requested a program to calculate the cost of different hot...

Program Description

A local company has requested a program to calculate the cost of different hot tubs that it sells. Use the following instructions to code the program:

File 1 - HotTubLastname.java

  • Write a class that will hold fields for the following:
    • The model of the hot tub
    • The hot tub’s feature package (can be Basic or Premium)
    • The hot tub’s length (in inches)
    • The hot tub’s width (in inches)
    • The hot tub’s depth (in inches)
  • The class should also contain the following methods:
    • A constructor that doesn’t accept arguments
    • A constructor that accepts arguments for each field.
    • Appropriate accessor and mutator methods (i.e., setters and getters).
    • A method named getCapacity that accepts no arguments and calculates and returns the capacity (in gallons) of the hot tub.

The capacity of the hot tub can be calculated by using the following formula:

Capacity in gallons = (Length * Width * Depth) / 1728 * 4.8

    • A method named getPrice that accepts no arguments and calculates and returns the price of the hot tub based on the following table:

Hot Tub Capacity

Package

Cost Per Gallon

Less than 350 gallons

Basic

$5.00

Less than 350 gallons

Premium

$6.50

350 but not more than 500 gallons

Basic

$6.00

350 but not more than 500 gallons

Premium

$8.00

Over 500 gallons

Basic

$7.50

Over 500 gallons

Premium

$10.00


Price = Cost Per Gallon * Capacity

File 2 - DemoLastname.java

  • Write a program that demonstrates the class and calculates the cost of hot tubs. The program should ask the user for the following:
    • The model of the hot tub
    • The hot tub’s feature package (can be Basic or Premium)
    • The hot tub’s length (in inches). The program should not accept a number less than 60 for the length.
    • The hot tub’s width (in inches). The program should not accept a number less than 60 for the width.
    • The hot tub’s depth (in inches). The program should not accept a number less than 30 for the depth.
  • The program should prompt the user for all the data necessary to fully initialize the objects. IMPORTANT: Store your objects in a container that will automatically expand as objects are added.
  • The program should continue allowing the user to input information until the user indicates to stop (see sample input on page 3). Note: The program should accept lower or upper case responses to this question.
  • After the user indicates that they wish to stop, the program should output the information for each hot tub.
    • The capacity of the hot tub should be formatted to two decimal places.
    • The price of the hot tub should be formatted as currency to two decimal places and include a comma in the thousands place.
  • After the program outputs the information for each hot tub, it should output the total cost of all of the hot tubs.
  • The program should display the input and output as shown in the provided sample output (Note: this includes blank lines).

Sample Input and Output (formatting and spacing should be exactly as shown below)

What is the model of the hot tub? HT Intro Model 4 Person
Is the hot tub package Basic or Premium? Basic
What is the length (in inches) of the hot tub? 60
What is the width (in inches) of the hot tub? 65
What is the depth (in inches) of the hot tub? 30

Do you wish to enter information for another hot tub (Y/N)? Y

What is the model of the hot tub? HT Relaxation 6 Person
Is the hot tub package Basic or Premium? Premium
What is the length (in inches) of the hot tub? 75
What is the width (in inches) of the hot tub? 75
What is the depth (in inches) of the hot tub? 32

Do you wish to enter information for another hot tub (Y/N)? y

What is the model of the hot tub? HT Ultimate 8 person
Is the hot tub package Basic or Premium? Premium
What is the length (in inches) of the hot tub? 100
What is the width (in inches) of the hot tub? 100
What is the depth (in inches) of the hot tub? 36

Do you wish to enter information for another hot tub (Y/N)? N

Model: HT Intro Model 4 Person
Package: Basic
Length: 60 inches
Width: 65 inches
Depth: 30 inches
Capacity: 325.00 gallons
Price: $1,625.00

Model: HT Relaxation 6 Person
Package: Premium
Length: 75 inches
Width: 75 inches
Depth: 32 inches
Capacity: 500.00 gallons
Price: $4,000.00

Model: HT Ultimate 8 person
Package: Premium
Length: 100 inches
Width: 100 inches
Depth: 36 inches
Capacity: 1000.00 gallons
Price: $10,000.00

The total cost of all the hot tubs is: $15,625.00

Homework Answers

Answer #1

Hi,

Please find below code as per yout requirement.

Let me know if you have any doubt/concerns in this answer via comments.

Hope this answer helps you.

Thanks

/********************JAVA CODE**********************/

/***************HotTubLastname.java*******************/

public class HotTubLastname {

   //Instance variable
   private String model;
   private String tubPackage;
   private int length;
   private int width;
   private int depth;

   /**
   * This is default constructor used to construct object by default values
   */
   public HotTubLastname() {

   }

   /**
   * This is parameterized constructor used to construct object by given parameters
   * @param model
   * @param tubPackage
   * @param length
   * @param width
   * @param depth
   */
   public HotTubLastname(String model, String tubPackage, int length, int width, int depth) {
       this.model = model;
       this.tubPackage = tubPackage;
       this.length = length;
       this.width = width;
       this.depth = depth;
   }

   /**
   * @return the model
   */
   public String getModel() {
       return model;
   }

   /**
   * @param model the model to set
   */
   public void setModel(String model) {
       this.model = model;
   }

   /**
   * @return the tubPackage
   */
   public String getTubPackage() {
       return tubPackage;
   }

   /**
   * @param tubPackage the tubPackage to set
   */
   public void setTubPackage(String tubPackage) {
       this.tubPackage = tubPackage;
   }

   /**
   * @return the length
   */
   public int getLength() {
       return length;
   }

   /**
   * @param lenght the length to set
   */
   public void setLength(int length) {
       this.length = length;
   }

   /**
   * @return the width
   */
   public int getWidth() {
       return width;
   }

   /**
   * @param width the width to set
   */
   public void setWidth(int width) {
       this.width = width;
   }

   /**
   * @return the depth
   */
   public int getDepth() {
       return depth;
   }

   /**
   * @param depth the depth to set
   */
   public void setDepth(int depth) {
       this.depth = depth;
   }

   /**
   * This method returns capacity of the tub using length,width and depth
   * @return
   */
   public double getCapacity() {
       return (Double.valueOf(length) * Double.valueOf(width) * Double.valueOf(depth)) / 1728 * 4.8;
   }

   /**
   * This method returns price of the tub on the basis of type and capacity
   * @return
   */
   public double getPrice() {

       double price = 0;
       double capacity = getCapacity();
       String basic = "Basic";
       String premium = "Premium";

       if(capacity < 350) {
           if(getTubPackage().equalsIgnoreCase(basic)) {
               price = capacity * 5.00;
           } else if (getTubPackage().equalsIgnoreCase(premium)) {
               price = capacity * 6.50;
           }
       } else if(capacity >=350 && capacity <500) {
           if(getTubPackage().equalsIgnoreCase(basic)) {
               price = capacity * 6.00;
           } else if (getTubPackage().equalsIgnoreCase(premium)) {
               price = capacity * 8.00;
           }
       } else if(capacity >=500) {
           if(getTubPackage().equalsIgnoreCase(basic)) {
               price = capacity * 7.50;
           } else if (getTubPackage().equalsIgnoreCase(premium)) {
               price = capacity * 10.00;
           }
       }
       return price;
   }

  
  
}

/***************DemoLastname.java*******************/

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;

public class DemoLastname {

   public static void main(String[] args) {

       //Scanner is used to ask user input from console
       Scanner sc = new Scanner(System.in);

       //Creating ArrayList of Hot tub to store user entered data for end result
       ArrayList<HotTubLastname> listOfTub = new ArrayList<HotTubLastname>();

       //Decimal format is used to format amount
       DecimalFormat df = new DecimalFormat("#.00");
       //NumberFormat is used to format currency in dollar
       NumberFormat currencyFormatter =
               NumberFormat.getCurrencyInstance(Locale.US);

       //Temporary variables to store data for validation and constructing object
       String packageType = null;
       String modelName = null;
       int length = 0;
       int width = 0;
       int depth = 0;
       double totalCost = 0;
       String basic = "Basic";
       String premium = "Premium";

       while(true) {
           //Getting model from user
           System.out.print("What is the model of the hot tub? ");
           modelName = sc.nextLine();

           //Checking package of Tub if invalid input then continue asking details otherwise breaking from loop
           do {
               System.out.print("Is the hot tub package Basic or Premium? ");
               packageType = sc.nextLine();
               if(!(packageType.equalsIgnoreCase(basic) || packageType.equalsIgnoreCase(premium))) {
                   System.out.println("Invalid Package, can be Basic or Premium");
                   continue;
               } else {
                   break;
               }
           } while(true);

           //Checking length of Tub if invalid input then continue asking details otherwise breaking from loop
           do {
               System.out.print("What is the length (in inches) of the hot tub? ");
               length = Integer.parseInt(sc.nextLine());
               if(length < 60) {
                   System.out.println("Invalid length,Please enter 60 and above.");
               } else {
                   break;
               }
           } while(true);

           //Checking width of Tub if invalid input then continue asking details otherwise breaking from loop
           do {
               System.out.print("What is the width (in inches) of the hot tub? ");
               width = Integer.parseInt(sc.nextLine());
               if(width < 60) {
                   System.out.println("Invalid width,Please enter 60 and above.");
               } else {
                   break;
               }
           } while(true);

           //Checking depth of Tub if invalid input then continue asking details otherwise breaking from loop
           do {
               System.out.print("What is the depth (in inches) of the hot tub? ");
               depth = Integer.parseInt(sc.nextLine());
               if(depth < 30) {
                   System.out.println("Invalid depth,Please enter 30 and above.");
               } else {
                   break;
               }
           } while(true);

           //Creating object of Hot tub by above given input
           HotTubLastname hotTubLastname = new HotTubLastname(modelName, packageType, length, width, depth);
           //Adding object of hot tub to print details later
           listOfTub.add(hotTubLastname);

           //Asking if user wants to continue adding tub or not, If Y then continue otherwise exit from while loop
           System.out.print("Do you wish to enter information for another hot tub (Y/N)? ");
           String decision = sc.nextLine();
           if(decision.equalsIgnoreCase("Y")) {
               continue;
           } else if (decision.equalsIgnoreCase("N")) {
               break;
           } else {
               System.out.println("Invalid input");
               break;
           }

       }

       //Printing individual hot tub details in required format
       for (HotTubLastname hotTubLastname : listOfTub) {
           double price = hotTubLastname.getPrice();
           totalCost+= price;
           String result = "Model: " + hotTubLastname.getModel() + "\n Package: " + hotTubLastname.getTubPackage() + "\n Length:" + hotTubLastname.getLength() + " inches \n Width: "
                   + hotTubLastname.getWidth() + "inches \n Depth: " + hotTubLastname.getDepth() + " inches \n Capacity: "+df.format(hotTubLastname.getCapacity()) + " gallons \n Price: "+currencyFormatter.format(price);

           System.out.println(result);
       }

       //Printing total cost of the hot tubs
       System.out.println("The total cost of all the hot tubs is: "+currencyFormatter.format(totalCost));

   }

}

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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT