0% found this document useful (0 votes)
13 views2 pages

Java Box Class Volume Calculation

Uploaded by

elan29102004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Java Box Class Volume Calculation

Uploaded by

elan29102004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Started on Monday, 15 April 2024, 7:40 PM

State Finished
Completed on Monday, 15 April 2024, 8:18 PM
Time taken 38 mins 46 secs
Marks 20.00/20.00
Grade 100.00 out of 100.00
Name 730422243022 CHANDRU S
Question 1 Create a class Box that uses a parameterized method to initialize the dimensions of a box. The class
Correct should have a method that can return volume. Instantiate an object and print the corresponding volume
Mark 10.00 out in main() method.
of 10.00
Note: Dimensions are width, height, depth of double type.

For example:

Input Result

5.2 8.5 6.4 282.88

Answer: (penalty regime: 0 %)


1 ▼ import java.util.Scanner;
2
3 ▼ public class Box {
4 private double width;
5 private double height;
6 private double depth;
7
8 ▼ public void setDimensions(double width, double height, double depth) {
9 this.width = width;
10 this.height = height;
11 this.depth = depth;
12 }
13
14 ▼ public double getVolume() {
15 return width * height * depth;
16 }
17
18 ▼ public static void main(String[] args) {
19 Scanner sc = new Scanner(System.in);
20 String input = sc.nextLine();
21 String[] parts = input.split(" ");
22 double s = Double.parseDouble(parts[0]);
23 double d = Double.parseDouble(parts[1]);
24 double k = Double.parseDouble(parts[2]);
25 Box myBox = new Box();
26 myBox.setDimensions(s, d, k);
27
28 // Format the output to display only two decimal places
29 String formattedOutput = String.format("%.2f", myBox.getVolume());
30 System.out.println(formattedOutput);
31
32 sc.close();
33 }
34 }

Input Expected Got

 5.2 8.5 6.4 282.88 282.88 

 6.3 7.9 5.6 278.71 278.71 

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

You might also like