(JAVA) A class is a group of objects that have “similar” characteristics and sha
ID: 3823400 • Letter: #
Question
(JAVA) A class is a group of objects that have “similar” characteristics and share common methods such as a “constructor”. The class definition can be coded with the calling main or be coded as a separate externally called file (i.e. one file containing the main and another file containing just the class definition itself).
The main will allow the user to:
Enter a length, width and depth for a rectangular pool in feet OR diameter and depth for a circular pool. Allow the user to enter decimal values (such as 12.5 or 30.75 feet)
Allow the user to select whether to compute for a rectangular pool, a circular pool or Quit. Depending on what type of pool, the capacity constant will be different (i.e. 7.48 gallons per cubic foot for a rectangular vs. 5.9 gallons per cubic foot for a circular pool)
Display the dimensions of that pool (i.e. length, width, depth or diameter and depth) that the user entered
Return:
the volume of the pool in cubic feet
the capacity of that pool in the total gallons of water that it can hold
the amount of time in hours that the pool needs to be filled to full capacity
Run this application as long as the user wishes. Format the output to a maximum of two decimal positions. Add commas to the numerical display
The class will need:
A pool constructor
Volume calculation (Length X Width X Depth for a rectangular pool or Diameter X Diameter X Depth for a circular pool)
Set the number of gallons per cubic foot depending on whether a rectangular or circular pool was selected. 7.48 gallons (a cubic foot contains 7.48 gallons of water) for a rectangular pool and 5.9 gallons for a circular pool (a cubic foot in a circular pool has less gallons).
Capacity calculation (Volume X Gallons per cubic foot).
The number of hours to fill this pool to full capacity. The water source is a maximum of 50 gallons per minute at full speed. (the formula is Capacity / (number of gallons per hour) )
Number of Gallons per hour = Gallons X 60 minutes
Example output:
Your Rectangular Pool is:
24.50 feet in length
30.75 feet across in width
10.25 feet deep
The pool has a volume of 7,722.09 cubic feet
The pool has a capacity of 57,761.26 gallons of water
The pool has a fill time of 19.25 hours
Your Circular Pool has a:
Diameter of 20.00 feet diameter
8.00 feet deep
The pool has a volume of 3,200.00 cubic feet
The pool has a capacity of 18,880.00 gallons of water
The pool has a fill time of 6.29 hours
Explanation / Answer
import java.io.*;//package imported for taking input
class Pool
{
public Pool(double l,double w, double d1) //constructor for rectangular pool
{
double volume=l*w*d1;
double capacity=volume*7.48d;
double time=capacity/(50*60);
System.out.println("The pool has a volume of %.2f"+volume+" cubic feet");
System.out.println("The pool has a capacity of %.2f"+capacity+" gallons of water");
System.out.println("The pool has a fill time of %.2f"+time+" hours");
}//end of constructor
public Pool(double d1,double dep) //constructor for circular pool
{
double volume=d1*d1*dep;
double capacity=volume*5.9d;
double time=capacity/(50*60);
System.out.println("The pool has a volume of %.2f"+volume+" cubic feet");
System.out.println("The pool has a capacity of %.2f"+capacity+" gallons of water");
System.out.println("The pool has a fill time of %.2f"+time+" hours");
}//end of constructor
}//end of class Pool
class DemoPool
{
public static void main(String args[])throws IOException //main method
{
BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); //statement for taking input from user
while(flag==1)
{
System.out.println("Enter 1 for Rectangular pool");
System.out.println("Enter 2 for Circular pool");
System.out.println("Enter 3 to Quit");
int choice=Integer.parseInt(d.readLine());
if(choice==1)
{
System.out.println("Enter length for Rectangular pool in feet");
double length=Integer.parseInt(d.readLine());
System.out.println("Enter width for Rectangular pool in feet");
double width=Integer.parseInt(d.readLine());
System.out.println("Enter depth for Rectangular pool in feet");
double depth=Integer.parseInt(d.readLine());
System.out.println("The Rectangular pool is "+length+" feet in length "+width+" feet across in width "+depth+" feet deep");
Pool obj=new Pool(length,width,depth);
}//end of if
else if(choice==2)
{
System.out.println("Enter diameter for Circular pool in feet");
double diameter=Integer.parseInt(d.readLine());
System.out.println("Enter depth for Circular pool in feet");
double depthCircular=Integer.parseInt(d.readLine());
System.out.println("The Circular Pool has a Diameter of "+diameter+" feet "+depthCircular+" deep");
Pool obj=new Pool(diameter,depthCircular);
}//end of elseif
else if(choice==3)
{
System.out.println("Quitting the program!!!!!");
break;
}//end of elseif
else
{
System.out.println("Invalid choice");
}
}//end of while
}//end of main
}//end of class
I have mentioned all the necessary comments in the program for easy understanding.
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.