Java please help Method square Area (double side) determines and returns the are
ID: 642955 • Letter: J
Question
Java please help Method square Area (double side) determines and returns the area of a square Method rectangle Area (double width, double length) determines and returns the area of a rectangle. Method circle Area (double radius) determines and returns the area of a circle. Method triangle Area (double base, double height) determines and returns the area of a triangle. Test the methods with different input value road from the user (in the main method) Design the main method of your program such that it allows the user to re -run the program with different inputs (I,e use a loop structure) Document your code. and organize and space the outputs properly. Use escape characters to organize the outputs Sample output isExplanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Area
{
double squareArea(double side)
{
return side*side;
}
double rectangleArea(double width,double length)
{
return width*length;
}
double circleArea(double radius)
{
return (3.14*radius*radius);
}
double triangleArea(double base,double height)
{
return (0.5*base*height);
}
public static void main (String[] args) throws IOException
{
double a,b;
Area obj = new Area();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 1 for square area, 2 for rectangle area, 3 for circle area , 4 for triangle area and 0 to exit" );
int ch=Integer.parseInt(br.readLine());
while(ch!=0)
{
if(ch==1)
{
System.out.println("Enter the Square Side: ");
a=Double.parseDouble(br.readLine());
System.out.println("Square Side = "+a);
System.out.println("Square Area = "+obj.squareArea(a));
}
else if(ch==2)
{
System.out.println("Enter the Rectangle Width: ");
a=Double.parseDouble(br.readLine());
System.out.println("Enter the Rectangle Length: ");
b=Double.parseDouble(br.readLine());
System.out.println("Rectangle Width = "+a);
System.out.println("Rectangle Length = "+b);
System.out.println("Rectangle Area= "+obj.rectangleArea(a,b));
}
else if(ch==3)
{
System.out.println("Enter the Circle Radius: ");
a=Double.parseDouble(br.readLine());
System.out.println("Circle Radius= "+a);
System.out.println("Circle Area= "+obj.circleArea(a));
}
else
{
System.out.println("Enter the Triangle Base: ");
a=Double.parseDouble(br.readLine());
System.out.println("Enter the Triangle Height: ");
b=Double.parseDouble(br.readLine());
System.out.println("Triangle Base = "+a);
System.out.println("Triangle Height= "+b);
System.out.println("Triangle Area= "+obj.triangleArea(a,b));
}
System.out.println("Enter 1 for square area, 2 for rectangle area, 3 for circle area , 4 for triangle area and 0 to exit" );
ch=Integer.parseInt(br.readLine());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.