Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Laba3;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner read=new Scanner(System.in);
- System.out.println("Введите число треугольников");
- int v1=read.nextInt();
- System.out.println("Введите число равносторонних треугольников");
- int v2=read.nextInt();
- System.out.println("Введите число пирамид");
- int v3=read.nextInt();
- List<Treug> triangles=new ArrayList<>();
- int counter=0;
- while(counter<(v1+v2+v3)){
- if(counter<v1)
- triangles.add(Treug.random());
- if(counter<v2)
- triangles.add(RavnostTreug.random());
- if(counter<v3)
- triangles.add(Pyramid.random());
- counter++;
- }
- double max1=0;
- double max2=0;
- for(Treug treug:triangles){
- treug.info();
- System.out.println();
- if(treug.getType().equals("треугольник")){
- if(treug.S()>max1)
- max1= treug.S();
- }
- else{
- if(treug.S()>max2)
- max2= treug.S();
- }
- }
- System.out.println("Максимальная площадь среди родительских классов: "+max1);
- System.out.println("Максимальная площадь среди классов-наследников: "+max2);
- }
- }
- //////////////
- package Laba3;
- public class Pyramid extends RavnostTreug{
- private double apofema;
- public Pyramid(double st1, double apofema) {
- super(st1);
- this.apofema = apofema;
- setType("пирамида");
- }
- public static Pyramid random(){
- double b=Math.random()*4+15;
- double c=Math.random()*4+15;
- return new Pyramid(b,c);
- }
- @Override
- public void info() {
- super.info();
- System.out.println("Апофема: "+round(apofema));
- }
- @Override
- public double S() {
- return super.S()+0.5*super.P()*apofema;
- }
- }
- //////////////
- package Laba3;
- public class RavnostTreug extends Treug{
- public RavnostTreug(double st1) {
- super(st1, st1, st1);
- setType("равносторонний треугольник");
- }
- public static RavnostTreug random(){
- double b=Math.random()*4+15;
- return new RavnostTreug(b);
- }
- }
- //////////////
- package Laba3;
- public interface Square {
- double S();
- }
- //////////////
- package Laba3;
- public class Treug implements Square{
- private double st1;
- private double st2;
- private double st3;
- private String type;
- public Treug(double st1, double st2, double st3) {
- this.st1 = st1;
- this.st2 = st2;
- this.st3 = st3;
- type="треугольник";
- }
- public double P(){
- return st3+st2+st1;
- }
- public static Treug random(){
- double a=Math.random()*4+15;
- double b=Math.random()*4+15;
- double c=Math.random()*4+15;
- return new Treug(a,b,c);
- }
- protected double round(double value){ //округление чисел
- return value=Math.round(value*100.0)/100.0;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public void info(){
- System.out.println("Класс "+type);
- System.out.println("Площадь: "+round(S()));
- System.out.printf("Стороны: %s,%s,%s\n",round(st1),round(st2),round(st3));
- }
- @Override
- public double S() {
- double p=(st1+st2+st3)/2;
- return Math.sqrt(p*(p-st1)*(p-st2)*(p-st3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement