Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //class Rectangle
- public class Rectangle {
- private double width;
- private double height;
- public Rectangle() {
- this.width = 0;
- this.height = 0;
- }
- public Rectangle(double width, double height) {
- this.width = width;
- this.height = height;
- }
- public double perimeter(){
- double peri = 2 * this.width + 2 * this.height;
- return peri;
- }
- public double area(){
- double area = this.width * this.height;
- return area;
- }
- @Override
- public String toString() {
- return "Recrangle: " +
- "Width= " + width +
- ", Height= " + height +
- ", Perimeter= " + perimeter() +
- ", Area= " + area();
- }
- }
- //class RectangleMain
- import java.util.ArrayList;
- import java.util.Random;
- public class RectangleMain {
- public static void main(String[] args) {
- Random rand = new Random();
- ArrayList<Rectangle> rectangles = new ArrayList<>();
- for(int i = 0; i < 10; i++){
- double rand_width = rand.nextDouble()*100;
- double rand_height = rand.nextDouble()*100;
- rectangles.add(new Rectangle(rand_width, rand_height));
- }
- for(Rectangle r: rectangles){
- System.out.println(r);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement