Question

Problem 1: Follow the instructions below: Use an ArrayList to store randomly generated circle or Triangle objects. 1. 2. Whil

color: white and filled: false Area: 1.1370703484889715 sde2 - 1.53634486532524 st Triangle: side1 1.7589437034441613 side 1.

Problem 1: Follow the instructions below: Use an ArrayList to store randomly generated circle or Triangle objects. 1. 2. While generating these objects, assign radius with a random number between 0 and 1 for Circle objects and three sides with random numbers between and 2 for Triangle objects. 3. While adding the objects into the ArrayList, you should reject those with area less than the existing obiects in the ArrayList and only add those with area greater than the existing elements. Stop the addition when you have five elements in the Arraylist. 4. Below is a sample run: An ArrayList with five Circle or Triangle objects Triangle: side1 1.1673926159631258 side2 1.276667548995706 side3- 1.6847518900283591 created on Sat May 26 15:29:37 CST 2018 color: white and filled: false Area: 0,7441870800920893 Triangle: sidel 1.4205959568764617 side2 1.255499231552378 side3 1.4881396928637742 created on Sat May 26 15:29:37 CST 2018 color: white and filled: false Area: 0.8223603431700648 Circle: radius 0.6016151038749673 created on Sat May 26 15:29:37 CST 2018
color: white and filled: false Area: 1.1370703484889715 sde2 - 1.53634486532524 st Triangle: side1 1.7589437034441613 side 1.5363448653392624 side3- 4248425523569 created on sa 1.7424842508225649 created on Sat May 26 15:29:37 CST 2018 color: white and filled: false Area: 1.208407871776394 Circle: radius 0.9361775725214883 created on Sat May 26 15:29:37 CST 2018 color: white and filled: false Area: 2.7533811714103673
0 0
Add a comment Improve this question Transcribed image text
Answer #1

As circle and triangle class not given so i have wrote those also

// Circle.java

import java.util.Date;

public class Circle {
    private double radius;
    private boolean isFilled;
    private Date date;
    private String color;

    public Circle(double r, boolean i, String c) {
        this.radius = r;
        this.isFilled = i;
        this.color = c;
        date = new Date();
    }

    public double getArea() {
        return Math.PI * this.radius * this.radius;
    }

    public String toString() {
        return String.format("Circle: radius = %f created on %s\ncolor: %s and filled: %s\nArea: %f", this.radius, this.date.toString(), this.color, String.valueOf(this.isFilled), getArea());
    }
}

//Triangle.java

import java.util.Date;

public class Triangle {
    private double side1, side2, side3;
    private boolean isFilled;
    private String color;
    private Date date;

    public Triangle(double side1, double side2, double side3, boolean isFilled, String color) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
        this.isFilled = isFilled;
        this.color = color;
        this.date = new Date();
    }

    public double getArea() {
        double s = (this.side1 + this.side2 + this.side3) / 2;
        double area = Math.sqrt(s * (s - this.side1) * (s - this.side2) * (s - this.side3));
        return area;
    }

    @Override
    public String toString() {
        return String.format("Triangle: side1 = %f  side2 = %f  side3 = %f\ncreated on %s\ncolor: %s and filled: %s\nArea: %f",
                this.side1, this.side2, this.side3, date.toString(), this.color, String.valueOf(this.isFilled), getArea());
    }
}

//Main.java

import java.util.ArrayList;
import java.util.Random;

public class Main {
    private static Random random = new Random();

    public static void main(String[] args) {
        // arraylist for adding objects
        ArrayList list = new ArrayList();
        double circle_max_area = 0, triangle_max_area = 0;
        int count = 0;
        while (true) {
            int decide = random.nextInt(3);
           // deciding which object will be created randomly
            if (decide > 1) {
                // creating and adding new triangle object
                Triangle t = new Triangle(getRandom(1), getRandom(1), getRandom(1), false, "white");
                if (t.getArea() > triangle_max_area) {
                    triangle_max_area = t.getArea();
                    list.add(t);
                    count++;
                }
            } else {
                Circle c = new Circle(getRandom(0), false, "white");
                if (c.getArea() > circle_max_area) {
                    circle_max_area = c.getArea();
                    list.add(c);
                    count++;
                }
            }
            if (count >= 5) break;
        }

        for (int i=0;i<list.size();i++){
            System.out.println(list.get(i));
            System.out.println();
        }
    }

    public static double getRandom(int min) {
        return random.nextDouble() + min;
    }
}

//OUT

Circle: radius = 0.942221 created on Sat May 25 22:27:10 IST 2019
color: white and filled: false
Area: 2.789046

Triangle: side1 = 1.894986 side2 = 1.521876 side3 = 1.217940
created on Sat May 25 22:27:10 IST 2019
color: white and filled: false
Area: 0.925310

Triangle: side1 = 1.584510 side2 = 1.671471 side3 = 1.793482
created on Sat May 25 22:27:10 IST 2019
color: white and filled: false
Area: 1.217016

Circle: radius = 0.943565 created on Sat May 25 22:27:10 IST 2019
color: white and filled: false
Area: 2.797005

Triangle: side1 = 1.699183 side2 = 1.889059 side3 = 1.749157
created on Sat May 25 22:27:10 IST 2019
color: white and filled: false
Area: 1.361948

Please do let me know if u have any concern...

Add a comment
Know the answer?
Add Answer to:
Problem 1: Follow the instructions below: Use an ArrayList to store randomly generated circle or Triangle object...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • Read the Article posted below, then answer the following questions: 1. As a junior member of...

    Read the Article posted below, then answer the following questions: 1. As a junior member of your company’s committee to explore new markets, you have received a memo from the chairperson telling you to be prepared at the next meeting to discuss key questions that need to be addressed if the company decides to look further into the possibility of marketing to the BOP segment. The ultimate goal of this meeting will be to establish a set of general guidelines...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT