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...
Problem 1: Follow the instructions below: Use an ArrayList to store randomly generated circle or Triangle object...
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 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...