IN addition, your error basically comes down to that your return statement is not a single expression as it needs to be. You need to: A. Typecast the numerator and denominator to string values rather than integers. B. Use the string concatonator (+) to combine the string representations of the numerator and denominator, along with their separating '/'.
But everything else Sina advised about this class applies. On 11/14/2010 9:50 PM, Sina Bahram wrote:
There exists a great deal of problems with your class. First of all, your instance variables are static, and that is wrong. You want them to simply be regular private variables. I would strongly suggest looking up what static means and understanding that, before proceeding. Secondly, you are breaking every single rule in the book by making getters that do not reflect the name of the private variable. It's good style sense, it's good java sense, and it's good common sense. When you have a variable named numerator, then it's getter should be called getNumerator. Thirdly, the getters need to be regular public methods , not static. Fourth, your toString method returns a double, not a String, so this is obviously incorrect. Fix this. After you make those changes, and fully understand them all, maybe you should resubmit the class for further evaluation. Good luck, and write back if you need help with the concepts. Take care, Sina ________________________________ From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Gilbert Neiva Sent: Sunday, November 14, 2010 9:02 PM To: programmingblind@xxxxxxxxxxxxx Subject: Java Problem I am trying to write a program in which it adds two fractions together. I made a fraction class which consists of the following: public class Fraction { static int numerator; // Holds numerator static int denominator; // Holds denominator // Constructor passes int values from num and denum to numerator, and denominator. public Fraction(int num, int denom) { numerator = num; denominator = denom; } // Default constructor sets data fields to 0 public Fraction() { numerator = 0; denominator = 0; } // Methods return values of numerator and denominator public static int getNum() { return numerator; } public static int getDenom() { return denominator; } // Returns printed formatted string public static String toString() { return numerator "/" denominator; } // Adds two fractions together public static Fraction add(Fraction frac1) { Fraction fraction_1 = new Fraction(); Fraction fraction_2 = new Fraction(); Fraction result = new Fraction(); fraction_1.denominator = denominator * frac1.denominator; fraction_1.numerator = numerator * frac1.denominator; fraction_2.denominator = frac1.denominator * denominator; fraction_2.numerator = frac1.numerator * denominator; result.numerator = fraction_1.numerator + fraction_2.numerator; return result; } } // End of class My driver class, the class that has the main method has the following. import java.util.Scanner; // Import Scanner class // Class FractionDriver inputs two fractions and adds them together public class FractionDriver { // Declare data fields static int numerator; // Holds numerator static int denominator; // Holds denominator static Scanner inData = new Scanner(System.in); // Scanner object public static void main(String[] args) { // Declare local data fields Fraction frac1 = new Fraction(); Fraction frac2 = new Fraction(); // Ask user for numerators and denominators. System.out.println("Enter numerator for fraction 1:"); numerator = inData.nextInt(); System.out.println("Enter denominator for fraction 1:"); denominator = inData.nextInt(); frac1 = new Fraction(numerator, denominator); System.out.println("Fraction 1 is " + frac1 + "."); System.out.println("Enter numerator for fraction 2:"); numerator = inData.nextInt(); System.out.println("Enter denominator for fraction 2:"); denominator = inData.nextInt(); frac2 = new Fraction(numerator, denominator); System.out.println("Fraction 2 is " + frac2 + "."); System.out.println(frac1 + " + " + frac2 + " = " + frac1.add(frac2)); } } When I try to compile it with J2SDK console, I get the following error. C:\Users\user\Desktop\COMP268\Practice\Fractions\src>javac *.java Fraction.java:28: ';' expected return numerator "/" denominator; ^ 1 error C:\Users\user\Desktop\COMP268\Practice\Fractions\src> The Fraction.java file is the class that defines a fraction object. I looked in the source code for that file, and I cannot find any lines of code that need a cemmi-colon. When I run the program with Eclipse, I get the following. Enter numerator for fraction 1: 3 Enter denominator for fraction 1: 5 Fraction 1 is Fraction@ca0b6. Enter numerator for fraction 2: 2 Enter denominator for fraction 2: 4 Fraction 2 is Fraction@10b30a7. Fraction@ca0b6 + Fraction@10b30a7 = Fraction@1a758cb Eclipse does warn me that there are errors in the application before running it. Can someone help me with this issue? What am I doing wrong? Gilbert Neiva __________ View the list's information and change your settings at //www.freelists.org/list/programmingblind
__________View the list's information and change your settings at //www.freelists.org/list/programmingblind