简体   繁体   中英

How do I create a loop to iterate objects and display calculations of the objects from the array?

I am new to Java and currently doing a small project to learn. These are the requirements for the project:

  1. Create a class named Circle with a field named radius.
  • Include default constructor
  • Include a default constructor that sets the radius to 1 by default.
  • Include another constructor that receives the radius and sets the radius to the value received.
  • Include a method that returns the area of the circle. -Include another method that returns the circumference of the circle.
  • Use the PI constant of the Math class for this calculation.
  1. Create a class named TestCircle whose main() method declares 5 Circle objs and stores them in an array
  • 5 circle objects will have different radius values
  • Using a loop, iterate the objects and display areas and circumference of the objects from the array
  • if the radius is one, display a message saying "This is a unit circle".

I managed to complete some of the requirements to the best of my ability but I don't know how to

Using a loop, iterate the objects and display areas and circumference of the objects from the array. If the radius is one, display a message saying "This is a unit circle".

class Circle {
    double radius;
    //constructor to default radius to 1
    public Circle() {
        this.radius = 1;
    }
    //constructor to receive values and set it as radius
    public Circle(double [] circlesRad) {
        this.radius = circlesRad[0];
    }

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

    public double computeCircumference() {
        return Math.PI *2*radius;   
    }
}


public class TestCircle {

    public static void main(String[] args) {
        Circle c1 = new Circle();
        double circlesRad[] = {1, 34, 56, 23, 93, 18};
    
        for (double rad : circlesRad) {
            System.out.println("Circle:"+"\nArea: "+ c1.computeArea()+"\nCircumference: "+ c1.computeCircumference());
            if (rad == 1){
                System.out.println("Circle:"+"\nArea: "+ c1.computeArea()+"\nCircumference: "+ c1.computeCircumference());
                System.out.println("This is a unit circle.");
            }
        }
    }
}

I know its very wrong, and I apologise. Any help would be much appreciated.

Below is the code:-


class Circle {
    double radius;
    //constructor to default radius to 1
    public Circle() {
        this.radius = 1;
    }
    //you should consider this as just passing a radius value.
    public Circle(double circlesRad) {
        this.radius = circlesRad;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

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

    public double computeCircumference() {
        return Math.PI *2*radius;
    }
}


public class TestCircle {

    public static void main(String[] args) {
        Circle[] circles = {new Circle(1), new Circle(34), new Circle(56),
        new Circle(23), new Circle(93), new Circle(18)};

        for (Circle circle : circles) {
            System.out.println("Circle:"+"\nArea: "+ circle.computeArea()+"\nCircumference: "+ circle.computeCircumference());
            if (circle.getRadius() == 1){
                System.out.println("This is a unit circle.");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM