I'm not quite sure whether there is something wrong with my code or with my math, but we are having to calculate the angles of a triangle when the user inputs the three sides. Once we have the angles, we are to determine the sides of a triangle that will have double the area of the first triangle.
I do know that the formula I am using here will work, because I have worked it out on paper.
The problem I encountered was that the area of the first triangle was coming up NaN. I traced this back to the actual problem being the angles were coming up NaN.
Here is what I have:
public class Triangle{
private double sideA1;//this is a data field
private double sideB1;
private double sideC1;
private double sidea2;
private double sideb2;
private double sidec2;
private double angleA;
private double angleB;
private double angleC;
private double area1;
private double area2;
private double perimeter;
public Triangle(double a, double b, double c){//this is a constructor for an object rectangle of the class rectangle
sideA1 = a;
sideB1 = b;
sideC1 = c;
area2 = (sideA1*sideB1*Math.sin(angleC));
perimeter = a+b+c;
}
public double getAngleA() {
angleA = Math.acos((sideA1*sideA1 + sideB1*sideB1 + sideC1*sideC1)/(-2*sideB1*sideC1));
return angleA;
}
public double getAngleB(){
double angleB = Math.acos((sideA1*sideA1 + sideB1*sideB1 + sideC1*sideC1)/(-2*sideA1*sideC1));
return angleB;
}
public double getAngleC() {
angleC = Math.PI - angleA - angleB;
return angleC;
}
public double findPerimeter(){
return perimeter;
}
public double findArea(){
area1 = (sideA1*sideB1*Math.sin(angleC))/2;
return area1;
}
Then the test class is:
import javax.swing.JOptionPane;
public class Project2 {
public static void main(String[] args){
String ainput, binput, cinput;
double a = 0;
double b = 0;
double c = 0;
ainput = JOptionPane.showInputDialog("What is the value of a side of the triangle?");
a = Double.parseDouble(ainput);
binput = JOptionPane.showInputDialog("What is the value of the next side?");
b = Double.parseDouble(binput);
cinput = JOptionPane.showInputDialog("What is the value of the last side?");
c = Double.parseDouble(cinput);
Triangle t = new Triangle(a, b, c);
t.print();
//double area = t.findArea();
double p = t.findPerimeter();
System.out.println("Angle A = " + t.getAngleA());
System.out.println("Angle B = " + t.getAngleB());
System.out.println("Angle C = " + t.getAngleC());
//System.out.println("area = " + area);
System.out.println("Perimeter = " + p);
}
}

New Topic/Question
Reply




MultiQuote







|