I’m using the Stanford version of Eclipse as I’m learning from one of their videos, and using their blank project template.
Basically, I’m writing a custom class “Fraction”. One of the constructors takes two numbers (doubles) and converts them to ints before storing them as numerator and denominator.
I’ve done
import java.lang.*;
public class Fraction {
And somewhere down I have
double numerator = Double(fractionComponents[0]); // fractionComponents is an array of string
double denominator = Double(fractionComponents[1]);
but I get the error:
The method Double(String) is undefined for the type Fraction.
I’ve also tried extending the class from Double.
You want to use the static
parseDoublemethod, like so:Note that you could also instantiate a
Double, but you should prefer to use the static method as it leaves the possibility for the implemention to cacheDoubleobjects, and it’s more clear to the reader what is going on – you’re parsing a string for a double value.If you were to use the constructor, it’s not immediately clear what the type of
fractionComponents[0]is because there’s both aDouble(String)andDouble(double)constructor.