Results 1 to 16 of 16

Thread: Circle arrays

  1. #1
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default 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:
    double largestradius = 0;
    for (int i = 0; i < nbCircles; i++)
    if (c.radius > largestradius)
    largestradius = c.radius;
    System.out.printf(largestradius);
    Can anyone help me?

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by gcalvin View Post
    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.

  5. #5
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

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


    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 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. :)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  7. #7
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    Here you go :)

    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);
    
    }
    }
    Here's what it looks like when you run it:

    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
    And here's the code i suggested to get the largest radius.
    Java Code:
    double largestradius = 0;
    for (int i = 0; i < nbCircles; i++)
    if (c.radius > largestradius)
    largestradius = c.radius;
    System.out.printf(largestradius);

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by n00b View Post
    And here's the code i suggested to get the largest radius.
    Java 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

  9. #9
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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++)
       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;
    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.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  11. #11
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    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"

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by n00b View Post
    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"
    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

  13. #13
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    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
    {
    [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.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

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

    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; }
    Stop that all; first answer this question: what was the output (if any)?

    kind regards,

    Jos

  15. #15
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

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

    kind regards,

    Jos
    I don't know what you mean...

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

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

    kind regards,

    Jos

Similar Threads

  1. Circle and line
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 03:56 AM
  2. drawing a dashed circle
    By Alarmmy in forum SWT / JFace
    Replies: 0
    Last Post: 07-13-2009, 09:46 AM
  3. How to write numbers around a circle
    By pheonix in forum New To Java
    Replies: 8
    Last Post: 06-11-2009, 10:20 AM
  4. Generate numbers around a circle?
    By pheonix in forum New To Java
    Replies: 4
    Last Post: 06-05-2009, 05:08 PM
  5. In which circle is the Point lying?
    By nidhirastogi in forum New To Java
    Replies: 1
    Last Post: 07-02-2008, 11:12 PM

Posting Permissions

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