いちばん失敗した人決定戦

java初心者です。以下のプログラムをわかる方教えてもらえますか?

三角形の2つの角と爽辺をN組読み込んで、
三角形の面積、角度、三角形の長さを出力するプログラムです。

お願いします。

A 回答 (2件)

ちょっとしたサンプルを作ってみました。


N組読み込む所は省略
----------------------------------------------------------------
abstract class Triangle {
public static double yogen(double a, double b, double C){
return Math.sqrt(a*a+b*b-2*a*b*Math.cos(C));
}
public static double heron(double a, double b, double c){
double s = (a + b + c)/2.0;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
public static double areaBy_abC(double a, double b, double C){
return a*b*Math.sin(C)/2.0;
}

protected double a, b, c;
protected double A, B, C;

public double get_a(){ return a; }
public double get_b(){ return b; }
public double get_c(){ return c; }
public double getAngle_A(){ return A; }
public double getAngle_B(){ return B; }
public double getAngle_C(){ return C; }

abstract public double area();
}

class Triangle_ab_C extends Triangle {
//2辺とその挟む角がわかっている三角形
public Triangle_ab_C(double a, double b, double C){
this.a = a;
this.b = b;
this.C = C;

this.c = yogen(a, b, C);
A = Math.acos((b*b+c*c-a*a)/2.0/b/c);
B = Math.PI - A - C;
}
public double area(){
return areaBy_abC(a, b, C);
}
}

class Triangle_abc extends Triangle {
//3辺がわかっている三角形
public Triangle_abc(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;

A = Math.acos((b*b+c*c-a*a)/2.0/b/c);
B = Math.acos((a*a+c*c-b*b)/2.0/a/c);
C = Math.PI - A - B;
}
public double area(){
return heron(a, b, c);
}
}
class Triangle_a_BC extends Triangle {
//1辺とその両端の角がわかっている三角形
public Triangle_a_BC(double a, double B, double C){
this.a = a;
this.B = B;
this.C = C;

this.A = Math.PI - B - C;
this.b = a*Math.sin(B)/Math.sin(A);
this.c = a*Math.sin(C)/Math.sin(A);
}
public double area(){
return heron(a, b, c);
}
}
class Sample {
public static void main(String args[] ) {
Triangle[] triangles = {
// 正三角形をそれぞれのクラスで作成
new Triangle_ab_C(10, 10, Math.toRadians(60)),
new Triangle_abc(10, 10, 10),
new Triangle_a_BC(10, Math.toRadians(60), Math.toRadians(60)),
};

for(Triangle tri : triangles){
System.out.printf("a=%f,b=%f,c=%f%n",
tri.get_a(),tri.get_b(),tri.get_c());
System.out.printf("∠A=%f,∠B=%f,∠C=%f%n",
tri.getAngle_A(),tri.getAngle_B(),tri.getAngle_C());
System.out.printf("S=%f%n", tri.area());
}
}
}
    • good
    • 1

数学の問題として”三角形の2つの角と爽辺が分かている場合、三角形の面積、角度、三角形の長さを計算するのはどうするのか。

”を解決してからjavaとしての解決法を質問すべきじゃないですか?
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!