# Circle arrays

• 05-05-2010, 12:06 AM
n00b
Circle arrays
Hi
i'm trying to write a program that contains an array of circles and that can post the circle with the biggest radius and can calculate the area of all the circles.
this is the begining of the code:

Quote:

class Cercle
{

public Cercle(double r) {

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

public double surface() {

public void afficher(String message) {
System.out.printf("Circle %s \n", message);
System.out.printf(" - length of circumference : %6.2f\n", circumference());
System.out.printf(" - area : %6.2f\n\n", area()); }

}

public class Array {

static void afficher (Circle[] c, int nbCircles)
{
System.out.printf("\n The content of the arrray:\n");
for (int i = 0; i < nbCircles; i++)
c[i].display("at the index " + i + " :");
System.out.printf("\n");
}

public static void main(String[] args) {

Circle[] c = { new Circle(3.3), new Circle(2.1), new Circle(1.9),
new Circle(5.6), new Circle(7.0)} ;
int nbCircles = c.length;

display (c, nbCircles);

Circle[] d = { new Circle(8.4), new Circle(4.7), new Circle(5.1),
new Circle(7.5)} ;

int nbCircles1 = d.length;
display (d, nbCercles1);

}
}

here's the code i came up with to display the cercle with the largest radius, i dont know where to put it though:
Quote:

for (int i = 0; i < nbCircles; i++)
Can anyone help me?
• 05-05-2010, 12:20 AM
gcalvin
Please use CODE tags rather than QUOTE tags.

The first problem I notice is that your class and constructor are named Cercle, but your main() tries to create a Circle[] array. I haven't looked any deeper than that.

-Gary-
• 05-05-2010, 02:49 AM
Eranga
If you don't know how to use code tags please search in my signature. You can find a link to relevant explanation on that.
• 05-05-2010, 02:52 AM
Eranga
Quote:

Originally Posted by gcalvin
The first problem I notice is that your class and constructor are named Cercle, but your main() tries to create a Circle[] array. I haven't looked any deeper than that.

Yeah, I wonder that OP didn't notice this at all. Could be an error there.
• 05-05-2010, 03:15 AM
n00b
Quote:

Please use CODE tags rather than QUOTE tags.
My mistake :) sorry i'm new here.

Code:

```class Circle { private double radius; public Circle(double r) { radius = r; } public double circumference() { return 2 * Math.PI * radius; } public double surface() { return Math.PI * radius * radius; } public void afficher(String message) { System.out.printf("Circle %s \n", message); System.out.printf(" - radius : %6.2f\n", radius); System.out.printf(" - length of circumference : %6.2f\n", circumference()); System.out.printf(" - area : %6.2f\n\n", area()); } public double getRadius() { return radius; } public void setRadius(double newRadius){ radius = newRadius;} } public class Array { static void afficher (Circle[] c, int nbCircles) { System.out.printf("\n The content of the arrray:\n"); for (int i = 0; i < nbCircles; i++) c[i].display("at the index " + i + " :"); System.out.printf("\n"); } public static void main(String[] args) { Circle[] c = { new Circle(3.3), new Circle(2.1), new Circle(1.9), new Circle(5.6), new Circle(7.0)} ; int nbCircles = c.length; display (c, nbCircles); Circle[] d = { new Circle(8.4), new Circle(4.7), new Circle(5.1), new Circle(7.5)} ; int nbCircles1 = d.length; display (d, nbCercles1); } }```

Quote:

Yeah, I wonder that OP didn't notice this at all. Could be an error there.
Yeah, I'm sorry. It's a mistake I do while translating codes to English so you guys can understand the object subject. The mistake of Cercle / Circle is not in the original codes. :)
• 05-05-2010, 07:12 AM
JosAH

kind regards,

Jos
• 05-05-2010, 02:20 PM
n00b
Quote:

Originally Posted by JosAH

kind regards,

Jos

Here you go :)

Code:

``` /* [U]BEGINNING OF CLASS: CIRCLE [/U]*/ class Circle { /* [U]TO CALCULATE THE RADIUS[/U] */ private double radius; public Circle(double r) { radius = r; } /* [U]TO CALCULATE THE CIRCUMFERANCE[/U] */ public double circumference() { return 2 * Math.PI * radius; } /* [U]TO CALCULATE THE AREA[/U] */ public double area() { return Math.PI * radius * radius; } /* [U]DISPLAY OF THE INFORMATION OF EACH CIRCLE IN THE ARRAY[/U] */ public void afficher(String message) { System.out.printf("Circle %s \n", message); System.out.printf(" - radius : %6.2f\n", radius); System.out.printf(" - length of circumference : %6.2f\n", circumference()); System.out.printf(" - area : %6.2f\n\n", area()); } /* [U]ACCES TO THE RADIUS FROM OTHER CLASS[/U] */ public double getRadius() { return radius; } public void setRadius(double newRadius){ radius = newRadius;} } /* [U]END OF CLASS CIRCLE[/U] */ /* [U]BEGINING OF CLASS ARRAY[/U] */ public class Array { /* [U]TO DISPLAY THE CONTENT OF THE ARRAY FOR EACH CIRCLE[/U] */ static void afficher (Circle[] c, int nbCircles) { System.out.printf("\n The content of the arrray:\n"); for (int i = 0; i < nbCircles; i++) c[i].display("at the index " + i + " :"); System.out.printf("\n"); } public static void main(String[] args) { /* [U]FIRST ARRAY[/U] */ Circle[] c = { new Circle(3.3), new Circle(2.1), new Circle(1.9), new Circle(5.6), new Circle(7.0)} ; int nbCircles = c.length; display (c, nbCircles); /* [U]SECOND ARRAY[/U] */ Circle[] d = { new Circle(8.4), new Circle(4.7), new Circle(5.1), new Circle(7.5)} ; int nbCircles1 = d.length; display (d, nbCercles1); } }```
Here's what it looks like when you run it:
Quote:

The content of the arrray:
Cercle at the index 0 :
- circumference : 52.78
- area : 221.67

Cercle at the index 1 :
- circumference : 29.53
- area : 69.40

Cercle at the index 2 :
- circumference : 32.04
- area : 81.71

Cercle at the index 3 :
- circumference : 47.12
- area : 176.71

And here's the code i suggested to get the largest radius.
Code:

```double largestradius = 0; for (int i = 0; i < nbCircles; i++) if (c.radius > largestradius) largestradius = c.radius; System.out.printf(largestradius);```
• 05-05-2010, 02:51 PM
JosAH
Quote:

Originally Posted by n00b
And here's the code i suggested to get the largest radius.
Code:

```double largestradius = 0; for (int i = 0; i < nbCircles; i++)   if (c.radius > largestradius)       largestradius = c.radius; System.out.printf(largestradius);```

I still don't see any indentation; I added it for you in that last piece of code. I assume that 'c' is a single circle. Why are you comparing its radius in that loop over and over again? Its radius won't change magically from one loop pass to another. Aren't you supposed to check all circles, in an array maybe? If 'c' is an array of circles shouldn't you compare circle c[i] in the body of that loop?

kind regards,

Jos
• 05-05-2010, 03:15 PM
n00b
Quote:

Originally Posted by JosAH
I still don't see any indentation; I added it for you in that last piece of code. I assume that 'c' is a single circle. Why are you comparing its radius in that loop over and over again? Its radius won't change magically from one loop pass to another. Aren't you supposed to check all circles, in an array maybe? If 'c' is an array of circles shouldn't you compare circle c[i] in the body of that loop?

kind regards,

Jos

this is what I'm trying to do. I tried before exactly what you suggested (i put it in the class Arrays right after the display of the content of the arrays) , I changed the c.radius to c[i].radius but it didnt work and I tried other stuff and my last shot was the code I gave you.

Code:

```double largestradius = 0; for (int i = 0; i < nbCircles; i++)   if (c[i].radius > largestradius)       largestradius = c[i].radius; System.out.printf(largestradius);```
In this loop, I'm comparing the radius of each cercle to the term "largestradius" = 0.

I went with the following logic;
now the loop should jump to the next circle which is c[1].
Then the loop should move on to next circle which is c[2]. and so on.
This is the logic of my loop.

I think the codes of my loop are correct but I jut don't know exactly where to put it in my codes.
• 05-05-2010, 03:22 PM
JosAH
Your logic is fine; why not stick it in its own method? You have a couple of methods already so it shouldn't be a problem to create another one. Then call that method to find the largest radio; problem solved ...

kind regards,

Jos
• 05-05-2010, 04:35 PM
n00b

Code:

```/* SECOND ARRAY */ Circle[] d = { new Circle(8.4), new Circle(4.7), new Circle(5.1), new Circle(7.5)} ; int nbCircles1 = d.length; display (d, nbCercles1); }   static void display (Circle[] c, int nbCircles)        { double largestradius = 0; for (int i = 0; i < nbCircles; i++)   if (c[i].radius > largestradius)       largestradius = c[i].radius; System.out.printf("the largest radius is:" + largestradius); } }```

Compilation works fine
When I run it, I can see ""the largest radius is:" + largestradius"
• 05-05-2010, 04:51 PM
JosAH
Quote:

Originally Posted by n00b

Code:

```/* SECOND ARRAY */ Circle[] d = { new Circle(8.4), new Circle(4.7), new Circle(5.1), new Circle(7.5)} ; int nbCircles1 = d.length; display (d, nbCercles1); }   static void display (Circle[] c, int nbCircles)        { double largestradius = 0; for (int i = 0; i < nbCircles; i++)   if (c[i].radius > largestradius)       largestradius = c[i].radius; System.out.printf("the largest radius is:" + largestradius); } }```

Compilation works fine
When I run it, I can see ""the largest radius is:" + largestradius"

Do you see that literally or do you see an actual number? Ah, I see it: use System.out.println( ... ) not System.out.printf( ... ).

kind regards,

Jos
• 05-05-2010, 04:54 PM
n00b
Quote:

Ah, I see it: use System.out.println( ... ) not System.out.printf( ... ).
Tried that. It didn't work... :(

Also i edited the first method to public

Code:

```class Circle { [U]public[/U] double radius; public Circle(double r) { radius = r; } public double circumference() { return 2 * Math.PI * radius; } public double surface() { return Math.PI * radius * radius; }```
• 05-05-2010, 05:00 PM
JosAH
Quote:

Originally Posted by n00b
Tried that. It didn't work... :(

Also i edited the first method to public

Code:

```class Circle { [U]public[/U] double radius; public Circle(double r) { radius = r; } public double circumference() { return 2 * Math.PI * radius; } public double surface() { return Math.PI * radius * radius; }```

Stop that all; first answer this question: what was the output (if any)?

kind regards,

Jos
• 05-05-2010, 05:03 PM
n00b
Quote:

Originally Posted by JosAH
Stop that all; first answer this question: what was the output (if any)?

kind regards,

Jos

I don't know what you mean...
• 05-05-2010, 05:04 PM
JosAH
Quote:

Originally Posted by n00b
I don't know what you mean...

What was the output of your program?

kind regards,

Jos