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 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...
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...
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...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: -...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: - have one that defines an exception - have that exception throw(n) in one method and handled in another -has the program continue even if the user inputs incorrect data -be creative/unique in some way
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
In Python, create a program with 2 classes that do the following. HashCreate class, this class...
In Python, create a program with 2 classes that do the following. HashCreate class, this class will accept a directory and hash each file in the directory and store the results in a dictionary. The dictionary will contain the hash value and file name. HashVerify, the second class will accept the dictionary as input and save that in an instance attribute. This class must also contain a method for lookups that require a file path as input. The lookup method...
WRITE A JAVA PROGRAM - define a class that maintains information about circles in 2 dimensional...
WRITE A JAVA PROGRAM - define a class that maintains information about circles in 2 dimensional plane. Be sure to include required data members and methods to initialize a circle object. provide information on the circle and reposition the circle. do not specify any other methods . in addition make sure data members are accessible in derived classes..
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT