Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0

    Circle class-Help needed


    Hello all,

    I have to create the following program, and I am totally confused.Your help is greatly appreciated . Thanks.

    Program:

    Write a class called Circle which has two constructors. The first constructor (default constructor) does not take any parameter and supplies default values for the coordinates and the radius. The second constructor takes three doubles as parameters corresponding to the X and Y coordinates and the radius. The class must include these methods:

    public double circumference() returns the circumference of the circle.

    public double area() returns the area of the circle.

    public void setRadius(double r) is called in the constructor and checks the radius against a maximum. If the radius is greater than the maximum, setRadius resets it to the maximum (using the ternary conditional operator). You may set your own maximum value. ---I dont know how to do so.

    public void printAttributes() prints the coordinates, the radius, the circumference, and the area.

    public boolean isInside(double x, double y) return true if a point represented in the parameters falls inside the circle, false otherwise.----I dont get the question

    public void move(int x, int y) moves the origin by a specified amount. --- I dont get the question

    My work so far:


    Code:
    public class Circle {
    
    public double radius;
    public double xCoord;
    public double yCoord;
    public double maxR=100.00;
    
    
    public static void main(String[] args) {
    	Circle cir= new Circle();
    	cir.printAttributes();
    
    }
    public Circle(){
    	
    	radius=0;
    	xCoord=0;
    	yCoord=0;
    }
    
    public Circle(double x,double y,double r){
    	radius=r;
    	xCoord=x;
    	yCoord=y;
    	
    }
    //It is called in the constructor and checks the radius against a maximum.
    public void setRadius(double r){
    	radius=r;
    	
    	//r=radius>maxR ?maxR:radius;
    
    	
    }
    //Returns the circumference of the circle.
    public double circumference(){
    	return Math.PI*2*radius;
    }
    	
    
    //Returns the area of the circle.
    public double area(){
    	return (radius*radius)*Math.PI;
    }
    	
    
    
    //Prints the coordinates, the radius, the circumference, and the area.
    public void printAttributes(){
    	System.out.println("Area is:"+area()+" Circumference is: "+circumference()+" Radius is: "+radius);
    	
    }
    //Returns true if a point represented in the parameters falls inside the circle, false otherwise.
    public boolean isInside(double x, double y) {
    	double distance;
    	distance= Math.sqrt(x*x+y*y);
    	if(distance<radius)
    		return true;
    	else
    		return false;
    	
    }
    //Moves the origin by a specified amount.
    public void move(int x, int y) {
    	x=x;
    	y=y;
    	
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit your post and wrap the code in code tags.
    moves the origin by a specified amount. --- I dont get the question
    The x,y values passed to the method are to be added to the current x,y values for the center of the circle.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    public boolean isInside(double x, double y) return true if a point represented in the parameters falls inside the circle, false otherwise.----I dont get the question
    You have the radius, so you know the maximum amount that your x and y coordinates can extend past the centre on the circle. So, check to see if your instance variable x + radius is greater than or equal to x and if your instance variable for y is greater than or equal to y. If both are true, then the point is inside the circle.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    With the inSide function, you have an error in this:
    Code:
    distance= Math.sqrt(x*x+y*y);
    What is your knowledge about the Pythagorean theorem?
    The input to your function is a point with x and y coords, but for calculation you will have to use the length between the xCoords and x as well as yCoords and y.
    something like his: (untested)
    Code:
    xDist = x - xCoords;
    yDist = y - yCoords ;
    distance= Math.sqrt((xDist*xDist) + (yDist*yDist));
    In your move function, you should add the value of x and y to xCoords and yCoords receptively.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    You guys are awesome. Thanks for reminding of Pythagorean theorem, it helped me a lot. I changed the code and now the ternary condition in the setRadius method doesn't work.

    Code:
    public class Circle {
    //Fields of the class.
    public double radius;
    public double xCoord;
    public double yCoord;
    public int x;
    public int y;
    
    //Main method and the object of the class.
    public static void main(String[] args) {
    	Circle cir= new Circle();
    	cir.printAttributes();
    }
    
    //First constructor; sets default values.
    public Circle(){
    	
    	this.radius=200;
    	this.xCoord=10;
    	this.yCoord=10;
    }
    
    //The second constructor takes three doubles as parameters corresponding to the X and Y coordinates & the radius.
    public Circle(double x,double y,double r){
    	radius=r;
    	xCoord=x;
    	yCoord=y;
    	
    }
    
    //It is called in the constructor and checks the radius against a maximum.
    public void setRadius(double r){
    	double maxR=100.00;
    	r=radius;
    	r=radius>maxR?maxR:radius;
    	
    	}
    
    
    public double getRadius() {
    	return radius;
    }
    
    //Returns the circumference of the circle.
    public double circumference(){
    	return Math.PI*2*radius;
    }
    	
    
    //Returns the area of the circle.
    public double area(){
    	return (radius*radius)*Math.PI;
    }
    	
    //Prints the coordinates, the radius, the circumference, and the area.
    public void printAttributes(){
    	System.out.println("Area is:"+area()+" .Circumference is: "+circumference()+". Radius is: "+getRadius()+". Points fall inside the circle? "+isInside(x, y));
    	
    }
    
    //Returns true if a point represented in the parameters falls inside the circle, false otherwise.
    public boolean isInside(double x, double y) {
    	double distance;
    	x=x-xCoord;
    	y=y-yCoord;
    	distance= Math.sqrt((x*x)+(y*y));
    	if(distance<radius)
    		return true;
    	else
    		return false;
    	
    }
    
    //Moves the origin by a specified amount.
    public void move(int x, int y) {
    	x=10;
    	y=10;
    	
    }
    
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    ternary condition in the setRadius method doesn't work.
    Can you explain what "doesn't work" means?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by NormR
    Can you explain what "doesn't work" means?
    The program is supposed to reset the radius to the maximum value if the radius is greater than the max value. In this case maximum value is set to 100, however the radius is 200. Shouldn't the program reset the printed radius value to a max of 100?

    Thanks.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Where is a value assigned to the variable: radius that is different from the value you expected?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    The first constructor assigns the default values:

    Code:
    public Circle(){
    	
    	this.radius=200;
    	this.xCoord=10;
    	this.yCoord=10;
    }

    then the set method:

    Code:
    public void setRadius(double r){
    	double maxR=100.00;
    	r=radius;
    	r=radius>maxR?maxR:radius;
    	
    	}
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    If you want to set a variable equal to something you say:
    variable = result; It's a style thing, but I think its important to be consistent and logically that is how its always been done in Mathematics.
    Code:
    public void setRadius(double r){
    	double maxR=100.00;
    	radius= (r > maxR) ? maxR : r;
    	}
    I removed the middle line because it is redundant.

    I think its much clearer to write. Once again this is just style, but I do think its clearer.
    Code:
    if (r > maxR)
        this.radius = maxR;
    else
        this.radius = r;
  20. #11
  21. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    where do you call the setRadius function?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    I totally get that part, however I am still not getting the result. The question asks for:

    1) public void setRadius(double r) – is called in the constructor and checks the radius against a maximum. If the radius is greater than the maximum, setRadius resets it to the maximum (using the ternary conditional operator). You may set your own maximum value.

    2)The first constructor (default constructor) does not take any parameter and supplies default values for the coordinates and the radius. The second constructor takes three doubles as parameters corresponding to the X and Y coordinates and the radius.


    Now I think I have done all it asks through my code, however I am not entirely certain about it. Thanks for your help guys, truly appreciate it.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Your actually not calling setRadius at all. Take a close look at your second constructor. Your radius logic isn't very helpful if you never use it

    Code:
    //The second constructor takes three doubles as parameters corresponding to the X and Y coordinates & the radius.
    public Circle(double x,double y,double r){
    	radius=r;
    	xCoord=x;
    	yCoord=y;
    	
    }
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    Once again, you guys rock. I totally get it now and I modified the code. One more question: What's the point of move method?

    Code:
    //Moves the origin by a specified amount.
    public void move(int x, int y) {
    	x=10;
    	y=10;
    	
    }
    I can thank you guys enough.
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What's the point of move method?
    moves the origin by a specified amount.
    Where is the value of the origin? It is supposed to be changed by the values of the args passed to the move() method.
    If the current origin's x value is 234 and it is to be moved by 40, then its new value would be 274.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo