Simonefleek / Zybooks

Random Number
0 stars 0 forks source link

5.4.1: Writing mathematical methods. #13

Open Simonefleek opened 3 months ago

Simonefleek commented 3 months ago

Define the following methods:

calculateBaseArea() has one double parameter as a cube's side length. The method returns the area of the cube's base as a double. The area of the base is calculated by: calculateVolume() has one double parameter as a cube's side length. The method returns the cube's volume as a double, and uses the calculateBaseArea() method to calculate the cube's base area. The volume is calculated by: Click here for example Ex: If the input is 2.0, then the output is: Side length: 2.0 Base area: 4.0 Volume: 8.0

Simonefleek commented 3 months ago

import java.util.Scanner;

public class CubeCalculations {

public static double calculateBaseArea(double sideLength) { return Math.pow(sideLength, 2); }

// Calculate the volume of the cube
public static double calculateVolume(double sideLength) {
    double baseArea = calculateBaseArea(sideLength);
    return baseArea * sideLength;
}

public static void main(String[] args) { Scanner scan = new Scanner(System.in); double sideLength;

  sideLength = scan.nextDouble();

  System.out.println("Side length: " + sideLength);
  System.out.printf("Base area: %.1f\n", calculateBaseArea(sideLength));
  System.out.printf("Volume: ");
  System.out.printf("%.1f\n", calculateVolume(sideLength));

} }