Java Program: Area Calculation of Triangles and Rectangles

Verified

Added on  2023/04/08

|2
|257
|314
Homework Assignment
AI Summary
This Java program calculates the area of triangles and rectangles based on user input. The program prompts the user to select either a triangle or a rectangle. Then, it asks for the necessary dimensions (height and width for a rectangle, or height and base width for a triangle). Input validation ensures that the dimensions entered are positive decimal values. Finally, the program calculates and displays the area of the selected shape, using the formulas width * height for rectangles and 0.5 * base width * height for triangles. This code, designed in Eclipse IDE, demonstrates basic input/output operations and conditional logic in Java, offering a practical solution for a common geometric calculation task, available for students on Desklib.
Document Page
Java Code
import java.util.Scanner;
/**
*
* File name :AreaCalculator.java
* Date :16-Mar-2019
* Time :7:05:45 PM
*/
public class AreaCalculator {
private static Scanner sc;
public static void main(String[] args) {
sc=new Scanner(System.in);
String figure=getFigure();
System.out.print("\nEnter the height for the "+figure+": ");
double height=readDouble();
if(figure.equals("rectangle"))
{
System.out.print("Enter the width for the "+figure+": ");
double width=readDouble();
System.out.println("\n\nArea of the Rectangle is
"+height*width);
}
else
{
System.out.print("Enter the base width for the "+figure+": ");
double width=readDouble();
System.out.println("\n\nArea of the Triangle is
"+String.format("%.2f",0.5*height*width));
}
}
/**
* Read a positive decimal value and it should be greater than 0
* @return the read decimal value
*/
private static double readDouble() {
try
{
double val=Double.parseDouble(sc.nextLine());
if(val<=0)
throw new Exception();
return val;
}
catch(Exception e)
{
System.out.print("\nPlease enter a positive decimal value
(Greater than 0) : ");
return readDouble();
}
}
/**
* Read a positive integer and it should be greater than 0
* @return the read integer
*/
private static int getInt()
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
{
try
{
int val=Integer.parseInt(sc.nextLine());
if(val<=0)
throw new Exception();
return val;
}
catch(Exception e)
{
System.out.print("\nPlease enter a positive int (Greater than
0) : ");
return getInt();
}
}
/**
* Give the user a choice and return the selected figure by the user
* @return
*/
private static String getFigure() {
System.out.println("Select the required option.");
System.out.println("1. Calculate area of a triangle");
System.out.println("2. Calculate area of a rectangle");
System.out.print("Please select one option :");
int val=getInt();
if(val==1)
return "triangle";
else if(val == 2)
return "rectangle";
else
{
System.out.println("\n\nPlease select a valid option.");
return getFigure();
}
}
}
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]