Reputation: 43
I'm new to Java programming and having a hard time understanding the use of methods and how to use them in the code. I know this is really basic things and I'm trying, it's just hard to grasp at first. So tl;dr I don't quite understand this error or how to fix it.
public class TriangleInfo {
public static void main(String args[]) {
Triangle versuch = createTriangle();
}
public static createTriangle() {
double side1 = 90;
double side2 = 80;
double hypotenuse = getHypotenuse();
Triangle thisTriangle = new Triangle(side1, side2, hypotenuse);
return thisTriangle;
}
public static double getHypotenuse() {
return Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2));
}
}
The error I'm getting is:
The method createTriangle() is undefined for the type TriangleInfo
I also have this written in another file:
public class Triangle {
double side1;
double side2;
double hypotenuse;
// Konstrukturen
public Triangle(double sideOne, double sideTwo, double hypotenuse) {
this.sideOne = sideOne;
this.sideTwo = sideTwo;
this.hypotenuse = hypotenuse;
}
}
Could someone please help me understand this error and how to correct it? Thank you!
Upvotes: 4
Views: 6896
Reputation: 2134
Okay, first of all, looking at the code you've provided, the method createTriangle
does not have a return type specified, all you need to do here is refactor it like so:
public static Triangle createTriangle() { // Body of the method...
Then there's the matter of the getHypotenuse
method, since it has no reference to the values side1
or side2
, you need to either alter it such that the these variables are properties within the class, or you can update the method & the caller, like so:
Caller
double hypotenuse = getHypotenuse(side1, side2);
Method
public static double getHypotenuse(double side1, double side2) { // Body of the method...
Finally, in the Triangle
class, you have the property names stated as side
, but in the constructor of the Triangle
class, you try to assign this.sideOne
, it should either be side1
in the constructor, or you should change the name(s) of the class properties.
To be fair, I appreciate that you're a beginner & to be fair, you weren't too far from having a working implementation.
import java.lang.Math;
public class TriangleInfo {
public static void main(String args[]) {
Triangle versuch = createTriangle();
}
public static Triangle createTriangle() {
double side1 = 90;
double side2 = 80;
double hypotenuse = getHypotenuse(side1, side2);
Triangle thisTriangle = new Triangle(side1, side2, hypotenuse);
return thisTriangle;
}
public static double getHypotenuse(double side1, double side2) {
return Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2));
}
}
class Triangle {
double side1;
double side2;
double hypotenuse;
// Konstrukturen
public Triangle(double sideOne, double sideTwo, double hypotenuse) {
this.side1 = sideOne;
this.side2 = sideTwo;
this.hypotenuse = hypotenuse;
}
}
Upvotes: -1
Reputation: 2412
The error is that your method createTriangle()
doesn't have a return type. Since you are returning a Triangle
, you need to add that.
public static Triangle createTriangle() {
And continue with your normal code.
Also, a good catch from @JO3-W3B-D3V, the side1
and side2
are not globally accessible in the class, so you need to do:
public static double getHypotenuse(double side1, double side2) {
return Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2));
}
So, your complete createTriangle()
function becomes:
public static Triangle createTriangle(){
double side1 = 90;
double side2 = 80;
double hypotenuse = getHypotenuse(side1, side2);
Triangle thisTriangle = new Triangle(side1, side2, hypotenuse);
return thisTriangle;
}
Upvotes: 2