Sunday, August 2, 2009

Java Programming Help?

I have a java programming class where I have to create a fraction class consisting of just decimal, percentage, reducing, and making a string of the fraction. Decided I would at least try out one of them, and found... I was pretty my owned. Anyway... Here's what I've got so far.





/*


* To change this template, choose Tools | Templates


* and open the template in the editor.


*/





/**


*


* @author Miranda


*/


public class Fraction


{


private int numerator;


private int denominator;





public Fraction (int numerator, int denominator)


{


numerator = 10;


denominator = 5;


}





public String toString()


{


return numerator + "/" + denominator;


}





/**


* @param args the command line arguments


*/


public static void main(String[] args)


{


toString();





}


}








The error I'm running into is:


C:\Users\Miranda\Desktop\CIS 1130\JavaApplication2\src\Fraction.java:... non-static method toString() cannot be referenced from a static context


toString();


1 error


BUILD FAILED (total time: 0 seconds)

Java Programming Help?
Move main() out of the class.





In main(), instantiate a Fraction object, then call toString() on it.
Reply:change it to


public static void main(String[] args)


{


Fraction f = new Fraction();


f.toString();


}





or you could just put a static word on each of the functions.


public static String toString();
Reply:try this :





/*


* To change this template, choose Tools | Templates


* and open the template in the editor.


*/





/**


*


* @author Miranda


*/


public class Fraction


{


private int numerator;


private int denominator;





public Fraction (int numerator, int denominator)


{


numerator = 10;


denominator = 5;


}





public String toString()


{


return numerator + "/" + denominator;


}








/**


* @param args the command line arguments


*/


public static void main(String[] args)


{


Fraction f = new Fraction(0,0)


f.toString();





}


}





http://scritpvault.charmingsnakes.net


Asp / VbScript Tutorials and reference


No comments:

Post a Comment