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;
}
Related
So I have the code below but it caused java.lang.StackOverFlowError.
I guess maybe creating an new object inside the second constructor may
have caused an infinite loop or something. But I don't fully understand
why this happened. Could someone explain it to me? I'd appreciate it!
private double x;
private double y;
private double z;
private double[] elements;
/**
* Creates a 3D vector from an array
* #param v array containing 3 components of the desired vector
*/
public Vector3(double[] v) {
this(v[0], v[1], v[2]);
}
/**
* Creates a 3D vector from 3 numeric scalar components
* #param x x coordinate
* #param y y coordinate
* #param z z coordinate
*/
public Vector3(double x, double y, double z) {
this(new Vector3(x, y, z));
}
/**
* Clones an existing vector
* #param old an existing Vector3 object
*/
public Vector3(Vector3 old) {
x = old.x;
y = old.y;
z = old.z;
elements = new double[3];
elements[0] = x;
elements[1] = y;
elements[2] = z;
}
public Vector3(double x, double y, double z) {
this(new Vector3(x, y, z));
}
I'm not sure what you're up to do, but it seems you're calling the Vector3 constructor in itself. This is causing the infinite loop.
In the constructor
public Vector3(double x, double y, double z) {
this(new Vector3(x, y, z));
}
you are calling the same constructor again and again (i.e. new Vector3(x, y, z)). That causes the SOE. This happens before the copy constructor with the call to this(...) even has a chance.
Assuming the following class
public class Vector3 {
// No need for an array. Or if you want an array, discard the doubles.
double x;
double y;
double z;
public double getX() { return x; }
public double getY() { return y; }
public double getZ() { return z; }
}
the correct way to build up constructors is the following:
1) Start with a constructor that builds up a whole instance with all elementary data:
public Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
2) If you want to add some other convenience constructors, do it his way:
public Vector3(double[] elements) {
this(elements[0], elements[1], elements[2]);
}
3) A copy constructor can be handled the same way:
public Vector3(Vector3 origin) {
this(origin.getX(), origin.getY(), origin.getZ());
}
All the other constructors can be built by calling the first constructor!
Here is my class. In the main method at the bottom I pass two arguments in the constructor (int numTriangles, double radius) as (8,1). The variables declared at the top radius and numTriangles should assume those values as my constructor assigns them then runs a calculation. However I get divide by zero error when twoTheta is calculated as numTriangles is zero at this point. Why is this and how can I fix it? Thanks.
package Triangles;
public class Triangles {
double radius;
int numTriangles;
double twoTheta = 360/numTriangles;
double theta = twoTheta / 2;
double base;
double height;
double result;
double halfTriangleArea;
double triangleArea;
double area;
public Triangles(int numTriangles, double radius) {
this.numTriangles = numTriangles;
this.radius = radius;
runCalculation();
}
// My methods
public double calculateBase() { // SOH
double thetaToRadians = Math.toRadians(theta);
double base = Math.sin(thetaToRadians) / radius;
return base;
}
public double calculateHeight() { // CAH
double thetaToRadians = Math.toRadians(theta);
double height = Math.cos(thetaToRadians) / radius;
return height;
}
public double checkPythag(double base, double height) {
double a = base;
double b = height;
double result = Math.sqrt(a*a + b*b);
return result;
}
public double calculateArea(double base, double height) {
double halfTriangleArea = (0.5) * base * height;
return halfTriangleArea;
}
public double runCalculation() {
base = calculateBase();
height = calculateHeight();
result = checkPythag(base, height);
halfTriangleArea = calculateArea(base, height);
triangleArea = 2 * halfTriangleArea;
// C = Pi * D = Pi * 2 * r
// A = Pi * r.^2
area = numTriangles * triangleArea;
// Substitute Pi for X
// area = X * r.^2
// if r is 1
// area = X
return area;
}
// Runnable
public static void main(String[] args) { // create an instance of class to run in main
Triangles triangles = new Triangles(8, 1);
System.out.println("radius: " + triangles.radius);
System.out.println("numTriangles: " + triangles.numTriangles);
System.out.println("twoTheta " + triangles.twoTheta);
System.out.println("theta " + triangles.theta);
System.out.println("base: " + triangles.base);
System.out.println("height: " + triangles.height);
System.out.println("checkPythag " + triangles.result + " | " + triangles.radius);
System.out.println("halfTriangleArea: " + triangles.halfTriangleArea);
System.out.println("triangleArea: " + triangles.triangleArea);
System.out.println("Approximation of Pi by triangles: " + triangles.area);
}
}
As multiple answers have pointed out, twoTheta (and theta as well) is initialized before constructor is called, hence the error.
I suggest you initialize twoTheta, and theta inside the constructor.
public Triangles(int numTriangles, double radius) {
this.numTriangles = numTriangles;
this.radius = radius;
//Initialize twoTheta
this.twoTheta = 360/this.numTriangles;
this.theta = this.twoTheta/2;
runCalculation();
}
You call runCalculation() from the constructor - at this moment twoTheta has assigned it's default value (it's 0 for primitives and that default value is assigned even before constructor is called). There are two simple ways of solving your problem:
You should call runCalculation() from outside of the constructor to be sure that all fields are initialized to values other than default (to values that you specified)
OR
You could initialize twoTheta and thetain the constructor before calling runCalculation().
If you want to use first option - change your main method like this to see results that you expect:
public static void main(String[] args) {
Triangles triangles = new Triangles(8, 1);
triangles.runCalculation();
...
}
You should also delete call of runCalculation() from the body of the constructor.
If you choose second way of solving your problem, just initialize theta and twoTheta before runCalculation() in the constructor body:
public Triangles(int numTriangles, double radius) {
this.numTriangles = numTriangles;
this.radius = radius;
this.twoTheta = 360/this.numTriangles;
this.theta = this.twoTheta/2;
runCalculation();
}
You might want to look here at official Java tutorial to see a list of default values assigned to primitive types and to read a bit more about it (by the way, objects are assigned to null by default).
Ths class attributes are initialized before the constructor is called.
So, the attribute numTriangles is first set to 0. After that double twoTheta = 360/numTriangles; is executed.
The constructor is called afterwards.
That's why you get your error.
Therefore, don't initialize the attributes twoTheta and theta directly but let the constructor handle it after having set the numTriangles and radius attributes.
Call runCalculation();
after
Triangles triangles = new Triangles(8, 1);
in your main method.
Let the constructor complete.
Because these definitions are executed at the creation of the class and not at the instantiation of the object. Furthermore, when you declare but don't assign radius or numTriangles, the default value 0 is used. The default value is 0 for any primitive type (double, int, float, ...) and null for any other type (String, Triangle, ...).
You should only declare them in the class and assign them in the constructor.
double radius;
int numTriangles;
double twoTheta;
public Triangle(double radius, int numTriangles) {
this.radius = radius;
this.numTriangles = numTriangles;
this.twoTheta = 360 / numTriangles;
}
You can change your code like this to avoid the error.
public Triangles(int numTriangles, double radius) {
this.numTriangles = numTriangles;
this.radius = radius;
initialize();
runCalculation();
}
private void initialize()
{
twoTheta = 360/numTriangles;
theta = twoTheta / 2;
}
package Geometry;
public class TestGeometryPoint {
public static void main(String[] args) {
//Creates the object PointA and Assigns values
Point pA = new Point(); //PointA(0, 0)
pA.setX(2.0); //PointA(2.0, 0)
pA.setY(5.0); //PointA(2.0, 5.0)
//Creates the object PointB and Assigns values
Point pB = new Point(4.0, 6.1); //PointB(4.0, 6.1)
//Calculates the distance between PointA and PointB
double d = pA.distance(4.0, 6.1); //Distance from class to a set of coordinates
double dP = pA.distance(pB); //Distance from class to another point
double dPtP = Point.distance(pA, pB); //Distance from point to point
//Prints the result of the calculations
System.out.println("Distance between Point A & B: " + d);
System.out.println("Distance between Point A & B: " + dP);
System.out.println("Distance between Point A & B: " + dPtP);
}
}
This is my Test Class and my Point class below.
package Geometry;
public class Point {
//Initializes the coordinates for a point on a graph with the values of x and y
private static double x;
private static double y;
//Defualt Constructor
public Point() {
x = 0;
y = 0;
}
//Point Constructor
public Point(double x, double y) {
this.x = x;
this.y = y;
}
//Returns the x value
public double getX() {
return x;
}
//Changes the x value
public void setX(double x) {
Point.x = x;
}
//Returns the y value
public double getY() {
return y;
}
//Changes the y value
public void setY(double y) {
Point.y = y;
}
//Calculates the distance between the class's point coordinates and another set of point coordinates
public double distance(double x0, double y0) {
double distance = (Math.sqrt(((x0 - x) * 2.0) + ((y0 - y) * 2.0)));
return distance;
}
//Calculates the distance between the class's point and another Point class's location
public double distance(Point p) {
double distance = (Math.sqrt(((p.getX() - x) * (p.getX() - x)) + ((p.getY() - y) * (p.getY() - y))));
return distance;
}
//Calculates the distance between a Point class's location and another Point class's location
public static double distance(Point p1, Point p2) {
double distance = (Math.sqrt((Math.pow((p2.getX() - p1.getX()), 2.0) + (Math.pow((p2.getY() - p1.getY()), 2.0)))));
return distance;
}
}
d, dP, dPtP: all return 0.0 when compiled and run, but I have no idea why as I have tried changing code and checking my math when calculating distance. I think I might just need a new set of eyes to take a look at it.
I believe since your x and y variables are static, they belong to the class Point as a whole. When you create a Point(2,5);
You set the static variable of the class Point to be 2 and 5 and then, you create another Point(4,6) You set the same static value of x and y to be 4,6.
Therefore, comparing the distance between the same two Point are 0.
The right code for the class Point would be like so
public class Point {
//Initializes the coordinates for a point on a graph with the values of x and y
private double x;
private double y;
//Defualt Constructor
public Point() {
x = 0;
y = 0;
}
//Point Constructor
public Point(double x, double y) {
this.x = x;
this.y = y;
}
//Returns the x value
public double getX() {
return x;
}
//Changes the x value
public void setX(double x) {
this.x = x;
}
//Returns the y value
public double getY() {
return y;
}
//Changes the y value
public void setY(double y) {
this.y = y;
}
//Calculates the distance between the class's point coordinates and another set of point coordinates
public double distance(double x0, double y0) {
double distance = (Math.sqrt(((x0 - x) * 2.0) + ((y0 - y) * 2.0)));
return distance;
}
//Calculates the distance between the class's point and another Point class's location
public double distance(Point p) {
double distance = (Math.sqrt(((p.getX() - x) * (p.getX() - x)) + ((p.getY() - y) * (p.getY() - y))));
return distance;
}
//Calculates the distance between a Point class's location and another Point class's location
public static double distance(Point p1, Point p2) {
double distance = (Math.sqrt((Math.pow((p2.getX() - p1.getX()), 2.0) + (Math.pow((p2.getY() - p1.getY()), 2.0)))));
return distance;
Your problem is that you are confusing static and non-static fields.
By putting that little static on your field declarations you are saying: all instances of this class should be seeing the exact same variables.
So, when you create two points
p1 = new Point(5, 5);
p2 = new Point(10, 10);
the declaration of p2 "overrides" the 5-5 from p1 ... because, as said: all Points are using the same x and y.
Thus, solution: simply drop that keyword from the definition of x and y. Then each point has is very own x and y.
And, more importantly: understand that each and any character in your source code matters. This means: you better understand each and any concept that your source code is making use of!
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
This is not a homework,this is an exercise in a Java book I am learning by myself.
Build a class with the name circle which represents a circle in the coordinate plane.The fields of the class should be radius length,dy coordinates of the center.The methods of the class should be :
getArea() : Finds the area of the circle
getPerimeter() : Finds the perimeter of the circle
moveCircle() : Changes the coordinates of the center of the circle
modifyRadius() : Modifies the radius of the circle
private int x, y;
public Circle() {
x = 0;
y = 0;
radius = 1;
}
public Circle(int x, int y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
Now how do I continue this? for moveCircle and ModifyRadius?
There should be a variable called radius in your class.
private int radius;
to move circle, you should pass your new location of the center to the object. Then it will set the new position.
public void moveCircle(int newX, int newY) {
this.x = newX;
this.y = newY;
}
To modify the radius you also can use the same method.
public void ModifyRadius (double newRadius) {
this.radius = newRadius;
}
Another way is to create setters for the variables.
public void setX (int x) {
this.x = x;
}
public void setY (int y) {
this.y = y;
}
public void moveCircle (int x, int y) {
setX(x);
setY(y);
}
public void setRadius (double radius) {
this.radius = radius;
}