SlideShare a Scribd company logo
1 of 11
Download to read offline
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
Solution
// 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:ttt" + getLength() + " feet");
System.out.println("Width:ttt" + getWidth() + " feet");
System.out.println("Depth:ttt" + getDepth() + " feet");
System.out.println("Fill rate:tt" + getFillRate() + " gallons/minute");
System.out.println("Drain rate:tt" + getDrainRate() + " gallons/minute");
System.out.println("Current gallons:t" + getCurrentGallons() + " gallons");
System.out.println("Total capacity:tt" + 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
*/

More Related Content

Similar to java programming 5th edition chapter 8 user defined classes and ADTs.pdf

LINE WATER JET CLEANING works 2-1.R1 comp
LINE WATER JET CLEANING works   2-1.R1 compLINE WATER JET CLEANING works   2-1.R1 comp
LINE WATER JET CLEANING works 2-1.R1 comp
john marasigan
 
Pumps and Cavitation
Pumps and CavitationPumps and Cavitation
Pumps and Cavitation
Living Online
 
main.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdfmain.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdf
arasanlethers
 
An Minh Tran - Case 3 - Cheg 407
An Minh Tran - Case 3 - Cheg 407An Minh Tran - Case 3 - Cheg 407
An Minh Tran - Case 3 - Cheg 407
AN TRAN
 

Similar to java programming 5th edition chapter 8 user defined classes and ADTs.pdf (20)

Condenser pg test
Condenser pg testCondenser pg test
Condenser pg test
 
waste_water[1]. .pptx
waste_water[1].                      .pptxwaste_water[1].                      .pptx
waste_water[1]. .pptx
 
Thermo fluids lab key
Thermo fluids lab keyThermo fluids lab key
Thermo fluids lab key
 
CSEC Physics Lab - Half Life of liquid draining from burette
CSEC Physics Lab - Half Life of liquid draining from buretteCSEC Physics Lab - Half Life of liquid draining from burette
CSEC Physics Lab - Half Life of liquid draining from burette
 
fluid mechanics exp. Flow rate
fluid mechanics exp. Flow rate fluid mechanics exp. Flow rate
fluid mechanics exp. Flow rate
 
Intro to Fluid Mechanics Design Project
Intro to Fluid Mechanics Design ProjectIntro to Fluid Mechanics Design Project
Intro to Fluid Mechanics Design Project
 
LINE WATER JET CLEANING works 2-1.R1 comp
LINE WATER JET CLEANING works   2-1.R1 compLINE WATER JET CLEANING works   2-1.R1 comp
LINE WATER JET CLEANING works 2-1.R1 comp
 
Pipe line sizing
Pipe line sizingPipe line sizing
Pipe line sizing
 
Pumps and Cavitation
Pumps and CavitationPumps and Cavitation
Pumps and Cavitation
 
Flood-Con (Innovative Stormwater Management)
Flood-Con (Innovative Stormwater Management)Flood-Con (Innovative Stormwater Management)
Flood-Con (Innovative Stormwater Management)
 
Discharge Under a Sluice Gate | Jameel Academy
Discharge Under a Sluice Gate | Jameel AcademyDischarge Under a Sluice Gate | Jameel Academy
Discharge Under a Sluice Gate | Jameel Academy
 
main.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdfmain.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdf
 
Calculation
CalculationCalculation
Calculation
 
An Minh Tran - Case 3 - Cheg 407
An Minh Tran - Case 3 - Cheg 407An Minh Tran - Case 3 - Cheg 407
An Minh Tran - Case 3 - Cheg 407
 
Flow rate and pressure head
Flow rate and pressure headFlow rate and pressure head
Flow rate and pressure head
 
pipe friction for turbulent
pipe friction for turbulentpipe friction for turbulent
pipe friction for turbulent
 
pipe friction for turbulent
pipe friction for turbulentpipe friction for turbulent
pipe friction for turbulent
 
Fluid flow rate Experiment No. 5.pdf
Fluid flow rate Experiment No. 5.pdfFluid flow rate Experiment No. 5.pdf
Fluid flow rate Experiment No. 5.pdf
 
Hydraulics2012
Hydraulics2012Hydraulics2012
Hydraulics2012
 
Fluid mechanics lab manual
Fluid mechanics lab manualFluid mechanics lab manual
Fluid mechanics lab manual
 

More from venkatesh24685

Hi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdfHi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdf
venkatesh24685
 
Explain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdfExplain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdf
venkatesh24685
 
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdfC# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
venkatesh24685
 
“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf
venkatesh24685
 
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdfYou have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
venkatesh24685
 
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdfWhy are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
venkatesh24685
 
When we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdfWhen we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdf
venkatesh24685
 

More from venkatesh24685 (20)

I need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfI need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdf
 
How each of the main themes of The Fifth Discipline are related to e.pdf
How each of the main themes of The Fifth Discipline are related to e.pdfHow each of the main themes of The Fifth Discipline are related to e.pdf
How each of the main themes of The Fifth Discipline are related to e.pdf
 
Hi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdfHi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdf
 
Favor Composition does what and howSolutionWe know what is in.pdf
Favor Composition does what and howSolutionWe know what is in.pdfFavor Composition does what and howSolutionWe know what is in.pdf
Favor Composition does what and howSolutionWe know what is in.pdf
 
Explain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdfExplain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdf
 
astronomy One Nineteenth Century observation which convinced many.pdf
astronomy One Nineteenth Century observation which convinced many.pdfastronomy One Nineteenth Century observation which convinced many.pdf
astronomy One Nineteenth Century observation which convinced many.pdf
 
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdfA) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
 
Describe two (2) attributes of a globally aware EngineerEngineering.pdf
Describe two (2) attributes of a globally aware EngineerEngineering.pdfDescribe two (2) attributes of a globally aware EngineerEngineering.pdf
Describe two (2) attributes of a globally aware EngineerEngineering.pdf
 
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdfC# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
 
“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf
 
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
 
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdfYou have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
 
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdfWhy are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
 
Which of the following are parts of a bond Choose one or more A. t.pdf
Which of the following are parts of a bond Choose one or more A. t.pdfWhich of the following are parts of a bond Choose one or more A. t.pdf
Which of the following are parts of a bond Choose one or more A. t.pdf
 
When we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdfWhen we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdf
 
What is the role of ports for global commerce and why is that roe im.pdf
What is the role of ports for global commerce and why is that roe im.pdfWhat is the role of ports for global commerce and why is that roe im.pdf
What is the role of ports for global commerce and why is that roe im.pdf
 
What are the stages of team development Why is it important for a s.pdf
What are the stages of team development Why is it important for a s.pdfWhat are the stages of team development Why is it important for a s.pdf
What are the stages of team development Why is it important for a s.pdf
 
Two numbers together add to 300. One number is twice the size of the.pdf
Two numbers together add to 300. One number is twice the size of the.pdfTwo numbers together add to 300. One number is twice the size of the.pdf
Two numbers together add to 300. One number is twice the size of the.pdf
 
This Question 2 Pts The primary goal of financial accounting is to p.pdf
This Question 2 Pts The primary goal of financial accounting is to p.pdfThis Question 2 Pts The primary goal of financial accounting is to p.pdf
This Question 2 Pts The primary goal of financial accounting is to p.pdf
 
The irregularity of innovations and the variability of business prof.pdf
The irregularity of innovations and the variability of business prof.pdfThe irregularity of innovations and the variability of business prof.pdf
The irregularity of innovations and the variability of business prof.pdf
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 

Recently uploaded (20)

21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 

java programming 5th edition chapter 8 user defined classes and ADTs.pdf

  • 1. 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
  • 2. 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
  • 3. 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
  • 4. Solution // 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; }
  • 5. 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) {
  • 6. 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:ttt" + getLength() + " feet"); System.out.println("Width:ttt" + getWidth() + " feet"); System.out.println("Depth:ttt" + getDepth() + " feet"); System.out.println("Fill rate:tt" + getFillRate() + " gallons/minute"); System.out.println("Drain rate:tt" + getDrainRate() + " gallons/minute"); System.out.println("Current gallons:t" + getCurrentGallons() + " gallons"); System.out.println("Total capacity:tt" + getTotalGallons() + " gallons"); } } // SwimmingPoolTest.java
  • 7. 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());
  • 8. 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 {
  • 9. 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
  • 10. 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
  • 11. 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 */