1. Member Join Date
May 2010
Posts
10
Rep Power
0

## 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:

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:
for (int i = 0; i < nbCircles; i++)
Can anyone help me?  Reply With Quote

2. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
11

## 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-  Reply With Quote

3. ## 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.  Reply With Quote

4. ##  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.  Reply With Quote

5. Member Join Date
May 2010
Posts
10
Rep Power
0

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

Java Code:
```class Circle
{

public Circle(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);

}
}```

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. :)  Reply With Quote

6. ## kind regards,

Jos  Reply With Quote

7. Member Join Date
May 2010
Posts
10
Rep Power
0

##  Originally Posted by JosAH kind regards,

Jos
Here you go :)

Java Code:
```/* [U]BEGINNING OF CLASS: CIRCLE [/U]*/
class Circle
{

/* [U]TO CALCULATE THE RADIUS[/U] */

public Circle(double r) {

/* [U]TO CALCULATE THE CIRCUMFERANCE[/U] */
public double circumference() {
return 2 * Math.PI * radius; }

/* [U]TO CALCULATE THE AREA[/U] */
public double area() {

/* [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(" - 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] */

}
/* [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:

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.
Java Code:
```double largestradius = 0;
for (int i = 0; i < nbCircles; i++)  Reply With Quote

8. ##  Originally Posted by n00b And here's the code i suggested to get the largest radius.
Java Code:
```double largestradius = 0;
for (int i = 0; i < nbCircles; i++)
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  Reply With Quote

9. Member Join Date
May 2010
Posts
10
Rep Power
0

##  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.

Java Code:
```double largestradius = 0;
for (int i = 0; i < nbCircles; i++)
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.
Then the loop should move on to next circle which is c. 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.  Reply With Quote

10. ## 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  Reply With Quote

11. Member Join Date
May 2010
Posts
10
Rep Power
0

## Java 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)
{
for (int i = 0; i < nbCircles; i++)
}
}```

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

12. ##  Originally Posted by n00b Java 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)
{
for (int i = 0; i < nbCircles; i++)
}
}```

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  Reply With Quote

13. Member Join Date
May 2010
Posts
10
Rep Power
0

## 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

Java Code:
```class Circle
{

public Circle(double r) {

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

public double surface() {
Last edited by n00b; 05-05-2010 at 04:57 PM.  Reply With Quote

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

Also i edited the first method to public

Java Code:
```class Circle
{

public Circle(double r) {

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

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

kind regards,

Jos  Reply With Quote

15. Member Join Date
May 2010
Posts
10
Rep Power
0

##  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...  Reply With Quote

16. ##  Originally Posted by n00b I don't know what you mean...
What was the output of your program?

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•