Last active
August 29, 2015 14:07
-
-
Save OlgaKulikova/301a5b65a4c6f9de6b5e to your computer and use it in GitHub Desktop.
Проект Фигура
This file contains hidden or 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 Figure; | |
public class Circle extends Figure { | |
private double radius; | |
public Circle(double radius) { | |
this.radius = radius; | |
} | |
@Override | |
public double square() { | |
return Math.PI * (radius * radius); | |
} | |
} |
This file contains hidden or 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 Figure; | |
public abstract class Figure { | |
public abstract double square(); | |
} |
This file contains hidden or 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 Figure; | |
import java.util.Comparator; | |
class FigureComparator implements Comparator { | |
@Override | |
public int compare(Object o1, Object o2) { | |
Figure f1 = (Figure) o1; | |
Figure f2 = (Figure) o2; | |
while (f1 != null && f2 != null) { | |
if (f1.square() < f2.square()) | |
return -1; | |
else if (f1.square() == f2.square()) | |
return 0; | |
else if (f1.square() > f2.square()) | |
return 1; | |
} | |
return 0; | |
} | |
} |
This file contains hidden or 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 Figure; | |
import java.util.Arrays; | |
public class FigureList { | |
Figure[] list = new Figure[100]; | |
int p = 0; | |
public void add(Figure f) { | |
list[p++] = f; | |
} | |
public void printSquare() { | |
for(Figure t : list) { | |
if (t != null) { | |
System.out.format("%.2f%n", t.square()); | |
} | |
} | |
} | |
public void sortSquare() { | |
Arrays.sort(list, new FigureComparator()); | |
for (Figure t : list) { | |
if (t != null) { | |
System.out.println(t.square()); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 Figure; | |
// Построить иерархию классов «Фигуры» с учетом знаний о полиморфизме. | |
// У каждого класса фигуры должен быть метод подсчета площади. | |
// Создать список фигур. Вывести площади всех фигур на экран. | |
//Создать список фигур. Реализовать сортировку списка по возрастанию площади фигуры. | |
public class Main { | |
public static void main(String[] args) { | |
Rectangle rectangle = new Rectangle(2, 3); | |
Triangle triangle = new Triangle(2.6, 4.0); | |
Circle circle = new Circle(3.2); | |
FigureList list = new FigureList(); | |
list.add(rectangle); | |
list.add(triangle); | |
list.add(circle); | |
list.printSquare(); | |
list.sortSquare(); | |
} | |
} |
This file contains hidden or 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 Figure; | |
public class Rectangle extends Figure { | |
public int height, width; | |
public Rectangle(int height, int width) { | |
this.height = height; | |
this.width = width; | |
} | |
@Override | |
public double square() { | |
return height * width; | |
} | |
} |
This file contains hidden or 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 Figure; | |
public class Triangle extends Figure{ | |
private double height, base; | |
public Triangle(double height, double base) { | |
this.height = height; | |
this.base = base; | |
} | |
@Override | |
public double square() { | |
return (height * base) / 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment