Advertisement
sfrsnyz

Вобликова ЯП3

May 26th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package Laba3;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner read=new Scanner(System.in);
  10. System.out.println("Введите число треугольников");
  11. int v1=read.nextInt();
  12. System.out.println("Введите число равносторонних треугольников");
  13. int v2=read.nextInt();
  14. System.out.println("Введите число пирамид");
  15. int v3=read.nextInt();
  16. List<Treug> triangles=new ArrayList<>();
  17. int counter=0;
  18. while(counter<(v1+v2+v3)){
  19. if(counter<v1)
  20. triangles.add(Treug.random());
  21. if(counter<v2)
  22. triangles.add(RavnostTreug.random());
  23. if(counter<v3)
  24. triangles.add(Pyramid.random());
  25. counter++;
  26. }
  27. double max1=0;
  28. double max2=0;
  29. for(Treug treug:triangles){
  30. treug.info();
  31. System.out.println();
  32. if(treug.getType().equals("треугольник")){
  33. if(treug.S()>max1)
  34. max1= treug.S();
  35. }
  36. else{
  37. if(treug.S()>max2)
  38. max2= treug.S();
  39. }
  40. }
  41. System.out.println("Максимальная площадь среди родительских классов: "+max1);
  42. System.out.println("Максимальная площадь среди классов-наследников: "+max2);
  43. }
  44. }
  45.  
  46. //////////////
  47. package Laba3;
  48.  
  49. public class Pyramid extends RavnostTreug{
  50. private double apofema;
  51.  
  52. public Pyramid(double st1, double apofema) {
  53. super(st1);
  54. this.apofema = apofema;
  55. setType("пирамида");
  56. }
  57. public static Pyramid random(){
  58. double b=Math.random()*4+15;
  59. double c=Math.random()*4+15;
  60. return new Pyramid(b,c);
  61. }
  62.  
  63. @Override
  64. public void info() {
  65. super.info();
  66. System.out.println("Апофема: "+round(apofema));
  67. }
  68.  
  69. @Override
  70. public double S() {
  71. return super.S()+0.5*super.P()*apofema;
  72. }
  73. }
  74. //////////////
  75. package Laba3;
  76.  
  77. public class RavnostTreug extends Treug{
  78. public RavnostTreug(double st1) {
  79. super(st1, st1, st1);
  80. setType("равносторонний треугольник");
  81. }
  82. public static RavnostTreug random(){
  83. double b=Math.random()*4+15;
  84. return new RavnostTreug(b);
  85. }
  86. }
  87. //////////////
  88. package Laba3;
  89.  
  90. public interface Square {
  91. double S();
  92. }
  93. //////////////
  94. package Laba3;
  95.  
  96. public class Treug implements Square{
  97. private double st1;
  98. private double st2;
  99. private double st3;
  100. private String type;
  101.  
  102. public Treug(double st1, double st2, double st3) {
  103. this.st1 = st1;
  104. this.st2 = st2;
  105. this.st3 = st3;
  106. type="треугольник";
  107. }
  108. public double P(){
  109. return st3+st2+st1;
  110. }
  111. public static Treug random(){
  112. double a=Math.random()*4+15;
  113. double b=Math.random()*4+15;
  114. double c=Math.random()*4+15;
  115. return new Treug(a,b,c);
  116. }
  117. protected double round(double value){ //округление чисел
  118. return value=Math.round(value*100.0)/100.0;
  119. }
  120. public String getType() {
  121. return type;
  122. }
  123.  
  124. public void setType(String type) {
  125. this.type = type;
  126. }
  127.  
  128. public void info(){
  129. System.out.println("Класс "+type);
  130. System.out.println("Площадь: "+round(S()));
  131. System.out.printf("Стороны: %s,%s,%s\n",round(st1),round(st2),round(st3));
  132. }
  133.  
  134. @Override
  135. public double S() {
  136. double p=(st1+st2+st3)/2;
  137. return Math.sqrt(p*(p-st1)*(p-st2)*(p-st3));
  138. }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement