In: Computer Science
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks =========================================================================== public class SwimmingPool { private int length; private int width; private int depth; private double rateOfFilling; private double rateOfDraining; private int currentDepth; public SwimmingPool() { length = width = depth = 0; rateOfDraining = rateOfFilling = 0.0; currentDepth = 0; } public SwimmingPool(int length, int width, int depth) { this.length = length; this.width = width; this.depth = depth; this.rateOfFilling = 0; this.rateOfDraining = 0; currentDepth = 0; } //determine the amount of water needed to fill an empty public double waterToFillCompletely() { return length * width * depth; } //water needed to fill an partially filled pool, given height public double waterToFillDepth(int height) { return length * width * height; } //determine the time needed to completely public double timeToFillCompletely() { return waterToFillCompletely() / rateOfFilling; } //determine the time needed to completely public double timeToDrainCompletely() { return waterToFillCompletely() / rateOfDraining; } public double timeToFillDepth(int height) { return waterToFillDepth(height) / rateOfFilling; } public double timeToDrainDepth(int height) { return waterToFillDepth(height) / rateOfDraining; } // add water upto a given height public void addWater(int height) { if (height >= 0 && (height + currentDepth) <= depth) { currentDepth += height; } else { System.out.println("Height entered is greater than the depth of the swimming pool."); } } // drain water upto height public void drainWater(int height) { if (height >= 0 && (currentDepth - height) >= 0) { currentDepth -= height; } else { System.out.println("Invalid height. Current depth will be in negative."); } } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getDepth() { return depth; } public void setDepth(int depth) { this.depth = depth; } public double getRateOfFilling() { return rateOfFilling; } public void setRateOfFilling(double rateOfFilling) { this.rateOfFilling = rateOfFilling; } public double getRateOfDraining() { return rateOfDraining; } public void setRateOfDraining(double rateOfDraining) { this.rateOfDraining = rateOfDraining; } public int getCurrentDepth() { return currentDepth; } public void setCurrentDepth(int currentDepth) { this.currentDepth = currentDepth; } }
============================================================
import java.util.Scanner; public class MainSwimmingPool { public static void main(String[] args) { int length, width, height; Scanner scanner = new Scanner(System.in); System.out.print("Enter length, width and height (separated by a space)? "); length = scanner.nextInt(); width = scanner.nextInt(); height = scanner.nextInt(); //The main program will prompt the user for information using a the pool’s parameter constructor or default constructor with setters and getters. SwimmingPool pool = new SwimmingPool(length, width, height); //The main program should use the pool class to determine how much and how long it will take fill an empty pool. double fillingRate, drainingRate; System.out.print("Enter filling rate and draining rate (separated by space)? "); fillingRate = scanner.nextDouble(); drainingRate = scanner.nextDouble(); pool.setRateOfFilling(fillingRate); pool.setRateOfDraining(drainingRate); System.out.println("Time taken to fill the pool completely is: " + pool.timeToFillCompletely() + " mins"); //The main program should use the pool class to determine how much and how long it will take to empty a full pool. System.out.println("Time taken to drain the pool completely is: " + pool.timeToDrainCompletely() + " mins."); //The main program should use the pool class to determine how much and how long it will take to fill an partially filled pool. System.out.print("Enter the current depth of the pool: "); int currentDepth = scanner.nextInt(); pool.addWater(currentDepth); //The main program should use the pool class to determine how much and how long it will take to empty a partial filled pool. System.out.println("Water needed to fill " + currentDepth + " height is: " + pool.waterToFillDepth(currentDepth) + " gallons."); System.out.println("Time needed will be: " + pool.timeToFillDepth(currentDepth) + " mins."); System.out.println("Time needed to drain pool having " + currentDepth + " height is: " + pool.timeToDrainDepth(currentDepth)); } }
==============================================================