Hello, I am new in trying to figure out how to use two classes together. One class is called circle, and is supposed to hold attributes of radius and area. It is also supposed to have the methods of print and computeArea.

public class Circle
{
    //global data
    private double radius;
    private double area;
    //constructor
    /**
     * <pre>
     * Description: This is a constructor 
     * 
     * Pre:         None
     * 
     * Post:        Creates an object of type circle 
     * </pre>
     */
    public Circle()
    {

    }

    //work methods
    /**
     * <pre>
     * Description: This computes the area
     * 
     * Pre:         None
     * 
     * Post:        None        
     * </pre>
     */
    public double computeArea()
    {
        area = radius*radius*Math.PI;
        return area;
    }

    /**
     * <pre>
     * Description: Prints the radius and area in a user friendly way
     * 
     * Pre:         None
     * 
     * Post:        Radius and area are printed     
     * </pre>
     */
    public void print()
    {
        System.out.println("radius = " + radius);
        System.out.println("area = " + area);
    }

    public double setRadius(double upperRadius)
    {
        radius = upperRadius;
        return radius;
    }

}

The next class is the geometry class and is supposed to have an array of Circle[] circles of size 10. Now my question is, how would I go about making an array that will hold the attributes of the class circle in the geometry class?

import java.util.Scanner;
public class Geometry
{
    //global data
    private double[] circles = new double[10];
    private Circle circles1 = new Circle();
    //constructor
    public Geometry()
    {

    }

    //work methods
    public double addCircle(double radx)
    {
        //Use the findFirstEmpty to locate the first empty space
        int x = findFirstEmpty();
        //Take the empty spot and put the circle in it
        if (x >= 0)
        {
            circles[x] = radx;
            circles1.setRadius(radx);
            System.out.println("Circle was added in index " + x + " and is = " + radx);
        }
        else
        {
            //indicates that the array is full
            System.out.println("There is no more room in the array");
        }
        return radx;
    }

    public void computeAreaAll()
    {
        for (int i = 0; i < circles.length; i++)
        {
            circles1.computeArea();
            System.out.println("Computed area of circle " + i + " is = " + circles1.computeArea());
        }
    }

    public void printAll()
    {

    }

    public void deleteCircleByRadius(double radx)
    {

    }

    public int findFirstEmpty()
    {
        for (int i = 0; i < circles.length; i++)
        {
            if (circles[i] == 0.0)
            {
                //Indicates that the scan located a null spot in the array
                return i;
            }
        }
        //Indicates that the scan produced no null spots in array
        return -1;
    }


    static public void main(String[] args)
    {
        //test all methods in main
        Geometry circles = new Geometry();
        circles.addCircle(1);
        circles.addCircle(3);
        circles.computeAreaAll();
        circles.addCircle(5);
        circles.computeAreaAll();

        //write solution to problem in main
    }
}

I have made an array to test my methods so far, but I need to figure out how to make an array that will hold the Circle from my other class.

//global data
    private double[] circles = new double[10];

what exactly do you mean by that? first, there are no global variables in Java, just to avoid confusion.
secondly, if I understood correctly, you are trying to create an array of Circle objects, not an array of doubles, so what do you need that array for?

stultuske makes a good point: You need to create an array of 10 Circles, to hold the circles. And then you need to create ten Circle objects and put them in the array.

Now my question is, how would I go about making an array that will hold the attributes of the class circle in the geometry class?

Answer: you don't. The individual Circle objects hold their own attributes, all you need is an array of Circle objects

I think I was having a problem understanding the way that constructors could help me. I wasn't grasping that I had to make aan array of objects, and up until now I thought that arrays had to be a specific type, like double or int. So when I figured out that I needed an array of objects, i fixed the code to look more like this Circle[] circles = new Circle[10] and it worked once I properly formatted the constructors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.