(java programming,using Net Beans,if statements only,use of print,println, print
ID: 3625437 • Letter: #
Question
(java programming,using Net Beans,if statements only,use of print,println, printf,declare variables,scanner, keyboard,etc): This program calculates & prints theperimeter of a triangle. Users are asked to input the Cartesian x & y coord. of a triangle's 3 vertices. If the entered points do not form a triangle a message is printed
to that affect. After printing the results a user is asked if new points are to be entered. When no more points are to be entered a summary of the program execution if to be printed. After the first data points you are to be asked twice more to enter data points then after a third time the answer is No. A total of 1 triangle was formed while data for 2 triangles were entered that did not create triangles.
data points:1. (x=1,y=5), (x=-6,y=-4), (x=-3,y=5)
2. (x=1,y=8), (x=2,y=9), (x=3,y=10),3. (x=3,y=0), (x=-3,y=0),(x=6,y=0)
Explanation / Answer
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @author Reena
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception
{
int x1,x2,x3,y1,y2,y3;
String repeat="";
double perimeter;
Scanner keyboard=new Scanner(System.in);
do
{
System.out.print("Enter x1:");
x1=keyboard.nextInt();
System.out.print("Enter y1:");
y1=keyboard.nextInt();
System.out.print("Enter x2:");
x2=keyboard.nextInt();
System.out.print("Enter y2:");
y2=keyboard.nextInt();
//Enter cordinates(x3,y3)
System.out.print("Enter x3:");
x3=keyboard.nextInt();
System.out.print("Enter y3:");
y3=keyboard.nextInt();
if(x1==x2||x2==x3||x3==x1)
{
if(y1==y2||y2==y3||y3==y1)
{
System.out.print("Points doesn't form triangle");
}
}
else
{
double length1=Math.sqrt((Math.pow(x2,2)-Math.pow(x1,2))+((Math.pow(y2,2)-Math.pow(y1,2))));
double length2=Math.sqrt((Math.pow(x3,2)-Math.pow(x2,2))+((Math.pow(y3,2)-Math.pow(y2,2))));
double length3=Math.sqrt((Math.pow(x3,2)-Math.pow(x1,2))+((Math.pow(y3,2)-Math.pow(y1,2))));
perimeter=length1+length2+length3;
System.out.println("Perimeter:"+perimeter);
}
System.out.print("Enter 'Yes' to Repeat ");
repeat=keyboard.nextLine();
}while(repeat=="Yes");
}//end main
}
Hope this will help you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.