My code compiles but it returns no data or anything?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blewie07
    New Member
    • Oct 2014
    • 3

    My code compiles but it returns no data or anything?

    I have to Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius () and getRadius (). The setRadius () method not only sets the radius, it also calculates the other two values. (Remember that the diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius.) Save the class as Circle.java

    And when I run this code I get no errors but it does not return any data what am I missing?
    Code:
     public static void main(String[] args) {
           // default constructor
    	    Circle myCircle = new Circle((int) 1);
                Circle yourCircle = new Circle();
        }
    Code:
        public int radius;
        public double diameter;
        public int area;
                public Circle(int inradius) {
                    int radius;
                        radius = inradius;
                }
                public Circle() {
                    int radius;
                        radius = 1;
                }
    	    //Create a circle with a radius of 1.0
    	     
    	    public double getRadius() {
                double radius = 1;
    	    return radius;
    	       }
    	    public void setRadius(double radius) {
    	        this.radius = (int) radius;
    	      }
    	    public double getDiameter() {
                double diameter = 1;
    	    return diameter;
    	       }
    	    public void setDiameter(double diameter) {
    	    this.diameter = diameter;
    	        }
    	    public double getArea() {
                double area = 1;
    	    return area;
    	          }    
    	    public Circle(double area) {
    	    this.area = (int) area;
    	        }
    	    public double ComputeDiameter(){
    	    return diameter;
    	        }  
    	    public double ComputeArea(){
    	    return area;
    	      }
    	}
    So it runs and just says:
    run:
    BUILD SUCCESSFUL (total time: 6 seconds)
    please help me fix why it does not return anything.e
    Last edited by Frinavale; Nov 7 '14, 07:41 PM.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    In your main application you are instantiating a new circle but not doing anything with it. If you want to print values on the screen you need to actually do that.

    Also, in the constructor of your constructors for your circle class you are setting a method variable called radius but you should be using the class member variable instead.

    Like this:
    Code:
    private int radius;
    private double diameter;
    private int area;
    public Circle(int inradius) {
              this.radius = inradius;
    }
    public Circle() {
      this.radius = 1;
    }
    Likewise you should be getting the radiusclass member in the getRadius method.



    Like this:
    Code:
     public double getRadius() {
       return this.radius;
    }
    public void setRadius(double radius) {
      this.radius = (int) radius;
    }
    Really all of your get and set methods should be returning the class members because that is the purpose of them.....

    Except you were saying that by setting the radius the other members would get set.... which means that there should be no set methods associated with any members that aren't the radius or else the class will contain invalid data if someone were to set them.

    So, the first thing you need to do is fix your setRadius method....
    Code:
    public void setRadius(double radius) {
      this.radius = (int) radius;
      //here you should be doing something like:
      //this.diameter = this.radius + this.radius;
      //this.area =π*this.radius (Squared)
    }
    That way your class is coded up to specifications.

    Then you can delete the set methods and the calculate methods that you have defined for the area and the diameter.

    Once you have done that you can use the circle class the way that you intend it to be used:
    Code:
    public static void main(String[] args) {
      Circle myCircle = new Circle((int) 1);//please note there is a bug in this constructor
      Circle yourCircle = new Circle();  // default constructor
    
      yourCircle.setRadius = 2;
      System.out.printf("The radius is %d ", yourCircle.getRadius());
      System.out.printf("The diameter is %d ", yourCircle.getDiameter());
      System.out.printf("The area is %d ", yourCircle.getArea());
    }
    Please note that the constructor that sets the radius should either call the setRadius method or perform the necessary logic to properly instantiate the other members of the class according to the radius provided or it should do the calculations to properly instantiate the members.

    -Frinny
    Last edited by Frinavale; Nov 7 '14, 08:05 PM.

    Comment

    Working...