java - Adding objects with different parameter into the array -


so have question,

class circle:

  1. contains appropriate attribute store radius.
  2. contains constructor 1 parameter set radius.
  3. contains set , methods.
  4. contains method calculating area , method calculating circumference.
  5. circle should contain appropriate attribute keep track (count) of number of circle objects instantiated.

class testcircle:

create array of 10 circles of radii 1.0 , 2.0, ..., 10.0.

print area , circumference of each circle.

retrieve , print number of circles have been instantiated.

my code is:

public class circle {      public double radius= 0.0;   public int counter;   public circle (double radius){     this.radius = radius;     counter++;   }   public circle (){   }    public void setradius (double radius){     this.radius = radius;   }   public double getradius (){     return radius;   }   public double area (){     return 3.14*radius*radius;   }   public double circumference (){     return 2*3.14*radius;   } }   public class testcircle {   public static void main (string args []){     circle [] arr = new circle [10];     system.out.println ("the circumference" + arr.circumference());     system.out.println ("the area" + arr.area());     system.out.println ("the number of circles" + arr.counter);   } } 

my question is: how supposed create 10 circle objects different radius , add array? know idea add objects array using loop couldn't add radius process.

thank you.

your code change

public class circle {        private double radius;     private static int numberofcircles = 0;     public circle (double radius){       this.radius = radius;       numberofcircles++;       system.out.println("the circumference : " + getcircumference());       system.out.println("the area : " + getarea());     }      public double getradius (){       return radius;     }     public double getarea (){       return 3.14*radius*radius;     }     public double getcircumference (){       return 2*3.14*radius;     }      public static int getnumberofcirclescreated(){        return numberofcircles;     } }   public class testcircle {   public static void main (string args []) {       circle [] circles = new circle [10];       for(int counter=0;counter< circles.length;counter++){          circles[counter]=new circle((double)(counter+1));       }       system.out.println("number of circles : " + circle.getnumberofcirclescreated());    } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -