Re: Java Problem

  • From: Dave <davidct1209@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sun, 14 Nov 2010 23:20:10 -0800

A few notes in addition to everyone else's on the new class:
-  the numerator, denominator members should be private.
-  in your add method, you should use the accessers getNumerator/getDenominator
-  there's really no need to construct three new fraction objects (you
could just use two ints to hold the result num/dem's), or several if
you want to break up the expressions.  (optional).
-  stylistically, people usually distinguish their member variables by
naming them with some convention such as placing an underscore (_)
after the name or naming them "m_"...
-  not sure if this is for an assignment in which sighted folks will
review the code, but indentation is off (at least in my gmail view of
the message in IE).
-  Also, if for sighted consumption, wrap lines at 80 chars.
-  why are the member variables (num/dem) both int's?  Are you
building signed fractional expression?  How do you handle negative
denominators, for example?  Are those taken into account when you add?
-  some of the comments are not necessary and somewhat superfluous.
i.e. import java.util.Scanner; // Import Scanner class



On 11/14/10, black ares <matematicianu2003@xxxxxxxxxxx> wrote:
> don't forget the reduction stuff:)
> For example if you add
> 2/21 + 3/9
>
> ----- Original Message -----
> From: "Gilbert Neiva" <gneiva@xxxxxxx>
> To: <programmingblind@xxxxxxxxxxxxx>
> Sent: Monday, November 15, 2010 6:05 AM
> Subject: Re: Java Problem
>
>
>> Thanks everyone. I corrected the application with the suggestions you gave
>>
>> me. It works perfectly, as far as my first test goes. First the improved
>> Fraction class file.
>>
>> public class Fraction
>> {
>> int numerator; // Holds numerator
>> 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 int getNumerator() { return numerator; }
>> public int getDenominator() { return denominator; }
>>
>> // Returns printed formatted string
>> public String toString()
>> {
>> return numerator + "/" + denominator;
>> }
>>
>> // Adds two fractions together
>> public 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;
>> result.denominator = fraction_2.denominator;
>> return result;
>> }
>> } // End of class
>>
>> Now the source code for the driver class.
>>
>> import java.util.Scanner; // Import Scanner class
>>
>> // Class FractionDriver inputs two fractions and adds them together
>> public class FractionDriver
>> {
>> public static void main(String[] args)
>> {
>>  // Declare local data fields
>>  int numerator; // Holds numerator
>>  int denominator; // Holds denominator
>>  Scanner inData = new Scanner(System.in); // Scanner object
>> 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));
>> }
>> }
>>
>> My test is as follows.
>>
>> C:\Users\user\Desktop\COMP268\Practice\Fractions\src>java FractionDriver
>> Enter numerator for fraction 1:
>> 2
>> Enter denominator for fraction 1:
>> 5
>> Fraction 1 is 2/5.
>> Enter numerator for fraction 2:
>> 3
>> Enter denominator for fraction 2:
>> 7
>> Fraction 2 is 3/7.
>> 2/5 + 3/7 = 29/35
>> C:\Users\user\Desktop\COMP268\Practice\Fractions\src>
>>
>> I plan to put methods for subtracting fractions, multiplying, and dividing
>>
>> fractions in the Fraction class. I'll change the driver class accordingly.
>>
>> Gilbert Neiva
>>
>> ----- Original Message -----
>> From: "Jared Wright" <wright.jaredm@xxxxxxxxx>
>> To: <programmingblind@xxxxxxxxxxxxx>
>> Sent: Sunday, November 14, 2010 8:02 PM
>> Subject: Re: Java Problem
>>
>>
>>> 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
>>>
>>
>> __________
>> 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
>
>
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: