CSCI-2467 Lab 5 – Holding Tank Class

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.

 

Create an instance of HoldingTank, named tank with a maximum capacity of 1200 gallons and a current volume of 400 gallons.

Print the current volume of the tank

Add 400 gallons

Drain 150 gallons

Print the current volume of the tank

Add 800 gallons

Drain 250 gallons

Print the current volume of the tank

Drain 1200 gallons

Add 400 gallons

Drain 75 gallons

Print the current volume of the tank

Turning in Your Assignment

Once you’ve completed and thoroughly tested your assignment, you are ready to submit it. Zip your Intellij project folder for this lab and upload the zip file to Blackboard for grading.

 

Students who submit projects that do not compile will receive a zero for the lab.

 

Example Output

The tank volume is 400 gallons

 

The tank volume is 650 gallons

 

The tank volume is 950 gallons

 

The tank volume is 325 gallons

Need a custom answer at your budget?

This assignment has been answered 6 times in private sessions.

Or buy a ready solution below.

Buy ready solution

Solution previews

© 2024 Codify Tutor. All rights reserved