Skip to content

Instantly share code, notes, and snippets.

@Yurlov
Created June 30, 2016 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yurlov/cbfbf4a8e7705c20d6b02eddf112c6aa to your computer and use it in GitHub Desktop.
Save Yurlov/cbfbf4a8e7705c20d6b02eddf112c6aa to your computer and use it in GitHub Desktop.
Prog.kiev.ua
class Rectangle {
private int height;
private int weight;
Rectangle(int height, int weight) {
checkParameters(height, weight);
this.height = height;
this.weight = weight;
}
public void perRectagle() {
int x = 2 * (height + weight);
System.out.println("Perimeter is : " + x);
}
public int squareRectangle() {
return height * weight;
}
private void checkParameters(int height, int weight) {
if (height < 0 || weight < 0) {
throw new IllegalArgumentException("Illegal parameters");
}
}
}
public class RectangleRunner {
public static void main(String[] args) {
ArrayList<Rectangle> list = new ArrayList<>();
Rectangle rect3 = new Rectangle(7, 15);
Rectangle rect2 = new Rectangle(4, 14);
Rectangle rect1 = new Rectangle(8, 19);
list.add(rect1);
list.add(rect2);
list.add(rect3);
rect1.perRectagle();
System.out.println("Sum is " + getSum(list));
}
private static int getSum(ArrayList<Rectangle> list) {
int areaSum = 0;
for (Rectangle aList : list) {
areaSum = areaSum + aList.squareRectangle();
}
return areaSum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment