I can not understand my mistake in this code, it will compile if I write new Cosine(); but fails if I write new Cosine(x);
import java.lang.Object;
import java.lang.Math;
class Cosine {
double Cosine (double x) {
double result = Math.cos(Math.toRadians(x));
return result;
}
}
public class test {
public static void main (String[] args){
double x = 90;
new Cosine(x);
}
}
You haven’t given
Cosinea constructor that takes adouble. Try this:Although this raises the question why a simple static method couldn’t be used:
This doesn’t require the instantiation of a
Cosineobject for every computation.