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.