In: Computer Science
This is for Java programming. Please use ( else if,) when writing the program)
Write a java program to calculate the circumference for the triangle and the square shapes:
The circumference for: Triangle = Side1 + Side2 +Sid3
Square = 4 X Side
When the program executes, the user is to be presented with 2 choices.
Choice 1 to calculate the circumference for the triangle
Choice 2 to calculate the circumference for the square
Based on the user's selection the selected shape's circumference is calculated
Hand in: The Design in pseudocode
The output for both shapes
The Source codes (java program)
Note the output should look like :
Square:
Side =
Circumference =
Triangle:
Side1 =
Side2 =
Side 3 =
Circumference =
Please up vote ,comment if any query . Be safe.
Psuedo Code :
print "1. Circumference of Triangle."
print "1. Circumference of Square."
print "Enter choice: "
input choice
if choice equals to 1
input side 1,side 2,side 3
calculate circumference
print "Triangle :"
print "side1="
print "side2="
print "side3="
print "Circumference="
if choice equals to 2
input side of square
calculate circumference
print "Square:""
print "side1="
print "Circumference="
Program : ***************Main.java******************************
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new
Scanner(System.in);//scanner object
System.out.println("1.
Circumference of Triangle."); //print menu
System.out.println("1.
Circumference of Square.");
System.out.print("Enter
choice: "); //prompt for choice
int choice=sc.nextInt();
//read choice
if(choice==1)//if
choice is 1
{ //get all sides
System.out.print("Entet all three sides : ");
double side1=sc.nextDouble();
double side2=sc.nextDouble();
double side3=sc.nextDouble();
//print sides and circumference
System.out.println("Triangle=");
System.out.println("\tSide1="+side1);
System.out.println("\tSide2="+side2);
System.out.println("\tSide3="+side3);
System.out.println("\tCircumference = "+(side1+side2+side3));
}
else if(choice==2)
{
//prompt for side
System.out.print("Entet side : ");
double side1=sc.nextDouble();
//print circumference
System.out.println("Square:");
System.out.println("\tSide= "+side1);
System.out.println("\tCircumference = "+(side1*4));
}
else//print if choice
not 1 or 2
System.out.println("Invalid Choice try again.");
}
}
Output :
Please upvote ,comment if any query .