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 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 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Java Write a program to record the GPAs of a class and find the percentage of...
Java Write a program to record the GPAs of a class and find the percentage of students in each GPA. The program first allows the user to enter the number of students enrolled in the class, and then to enter the GPA of all students one by one. For each student, the input must be between 1 and 4 inclusively. Otherwise, the software displays a message as “Invalid number!” and asks for a new GPA for the same student Enter...
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number...
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT