Why are my constructor arguments not passed? - java

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;
}

Related

How to compute a fraction using float in java [duplicate]

This question already has answers here:
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Closed 5 years ago.
I have a problem in floats. My 1st problem is my public float area(), the problem is the result value is returning zero. 2nd is the public float computeHeight(), no value will return. I'm having headache with this. please help me thank you. Just delete if duplicate or repost. thank you
private int sideA, sideB, sideC;
private float computePerimeter;
private float area;
private float computeHeight;
public Triangle(){
}
// I want to set all sides to 10
public Triangle(int a, int b, int c){
sideA = a;
sideB = b;
sideC = c;
}
//setters & getters
//perimeter is the sum of all the sides of the triangle.
public float computePerimeter(){
computePerimeter = sideA + sideB + sideC;
return computePerimeter;
}
//A=1/2bh.
//A = Area of the triangle
//b = Length of the base of the triangle //SideB
//h = Height of the base of the triangle //SideA
public float area(){
area = 1/2 * (sideB * sideA);
return area;
}
public float computeHeight(){
sideC = 2 * (area/sideB) ;
return computeHeight;
}
public void display(){
System.out.println("Side A: "+getSideA() +" Side B: "+getSideB()+" Side C: "+getSideC() );
System.out.println("\nThe sum of all the sides of the triangle is: " +computePerimeter() );
System.out.println("The area of the triangle is: " + area() );
}
public static void main(String []args){
Triangle result = new Triangle();
result.setSideA(10);
result.setSideB(10);
result.setSideC(10);
result.display();
}
Maybe:
public float area(){
area = (float) (0.5f * (sideB * sideA));
return area;
}
public float computeHeight(){
sideC = 2f * (area/sideB) ;
return computeHeight;
}
Adding the "f" and the casting makes the numbers to be treated as a float.
Related to: I don't know how to cast to float in Java.
If you don't do this, then the number is processed as if it was an integer (1/2 in integer world = 0). Thus you lose a lot of precision.
1/2 return 0 because they are int, you need to use (1.0 / 2)
Also I would advise to replace all your float and int by double (for the 6 attributes) , it would allow you to not have warning possible loss convertion' to avoid casting(float)and usef` after the number
private double sideA, sideB, sideC, computePerimeter, area, computeHeight;
And a trick I'd learn to you, this will will assign the result to area AND return it :
public double area() {
return (area = 1.0 / 2 * (sideB * sideA));
}
Last thing, because you have a public Triangle(double a, double b, double c) constructor you may replace
Triangle result = new Triangle();
result.setSideA(10);
result.setSideB(10);
result.setSideC(10);
By Triangle result = new Triangle(10,10,10); in one-line

Can't display the proper value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have these classes:
interface Shape
{
public double getPerimeter();
public double getArea();
public void getDetails();
}
and
class Circle implements Shape
{
private double PI = 3.14;
private int radius;
double perimeter, area;
//constructor
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
}
public int getRadius()
{
return radius;
}
public double getPerimeter()
{
return perimeter;
}
public double getArea()
{
return area;
}
public void getDetails()
{
System.out.println("Shape Type: Circle " + "\n"
+ "Radius: " + getRadius() + "\n"
+ "Perimeter: " + getPerimeter() + "\n"
+ "Area: " + getArea() + "\n" );
}
}
and finally
public class TestShape
{
public static void main(String args[])
{
for(int i = 0; i < args.length; i++)
{
try
{
Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.err.print("wrong");
}
catch(IllegalArgumentException iae)
{
System.err.print("wrong");
}
if(args.length==1)
{
i = Integer.parseInt(args[0]);
Circle r1 = new Circle(i);
r1.getDetails();
}
}
}
}
We were assigned to input 1,2 or 3 numbers on the command line of the terminal and the output would display what kind of shape it is depending on the array size. I managed to get the proper parameter and area when I input a number but the radius keeps displaying 0 instead of the actual number I inputted.
This is what the output looks like
So what do you guys think?
You are not assigning the radius in the constructor of Circle...
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
this.radius = radius // this statement is missing
}
Therefore, circle.getRadius() always returns the uninitialized default int value 0.
In your constructor:
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
}
radius refers to the parameter radius, not the radius field declared in Circle. You need to make them the same by assigning the radius parameter to the radius field. To refer to the radius field, use this.radius.
this.radius = radius;
Because you did not do this in your code, the radius returned by getRadius is 0, which is the default value of an unassigned int.
Instance variable are assigned values by default if not initialized. In case of int it's 0 by default. So to assign the value to your instance variable radius you need to add this line in your constructor.
this.radius = radius
We are using this because your instance variable and local variable are having the same name.

Why does my class's test program return 0.0 for my answers?

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!

Encapsulation with Java

I'm trying to implement encapsulation in a program as part of some coursework however I've run into an error which I just can't seem to be able to fix with my limited knowledge which isn't helped by my Teacher/Lecturer who is very good at what he does however doesn't do very well when it actually comes to communicating the information, because of this could someone help me fix the error which is presented from the following program and explain to me why it's not working as intended.
class TwoDShapeEncap{
double width, height;
//Width
void setWidth(double w){
width = w;
}
double getWidth(){
return width;
}
//Height
void setHeight(double h){
height = h;
}
double getHeight(){
return height;
}
}
class Triangle extends TwoDShapeEncap{
String type;
private double sideA, sideB, sideC, adjacent, opposite;
//Side A
void setsideA(double a){
sideA = a;
}
double getsideA(){
return sideA;
}
//Side B
void setsideB(double b){
sideB = b;
}
double getsideB(){
return sideB;
}
//Side C
void setsideC(double c){
sideC = c;
}
double getsideC(){
return sideC;
}
//Adjacent
void setadjacent(double a){
adjacent = a;
}
double getadjacent(){
return adjacent;
}
//Opposite
void setopposite(double o){
width = o;
}
double getopposite(){
return opposite;
}
double getPerimeter(){
if(getsideB() == 0.0 && getsideC() == 0.0){
type = "equilateral";
return getsideA() * 3;
}
else if (getsideC() == 0.0){
type = "isosceles";
return getsideA() + getsideB() * 2;
}
else{
type = "scalene";
return getsideA() + getsideB() + getsideC();
}
}
//*******************************************************************************************
//* Paste the perimeter() and hypotenuse() methods from your previous class into this class *
//*******************************************************************************************
//***************************************
//* add an area method()into this class *
//***************************************
double area(double a, double b){
getWidth();
getHeight();
return (getWidth() * getHeight()/2);
}
}
class Rectangle extends TwoDShapeEncap{
boolean issquare;
private double height, width;
//Height
void setHeight(double h){
height = h;
}
double getHeight(){
return height;
}
//Width
void setWidth(double w){
width = w;
}
double getWidth(){
return width;
}
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
double area(double a, double b){
//getWidth();
//getHeight();
return (getWidth() * getHeight()/2);
}
boolean testSquare(double h, double w){
//getHeight();
//getWidth();
if (getHeight() == getWidth())
issquare = true;
else issquare = false;
return issquare;
}
//*********************************************
//* add area and perimeter methods this class *
//*********************************************
//*************************************************************************
//* add a testSquare method to test if a particular rectangle is a square *
//*************************************************************************
}
//Add a circle class which includes area and circumference methods
class Circle extends TwoDShapeEncap{
double radius, diameter;
double area (double r){
radius = r;
return Math.PI * (radius * radius);
}
double perimeter (double r){
radius = r;
return 2 * (Math.PI * radius);
}
}
class TwoDShapeEncapDemoNew {
public static void main(String args[]) {
//Triangle
Triangle t = new Triangle();
t.setsideA(5.7);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA() );
System.out.println("The type is " + t.type);
System.out.println();
t.setsideB(7.3);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA() );
System.out.println("If sideB is " + t.getsideB() );
System.out.println("The type is " + t.type);
System.out.println();
t.setsideC(2.7);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA());
System.out.println("If sideB is " + t.getsideB());
System.out.println("If sideC is " + t.getsideC());
System.out.println("The type is " + t.type);
System.out.println();
//Rectangle
Rectangle r = new Rectangle();
r.setHeight(7.8);
r.setWidth(4.2);
System.out.println("The perimeter is " + r.perimeter());
System.out.println("The");
}
}
Error message:
Main.java:186: error: method perimeter in class Rectangle cannot be applied to given types; System.out.println("The perimeter is " + r.perimeter()); ^ required: double,double found: no arguments reason: actual and formal argument lists differ in length 1 error –
When you call:
System.out.println("The perimeter is " + r.perimeter());
in r.perimeter you must pass two parameters (as your signature wants)
Your method in Rectangle class:
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
So fix:
System.out.println("The perimeter is " + r.perimeter(r.getHeight(), r.getWidth()));
You also can fix your method perimeter without parameters because in the body you use getHeigth() and getWidth() properties
So you can write:
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
The method perimeter in the class Rectangle expects two parameters, and you're not passing any. You could either call it like this:
r.perimeter(7.8,4.2);
Or redefine the method so it looks like this:
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
Thats because you are defining the perimeter function like this:
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
and calling System.out.println("The perimeter is " + r.perimeter()); with no parameters.
Since you are not really using double h and double w for nothing, you just have to remove them from the method definition
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
Since everybody is just facing the problem with the parameters I will face this problem: Getters are used to get the values of private fields if you're "outside" your class! If you're in a method in your class you don't have to use the getters, you can just use the variables themselfs:
Example:
public class SomeClass {
private int a;
public void setA(int anotherA) {
a = anotherA;
}
public int getA() {
return a;
}
public int getSquareOfA() {
// You don't use getA() to get the value now
// but you use a itself!
return a*a; // instead of 'return getA() * getA();'
}
}
You do have that problem at several points in your code!
According to your problem:
Your problem was that you're calling a method which has 2 parameters without any input parameters!
You can either remove the parameters of the method (which will be the logically right thing to do in your case), OR you pass some parameters.
In your specific case that means, change your perimiter() method as follows:
double perimiter() {
return (height + width) * 2;
// or if you want to impress your teacher ;) :
// return (height + width) << 1
}
Also you should change that methodname to getPerimiter() to keep up with your own naming conventions!
Modify your signature to remove the arguments.
class Rectangle extends TwoDShapeEncap{
///...
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
should be
class Rectangle extends TwoDShapeEncap{
///...
double perimeter(){
//Notice that you don't need to pass in these arguments
//as this function gets these arguments by itself.
return getHeight() * 2 + getWidth() * 2;
}

Having 2 compiling errors in my java program . clarification please

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;
}

Categories

Resources