Re: Java Problem

  • From: Jared Wright <wright.jaredm@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Mon, 15 Nov 2010 09:09:42 -0500

Hmm, yes, I do follow you and Sina. Good points.

On 11/15/2010 7:50 AM, black ares wrote:
it is no need for setters,
in my opinion a fraction class is a good candidate
for an imutable class.
So getters have not to be in payr with setters always.

Also the add operation is well done as a member of the class taking only one fraction as a parameter. so his work is very well done taking in account the recomendations from Sina. With a single notice, the addition with the oposite must be implemented in the class in a subtract method, not let out assuming that the user of the class will know that he can use add for subtraction.

----- Original Message ----- From: "Jared Wright" <wright.jaredm@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Monday, November 15, 2010 2:39 PM
Subject: Re: Java Problem


I'd personally anticipate add to accept two fractions as parameters and return their sum as a fraction. And, yes, since you've went to the trouble of implementing getters you might as well do setters as well and make the class's data members private.
On 11/14/2010 11:40 PM, Littlefield, Tyler wrote:
First, I don't know java, so I'll just give my input based off of what I do know of other languages. If you have a constructor that takes no values, you need setters otherwise that's useless? Second, why does add do whatever add does? you return a fraction and accept a fraction. If I call Add on a class that accepts another of the same class, I expect my class plus the new class to be returned or something--I'm not exactly sure what your getting at there. I suppose you could just set the values I commented about with their fields since they're public, but I've never liked exposing internal variables, guess that's just a matter of opinion though.
On 11/14/2010 9:05 PM, Gilbert Neiva wrote:
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


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: