Question

In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four...

In Java

Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four methods as follows:

Method squareArea (double side) returns the area of a square.

Method rectangleArea (double width, double length) returns the area of a rectangle.

Method circleArea (double radius) returns the area of a circle.

Method triangleArea (double base, double height) returns the area of a triangle.

In the main method, test all methods with different input value read from the user. Document your code and properly label the input prompts and the outputs as shown below.

Sample run:

Square side:    5.1

Square area:    26.01

Rectangle width:      4.0

Rectangle length:     5.5

Rectangle area:       22.0

Circle radius:        2.5

Circle area:          19.625

Triangle base:        6.4

Triangle height:      3.6

Triangle area:        11.52

Exercise #2: Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your code and properly label the input prompts and the outputs as shown below. Sample run: Square side: 5.1 Square area: 26.01 Rectangle width: 4.0 Rectangle length: 5.5 Rectangle area: 22.0 Circle radius: 2.5 Circle area: 19.625 Triangle base: 6.4 Triangle height: 3.6 Triangle area: 11.52

0 0
Add a comment Improve this question Transcribed image text
Answer #1

file:

import java.util.Scanner;
public class ComputeAreas {

public double circleArea(double radius)
{
double pi = 3.14;
double result = pi * radius * radius;
return result;
}
public double squareArea(double side)
{
double result = side * side;
return result;
}
public double rectangleArea(double width,double length)
{
double result = width * length;
return result;
}
public double triangleArea(double base, double height)
{
double result = (base * height)/2;
  
return result;
}
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
ComputeAreas a = new ComputeAreas();
  
System.out.print("Square side: ");
  
double s = sc.nextDouble();
double result = a.squareArea(s);
System.out.println("Square area: "+result);
  
System.out.print("Rectangle width: ");
double w = sc.nextDouble();
System.out.print("Rectangle length: ");
double l = sc.nextDouble();
result = a.rectangleArea(w, l);
System.out.println("Rectangle area: "+result);
  
System.out.print("Circle Radius: ");
double r = sc.nextDouble();
result = a.circleArea(r);
System.out.println("Circle area: "+ result);
  
System.out.print("Triangle base: ");
w = sc.nextDouble();
System.out.print("Triangle height: ");
double h = sc.nextDouble();
result = a.triangleArea(w,h);
System.out.println("Triangle area: "+result);
  
}
}

Output:

Square side: 2.0                                                                                                                 

Square area: 4.0                                                                                                                 

Rectangle width: 2.0                                                                                                             

Rectangle length: 3.0                                                                                                            

Rectangle area: 6.0                                                                                                              

Circle Radius: 2.0                                                                                                               

Circle area: 12.56                                                                                                               

Triangle base: 2.0                                                                                                               

Triangle height: 4.0                                                                                                             

Triangle area: 4.0

Add a comment
Know the answer?
Add Answer to:
In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design in Python (pseudocode) and implement (source code) a program (name it MyRectangle) that defines the...

    Design in Python (pseudocode) and implement (source code) a program (name it MyRectangle) that defines the following 3 methods: Method isValid() returns true if the sum of the width and height is greater than 30 Method Area() returns the area of the rectangle if it is a valid rectangle Method Perimeter() returns the perimeter of the rectangle if it is a valid rectangle The main method of MyRectangle prompts the user to enter the width and height of a rectangle...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • In Java code Exercise #1: (Java) Design and implement a program (name it MinMaxAvg) that defines...

    In Java code Exercise #1: (Java) Design and implement a program (name it MinMaxAvg) that defines three methods as follows: Method max (int x, int y, int z) returns the maximum value of three integer values. Method min (int X, int y, int z) returns the minimum value of three integer values. Method average (int x, int y, int z) returns the average of three integer values. In the main method, test all three methods with different input value read...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT