java programming 5th edition chapter 8 user defined classes and ADTs use with st
ID: 3782323 • Letter: J
Question
java programming 5th edition chapter 8 user defined classes and ADTs use with strings.
1. Write the definition of a class called SwimmingPool, to implement the properties of a swimming pool.
instance variables:
length (in feet)
width (in feet)
depth (in feet)
fillRate (rate at which water fills pool in gallons per minute)
drainRate (rate at which water drains from pool in gallons per minute)
currentGallons (current number of gallons to fill the pool)
totalGallons (number of gallons to fill the pool)
Constructor methods:
default constructor
values constructor
Member methods:
separate 'set' methods for all instance variables, except totalGallons, which will always be calculated
separate 'get' methods for all instance variables
Determine number of gallons the pool will hold (hint: the total gallons a rectangular pool will hold is the pool's volume * 7.5)
The time needed to completely fill the pool (based on the current gallons)
The time needed to completely drain the pool (based on the current gallons)
Modify the gallons if water is added or drained for a specific number of minutes
Output the values of all instance variables
2. Create a client that will do each of the following:
Instantiate a pool1 object with the default constructor.
Instantiate a pool2 object with the values constructor, use length = 16, width = 32, depth = 4.5, fillRate = 4, drainRate = 8, and currentGallons = 12000.
Use the output method to print all attribute values for both pools.
Use the set methods to assign length = 20, width = 40, depth = 5.5, fillrate = 4.5, drainrate = 10, and currentGallons = 25000 to pool1's attributes.
Calculate the totalGallons for pool1 and pool2.
Use the output methods to print the attribute's values for both pools.
Use the member method to calculate the time it will take to completely fill pool1. Use get methods to output the current gallons and additional gallons needed to fill the pool, along with the fill time.
Use the member method to calculate the time it will take to completly drain pool2. Use the get method to output the number of gallons along with the drain time.
Modify the currentGallons in pool1 if water is added for 5 hours
Modify the currentGallons in pool2 if water is drained for 10 hours
Use the output methods to print the attribute's values for both pools
output:
Pool 1 - Swimming Pool Attributes
Length: 0.0 feet
Width: 0.0 feet
Depth: 0.0 feet
Fill rate: 0.0 gallons/minute
Drain rate: 0.0 gallons/minute
Current gallons: 0.0 gallons
Total capacity: 0.0 gallons
Pool 2 - Swimming Pool Attributes:
Length: 16.0 feet
Width: 32.0 feet
Depth: 4.5 feet
Fill rate:4.0 gallons/minute
Drain rate: 8.0 gallons/minute
Current gallons: 12000.0 gallons
Total capacity: 0.0 gallons
Pool 1 - Swimming Pool Attributes:
Length: 20.0 feet
Width: 40.0 feet
Depth: 5.5 feet
Fill rate: 4.5 gallons/minute
Drain rate: 10.0 gallons/minute
Current gallons: 25000.0 gallons
Total capacity: 33000.0 gallons
Pool 2 - Swimming Pool Attributes:
Length: 16.0feet
Width: 32.0 feet
Depth: 4.5 feet
Fill rate: 4.0 gallons/minute
Drain rate: 8.0 gallons/minutes
Current gallons: 12000.0 gallons
Total capacity: 17280.0 gallons
Fill time data for pool 1:
Current gallons: 25000.0
Gallons needed to fill pool: 8000.0
Fill time: 1777.8 minutes
Drain time data for pool 2:
Current gallons: 12000.0
Drain time: 1500.0 minutes
Do you want to add water to or drain water from the pool?
1 - Add water
2 - Drain water
1
How many minutes will water water be added to the pool? 300
Do you want to add water to or drain water from the pool?
1 - Add water
2 - Drain water
2
How many minutes will water be drained from the pool? 600
Pool 1 - Swimming Pool Attributes:
Length: 20.0 feet
Width: 40.0 feet
Depth: 5.5 feet
Fill rate: 4.5 gallons/minute
Drain rate: 10.0 gallons/minute
Current gallons: 26350.0 gallons
Total capacity: 33000.0 gallons
Pool 2 - Swimming Pool Attributes:
Length: 16.0 feet
Width: 32.0 feet
Depth: 4.5 feet
Fill rate: 4.0 gallons/minute
Drain rate: 8.0 gallons/minute
Current gallons: 7200.0 gallons
Total capacity: 17280.0 gallons
Explanation / Answer
// SwimmingPool.java
public class SwimmingPool {
public SwimmingPool(){}
public SwimmingPool(double length, double width, double depth,
double fillRate, double drainRate, double currentGallons) {
this.length = length;
this.width = width;
this.depth = depth;
this.fillRate = fillRate;
this.drainRate = drainRate;
this.currentGallons = currentGallons;
}
private double length;
private double width;
private double depth;
private double fillRate;
private double drainRate;
private double currentGallons;
private double totalGallons;
public void calculateTotalGallons()
{
double volume = this.length * this.width * this.depth;
this.totalGallons = 7.5 * volume;
}
public double timeToFill()
{
double volRemaining = this.totalGallons - this.currentGallons;
double time = volRemaining/this.fillRate;
return time;
}
public double timeToDrain()
{
double time = this.currentGallons/this.drainRate;
return time;
}
public void addGallons(double time)
{
double volumeAdded = this.fillRate*time;
setCurrentGallons(volumeAdded+getCurrentGallons());
}
public void removeGallons(double time)
{
double volumeRemoved = this.drainRate*time;
setCurrentGallons(getCurrentGallons()-volumeRemoved);
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getDepth() {
return depth;
}
public void setDepth(double depth) {
this.depth = depth;
}
public double getFillRate() {
return fillRate;
}
public void setFillRate(double fillRate) {
this.fillRate = fillRate;
}
public double getDrainRate() {
return drainRate;
}
public void setDrainRate(double drainRate) {
this.drainRate = drainRate;
}
public double getCurrentGallons() {
return currentGallons;
}
public void setCurrentGallons(double currentGallons) {
this.currentGallons = currentGallons;
}
public double getTotalGallons() {
return totalGallons;
}
public void output() {
System.out.println("Length: " + getLength() + " feet");
System.out.println("Width: " + getWidth() + " feet");
System.out.println("Depth: " + getDepth() + " feet");
System.out.println("Fill rate: " + getFillRate() + " gallons/minute");
System.out.println("Drain rate: " + getDrainRate() + " gallons/minute");
System.out.println("Current gallons: " + getCurrentGallons() + " gallons");
System.out.println("Total capacity: " + getTotalGallons() + " gallons");
}
}
// SwimmingPoolTest.java
import java.util.Scanner;
public class SwimmingPoolTest {
public static void main(String[] args)
{
SwimmingPool pool1 = new SwimmingPool();
SwimmingPool pool2 = new SwimmingPool(16,32,4.5,4,8,12000);
System.out.println("Pool1 - Swimming Pool Attributes");
pool1.output();
System.out.println("Pool2 - Swimming Pool Attributes");
pool2.output();
pool1.setLength(20);
pool1.setWidth(40);
pool1.setDepth(5.5);
pool1.setFillRate(4.5);
pool1.setDrainRate(10);
pool1.setCurrentGallons(25000);
pool1.calculateTotalGallons();
pool2.calculateTotalGallons();
System.out.println("Pool1 - Swimming Pool Attributes");
pool1.output();
System.out.println("Pool2 - Swimming Pool Attributes");
pool2.output();
System.out.println();
System.out.println("Fill time data for pool 1:");
double time = pool1.timeToFill();
double gallonToFill = pool1.getTotalGallons() - pool1.getCurrentGallons();
System.out.println("Current gallons: " + pool1.getCurrentGallons());
System.out.println("Gallons needed to fill pool: " + gallonToFill);
System.out.println("Fill time: " + time + " minutes");
System.out.println("Drain time data for pool 2:");
double drainTime = pool2.timeToDrain();
System.out.println("Current gallons: " + pool1.getCurrentGallons());
System.out.println("Drain time: " + time + " minutes");
System.out.println("Do you want to add water to or drain water from the pool?");
System.out.println("1 - Add water");
System.out.println("2 - Drain water");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if (choice == 1)
{
System.out.println("How many minutes will water water be added to the pool?");
int timeAdded = sc.nextInt();
pool1.addGallons(timeAdded);
}
else {
System.out.println("How many minutes will water water be drained from the pool?");
int timeAdded = sc.nextInt();
pool1.removeGallons(timeAdded);
}
System.out.println("Do you want to add water to or drain water from the pool?");
System.out.println("1 - Add water");
System.out.println("2 - Drain water");
choice = sc.nextInt();
if (choice == 1)
{
System.out.println("How many minutes will water water be added to the pool?");
int timeAdded = sc.nextInt();
pool2.addGallons(timeAdded);
}
else {
System.out.println("How many minutes will water water be drained from the pool?");
int timeAdded = sc.nextInt();
pool2.removeGallons(timeAdded);
}
System.out.println("Pool1 - Swimming Pool Attributes");
pool1.output();
System.out.println("Pool2 - Swimming Pool Attributes");
pool2.output();
}
}
/*
Sample run
Pool1 - Swimming Pool Attributes
Length: 0.0 feet
Width: 0.0 feet
Depth: 0.0 feet
Fill rate: 0.0 gallons/minute
Drain rate: 0.0 gallons/minute
Current gallons: 0.0 gallons
Total capacity: 0.0 gallons
Pool2 - Swimming Pool Attributes
Length: 16.0 feet
Width: 32.0 feet
Depth: 4.5 feet
Fill rate: 4.0 gallons/minute
Drain rate: 8.0 gallons/minute
Current gallons: 12000.0 gallons
Total capacity: 0.0 gallons
Pool1 - Swimming Pool Attributes
Length: 20.0 feet
Width: 40.0 feet
Depth: 5.5 feet
Fill rate: 4.5 gallons/minute
Drain rate: 10.0 gallons/minute
Current gallons: 25000.0 gallons
Total capacity: 33000.0 gallons
Pool2 - Swimming Pool Attributes
Length: 16.0 feet
Width: 32.0 feet
Depth: 4.5 feet
Fill rate: 4.0 gallons/minute
Drain rate: 8.0 gallons/minute
Current gallons: 12000.0 gallons
Total capacity: 17280.0 gallons
Fill time data for pool 1:
Current gallons: 25000.0
Gallons needed to fill pool: 8000.0
Fill time: 1777.7777777777778 minutes
Drain time data for pool 2:
Current gallons: 25000.0
Drain time: 1777.7777777777778 minutes
Do you want to add water to or drain water from the pool?
1 - Add water
2 - Drain water
1
How many minutes will water water be added to the pool?
300
Do you want to add water to or drain water from the pool?
1 - Add water
2 - Drain water
2
How many minutes will water water be drained from the pool?
600
Pool1 - Swimming Pool Attributes
Length: 20.0 feet
Width: 40.0 feet
Depth: 5.5 feet
Fill rate: 4.5 gallons/minute
Drain rate: 10.0 gallons/minute
Current gallons: 26350.0 gallons
Total capacity: 33000.0 gallons
Pool2 - Swimming Pool Attributes
Length: 16.0 feet
Width: 32.0 feet
Depth: 4.5 feet
Fill rate: 4.0 gallons/minute
Drain rate: 8.0 gallons/minute
Current gallons: 7200.0 gallons
Total capacity: 17280.0 gallons
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.