Created
June 30, 2016 20:42
-
-
Save Yurlov/cbfbf4a8e7705c20d6b02eddf112c6aa to your computer and use it in GitHub Desktop.
Prog.kiev.ua
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
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"); | |
} | |
} | |
} |
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 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