Anda di halaman 1dari 5

//-----------------------------------------------------------------------------//

Author:
Kenny Chin
//
Panther ID:
4853379
//
Class:
COP 3337
//
Section:
U04
//
Assignment#:
2
//
Due Date:
September 15, 2016
//
//
I hereby certify that this collective work is my own
//
and none of it is the work of any other person or entity
//
//
//
//-----------------------------------------------------------------------------/*
Program Description
This program is meant to create a triangle that uses x,y coordinates. It
can
then be used to create a triangle object where relevant data on a triang
le
can be calculated, for example area. The triangle class validates the us
er
input.
This program uses Heron's formula to calculate Area, the formula to
find the distance between 2 points and the law of Cosines to find
the value of the angles.
-------------------------------------------------------------------------------How to compile:
javac CreateTriangle.java Tester.java
java Tester
*/
package triangle;
import java.util.Scanner;
import java.lang.Math;
public class CreateTriangle
{
//coordinates
private double x1 = 0.0,
y1 = 0.0,
x2
y2
x3
y3

=
=
=
=

0.0,
0.0,
0.0,
0.0;

Scanner input = new Scanner(System.in);


public CreateTriangle()
{
set_x1(0.0);
set_y1(0.0);
set_x2(0.0);
set_y2(0.0);
set_x3(0.0);
set_y3(0.0);
}

public void getInput()


{
double get_num = 0.0;
for (int i = 1; i <= 3 ; i++)
{
//Input Validation for x - coordinates
do {
System.out.print( "Please enter your x - coordinate: " )
;
if (input.hasNextDouble() == false)
{
System.out.println("That's not a valid input
!");
input.next();
}
} while (input.hasNextDouble() == false);
get_num = input.nextDouble();
System.out.println("Input validated: " + get_num);
if (i == 1)
{
set_x1(get_num);
}
else if (i == 2)
{
set_x2(get_num);
}
else
{
set_x3(get_num);
}
//Input Validation for y - coordinates
do {
System.out.print("Please enter your y - coordinate: ");
if ( input.hasNextDouble() == false )
{
System.out.println("That's not a valid input
!");
input.next();
}
} while (input.hasNextDouble() == false);
get_num = input.nextDouble();
System.out.println("Input validated: " + get_num);
if (i == 1)
{
set_y1(get_num);
}

else if (i == 2)
{
set_y2(get_num);
}
else
{
set_y3(get_num);
}
}
System.out.println();
input.close();
}
private void set_x1(double x_1)
{
x1 = x_1;
}
private void set_y1(double y_1)
{
y1 = y_1;
}
private void set_x2(double x_2)
{
x2 = x_2;
}
private void set_y2(double y_2)
{
y2 = y_2;
}
private void set_x3(double x_3)
{
x3 = x_3;
}
private void set_y3(double y_3)
{
y3 = y_3;
}
public double getSide1()
{
return Math.sqrt( Math.pow( (x2 - x1) , 2) + Math.pow( (y2 - y1)
,2 ) );
}
public double getSide2()
{
return Math.sqrt( Math.pow( (x3 - x1) , 2) + Math.pow( (y3 - y1)
,2 ) );
}
public double getSide3()
{
return Math.sqrt( Math.pow( (x3 - x2) , 2) + Math.pow( (y3 - y2)
,2 ) );

}
public double calculateAngle1()
{
return ( Math.acos(
(
( Math.p
ow(getSide2(), 2) ) +
( Math.p
ow(getSide3(), 2) ) ( Math.p
ow(getSide1(), 2) )
)
/
( 2 * ge
tSide2() * getSide3() )
) * (180/Math.PI)
);
}
public double calculateAngle2()
{
return ( Math.acos(
(
( Math.p
ow(getSide1(), 2) ) +
( Math.p
ow(getSide3(), 2) ) ( Math.p
ow(getSide2(), 2) )
)
/
( 2 * ge
tSide1() * getSide3() )
) * (180/Math.PI)
);
}
public double calculateAngle3()
{
return ( 180 - ( calculateAngle1() + calculateAngle2() ) );
}
public double getPerimeter()
{
return ( getSide1() + getSide2() + getSide3() );
}
public double getArea()
{
double s = ( getPerimeter() ) / 2;
return ( Math.sqrt( ( ( s * ( s - getSide1() ) *
( s - ge
tSide2() ) *
( s - ge
tSide3() )
)
)
)

);
}

Anda mungkin juga menyukai