6 Replies - 1236 Views - Last Post: 16 July 2012 - 10:42 AM Rate Topic: -----

#1 malay723   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-July 12

Program not outputting correctly (Beginner Java)

Posted 15 July 2012 - 11:45 AM

I am taking a beginner Java course online, and with my current assignment I am having issues with my output. This driver is supposed to read in fractions and perform addition, multiplication, prints the fraction, and prints the double. The issue I'm having is that whenever I run the program it prints the sum as 1 no matter what, and the multiplication is always the sum times the first fraction. The instructor gave us an exact method for the driver program, which you can see below. My question is what am I doing wrong to make it print out this way? I really appreciate any help you can give.


Class code:
/****************************************************************************************
Fraction.java
Michelle Lay

This is the new Fraction class that helps the driver to perform addition, multiplication,
prints the fraction, and prints as a double.
****************************************************************************************/

import java.util.Scanner;

public class Fraction
{
	private int numerator; // numerator
	private int denominator; // denominator

	/********************/
	public Fraction()
	{
		this(1,1);
	}
	/********************/
	
	public Fraction (int numerator, int denominator)
	{
		this.numerator = numerator;
		this.denominator = denominator;
	}
	
	/**addition of the fractions**/
	public Fraction add(Fraction c)
	{
		int newNumerator = ((numerator * c.denominator) + (c.numerator * denominator));
		int newDenominator = (c.denominator * denominator);
		Fraction x = new Fraction (newNumerator, newDenominator);
	
		return x;
	}
	
	/**multiplication of the fractions**/
	
	public Fraction multiply(Fraction d)
	{
		this.numerator = (numerator * d.numerator);
		this.denominator = (denominator * d.denominator);
		Fraction x = new Fraction (this.numerator, this.denominator);
	
		return x;
	}
	
	/**printing the fraction**/
	
	public void print()
	{
		System.out.println(this.numerator + "/" + this.denominator);
	}

	/**print as double**/
	
	public void printAsDouble()
	{
		System.out.println((double) numerator / (double) denominator);
	}//end printAsDouble
	
}//end class



Driver code:
/************************************************************************
LayMichelleProg7.java
Michelle Lay

This driver demonstrates the fraction class.  It performs addition,
multiplication, prints as a fraction, and prints as a double.
*************************************************************************/

import java.util.Scanner;

public class LayMichelleProg7

{
	public static void main(String args[])

	{
		Scanner stdIn = new Scanner(System.in);
		Fraction c, d, x; // fraction objects

		System.out.println("Enter numerator; then denominator.");
		c = new Fraction (stdIn.nextInt(), stdIn.nextInt());
		c.print();

		System.out.println("Enter numerator; then denominator.");
		d = new Fraction (stdIn.nextInt(), stdIn.nextInt());
		d.print();

		x = new Fraction(); // create a fraction for number 0

		System.out.println("Sum:");
		x.add(c).add(d);
		x.print();
		x.printAsDouble();

		x = new Fraction(1,1); // create a fraction for number 1

		System.out.println("Product:");
		x.multiply(c).multiply(d);
		x.print();
		x.printAsDouble();

		System.out.println("Enter numerator; then denominator.");
		x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
		x.printAsDouble();
	} //end main
} //end class LayMichelleProg7



Is This A Good Question/Topic? 0
  • +

Replies To: Program not outputting correctly (Beginner Java)

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Program not outputting correctly (Beginner Java)

Posted 15 July 2012 - 12:42 PM

First of all the line x = new Fraction(); is not a zero fraction it is the fraction 1/1 or 1. So you are actually adding 3 fractions.

For instance, if I enter in 1/2 and 1/3 when prompted I am adding 1/1 + 1/2 + 1/3. Add these together and you are going to get 6/6 + 3/6 + 2/6 or 11/6.

Now in your add instead of creating a new fraction, return the CURRENT fraction after the add.

public Fraction add(Fraction c)
{
     // Manipulate the current object's numerator and denominator
     numerator = ((numerator * c.denominator) + (c.numerator * denominator));
     denominator = (c.denominator * denominator);
		
     // Return the current fraction "this"
     return this;
}



What this is doing is when you define "x" you are going to take the first fraction (eg 1/2) and manipulate x's value. Then you return "x". This object is then used in the next call to add() where we add 1/3. This makes it 3/2 (aka 1/1 + 1/2) and adds the 1/3 to x's member variables again.

The result of this is 11/6. Now of course this isn't the sum of 1/2 plus 1/3.

What your test will need to be is that you can't create a basic non value fraction. You should collect the numerator and denominator, create two separate fraction instances and add one to the other. In other words... c.add(d); and then print fraction "c"

Hope you get what I am saying here. :)

Edit: See revised example in my code below.

This post has been edited by Martyr2: 16 July 2012 - 10:03 AM

Was This Post Helpful? 1
  • +
  • -

#3 malay723   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-July 12

Re: Program not outputting correctly (Beginner Java)

Posted 15 July 2012 - 01:28 PM

Thank your for your help. I think I get what your saying, so now my addition problem is fixed. But the multiplication still remains. I've tried applying some of your ideas here and re-assessed my multiplication part of the code, but I still seem to be having some issue. It seems that my code is multiplying the first fraction by the sum. Any tips on correcting that? I've looked at the class code and the driver code and have changed both with the same effect. I've modified it like I did with the addition and changed
System.out.println("Product:");
x.multiply(c).multiply(d); 
x.print(); 
x.printAsDouble(); 


to
System.out.println("Product:"); 
(c).multiply(d); 
c.print(); 
c.printAsDouble(); 

and in the class code I've added and removed this and Fraction x to see if anything would change, but the results are the same no matter what.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Program not outputting correctly (Beginner Java)

Posted 16 July 2012 - 09:43 AM

Show us your multiply method now after you have fixed it. It is the same principle as the add. You manipulate the current numerator/denominator and return "this"

:)

This post has been edited by Martyr2: 16 July 2012 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Program not outputting correctly (Beginner Java)

Posted 16 July 2012 - 09:48 AM

View PostMartyr2, on 15 July 2012 - 02:42 PM, said:

Now in your add instead of creating a new fraction, return the CURRENT fraction after the add.

public Fraction add(Fraction c)
{
     // Manipulate the current object's numerator and denominator
     numerator = ((numerator * c.denominator) + (c.numerator * denominator));
     denominator = (c.denominator * denominator);
		
     // Return the current fraction "this"
     return this;
}




I may be misunderstanding here, but in this case, if I start with 1/3 and 1/4, and I add them, I end up with one of those fractions becoming 7/12, which is incorrect. If I add 2 to 3, 2 does not take the value of 5.

I would think you'd want to create a new Fraction, and leave the original ones as they are.
Was This Post Helpful? 1
  • +
  • -

#6 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Program not outputting correctly (Beginner Java)

Posted 16 July 2012 - 09:59 AM

I suppose you are right. I was thinking about easy chaining here without the need for creating new objects but right you wouldn't want the operands themselves to change value. But of course that also means that the tests would have to turn into assignment statements to store the resulting fraction.

maylay, you can change your add back to what you had before but keep the test to what I said before... c.add(d). In addition to that you will need to make it an assignment to X

Fraction x;

x = c.add(d);
x.print();



Again, multiply will be the same style.

:)

This post has been edited by Martyr2: 16 July 2012 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Program not outputting correctly (Beginner Java)

Posted 16 July 2012 - 10:42 AM

Quote

I was thinking about easy chaining here without the need for creating new objects but right you wouldn't want the operands themselves to change value


You can still do the chaining with the original version, I think.

oneTwelfth = oneThird.multiply(oneHalf).multiply(oneHalf); 


Should unroll correctly: oneThird.multiply(oneHalf) returns a new Fraction(1, 6), on which you call multiply(oneHalf). In fact, I think the chaining would break in interesting ways if you did the assignment:
oneHalf.multiply(oneThird).multiply(oneHalf);
would return, I think 1/36 (1/2 * 1/3 * 1/6)

This post has been edited by jon.kiparsky: 16 July 2012 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1