### Thread: Circle class help needed

Page 1 of 2 12 Last
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 xCoord;
public double yCoord;
public double maxR=100.00;

public static void main(String[] args) {
Circle cir= new Circle();
cir.printAttributes();

}
public Circle(){

xCoord=0;
yCoord=0;
}

public Circle(double x,double y,double r){
xCoord=x;
yCoord=y;

}
//It is called in the constructor and checks the radius against a maximum.

}
//Returns the circumference of the circle.
public double circumference(){
}

//Returns the area of the circle.
public double area(){
}

//Prints the coordinates, the radius, the circumference, and the area.
public void printAttributes(){

}
//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);
return true;
else
return false;

}
//Moves the origin by a specified amount.
public void move(int x, int y) {
x=x;
y=y;

}
}```
2. 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.
3. 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.
4. With the inSide function, you have an error in this:
Code:
`distance= Math.sqrt(x*x+y*y);`
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.
5. 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 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.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){
xCoord=x;
yCoord=y;

}

//It is called in the constructor and checks the radius against a maximum.
double maxR=100.00;

}

}

//Returns the circumference of the circle.
public double circumference(){
}

//Returns the area of the circle.
public double area(){
}

//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));
return true;
else
return false;

}

//Moves the origin by a specified amount.
public void move(int x, int y) {
x=10;
y=10;

}

}```
6. ternary condition in the setRadius method doesn't work.
Can you explain what "doesn't work" means?
7. 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.
8. Where is a value assigned to the variable: radius that is different from the value you expected?
9. 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.xCoord=10;
this.yCoord=10;
}```

then the set method:

Code:
```public void setRadius(double r){
double maxR=100.00;

}```
10. 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)
else
11. where do you call the setRadius function?
12. 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.
13. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2012
Posts
103
Rep Power
3

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){
xCoord=x;
yCoord=y;

}```
14. 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.
15. 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