I'm extremely new to Java (started less than a week ago) and need some help for Computer Science A. I've tried various things such as searching up the errors i'm receiving but it has been to no avail.
public class Geometry
{
public static void main(String[] args)
{
GeometryHelper solver1 = new GeometryHelper(1.0, 2.0, 3.0, 4.0);
GeometryHelper solver2 = new GeometryHelper(3, 4);
System.out.println("Distance between (2,5) and (6,7): " distance);
System.out.println("Slope of a line with two points (2,5) and (6,7): " slope);
System.out.println("Hypotenuse of a right triangle with sides 3 and 4: " pythagoras);
}
}
public class GeometryHelper
{
public int x1;
public int x2;
public int y1;
public int y2;
public int a;
public int b;
public int c;
public double distance(double xl, double x2, double y1, double y2);
{
double x = Math.pow(x2 - x1, 2);
double y = Math.pow(y2 - y1, 2);
double dist = (Math.sqrt(x y));
return dist;
}
public double slope(double xl, double x2, double y1, double y2);
{
ansslope = (double)((y2-y1)/(x2-x1));
return ansslope;
}
public double pythagoras(double a, double b);
{
hypotenus = (Math.sqrt((a*a) (b*b)));
return hypotenus;
}
}
Errors:
/tmp/compilejava-fPdFIa/Geometry.java:7: error: constructor GeometryHelper in class GeometryHelper cannot be applied to given types;
GeometryHelper solver1 = new GeometryHelper(1.0, 2.0, 3.0, 4.0);
^
required: no arguments
found: double,double,double,double
reason: actual and formal argument lists differ in length
/tmp/compilejava-fPdFIa/Geometry.java:8: error: constructor GeometryHelper in class GeometryHelper cannot be applied to given types;
GeometryHelper solver2 = new GeometryHelper(3, 4);
^
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
/tmp/compilejava-fPdFIa/Geometry.java:9: error: cannot find symbol
System.out.println("Distance between (2,5) and (6,7): " distance);
^
symbol: variable distance
location: class Geometry
/tmp/compilejava-fPdFIa/Geometry.java:10: error: cannot find symbol
System.out.println("Slope of a line with two points (2,5) and (6,7): " slope);
^
symbol: variable slope
location: class Geometry
/tmp/compilejava-fPdFIa/Geometry.java:11: error: cannot find symbol
System.out.println("Hypotenuse of a right triangle with sides 3 and 4: " pythagoras);
^
symbol: variable pythagoras
location: class Geometry
/tmp/compilejava-fPdFIa/GeometryHelper.java:11: error: missing method body, or declare abstract
public double distance(double xl, double x2, double y1, double y2);
^
/tmp/compilejava-fPdFIa/GeometryHelper.java:16: error: return outside method
return dist;
^
/tmp/compilejava-fPdFIa/GeometryHelper.java:18: error: missing method body, or declare abstract
public double slope(double xl, double x2, double y1, double y2);
^
/tmp/compilejava-fPdFIa/GeometryHelper.java:20: error: cannot find symbol
ansslope = (double)((y2-y1)/(x2-x1));
^
symbol: variable ansslope
location: class GeometryHelper
/tmp/compilejava-fPdFIa/GeometryHelper.java:21: error: return outside method
return ansslope;
^
/tmp/compilejava-fPdFIa/GeometryHelper.java:23: error: missing method body, or declare abstract
public double pythagoras(double a, double b);
^
/tmp/compilejava-fPdFIa/GeometryHelper.java:25: error: cannot find symbol
hypotenus = (Math.sqrt((a*a) (b*b)));
^
symbol: variable hypotenus
location: class GeometryHelper
/tmp/compilejava-fPdFIa/GeometryHelper.java:26: error: return outside method
return hypotenus;
^
13 errors`
CodePudding user response:
constructor is missing in
GeometryHelper
class;
should not there in end of methodsif you want to use the methods of the classes, it should be like
this : solver.distance() , solver.slope() , etc
where GeometryHelper solver = new GeometryHelper();
I have debugged the code, hope below code helps you if there is doubt, you are free to ask
import java.util.*;
class GeometryHelper
{
public double distance(double x1, double x2, double y1, double y2)
{
double x = Math.pow(x2 - x1, 2);
double y = Math.pow(y2 - y1, 2);
double dist = (Math.sqrt(x y));
return dist;
}
public double slope(double x1, double x2, double y1, double y2)
{
double ansslope = (double)((y2-y1)/(x2-x1));
return ansslope;
}
public double pythagoras(double a, double b)
{
double hypotenus = (Math.sqrt((a*a) (b*b)));
return hypotenus;
}
}
public class Geometry
{
public static void main(String args[])
{
GeometryHelper solver = new GeometryHelper();
System.out.println("Distance between (2,5) and (6,7): " solver.distance(2,5,6,7));
System.out.println("Slope of a line with two points (2,5) and (6,7): " solver.slope(2,5,6,7));
System.out.println("Hypotenuse of a right triangle with sides 3 and 4: " solver.pythagoras(3,4));
}
}
CodePudding user response:
new GeometryHelper(1.0, 2.0, 3.0, 4.0);
Here you are trying to call a constructor which accepts 4 double
arguments.
But you do not have that constructor in GeometryHelper
class.
distance(double xl, double x2, double y1, double y2)
is a method not a constructor.`
To call this method, you will need an object of GeometryHelper
,
You can refer the below code.
public static void main(String[] args)
{
GeometryHelper solver1 = new GeometryHelper();
System.out.println("Distance between (2,5) and (6,7): " solver1.distance(1.0, 2.0, 3.0, 4.0));
System.out.println("Slope of a line with two points (2,5) and (6,7): " solver1.slope(2.0, 6.0, 5.0, 7.0));
System.out.println("Hypotenuse of a right triangle with sides 3 and 4: " solver1.pythagoras(3, 4));
}
CodePudding user response:
GeometryHelper would be better as a class with static methods that perform various computation. This way you won't need to create an object each time you have to compute the distance of two points.
Below is the code that you are probably looking for.
class GeometryHelper {
static double distance(double x1, double x2, double y1, double y2){
return (Math.sqrt(Math.pow(x2 - x1, 2) Math.pow(y2 - y1, 2)));
}
static double slope(double x1, double x2, double y1, double y2){
return(double)((y2-y1)/(x2-x1));
}
static double pythagoras(double a, double b){
return (Math.sqrt((a*a) (b*b)));
}
}
public class Main{
public static void main(String[] args){
System.out.println("Distance between (2,5) and (6,7): "
GeometryHelper.distance(1.0, 2.0, 3.0, 4.0));
System.out.println("Slope of a line with two points (2,5) and (6,7): "
GeometryHelper.slope(1.0, 2.0, 3.0, 4.0));
System.out.println("Hypotenuse of a right triangle with sides 3 and 4: "
GeometryHelper.pythagoras(3, 4));
}
}