計算された角度 angleA、angleB、angleC を返すメソッドを (別のクラスで) 呼び出すと、結果 NaN が返されます。すべての計算を 3 回チェックしたので、プログラムの設定方法に何らかの問題があるはずです。私は何を間違えましたか??
/* Write a Java program enabled to compute and show the following properties of a given triangle :
The individual length of all sides
The angles at all corners
The perimeter
The area
*/
public class Triangle
{
private double x1, x2, x3, y1, y2, y3;
double sideA, sideB, sideC;
private double angleA, angleB, angleC;
double longestSide, shortSide1, shortSide2;
private double perimeter, halfPerimeter, triangleArea;
private String stringLongestSide;
public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}
public double getSideA()
{
return (Math.sqrt(Math.pow((x3-x2),2)+Math.pow((y3-y2),2)));
}
public double getSideB()
{
return (Math.sqrt(Math.pow((x3-x1),2)+Math.pow((y3-y1),2)));
}
public double getSideC()
{
return (Math.sqrt(Math.pow((x2-x1),2)+ Math.pow((y2-y1),2)));
}
//Begin by using the cosine rule to find the largest angle
public double getAngleA()
{
//which side is the longest?
longestSide = sideA;
shortSide1 = sideB;
shortSide2 = sideC;
if (longestSide < sideC)
{
longestSide = sideC;
shortSide1 = sideA;
shortSide2 = sideB;
}
else
if (longestSide < sideB)
{
longestSide = sideB;
shortSide1 = sideA;
shortSide2 = sideC;
}
return (Math.acos((Math.pow(shortSide1,2)+Math.pow(shortSide2,2)-Math.pow(longestSide,2))/(2*shortSide1*shortSide2)))*180/Math.PI;
}
//Use the sine rule to find one of the remaining angles
public double getAngleB()
{
return ((Math.asin((shortSide1*Math.sin((angleA*Math.PI/180))/longestSide)))*180/Math.PI);
}
//Use the 'sum of internal angles' rule to find the third angle
public double getAngleC()
{
return (180 - (angleA + angleB));
}
//Calculating the perimeter
public double getPerimeter()
{
return (sideA + sideB + sideC);
}
//Calculating the area of the triangle
public double getArea()
{
halfPerimeter = perimeter/2;
return (Math.sqrt(halfPerimeter*(halfPerimeter-sideA) * (halfPerimeter-sideB) * (halfPerimeter-sideC)));
}
}
メソッドを呼び出すクラス:
/* ShellApplication
Rakel Bára Þorvaldsdóttir
*/
import java.util.Scanner;
import java.text.DecimalFormat;
public class Interaction
{
public static void main(String [] args) //required
{
//write your code here
double x1, x2, x3, y1, y2, y3;
double sideA, sideB, sideC;
double angleA, angleB, angleC;
double perimeter, area;
DecimalFormat coordinates = new DecimalFormat ("#");
DecimalFormat calculations = new DecimalFormat ("#.##");
System.out.println("Welcome!");
System.out.println("Please enter the coordinates of your triangle, the x-coordinate first each time and then the y-coordinate.\n\t------------------------\n");
String garbage;
Scanner scan = new Scanner( System.in );
System.out.print( "Enter the x-coordinate for the first point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
x1 = scan.nextDouble( );
System.out.print( "Enter the y-coordinate for the first point, and then press Enter: ");
while (! scan.hasNextInt()) //creating a while loop to ensure only integer numbers are accepted
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
y1 = scan.nextDouble( );
System.out.print( "Thanks for entering the first point. Now on to the next! \n\t------------------------\n");
System.out.print( "Enter the x-coordinate for the second point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
x2 = scan.nextDouble( );
System.out.print( "Enter the y-coordinate for the second point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
y2 = scan.nextDouble( );
System.out.print( "Thanks for entering the second point. Just one more left! \n\t------------------------\n");
System.out.print( "Enter the x-coordinate for the third point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
x3 = scan.nextDouble( );
System.out.print( "Enter the y-coordinate for the third point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
y3 = scan.nextDouble( );
System.out.println( "You did it! Your triangle has the following coordinates: ");
System.out.println( "Point A: ("+coordinates.format(x1) +" , " +coordinates.format(y1) +")" + " , Point B: ("+coordinates.format(x2) +" , " +coordinates.format(y2) +")" + " , Point C: ("+coordinates.format(x3) +" , " +coordinates.format(y3) +")");
Triangle userTriangle = new Triangle(x1, y1, x2, y2, x3, y3);
sideA = userTriangle.getSideA();
sideB = userTriangle.getSideB();
sideC = userTriangle.getSideC();
angleA = userTriangle.getAngleA();
angleB = userTriangle.getAngleB();
angleC = userTriangle.getAngleC();
perimeter = userTriangle.getPerimeter();
area = userTriangle.getArea();
System.out.println( "-----------------");
System.out.println( "SideA is: " +calculations.format(sideA));
System.out.println( "SideB is: " +calculations.format(sideB));
System.out.println( "SideC is: " +calculations.format(sideC));
System.out.println( "-----------------");
System.out.println( "AngleA is: " +angleA);
System.out.println( "AngleB is: " +angleB);
System.out.println( "AngleC is: " +angleC);
System.out.println( "-----------------");
System.out.println( "Perimeter is: " +perimeter);
System.out.println( "Area is: " +area);
}
}
結果は次のとおりです。
SideA: 14,32 SideB: 5,83
SideC: 11,18
角度 A は: NaN 角度 B は: NaN
角度 C は: 180.0
周囲: 0.0 面積: 0.0