I am writing a program that allows the user to input the sides of a triangle and recieve information about their triangle such as height, angle measurment, type of triangle, and area. For some reason my program wont register the if statments. I have tried switching conditions and other various measures yet nothing works. If somebody could take a look I would appreciate it.
import java.lang.*;
public class Mathey
{
public double squareRoot(double n)
{
return Math.sqrt(n);
}
public double square(double n)
{
return Math.pow(n,2);
}
public double sin(double n)
{
return Math.sin(n);
}
public double arcCos(double n)
{
return Math.acos(n);
}
public double round(double n)
{
return Math.round(n);
}
public double degrees (double n)
{
return Math.toDegrees(n);
}
public double radians (double n)
{
return Math.toRadians(n);
}
} Here is the triangle class
import java.util.*;
public class TriangleCrazy
{
private int a,b,c;
private double h,alpha2,beta2,gamma2;
private int type;
Mathey mathStuff = new Mathey ();
Scanner reader = new Scanner (System.in);
public TriangleCrazy(int a2, int b2, int c2)
{
a = a2;
b = b2;
c = c2;
}
public void triTest()
{
int flag =1;
while (flag==1)
{
if (a+b>c && b+c>a && a+c>b)
{
System.out.println("You have enetered a proper triangle");
flag =0;
}
else if (a+b<=c || b+c<=a || a +c<=b)
{
System.out.println("Sorry you did not enter a proper triangle please try again");
System.out.print("Please enter the sides of your triangle (A B C) " );
int a = reader.nextInt();
int b = reader.nextInt();
int c = reader.nextInt();
this.a = a;
this.b = b;
this.c = c;
}
}
}
public void triType()
{
double test = mathStuff.square(a) + mathStuff.square(c);
double cTest = mathStuff.square(c);
int type;
if(test==cTest)
{
type =1;
}
else if( test>cTest)
{
type =2;
}
else if( test<cTest)
{
type=3;
}
}
public int getType()
{
return type;
}
public void angleFinder()
{
double a2 = mathStuff.square(a);
double b2 = mathStuff.square(b);
double c2 = mathStuff.square(c);
double pi = 3.14159265359;
double total = 180;
double cosA = ((a2+c2-b2)/(2*a*c));
;
double cosB = ((a2+b2-c2)/(2*a*b));
double cosC =((b2 + c2)-a2)/((2*b)*c);
double arcCosA = mathStuff.arcCos(cosA);
double arcCosB = mathStuff.arcCos(cosB);
double arcCosC = mathStuff.arcCos(cosC);
double alpha = mathStuff.degrees(arcCosA);
double beta = mathStuff.degrees(arcCosB);
double gamma = mathStuff.degrees(arcCosC);
double alpha2 = mathStuff.round(alpha);
double beta2 = mathStuff.round(beta);
double gamma2 = mathStuff.round(gamma);
this.alpha2=alpha2;
this.beta2=beta2;
this.gamma2=gamma2;
System.out.println("The measurement of the angles for your triangle is " + alpha2 + " " + beta2 + " " + gamma2);
}
public void heronForm()
{
double sideTotal = .5*(a + b + c);
double next = ((sideTotal) * (sideTotal-a) * (sideTotal-b) / (sideTotal-c));
double area = mathStuff.squareRoot(next);
System.out.println("The program will now calculate the area using Heron's Formula: ");
System.out.println("s=(.5 * " + a + "+" + b + "+" + c + ")");
System.out.println("The square root of s(s-" + a + ")(s-"+b+")(s-"+c +") = area");
System.out.println("The area is " + area);
System.out.println();
}
public void height()
{
double h = c * mathStuff.sin(alpha2);
h=mathStuff.round(h);
this.h=h;
}
public double getHeight()
{
return h;
}
}
Here is my driver
import java.util.*;
public class Driver
{
public static void main(String[] args)
{
Scanner reader = new Scanner (System.in);
System.out.print("Please enter the sides of your triangle from smallest to largest (A B C): ");
int a = reader.nextInt();
int b = reader.nextInt();
int c = reader.nextInt();
System.out.println();
TriangleCrazy tri = new TriangleCrazy(a,b,c);
tri.triTest();
System.out.println();
tri.triType();
int type = tri.getType();
if (type==1)
{
System.out.println("The triangle is an Right Triangle");
}
else if (type==2)
{
System.out.println("The triangle is an Acute Triangle");
}
else if (type==3)
{
System.out.println("The triangle is an Obtuse Triangle");
}
System.out.println();
tri.angleFinder();
System.out.println();
tri.heronForm();
System.out.println();
tri.height();
if (type==1)
{
System.out.println("The height of your triangle is " + a);
}
else if (type==2)
{
double h = tri.getHeight();
System.out.println("The height of your triangle is " + h);
}
else if (type==3)
{
double h = tri.getHeight();
System.out.println("The height of your triangle is " + h);
}
}
}
Sample output so you know what I am talking about. Please enter the sides of your triangle from smallest to largest (A B C): 3 4 5
You have enetered a proper triangle
The measurement of the angles for your triangle is 53.0 90.0 37.0
The program will now calculate the area using Heron's Formula: s=(.5 * 3+4+5) The square root of s(s-3)(s-4)(s-5) = area The area is 6.0
As you can see it ignores the if statments I created in order to find the type of triangle. I have no idea why this is. I had to create them in order to know if the height was to be found using the formula or was one of the sides in a right triangle. The height in a right triangle is as you know the smallest side.
If anybody could take a look and help me fine the bug I would appreciate it greatly.
Aucun commentaire:
Enregistrer un commentaire