Home > other >  Instantiate an ArrayList of Circles
Instantiate an ArrayList of Circles

Time:12-08

so the question basically says to Use a for loop to add 10 Circles to the ArrayList each with a random radius in the range of 2-5 and Print a table of all of the Circles in the ArrayList. You can use System.out.println(String.format("%.3f", this.getRadius)); to print the circle dimensions to 3 decimal places. but how does this work.

public class CirclesDriver
{
    public static void main(String[] args) {
        Random rand = new Random();
        // ** Variables constants and objects **
        Scanner scanner = new Scanner(System.in);
        ArrayList<Circle> circles = new ArrayList<>();

        // ** load the array list with circles **
        for (int i = 0; i <= 10; i  ) {
            Circle circle = new Circle();
            double radius = rand.nextDouble(5)   2;

        }
        
        // print the whole list
        
        System.out.println("--------------------------------\n");
        
        // ** Make sure your Circle class and ArrayList work with the following driver code **
        System.out.println();
        
        Circle c1 = new Circle();
        System.out.println("C1: "   c1.getID());
        System.out.println("Radius: "   c1.getRadius());
        System.out.println("--------------------------------\n");
        
        c1.setRadius(1.5);
        System.out.println("C1: "   c1.getID());
        System.out.println("Radius: "   c1.getRadius());
        System.out.println("Area: "   c1.getArea());
        System.out.println("--------------------------------\n");
        
        circles.get(3).setRadius(2.2);
        System.out.println("C1: "   circles.get(3).getID());
        System.out.println("Radius: "   circles.get(3).getRadius());
        System.out.println("Diameter: "   circles.get(3).getDiameter());
        System.out.println("--------------------------------\n");
        
        Circle c2 = circles.remove(9);
        System.out.println("C2: "   c2.getID());
        System.out.println("Radius: "   c2.getRadius());
        System.out.println("Circumference: "   c2.getCircumference());
        System.out.println("--------------------------------\n");
        
        // ** output
        
        // ** closing message **
        System.out.println("\nend of program");

CodePudding user response:

Based on the question, given code doesn't work. Here is the complete code and corresponding output.

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

public class CirclesDriver {
    public static void main(String[] args) {
        Random rand = new Random();
        // ** Variables constants and objects **
        Scanner scanner = new Scanner(System.in);
        ArrayList<Circle> circles = new ArrayList<>();

        // ** load the array list with circles **
        for (int i = 1; i <= 10; i  ) {
            String id = "C"   i;
            double random = rand.nextDouble();
            double radius = 2   (random * (5 - 2));
            Circle circle = new Circle(id, radius);
            circles.add(circle);
        }

        // print the whole list

        System.out.println("--------------------------------\n");

        // ** Make sure your Circle class and ArrayList work with the following driver code **
        for(int i=0; i<circles.size(); i  ) {
            Circle circle = circles.get(i);
            System.out.println();
            System.out.println("C1: "   circle.getId());
            System.out.println("Radius: "   String.format("%.3f", circle.getRadius()));
            System.out.println("Diameter: "   String.format("%.3f", circle.getDiameter()));
            System.out.println("Circumference: "   String.format("%.3f", circle.getCircumference()));
            System.out.println("Area: "   String.format("%.3f", circle.getArea()));
            System.out.println();
        }
    }
}

class Circle {
    private String id;
    private double radius = 0;

    public Circle(String id, double radius) {
        this.id = id;
        this.radius = radius;
    }

    public String getId() {
        return id;
    }

    public double getRadius() {
        return radius;
    }

    public double getDiameter() {
        return radius * 2;
    }

    public double getArea() {
        return 2 * 3.14 * radius * radius;
    }

    public double getCircumference() {
        return 2 * 3.14 * radius;
    }
}

Output

--------------------------------


C1: C1
Radius: 4.740
Diameter: 9.481
Circumference: 29.770
Area: 141.121


C1: C2
Radius: 3.677
Diameter: 7.353
Circumference: 23.089
Area: 84.886


C1: C3
Radius: 4.333
Diameter: 8.665
Circumference: 27.209
Area: 117.888


C1: C4
Radius: 4.592
Diameter: 9.184
Circumference: 28.838
Area: 132.423


C1: C5
Radius: 4.468
Diameter: 8.937
Circumference: 28.062
Area: 125.393


C1: C6
Radius: 3.024
Diameter: 6.047
Circumference: 18.988
Area: 57.413


C1: C7
Radius: 3.628
Diameter: 7.256
Circumference: 22.783
Area: 82.656


C1: C8
Radius: 2.086
Diameter: 4.173
Circumference: 13.103
Area: 27.339


C1: C9
Radius: 2.447
Diameter: 4.894
Circumference: 15.368
Area: 37.610


C1: C10
Radius: 4.301
Diameter: 8.602
Circumference: 27.010
Area: 116.172

CodePudding user response:

Pass radius to constructor

Simpler to define a constructor that takes the radius as an argument.

Define your Circle class. If the main purpose of your class is to communicate data transparently and immutably, write your class as a record. With a record, the compiler by default implicitly creates the constructor, getters, equals & hashCode, and toString. You can declare a record locally (within a method), or separately.

record Circle ( double radius ) {}

A DoubleStream can create a series of randomly generated double values. (The following code is untested)

List< Circle > circles = 
    ThreadLocalRandom
        .current()  // Returns a `ThreadLocalRandom `. 
        .doubles(   
            10L,    // streamSize
            2.0d,   // randomNumberOrigin
            5.0d    // randomNumberBound
         )          // Returns a `DoubleStream`. 
        .mapToObj( double d -> return new Circle( d ) ) // Generates a stream of `Circle` objects. 
        .toList()   // Returns a `List`.
;

We might be able to reduce .mapToObj( double d -> return new Circle( d ) ) with .mapToObj( Circle :: new ), a method reference referring to the constructor.

For output, loop the list. Add methods to the Circle class to return diameter and circumference.

  • Related