logo

import java.util.Scanner; public class rectangle { private

   

Added on  2023-04-08

2 Pages241 Words184 Views
import java.util.Scanner;
public class rectangle {
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()
{
try
{
int val=Integer.parseInt(sc.nextLine());
import java.util.Scanner; public class rectangle { private_1

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Java Code import java.util.Scanner; /** * * File name
|2
|257
|314

Design Documentation Pseudo Code BEGIN Display Welcome message BEGIN
|13
|1512
|68

JAVA Program Using Array of Objects
|28
|2144
|15

Java Assignment Help | Online Homework
|13
|1219
|353

Vehicle Data Inventory System
|18
|2151
|53

CarYardMIT162011 Class Implementation in Java
|6
|1107
|393