Question

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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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");
   }
}

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

Add a comment
Know the answer?
Add Answer to:
Background You will create a Java class that simulates a water holding tank. The holding tank...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

  • public class CylindricalTank {    private String idTag;           // Unique String identifying the tank...

    public class CylindricalTank {    private String idTag;           // Unique String identifying the tank    private double radius;           // The non-negative radius of the base of the tank in meters    private double height;           // The non-negative height of the tank in meters    private double liquidLevel;       // The current height of the liquid in the tank in meters    /**    * CylindricalTank General Constructor    */    public CylindricalTank(String tag, double...

  • To be written in JAVA We want you to write a Java class named Character WithStatus...

    To be written in JAVA We want you to write a Java class named Character WithStatus that can be used to keep track of random game effects status on a role playing game character. The following requirements specify what fields you are expected to implement in your class: - A String data field named name to store the character's name - An int data field named health to store the character's health point; at zero they are dead. - An...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat....

    USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat. Follow the detailed steps below. This is based on Exercises 2.93 - 2.94 (6e) / 2.92 - 2.93 (5e) in the book, but with slight modifications, so be sure to follow the instructions below. Create a new BlueJ project named LastName-heater using your last name. Create a class named Heater At the top of the source code for Heater, add documentation like this: /**...

  • 1) (10 points) Create a new Java project, name it "Quiz02". Create a java class Traveler...

    1) (10 points) Create a new Java project, name it "Quiz02". Create a java class Traveler that has four data members, String name, Date bDate, int id and int noOfDependens. Generate the following: 1- Getters and Setters. 2- Four parametrize constructor to initialize the four data members above. 3- toString() method. Create a java class Flight that has four data members, String number, String destination, int capacity, and ArrayList<Traveler> travelers. • Notel - travelers will be used to track the...

  • Course Class Create a class called Course. It will not have a main method; it is...

    Course Class Create a class called Course. It will not have a main method; it is a business class. Put in your documentation comments at the top. Be sure to include your name. Course must have the following instance variables named as shown in the table: Variable name Description crn A unique number given each semester to a section (stands for Course Registration Number) subject A 4-letter abbreviation for the course (e.g., ITEC, PHED, CHEM) number A 4-digit number associated...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • Create a java class Customer.java that will represent a water company customer. It should contain the...

    Create a java class Customer.java that will represent a water company customer. It should contain the attributes, constructors, and methods listed below, and when finished should be able to allow the included file TestWaterBills.java to work correctly. Customer class Attributes firstName: String type, initial value null lastName: String type, initial value null streetAddress: String type, initial value null city: String type, initial value null state: String type, initial value null zip: String type, initial value null previousMeterReading: int type, initial...

  • Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

    Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields. Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT