Problem 4 has you extend an existing program that calculates the volume of a swi
ID: 668048 • Letter: P
Question
Problem 4 has you extend an existing program that calculates the volume of a swimming pool. You will add the additional Java statements to accommodate a diving well, which will be deeper than the main pool.
Problem 4 (name this Lab1_Problem4)
This problem has you calculate the volume of a rectangular swimming pool of constant depth by modifying starter code (provided at the end of this problem). Once you get that working, you will continue to extend your program by adding a diving well and getting the user to supply the additional data needed to compute the additional volume.
Assume a constant depth for both the main pool and the diving well; there is no gradual slope.
If we were to be able to see the pool from the side, the MAIN part of the pool would look like this:
If we could see the “diving well” as a cross section, we'd see it added on to the left of the main pool—and it would be deeper:
Step 1: Starter code is provided at the end of the last step for this problem. Find it, being sure to include the closing curly brace. Then, copy it (Edit | Copy or Ctrl + C).
Step 2: Follow the instructions for creating a new Java project in Using DrJava.
Step 3: Paste in the starter code. Fill in the project's identification information. The developer is you, the description something like, “Lab1_Problem4: diving well addition to a pool.”
Step 4: Run your program (Compile, then Run). Debug as needed.
Step 5: Modify the verbiage for the prompts so they clearly ask the user for the main swimming pool’s volume.
Step 6: Add the code needed to declare two additional input-capture variables needed to accommodate the diving well: length and height; assume for this problem that the width is the same as for the main pool. Name them appropriately so they are easily distinguished from the variables used to store the dimensions of the main part of the swimming pool.
Step 7: Add the code needed to prompt the user for the length and height (depth) of the diving well in the INPUT section after the statements to get the dimensions for the main pool. Make sure your prompts make it clear that you want the diving well’s height.
Step 8: In PROCESSING AND CALCULATIONS, add a statement to compute the volume of the diving well; you will need to go back and declare an additional expression-result variables.
Step 9: Add another statement to compute the total volume of the main pool plus the diving well, also in the PROCESSING AND CALCULATIONS section.
Step 10: Display the resulting values so your output looks like this. You will need to add System.out.println(); commands to your output strategically to obtain the blank lines that are shown below. Test it with the values provided below (75, 25, 4 for the main pool; 15 and 12 for the diving well, respectively):
Enter the length of the main pool in feet: 75
Enter the width of the main pool in feet: 25
Enter the depth of the main pool in feet: 4
Enter the length of the diving well in feet: 15
Enter the depth of the diving well in feet: 12
Volume in cubic feet for main pool: 7500
Volume in cubic feet for diving well: 4500
Total volume in cubic feet: 12000
Step 11: Close DrJava and save your work.
Starter code:
import java.util.Scanner;
public class Lab1_Problem4
{
public static void main(String[] args)
{
// DECLARATIONS
// Input-capture variables:
double dLength;
double dWidth;
double dDepth;
// Expression-result variables:
double dCubicFeet;
// Counter, accumulator, or flag variables:
// Other variables:
// Instantiations:
Scanner cin = new Scanner(System.in);
// INITIALIZE VARIABLES
// INPUT
System.out.print("Enter length in feet: ");
dLength = cin.nextDouble();
System.out.println();
System.out.print("Enter width in feet: ");
dWidth = cin.nextDouble();
System.out.println();
System.out.print("Enter depth in feet: ");
dDepth = cin.nextDouble();
System.out.println();
// PROCESSING AND CALCULATIONS
dCubicFeet = dLength * dWidth * dDepth;
// OUTPUT
System.out.println("Volume in cubic feet for main pool: " + dCubicFeet + " cubic feet");
System.out.println();
}
}
Assume a constant depth for both the main pool and the If we were to be able to see the pool from the side, the MAIN part of the pool would look like this: If we could see the "diving well as a cross section, we'd see it added on to the left of the main pool-and it would be deeper:Explanation / Answer
Below is the working code, written according to the steps mentioned in the question. The new/modified code lines are marked in bold. The code is very clear & easy to understand, let me know(comment) if you need any explanation or have any doubt related to the answer.
import java.util.Scanner;
public class Lab1_Problem4
{
public static void main(String[] args)
{
// DECLARATIONS
// Input-capture variables:
double dLength;
double dWidth;
double dDepth;
double dwLength;
double dwDepth;
// Expression-result variables:
double dCubicFeet;
double dwCubicFeet;
// Counter, accumulator, or flag variables:
// Other variables:
// Instantiations:
Scanner cin = new Scanner(System.in);
// INITIALIZE VARIABLES
// INPUT
System.out.print("Enter the length of main pool in feet: ");
dLength = cin.nextDouble();
System.out.println();
System.out.print("Enter the width of main pool in feet: ");
dWidth = cin.nextDouble();
System.out.println();
System.out.print("Enter the depth of main pool in feet: ");
dDepth = cin.nextDouble();
System.out.println();
System.out.print("Enter the length of diving well in feet: ");
dwLength = cin.nextDouble();
System.out.println();
System.out.print("Enter the depth of diving well in feet: ");
dwDepth = cin.nextDouble();
System.out.println();
// PROCESSING AND CALCULATIONS
dCubicFeet = dLength * dWidth * dDepth;
dwCubicFeet = dwLength * dWidth * dwDepth;
// OUTPUT
System.out.println("Volume in cubic feet for main pool: " + dCubicFeet + " cubic feet");
System.out.println("Volume in cubic feet for diving well: " + dwCubicFeet + " cubic feet");
System.out.println("Total volume in cubic feet: " + (dCubicFeet + dwCubicFeet) + " cubic feet");
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.