Results 1 to 16 of 16
Thread: Circle arrays
- 05-05-2010, 12:06 AM #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
{
private double radius;
public Cercle(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);
}
}
here's the code i came up with to display the cercle with the largest radius, i dont know where to put it though:
Can anyone help me?double largestradius = 0;
for (int i = 0; i < nbCircles; i++)
if (c.radius > largestradius)
largestradius = c.radius;
System.out.printf(largestradius);
- 05-05-2010, 12:20 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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 #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-05-2010, 03:15 AM #5
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
My mistake :) sorry i'm new here.Please use CODE tags rather than QUOTE tags.
Java 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); } }
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. :)Yeah, I wonder that OP didn't notice this at all. Could be an error there.
- 05-05-2010, 07:12 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Where's your indentation? You didn't just copy and pasted your mess from your first post did you? Please realize that we have to read and understand what you have written. Help us to help you.
kind regards,
Jos
- 05-05-2010, 02:20 PM #7
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
Here you go :)
Here's what it looks like when you run it:Java 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); } }
And here's the code i suggested to get the largest radius.
The content of the arrray:
Cercle at the index 0 :
- radius : 8.40
- circumference : 52.78
- area : 221.67
Cercle at the index 1 :
- radius : 4.70
- circumference : 29.53
- area : 69.40
Cercle at the index 2 :
- radius : 5.10
- circumference : 32.04
- area : 81.71
Cercle at the index 3 :
- radius : 7.50
- circumference : 47.12
- area : 176.71
Java 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 #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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 #9
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
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.
In this loop, I'm comparing the radius of each cercle to the term "largestradius" = 0.Java Code:double largestradius = 0; for (int i = 0; i < nbCircles; i++) if (c[i].radius > largestradius) largestradius = c[i].radius; System.out.printf(largestradius);
I went with the following logic;
The loop is going to start with the first circle which is c[0]. The radius of the circle c[0] is surely > largestradius since radius != 0 and largestradius = 0.
so the new value of largestradius will be the c[0].radius.
now the loop should jump to the next circle which is c[1].
its going to compare the c[1].radius to the largestradius (which is equal now to c[0].radius).
If c[1].radius > largestradius then largestradius = c[1].radius.
largestradius takes the new bigger value which c[1].radius in this case.
if c[1].radius < largestradius then largestradius won't change and will still be equal to c[0].radius, the largest radius the loop has seen so far.
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 #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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 #11
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
I took your advice, and did this:
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) { 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 #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-05-2010, 04:54 PM #13
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
Tried that. It didn't work... :(Ah, I see it: use System.out.println( ... ) not System.out.printf( ... ).
Also i edited the first method to public
Java 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; }Last edited by n00b; 05-05-2010 at 04:57 PM.
- 05-05-2010, 05:00 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-05-2010, 05:03 PM #15
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
- 05-05-2010, 05:04 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Circle and line
By c_walker in forum New To JavaReplies: 1Last Post: 01-27-2010, 03:56 AM -
drawing a dashed circle
By Alarmmy in forum SWT / JFaceReplies: 0Last Post: 07-13-2009, 09:46 AM -
How to write numbers around a circle
By pheonix in forum New To JavaReplies: 8Last Post: 06-11-2009, 10:20 AM -
Generate numbers around a circle?
By pheonix in forum New To JavaReplies: 4Last Post: 06-05-2009, 05:08 PM -
In which circle is the Point lying?
By nidhirastogi in forum New To JavaReplies: 1Last Post: 07-02-2008, 11:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks