Open Simonefleek opened 5 months ago
class Package { private int length; private int width; private int height;
// Constructor with parameters
public Package(int l, int w, int h) {
length = l;
width = w;
height = h;
}
// Method to calculate and return the volume
public int getVolume() {
return width * height * length;
}
// Method to calculate and return the surface area
public int getSurfaceArea() {
return 2 * (width * length + height * length + height * width);
}
}
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
// Read input for length, width, and height
int length = scnr.nextInt();
int width = scnr.nextInt();
int height = scnr.nextInt();
// Create a Package object
Package myPackage = new Package(length, width, height);
// Display the package details
System.out.println("Package");
System.out.println("Volume is: " + myPackage.getVolume());
System.out.println("Area is: " + myPackage.getSurfaceArea());
scnr.close(); // Close the scanner
}
}
+--------------------------------+
A class Package (like a box) is designed as shown in the UML class diagram. It contains:
Three private instance variables: length, width and height all of type int. A constructor which takes three int arguments for length, width and height. Public methods: getVolume() and getSurfaceArea(), which return the volume and surface area of this instance, respectively. Use these formulas: volume = whl (w multiplied by h multiplied by l) Surface Area = 2(wl + hl + hw) meaning 2 multiplied by the sum of wl (w multiplied by l), plus hl plus hw.
The program reads the values of l, w, and h. Creates a Package object and calls the Package methods to produce the output. Example:
Input 2 2 2
Output Package Volume is : 8 Area is: 24
Input 2 5 10
Output Package Volume is: 100 Area is: 160