Last active
January 27, 2016 10:40
-
-
Save Viacheslav77/149e17300d5a6af3d91f to your computer and use it in GitHub Desktop.
Figures polimorfism + interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Figures_polimorfism; | |
public class Circle extends Figures { | |
public Circle(){ | |
a=5; | |
//System.out.println("Конструктор Круг"); | |
} | |
public Circle( int a){ | |
this.a=a; | |
//System.out.println("Конструктор Круг"); | |
} | |
@Override | |
public String getName(){ | |
return "Круг с радиусом: " + a + " "; | |
} | |
@Override | |
public int Area(){ | |
return (int) (Math.PI*Math.pow(a, 2)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Figures_polimorfism; | |
public abstract class Figures implements Comparable { | |
protected int a; | |
protected int b; | |
public Figures(){ | |
//System.out.println("Конструктор Фигуры"); | |
} | |
@Override | |
public int compareTo (Object another){ | |
Figures f = (Figures) another; | |
if(Area()<f.Area()) | |
return -1; | |
if(Area()>f.Area()) | |
return 1; | |
else return 0; | |
} | |
public String getName(){ | |
return "Фигура"; | |
} | |
public int getA (){ | |
return a; | |
} | |
public int getB (){ | |
return b; | |
} | |
public abstract int Area(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Figures_polimorfism; | |
import java.util.Arrays; | |
//1. Построить иерархию классов «Фигуры» с учетом знаний о | |
// полиморфизме. У каждого класса фигуры должен быть | |
// метод подсчета площади. | |
//2. Реализовать сортировку списка по возрастанию площади фигуры. | |
public class MyClass { | |
public static void main (String [] args){ | |
Figures [] list = { | |
new Triangle (), | |
new Rectangle(), | |
new Circle(), | |
new Triangle (9,4,5), | |
new Rectangle(2,5), | |
new Circle(8) | |
}; | |
Arrays.sort(list); | |
System.out.println("Cписок фигур по возрастанию площади \n"); | |
System.out.println("------------------------------------------------------"); | |
for(Figures f : list){ | |
System.out.println(f.getName() + " и площадью = " + f.Area()); | |
System.out.println("------------------------------------------------------"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Figures_polimorfism; | |
public class Rectangle extends Figures { | |
public Rectangle(){ | |
a=5;b=6; | |
//System.out.println("Конструктор Прямоугольник"); | |
} | |
public Rectangle(int a, int b){ | |
this.a=a; | |
this.b=b; | |
//System.out.println("Конструктор Прямоугольник"); | |
} | |
@Override | |
public String getName(){ | |
return "Прямоугольник со сторонами: " + a + " x " + b + " "; | |
} | |
@Override | |
public int Area(){ | |
return a*b; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Figures_polimorfism; | |
public class Triangle extends Rectangle { | |
protected int c; | |
public Triangle(){ | |
a=5;b=4;c=6; | |
//System.out.println("Конструктор Треугольник"); | |
} | |
public Triangle(int a, int b, int c){ | |
this.a=a; | |
this.b=b; | |
this.c=c; | |
//System.out.println("Конструктор Треугольник"); | |
} | |
@Override | |
public String getName(){ | |
return "Треугольник со сторонами: " + a + " x " + b + " x " + c; | |
} | |
@Override | |
public int Area(){ | |
return (a*b*c)/(4*c); | |
} | |
public int getC (){ | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment