In: Computer Science
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.
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
How to connect two files?
My code:
package home;
public class Main { public static void main(String[] args) { HoldingTank tank = new HoldingTank(600,1000); tank.print(); tank.add(300); error tank.drain(50); tank.print(); tank.add(500); error tank.drain(250); tank.print(); tank.drain(1200); tank.add(200); error tank.drain(25); tank.print(); } }
package home; public class HoldingTank { private int current; private int maxCapacity; public HoldingTank(int current,int maxCapacity) { if (this.current > maxCapacity) { this.current = maxCapacity; } else { this.current = current; } this.maxCapacity = maxCapacity; } public int getCurrent(){ return current; } public int getMaxCapacity() { return maxCapacity; } public void drain(int volume){ if((this.current-volume)<0){ this.current = 0; }else{ this.current-= volume; } } public void print(){ System.out.println("The tank volume is "+this.current+"gallon"); } }
Reason for the error :
public void add(int
volume){
if((this.current+volume)>maxCapacity){
this.current = maxCapacity;
}else{
this.current+= volume;
}
}
=====================================================================
Complete HoldingTank.java file
package home;
public class HoldingTank {
private int current;
private int maxCapacity;
public HoldingTank(int current,int maxCapacity) {
if (this.current > maxCapacity) {
this.current = maxCapacity;
} else {
this.current = current;
}
this.maxCapacity = maxCapacity;
}
public int getCurrent(){
return current;
}
public int getMaxCapacity() {
return maxCapacity;
}
public void add(int volume){
if((this.current+volume)>maxCapacity){
this.current = maxCapacity;
}else{
this.current+= volume;
}
}
public void drain(int volume){
if((this.current-volume)<0){
this.current = 0;
}else{
this.current-= volume;
}
}
public void print(){
System.out.println("The tank volume is " + this.current + "
gallons");
}
}
=============================================================================
=============================================================================
Sample output: