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....
Part 1: Write a program that has 2 classes. The first class is a test class...
Part 1: Write a program that has 2 classes. The first class is a test class with 2 methods; a main method, and a static method called drinkMilk(). The drinkMilk method returns void, and will throw an exception of type OutOfMilkException. The purpose of this exercise is to demonstrate that a method can get information back to the caller through an exceptions object that is thrown inside the called method and caught in the calling method.   (OutOfMilkException is the second...
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...
You will write several classes for this program in Java. The name of all classes for...
You will write several classes for this program in Java. The name of all classes for this program will start with y2y3. Concepts to be applied : Classes and objects, inheritance, polymorphism Assignment: An electronic store, named carrefoure, services many customers. The customers’ orders for electronics are delivered. Carrefoure bills their customers once each month. At the end of each month, the regional store manager requests a report of all customers. In this program, you will develop the inheritance hierarchy...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT