7 Replies - 696 Views - Last Post: 24 February 2010 - 08:38 AM Rate Topic: -----

#1 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:15 AM

Hello, some one help me out?

This is what i am trying to complete...
Write a program that creates and instance of a circle class you create. The circle class should have a data field for radius. Make the class have the default constructor and one that accepts radius input. Also create a getter method for getting the area of the circle.

This is what i have so far, and no luck. Can someone please help me out?
public class RAC06_02 
	{
        // default constructor
      private int radius;
               
      double diameter = 2 * radius; 
      double area = Math.PI * radius * radius;
      
      //Create a circle with a radius of 1.0
      
      public double getRadius() {
      return radius;
         }
      public void setRadius(double radius) {
              this.radius =(int) radius;
        }
      public double getDiameter() {
      return diameter;
         }
      public void setDiameter(double diameter) {
      this.diameter = diameter;
              }
      public double getArea() {
      return area;
                }             
      public void setArea(double area) {
      this.area = area;
              }
      public double ComputeDiameter(){
      return diameter;
              }       
      public double ComputeArea(){
      return area;
        }

	
	
	  public static void main(String[] args)
	  {
	   RAC06_02 myCircle = new RAC06_02();
	   RAC06_02 yourCircle = new RAC06_02();
	        double radius;
	        double diameter;
	        double area;
	        radius = myCircle.getRadius();
	   diameter = myCircle.getDiameter();
	        area = myCircle.getArea(); 
	        radius = yourCircle.getRadius();
	        diameter = yourCircle.getDiameter();
	        area = yourCircle.getArea();  
	   System.out.println("The area of the circle of radius " + radius + "is " + area + "with a diameter of " + diameter);
	        
	   System.out.println("The area of the circle of radius " + radius + "is " + area + "with a diameter of " + diameter);
	  }
	} 


Is This A Good Question/Topic? 0
  • +

Replies To: JAva program that creates aninstance of a circle help

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:19 AM

      double diameter = 2 * radius; 
      double area = Math.PI * radius * radius;



These values must be calculated in the getDiameter / getArea methods, otherwise, they will always have the same value, i.e. 0.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,773
  • Joined: 27-December 08

Re: JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:21 AM

Remember, constructor methods have the same name as the class, but with no return type. So a no-args constructor might look like this. You should initialize your variables inside them. So if you want the default radius to be 1 (Unit Circle), initialize radius to 1 then perform your diameter and area calculations. Like so:

public class RAC06_02{
   private int radius;
   private double area, diameter;

   public RAC06_02(){
       radius = 1;
       diameter = 2*radius;
       area = 2*Math.PI*radius;
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:26 AM

Okay great thanks i got it to work! In the question, do you think i need to also ask use for a radius and diameter also along the one i made?
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:26 AM

EdwinNameless is correct.
You have another problem, which is rather a logic problem.
look carefully at this code:
RAC06_02 myCircle = new RAC06_02(); 
           RAC06_02 yourCircle = new RAC06_02(); 
                double radius; 
                double diameter; 
                double area; 
                radius = myCircle.getRadius(); 



you create a new instance of RAC06_22, but you set no radius to it.
so the getRadius will always return 0 (since you declare it as global variable in that class, so it is initialized to 0).
to fix it, you may want to create a constructor for your class to pass a radius when creating a new Object, or you can use the setRadius method which you already have.

same with the rest of the variables diameter and area.

**wow i'm too slow... ninjad by macosxnerd101 regarding the constructor part :)

This post has been edited by japanir: 24 February 2010 - 08:30 AM

Was This Post Helpful? 1
  • +
  • -

#6 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:33 AM

okay, so should i use the

 public double getRadius() {
      return radius=(int)3.0;
         }
      public void setRadius(double radius) {
              this.radius =(int) radius;
        }
      public double getDiameter() {
      return diameter=6.0;
         }
      public void setDiameter(double diameter) {
      this.diameter = diameter;
              }
      public double getArea() {
      return area = Math.PI * radius * radius
      
      ;
      }             
      public void setArea(double area) {
      this.area = area;
              }
      public double ComputeDiameter(){
      return diameter;
              }       
      public double ComputeArea(){
      return area;
         

to ask the user what they want the diameter and radius to be and then i create my own circle?
Was This Post Helpful? 0
  • +
  • -

#7 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:34 AM

Haaa!!
The experts corrected all the things
japanir sir has said the constructor has no return type..
If you want detailed information about the constructors in java
please visit this link
Was This Post Helpful? 0
  • +
  • -

#8 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: JAva program that creates aninstance of a circle help

Posted 24 February 2010 - 08:38 AM

ha okay thanks.... im getting it now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1