Question

In: Computer Science

Write a Java program such that it consists of 2 classes: 1. a class that serves...

Write a Java program such that it consists of 2 classes:

1. a class that serves as the driver (contains main())

2. a class that contains multiple private methods that compute and display

a. area of a triangle (need base and height)

b area of a circle (use named constant for PI) (need radius)

c. area of rectangle (width and length)

d. area of a square (side)

e. surface area of a solid cylinder (height and radius of base)

N.B. You will also need an accessor public method (interface) that is called from the driver. This method should:

a. Display a menu of options to the user. (Suggest use switch statement)

b. Accept input from the user (keyboard) representing parameter values needed.

c. Invoke other [private] methods in the class to perform the necessary area computations

Add comments throughout your code as you deem appropriate.

Solutions

Expert Solution

import java.util.Scanner;

class Geometry {

    static Scanner in = new Scanner(System.in);
    static double PI = 3.14;

// public interface method which asks user for choice and calls private method
    public void performUserOperation() {
        // Show menu
        System.out.println("1. Compute Area of triangle");
        System.out.println("2. Compute Area of Circle");
        System.out.println("3. Compute Area of Rectangle");
        System.out.println("4. Compute Area of Square");
        System.out.println("5. Compute Surface Area of Cylinder");
        System.out.println("Enter choice: ");
        int choice = in.nextInt();

        switch (choice) {
        case 1:
            System.out.print("Enter triangle base: ");
            double base = in.nextDouble();
            System.out.print("Enter triangle height: ");
            double height = in.nextDouble();
            System.out.println("Area: " + triangleArea(base, height));
            break;

        case 2:
            System.out.print("Enter circle radius: ");
            double radius = in.nextDouble();
            System.out.println("Area: " + circleArea(radius));
            break;

        case 3:
            System.out.print("Enter Rectangle width: ");
            double width = in.nextDouble();
            System.out.print("Enter Rectangle height: ");
            height = in.nextDouble();
            System.out.println("Area: " + rectangleArea(width, height));
            break;

        case 4:
            System.out.print("Enter Square side: ");
            double side = in.nextDouble();
            System.out.println("Area: " + squareArea(side));
            break;

        case 5:
            System.out.print("Enter cylinder base radius: ");
            radius = in.nextDouble();
            System.out.print("Enter cylinder height: ");
            height = in.nextDouble();
            System.out.println("Area: " + cylindersurfaceArea(radius, height));
            break;

        default:
            break;
        }
    }

    private double cylindersurfaceArea(double radius, double height) {
        return 2 * PI * radius * (radius + height);
    }

    private double squareArea(double side) {
        return side * side;
    }

    private double rectangleArea(double width, double height) {
        return width * height;
    }

    private double circleArea(double radius) {
        return PI * radius * radius;
    }

    private double triangleArea(double base, double height) {
        return 0.5 * base * height;
    }
}

// driver class
public class GeometryHelper {

    public static void main(String[] args) {
        Geometry geometry = new Geometry();
        
        geometry.performUserOperation();
    }

}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such: Write the method body for the default constructor Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here: public class DynArray { private double[] array; private int size; private int nextIndex;    public int arraySize() { }    public int elements() { } public...
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT