Question

#1 Write a java program which calculates the area of either a Triangle or a Rectangle....

#1 Write a java program which calculates the area of either a Triangle or a Rectangle. Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle. Depending on that input - the program must call either the Triangle method or the Rectangle method. Each of those methods must receive 2 input fields from the user - one for base and one for height in order to calculate the area and then to print that area and the height and base from inside the method.

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

Program File

AreaCalculator.java

// import the scanner class to read input from stdin
import java.util.Scanner;

class AreaCalculator {
public static double triangleArea(double length, double base){

// area of triangle is 0.5 * length * base
return 0.5 * length * base;

}

public static double rectangleArea(double length, double width)
{
// area of rectangle is length * width
return length * width;
}

public static void main(String[] args) {

int userChoice;
double length;

// Intro message
System.out.println("***** Welcome to area calculator *****");

// scanner object to read input from stdin
Scanner scan = new Scanner(System.in);

System.out.println("Choose for what you want to calculate area\n1. Triangle\n2. Rectangle\n0. Exit");

// read user choice
userChoice = scan.nextInt();


switch(userChoice){
case 0:
System.exit(0);
break;

// When triangle is selected
case 1:

// read length from stdin
System.out.print("Enter the height of the triangle : ");
length = scan.nextDouble();

// read base from stdin
System.out.print("Enter the base of the triangle : ");
double base = scan.nextDouble();

// calculate area and print to stdout
System.out.println("The area of triangle is : " + triangleArea(length, base));

break;
  
// when rectangle is selected
case 2:

// read length from stdin
System.out.print("Enter the height of the rectangle : ");
length = scan.nextDouble();

// read width from stdin
System.out.print("Enter the width of the rectangle : ");
double width = scan.nextDouble();

// calculate area and print to stdout
System.out.println("The area of rectangle is : " + rectangleArea(length, width));
break;
  
default:
System.out.println("Invalid Entry!");
}
  
}

}

Output

***** Welcome to area calculator *****
Choose for what you want to calculate area
1. Triangle
2. Rectangle
0. Exit
1
Enter the height of the triangle : 10
Enter the base of the triangle : 10
The area of triangle is : 50.0

***** Welcome to area calculator *****
Choose for what you want to calculate area
1. Triangle
2. Rectangle
0. Exit
2
Enter the height of the rectangle : 10
Enter the width of the rectangle : 10
The area of rectangle is : 100.0

Hope this helps!

Please let me know if any changes needed.

Thank you!

Hope you're safe during the pandemic!

Add a comment
Know the answer?
Add Answer to:
#1 Write a java program which calculates the area of either a Triangle or a Rectangle....
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
  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

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

  • JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

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

  • Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate...

    Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate rectangle area. Some requirements: 1. User Scanner to collect user input for length & width 2. The formula is area = length * width 3. You must implement methods getLength, getWidth, getArea and displayData ▪ getLength – This method should ask the user to enter the rectangle’s length and then return that value as a double ▪ getWidth – This method should ask the...

  • Problem: Given information about a circle, rectangle and a triangle, calculate the area of the shape...

    Problem: Given information about a circle, rectangle and a triangle, calculate the area of the shape from the information supplied. Write pseudocode to solve this problem and write a Python program that asks the user to input the required information for a shape and then calculates the area of the shape. Geometry Calculator 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit Enter your choice (1...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • Question 4 Write a program that display the area of a triangle. The program calls the...

    Question 4 Write a program that display the area of a triangle. The program calls the following two functions: .getBaseHeight - This function uses a reference parameter variables to accept a double arguments base and height. This function should ask the user to enter the triangle's buse and height. Input validation: base and height should be positive numbers. calArea - This function should accept the triangle's base and height as arguments and return the triangle's area. The area is calculated...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Using C++, Write a program that will provide the user a menu from which the user...

    Using C++, Write a program that will provide the user a menu from which the user may select to calculate the area of one of four geometric shapes: a circle, a rectangle, a triangle, and a trapezoid. You should use the most appropriate SWITCH block for this program. The user will input all data needed to calculate the area. The program should output all data input by the user, the calculated area, and the geometric shape selected. Run this program...

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