fork download
  1. interface calcFigure{
  2. double calcArea( double x, double y );
  3. double calcVolume( double x, double y, double z );
  4. }
  5.  
  6. class Triangle implements calcFigure {
  7. @Override
  8. public double calcArea(double x, double y) {
  9. return x * y / 2;
  10. }
  11.  
  12. @Override
  13. public double calcVolume(double x, double y, double z) {
  14. return this.calcArea(x, y) * z / 3;
  15. }
  16. }
  17.  
  18. class Rectangle implements calcFigure {
  19. @Override
  20. public double calcArea(double x, double y) {
  21. return x * y;
  22. }
  23.  
  24. @Override
  25. public double calcVolume(double x, double y, double z) {
  26. return x * y * z;
  27. }
  28. }
  29.  
  30. class CalcShape{
  31. public static void main( String [] args ){
  32. Triangle tr = new Triangle();
  33. Rectangle rc = new Rectangle();
  34.  
  35. double x = Double.parseDouble( args[0] ); // 底面の1辺の長さ(△の底辺の長さ)
  36. double y = Double.parseDouble( args[1] ); // 底面のもう1辺の長さ(△の高さ)
  37. double z = Double.parseDouble( args[2] ); // 立体の高さ
  38.  
  39. System.out.printf("三角形の面積:%.1f\n", tr.calcArea(x,y));
  40. System.out.printf("三角錐の体積:%.1f\n", tr.calcVolume(x,y,z));
  41. System.out.printf("四角形の面積:%.1f\n", rc.calcArea(x,y));
  42. System.out.printf("直方体の体積:%.1f\n", rc.calcVolume(x,y,z));
  43. }
  44. }
Runtime error #stdin #stdout #stderr 0.04s 4575232KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at CalcShape.main(Main.java:35)