Problem: Did I properly make this UML Diagram?
UML Diagram:
Circle
-radius : double
+PI : double
+setRadius(rad: double) : void
+getRadius(rad: double) : void
+getArea() : double
+getDiameter() : double
+getCircumference() : double
Code:
public class Circle
{
//Declaration
private double radius;
public static final double PI = 3.14159;
public Circle()
{
radius = 0.0;
}
public Circle(double rad)
{
radius = rad;
}
/**
The setRadius method stores a value in the
radius field.
#param rad The value to store in radius.
*/
public void setRadius(double rad)
{
radius = rad;
}
/**
The getRadius method returns a Radius
for the Circle.
#return The entered radius in the radius field.
*/
public double getRadius(double rad)
{
return radius;
}
/**
The getArea method returns an Area
for the Circle.
#return The Area for the Circle.
*/
public double getArea()
{
return PI * (radius * radius);
}
/**
The getDiameter method returns a diameter
for the Circle.
#return The diameter for the Circle.
*/
Related
I am working on a Java Lab for school. The purpose is to calculate the perimeter, area, and radius of circle r given a polygon with x # of sides and n length.
My Code:
public class RegularPolygon {
private int myNumSides; // # of sides
private double mySideLength; // length of side
private double myR; // radius of circumscribed circle
private double myr; //radius of inscribed circle
/**
* This is a default constructor creating a 3 sided polygon (triangle)
* This means that myNumSides should be initialized to 3
*/
public RegularPolygon() {
this.myNumSides = 3;
}
/**
* This is a parameter constructor with one int parameter and one double parameter
* #param numSides This is a parameter that sets the # of sides for object RegularPolygon
* #param sideLength This is a parameter for the length of each side in object RegularPolygon
*/
public RegularPolygon(int numSides, double sideLength) {
this.myNumSides = numSides;
this.mySideLength = sideLength;
}
/**
* Private Method to calculate the radius of the inscribed circle
* #return Nothing
*/
private void calcr() {
this.myr = 0.5 * this.getSideLength() * (1/(Math.tan(Math.PI / this.getNumside())));
}
/**
* Private Method to calculate the radius of the circumscribed circle
* #return Nothing
*/
private void calcR() {
this.myR = 1 / 2 * this.getSideLength() * (1 / (Math.sin(Math.PI / this.getNumside())));
}
/**
* This is a method that calculates the Vertex Angle. The Vertex Angle is assigned to to the variable q
* #return double Returns the double value of q, the vertex angle
*/
public double vertexAngle() {
double q = ((this.getNumside() - 2) / this.getNumside()) * Math.toRadians(180);
return q;
}
/**
* Method that calculates the perimeter of the polygon. The perimeter is assigned to the double variable perimeter
* #return double Returns the double value of the perimeter
*/
public double Perimeter() {
double perimeter = this.getSideLength() * this.getNumside();
return perimeter;
}
/**
* Method that calculates the area of the polygon RegularPolygon
* #return double Returns the double value of the area
*/
public double Area() {
double area = 1 / 2 * this.getNumside() * Math.pow(this.getR(), 2) * Math.sin(2 * Math.PI / this.getNumside());
return area;
}
/**
* Getter method that returns the value of myNumSides
* #return int Value of myNumSides
*/
public int getNumside() {
return myNumSides;
}
/**
* Getter Method that returns the value of mySideLength
* #return double Value of mySideLength
*/
public double getSideLength() {
return mySideLength;
}
/**
* Getter Method that returns the value myR
* #return double Value of circumscribed circle myR
*/
public double getR() {
return myR;
}
/**
* Getter Method that returns the value of inscribed circle myr
* #return double Value of inscribed circle myr
*/
public double getr() {
return myr;
}
}
Everything is correct except for the calcR and calcr methods. For some reason the value returned is always 0.0. Not sure if I am using the Math methods incorrectly or something.
Here is my testing code:
public class PolygonDriver {
public static void main(String[] args) {
RegularPolygon poly = new RegularPolygon(4, 10);
System.out.println(poly.Area());
System.out.println(poly.getR());
System.out.println(poly.Perimeter());
}
}
After running it, I get:
0.0
0.0
40.0
What is wrong with my methods calcR() and calcr()?
calcr() and calcR are not being called anywhere in your code so myr and myR are never being set so you are getting the initialized value of 0.0
Besides not calling calcr() and calcR(), you should change the 1/2 in Area() and the calcR() to 0.5 so that you are working with the correct data type.
I have to make a program that has the Circle class which calculates the area and circumference of a circle by accepting a parameter for r from a new circle object in main. I fed it the radius value of 14. This is what it should output:
***** Circle *****
radius 14.0
area 615.75164
circumference 87.96452
But instead I'm getting this
***** Circle *****
radius 14.0
area 0.0
circumference 0.0
Why is this happening and how can I fix it? I was given the overall shell of the code to make work in java and I have to keep it for the most part how it was presented to me, so while I appreciate the intention of giving recommendations on better ways to structure things and make this program in general, the teacher wants me to do it this way. The only thing I'm not sure of is what to do with setRadius(), and whether or not I should have declared r at the top of the class.
package shapedemo;
class Circle {
private double radius;
private double circumference;
private double area;
private double r;
// constructors
public Circle(){
radius = 1;
}
public Circle(double r) {
radius = r;
}
// setters and getters
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
// other methods
public double calcArea() {
area = Math.PI * r * r;
return area;
}
public double calcCircumference(){
circumference = Math.PI * 2 * r;
return circumference;
}
// display method
public void display() {
/*blank line
***** Circle *****
radius nnnn.nn
area nnnn.nn
circumference nnnn.nn
blank line
*/
System.out.println("\n***** Circle *****\nradius " + radius + "\narea " + area + "\ncircumference " + circumference + "\n");
}
}
public class ShapeDemo {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Circle circle1 = new Circle(14);
circle1.display();
}
}
Your area and circumference variables are only being set if you call the appropriate calculation methods (calcArea and calcCircumference). You're not calling them, so they have the default values of 0.
You could fix this in your main method:
public static void main(String[] args) {
Circle circle = new Circle(14);
circle.calcArea();
circle.calcCircumference();
circle.display();
}
... although you'd also have to fix your methods to use radius instead of r, as you're never setting r.
Personally I think it would be better to either move the logic into your constructor or calculate it on demand with methods rather than having fields at all for them.
First approach, in the constructor:
final class Circle {
private final double radius;
private final double circumference;
private final double area;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
radius = r;
area = Math.PI * radius * radius;
circumference = Math.PI * 2 * radius;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + area +
"\ncircumference " + circumference + "\n");
}
}
Second approach, making methods compute the values:
final class Circle {
private final double radius;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getCircumference() {
return Math.PI * 2 * radius;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + getArea() +
"\ncircumference " + getCircumference() + "\n");
}
}
In both cases, I've made this an immutable type - while you can keep it mutable, in the first case that would mean recomputing the area and circumference. In that case you'd probably want your constructor to just call setRadius() and make that method do all the computations.
For example:
final class Circle {
private double radius;
private double circumference;
private double area;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
setRadius(r);
}
public void setRadius(double r) {
radius = r;
area = Math.PI * radius * radius;
circumference = Math.PI * 2 * radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return area;
}
public double getCircumference() {
return circumference;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + area +
"\ncircumference " + circumference + "\n");
}
}
You never call to calcArea() and calcCircumference(), so circumference and area have the intial 0 value.
Circle circle1 = new Circle(14);
circle1.calcArea();
circle1.calcCircumference();
circle1.display();
Or change your constructor to
public Circle(double r) {
radius = r;
calcArea();
alcCircumference();
}
You're not calling the calcArea and calcCircumference so they retain the default value 0.0d
this should be your public static void main()
public static void main(String[] args) {
Circle circle1 = new Circle(14);
circle1.calcArea();
circle1.calcCircumference();
circle1.display();
}
Also, change your return type of calcArea() and calcCircumference() to void
I have an issue with simple 2d collision detection with a circle and a rectangle. All the checker does is see if the center of the circle overlaps with any point on the rectangle.
Heres the checker:
boolean overlaps(Circle circle) {
for (double i = topLeft.x; i < (topLeft.x + width); i++)
{
for (double j = topLeft.y; j < (topLeft.y + height); j++)
{
if (circle.center.x == i && circle.center.y == j)
{
return true;
}
}
}
return false;
}`
Very simple, and to the point. The error comes into play at the if statement, with my circle.center.x, and/or circle.center.y.
Here is the code for my Circle class:
public class Circle {
Point center;
double radius;
/**
* Constructor.
*
* #param center the center Point of the circle
* #param radius the radius of the circle
*/
public Circle(Point center, double radius) {
center = this.center;
radius = this.radius;
}
/**
* Get the current center point of the circle.
*
* #return the center point of the circle
*/
public Point getCenter() {
return center;
}
/**
* Set the center point of the circle.
*
* #param center the (new) center point of the circle
*/
public void setCenter(Point center) {
this.center = center;
}
/**
* Get the current radius.
*
* #return the current radius
*/
public double getRadius() {
return radius;
}
/**
* Set the radius.
*
* #param radius the (new) radius of the circle
*/
public void setRadius(double radius) {
this.radius = radius;
}
}`
Where am i going wrong? It certainly cannot be an issue with my Point class, as that would mean plenty of other things would've gone wrong by now. Thanks in advance.
Your method should just be:
boolean overlaps( Circle circle )
{
return topLeft.x <= circle.center.x && circle.center.x <= topLeft.x + width &&
topLeft.y <= circle.center.y && circle.center.y <= topLeft.y + height;
}
The errors I am getting are
1 a cannot find symbol error
and
2 "constructor circle, in class circle cannot be applied to given types. "
At this point I just can seem to grasp what I did wrong.
public class Circle {
private double radius;
public Circle (double radius) {
radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
}
class B extends Circle {
private double length;
B (double radius, double length) {
Circle (radius);
length = length;
}
//**override getArea()*/
public double getArea() {
return getArea() * length;
}
}
At super-class Circle use this to refer current instance.
public Circle (double radius) {
this.radius = radius;// Use this
}
At sub-class use super() to access super-class constructor. Change from
B (double radius, double length) {
Circle (radius);// This is compilation error.
length = length;
}
To
B (double radius, double length) {
super(radius); // This is the way to access super-clss constructor.
this.length = length; //Use this to refer current instance length.
}
I'd recommend you to change:
radius = radius;
to
this.radius = radius; // 'this' makes reference to the actual instance
And change the B constructor to:
public Test(double radius, double length)
{
super(radius); // Calls super class constructor
this.length = length;
}
I have an assignment to create a Circle class which creates a circle using radius and then uses get to find the area, diameter, etc.
The next step in the assignment is to "Continuously use the Scanner object created at beginning to ask user to enter another value for radius." at first I tried to copy+paste the code to ask the user for radius and assign to a local variable like i did originally and I get the following error: "The assigned value is never used." Also the assignment says to change the value of the circle instance, not to make a new circle and assign that a different value.
I did some searches but am unsure how to proceed.
Code for my circle.java class:
public class Circle {
// intialize variable radius
private double radius;
// create constant PI
public final static double PI = 3.14159;
// Circle constructtor
public Circle(double r) {
radius = r;
}
// setRadius method
public void setRadius(double r) {
radius = r;
}
// getRaduis method
public double getRadius() {
return radius;
}
// getArea method
public double getArea() {
double area = PI * radius * radius;
return area;
}
// getDiameter method
public double getDiameter() {
double diameter = 2 * radius;
return diameter;
}
// getCircumference method
public double getCircumference() {
double circumference = 2 * PI * radius;
return circumference;
}
}
And the code for my program circleTest:
package circle;
import java.util.Scanner;
public class CircleTest {
public static void main(String[] args) {
double circleRadius;
double radius;
// import scanner
Scanner keyboard = new Scanner(System.in);
// ask user for raduis and store in a local variable
System.out.print("What is the radius of the circle?");
circleRadius = keyboard.nextDouble();
// Create a circle object called circle1
Circle circle1 = new Circle(circleRadius);
// display area using getArea method for circle1
System.out.println("The area of the circle is " + circle1.getArea());
// display diameter for circle1 using getDiameter method
System.out.println("The diamter of a the circle is "
+ circle1.getDiameter());
// display the circumference of circle1 using getCircumference
System.out.println("The circumference of a circle is "
+ circle1.getCircumference());
// ask user for radius and store in a local variable
System.out.print("What is the radius of the circle?");
circleRadius = keyboard.nextDouble();
}
}