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

shape.java :

//import package
import java.util.Scanner;

//Java class
public class shape {
   // Object of Scanner class
   Scanner sc = new Scanner(System.in);

//private methods that compute and display area of a triangle
   private void triangleArea(double base, double height) {
       // compute and print area of triangle
       System.out.println(
               "Area of triangle with base " + base + " and height " + height + " is : " + (base * height) / 2);
   }

   // private methods that compute and display area of a circle
   private void circleArea(double radius) {
       // compute and print area of circle
       System.out.println("Area of circle with radius " + radius + " is : " + (Math.PI * radius * radius));
   }

   // private methods that compute and display area of rectangle (width and length)
   private void rectangleArea(double width, double length) {
       // compute and print area of rectangle
       System.out.println(
               "Area of rectangle with width " + width + " and length " + length + " is : " + (width * length));
   }

   // private methods that compute and display area of a square (side)
   private void squareArea(double side) {
       // compute and print area of square
       System.out.println("Area of square with side " + side + " is : " + (side * side));
   }

   // private methods that compute and display surface area of a solid cylinder
   // (height and radius of base)
   private void cylinderSurafceArea(double height, double radius) {
       // compute and print surface area of a solid cylinder (height and radius of
       // base)
       System.out.println("Area of solid cylinder with height " + height + " and radius " + radius + " is : "
               + (2.0 * Math.PI * radius * height + 2.0 * Math.PI * radius * radius));
   }

   // This method will display menu to the user
   public void displayMenu() {
       System.out.println("Select Shape to calculate area : ");
       System.out.println("1.Area of a triangle");
       System.out.println("2.Area of a circle ");
       System.out.println("3.Area of a rectangle ");
       System.out.println("4.Area of a square ");
       System.out.println("5.Area of a solid cylinder ");
       // asking user choice
       System.out.print("Select choice : ");
       // reading choice
       int choice = sc.nextInt();
       // using switch statement
       switch (choice) {
       case 1:// when area of triangle is selected
               // asking user base
           System.out.print("Enter base : ");
           double base = sc.nextDouble();// reading base
           // asking user height
           System.out.print("Enter height : ");
           double height = sc.nextDouble();// reading height
           // call method and pass base and height
           triangleArea(base, height);
           break;
       case 2:// when area of circle is selected
               // asking user radius
           System.out.print("Enter radius : ");
           double radius = sc.nextDouble();// reading radius
           // call method and pass radius
           circleArea(radius);
           break;
       case 3:// when area of rectangle is selected
               // asking user width
           System.out.print("Enter width : ");
           double width = sc.nextDouble();// reading width
           // asking user length
           System.out.print("Enter length : ");
           double length = sc.nextDouble();// reading length
           // call method and pass width and length
           rectangleArea(width, length);
           break;
       case 4:// when area of square is selected
               // asking user side
           System.out.print("Enter side : ");
           double side = sc.nextDouble();// reading side
           // call method and pass side
           squareArea(side);
           break;
       case 5:// when area of solid cylinder is selected
               // asking user width
           System.out.print("Enter height : ");
           double height1 = sc.nextDouble();// reading height
           // asking user radius of base
           System.out.print("Enter radius of base : ");
           double radiusOfBase = sc.nextDouble();// reading radius of base
           // call method and pass height and radius
           cylinderSurafceArea(height1, radiusOfBase);
           break;
       default ://when invalid choice is entered
           System.out.println("Enter valid choice.");

       }
   }

}
***********************************

shapeTest.java :

//import package
import java.util.*;

//Java class
public class shapeTest {
   // main() method
   public static void main(String[] args) {
      
       // creating obejct of Shape class
       shape s = new shape();
       // calling method to display menu
       s.displayMenu();
      

   }

}
=========================================

Screen showing area of triangle :

Screen showing area of side :


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
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..
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT