Skip to content

Instantly share code, notes, and snippets.

@kdmgs110
Created December 1, 2016 05:54
Show Gist options
  • Save kdmgs110/b0faba47a974ae900b62ff2e501e2301 to your computer and use it in GitHub Desktop.
Save kdmgs110/b0faba47a974ae900b62ff2e501e2301 to your computer and use it in GitHub Desktop.
メソッドのオーバーロード
package pass01;
import lib.Input;
public class Pass01_01 {
/* メソッドのオーバーロードについて
* 同じ名前のメソッドは使えない
* ただし、引数、データ型が違えば同じ名前でもOK
* 今回は、正三角形、直角三角形、普通の三角形の面積を求めるプログラムを作成する
*/
public static void main(String[] args) {
System.out.println(sankaku(1)); //正三角形1cm
System.out.println(sankaku(1,2));//直角三角形3cm
System.out.println(sankaku(3,4,5)); //123cmの三角形
}
public static double sankaku(double a){// 1辺がacmのもの
return Math.sqrt(3)*Math.pow(a, 2) /4; // sqrt3/4 a^2の公式
}
public static double sankaku(double a, double b){// 1辺がacm,bcmのもの
return a * b /2; // 底辺×高さ/2
}
public static double sankaku(double a, double b, double c){
double s = (a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment