Question

In: Computer Science

Java Coding Background You will create a Java class that simulates a water holding tank. The...

Java Coding

Background

You will create a Java class that simulates a water holding tank. The holding tank can hold volumes of water (measured in gallons) that range from 0 (empty) up to a maximum. If more than the maximum capacity is added to the holding tank, an overflow valve causes the excess to be dumped into the sewer system.

Assignment

The class will be named HoldingTank. The class attributes will consist of two int fields – current and maxCapacity. The fields must be private. The current field represents the current number of gallons of water in the tank. The maxCapacity field represents the maximum number of gallons of water that the tank can hold.

The class will contain the following methods:

Constructor – the constructor will initialize the two fields. If current is greater than maxCapacity, it will be set to maxCapacity.

Getters – there will be a getter for each field.

Setters – no setters will be defined for this class

void add(int volume) – add volume gallons to the tank. If the current volume exceeds maxCapacity, it will be set to maxCapacity.

void drain(int volume) – try to remove volume gallons from the tank. If resulting current volume is less than zero, set it to zero.

void print() – prints the current volume of the tank (in gallons)

Now create a Main class with a main method to test the HoldingTank class. Add the following code to the main method.

  1. Create an instance of HoldingTank, named tank with a maximum capacity of 1000 gallons and a current volume of 600 gallons.
  2. Print the current volume of the tank
  3. Add 300 gallons
  4. Drain 50 gallons
  5. Print the current volume of the tank
  6. Add 500 gallons
  7. Drain 250 gallons
  8. Print the current volume of the tank
  9. Drain 1200 gallons
  10. Add 200 gallons
  11. Drain 25 gallons
  12. Print the current volume of the tank

Example Output

The tank volume is 600 gallons

The tank volume is 850 gallons

The tank volume is 750 gallons

The tank volume is 175 gallons

Solutions

Expert Solution

HoldTank.java
class HoldTank {
   private int current;
   private int maxCapacity;

   // constructor to initialize the values
   public HoldTank(int aCurrent, int aMaxCapacity) {
       super();
       current = aCurrent;
       maxCapacity = aMaxCapacity;
       if (current > maxCapacity)
           current = maxCapacity;
   }

   // setters and getters
   public int getCurrent() {
       return current;
   }

   public void setCurrent(int aCurrent) {
       current = aCurrent;
   }

   public int getMaxCapacity() {
       return maxCapacity;
   }

   public void setMaxCapacity(int aMaxCapacity) {
       maxCapacity = aMaxCapacity;
   }

   // adds the water to tank
   void add(int n) {
       current += n;
       if (current > maxCapacity)
           current = maxCapacity;
   }

   // drains water from tank
   void drain(int n) {
       current -= n;
       if (current < 0)
           current = 0;
   }

   // prints current water
   void print() {
       System.out.println("The tank volume is " + current + " gallons");
   }
}

TestHoldTank .java

public class TestHoldTank {
   public static void main(String[] args) {
       HoldTank h = new HoldTank(600, 1000);
       h.print();
       h.add(300);
       h.drain(50);
       h.print();
       h.add(500);
       h.drain(250);
       h.print();
       h.drain(1200);
       h.add(200);
       h.drain(25);
       h.print();
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

<terminated > TestHold Tank [Java Application) C:\Soft\PegaEclipse-win64-4 The tank volume is 600 gallons The tank volume is 850 gallons The tank volume is 750 gallons The tank volume is 175 gallons


Related Solutions

Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Your shopping cart should support...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Using the CLASSES BELOW Your...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). **PLEASE USE THE CLASSES BELOW***...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
Java Coding Part A Create a class Employee. Employees have a name.   Also give Employee a...
Java Coding Part A Create a class Employee. Employees have a name.   Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for taxes (we will do a flat 17%), and the final amount (for...
In C++ Create a class that simulates a school calendar for a course and has a...
In C++ Create a class that simulates a school calendar for a course and has a warner that provides the school administration with the option of warning students when the last day is that a student is permitted to drop the course. (Allow administrators to warn or not, as they wish. Do not make this a required function.) You will assume in this calendar that there are 12 months in a year, 4 weeks in a month, and 7 days...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Part C (25 pts) Coding question Create a Java class name Store_XXXXX with last 5 digits...
Part C (25 pts) Coding question Create a Java class name Store_XXXXX with last 5 digits of your student ID and write your code in there. A PC store sells many types of computers. The PC can have either 16 or 8 gigabytes of memory. The quality of the PCs can be either New, Refurbished, or Dented. The price list is given as follows: Memory size/Status New Refurbished Dented 16 gigabytes 849.99 729.99 609.99 8 gigabytes 699.99 579.99 439.99 Determine...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT