Last active
January 25, 2017 20:25
-
-
Save 13andrew13/b824018bab0a73d1346d08475d49aada to your computer and use it in GitHub Desktop.
Создать классы: 1. Rectangle (Прямоугольник), содержащий размеры (высоту и ширину), и умеющий подсчитывать свои периметр и площадь. 2. Rectangles (Прямоугольники), содержащий список прямоугольников, умеющий добавлять новые прямоугольники и подсчитывать их суммарную площадь. Подсказка: реализовать на основании ArrayList. 3. Клиентский класс Recta…
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
import java.util.ArrayList; | |
public class RectangleRunner { | |
public static void main(String[] args) | |
{ | |
Rectangle rectangle = new Rectangle(10,15); | |
Rectangle rectangle1 = new Rectangle(10,3); | |
Rectangle rectangle2 = new Rectangle(5,6); | |
Rectangles rectangles = new Rectangles(); | |
rectangles.addRectangle(rectangle); | |
rectangles.addRectangle(rectangle1); | |
rectangles.addRectangle(rectangle2); | |
rectangles.printArea(); | |
rectangles.printRectangles(); | |
} | |
} |
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
public class Rectangle { | |
private int height; | |
private int width; | |
public Rectangle(int height,int width) | |
{ | |
this.height=height; | |
this.width = width; | |
} | |
public void setHeight(int height) { | |
this.height = height; | |
} | |
public void setWidth(int width) { | |
this.width = width; | |
} | |
public int getHeight() { | |
return height; | |
} | |
public int getWidth() { | |
return width; | |
} | |
public int returnPerimeter() | |
{ | |
return (height+width)*2; | |
} | |
public int returnArea(){ | |
return height*width; | |
} |
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
import java.util.ArrayList; | |
public class Rectangles { | |
private ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>(); | |
public ArrayList<Rectangle> addRectangle(Rectangle rectangle) | |
{ | |
rectangles.add(rectangle); | |
return rectangles; | |
} | |
public int sumArea() | |
{ | |
int sum = 0; | |
for(Rectangle rectangle:rectangles) | |
sum +=rectangle.returnArea(); | |
return sum; | |
} | |
public void printArea() | |
{ | |
System.out.println("Sum of area of rectangles is " + sumArea()); | |
} | |
public void printRectangles() | |
{ | |
for(Rectangle rec :rectangles) { | |
rec.printRectangle(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment