Question 2:
Determining the Perimeter and Area of a Triangle. Use Stream Class.Code:
public class Test {
public static void main (String args[])
{
try
{
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
int n;
do
{
System.out.print("Select your option:");
n=Integer.parseInt(bufferedReader.readLine());
switch(n)
{
case 1:
System.out.print("Enter the legth of the side 1:");
double a=Double.parseDouble(bufferedReader.readLine());
System.out.print("Enter the legth of the side 2:");
double b=Double.parseDouble(bufferedReader.readLine());
System.out.print("Enter the legth of the side 3:");
double c=Double.parseDouble(bufferedReader.readLine());
System.out.println("The perimeter of the triangle is "+(a+b+c));
break;
case 2:
System.out.print("Enter the breath of the triangle:");
double breath=Double.parseDouble(bufferedReader.readLine());
System.out.print("Enter the heigth of the triangle:");
double height=Double.parseDouble(bufferedReader.readLine());
System.out.println("The area of the triangle is "+(0.5*breath*height));
}
}while(n!=3)
;
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Output:
1)To find Perimeter 2)To find Area 3)Exit
Select your option:1
Enter the legth of the side 1:3.0
Enter the legth of the side 2:4.5
Enter the legth of the side 3:8.7
The perimeter of the triangle is 16.2
1)To find Perimeter 2)To find Area 3)Exit
Select your option:2
Enter the breath of the triangle:4.8
Enter the heigth of the triangle:7.9
The area of the triangle is 18.96
1)To find Perimeter 2)To find Area 3)Exit
Select your option:3
No comments:
Post a Comment