Ok so I'm brand new to programming and Java so go easy on me. The assignment is to take fractions and do all the operations to them, add, subtract, multiply, and divide. We are also required to do this with a test file and then the actual file.
So far I have down:
making the fractions display correctly
flipping the negative sign to the numerator
putting the fraction in lowest terms

public class TestFraction {
	public static void main(String[] arg) 
	{
		//Given example of how Fraction.common works
		System.out.println("The greatest common divisor of 15 and 35 is "+Fraction.common(15,35));

		System.out.println("So 15/35 is really 3/7, dividing top and bottom by "+Fraction.common(15,35));
		
		//Test general to see if answer is displayed correctly
		Fraction f1 = new Fraction( 1, 3);

		if (f1.toString().equals("1/3"))
		{
			System.out.println("test f1.toString(): passed");
		}
		else
		{
			System.out.println("test f1.toString(): FAILED");
		}

		//Test to see if negative sign goes to numerator
		Fraction f2 = new Fraction( 2, -5);

		if (f2.toString().equals("-2/5"))
		{
			System.out.println("test f2.toString(): passed");
		}
		else
		{
			System.out.println("test f2.toString(): FAILED");
		}

		//Test to see if fraction gets put in lowest terms
		Fraction f3 = new Fraction( 2, 10);

		if (f3.toString().equals("1/5"))
		{
			System.out.println("test f3.toString(): passed");
		}
		else
		{
			System.out.println("test f3.toString(): FAILED");
		}


		//Multiply
		//Fraction a3 = f1.multiply(f2);
		//
		//if (a3.toString().equals("-2/15"))
		//{
		//	System.out.println("test a3.toString(): passed");
		//}
		//else
		//{
		//	System.out.println("test a3.toString(): FAILED");
		//}

	}
}

There is the test file

public class Fraction 
{
	//positive, zero or negative numerator and  positive denominator
	//do not use any other instance variables
	//greatest common divisor, use this to reduce a fraction to its lowest terms  

	private int top, bottom;  						        
	public static int common(int x, int y) 
	{                                      
		if ( y == 0 ) return x;
		else return common(y, x%y);
	}

	private int numerator, denominator;
	public Fraction(int numerator, int denominator)
	{
		this.numerator = numerator;
		if (denominator == 0)
			throw new RuntimeException("Attemt to create zero denominator Fraction");
		else if(denominator < 0)
			{
			this.numerator = (-1 * numerator);
			this.denominator = (-1 * denominator);
			}	
		else if (denominator >= 0)
			this.denominator = denominator;
		this.top = this.numerator;
		this.bottom = this.denominator;
	}

	public String toString()
	{		
		return (numerator / +Fraction.common(top, bottom)) + "/" + (denominator / +Fraction.common(top, bottom));
	}
}

There is the other file.
So I decided to start with multiply since that is the easiest, simply multiply numerator of fraction 1 by numerator fraction 2, same with denom. and put in lowest terms.
But I am confused on how to call my fractions f1 and f2 in the Fraction class.
Do I do??

public Fraction(int top, int bottom)

but how do I get the fractions I used in the testfraction file into the fraction file? If someone could just point me in the right direction or show me how to do multiply at least that would be greatly appreciated.

Thank you soo much

First, you would create the Fraction object using new Fraction(x, y); , with whatever values you need there instead of x and y. So, if you were multiplying 1/2 by 2/3, you would write:

Fraction first = new Fraction(1, 2);
Fraction second = new Fraction(2, 3);
Fraction third;

third = first.multiply(second);

Now, you note that you are using one of the Fraction objects to call the method. This is because, conceptually, the method is actually a message your sending the object telling it to do something according to a set of rules (hence the term 'method' instead of 'function'). When you write a (non-static) method, you don't need to explicitly name the object that it is called with; it has an implicit variable called 'this' which references the object. So, the signatures for Fraction.multiply() might look like:

public Fraction multiply(Fraction multiplicand) { ... }

public Fraction multiply(int multiplicand) { ... }

You'll note that I just defined two different methods with the same name, but different arguments - this is called overloading and it can make some things a lot easier, if you're careful not to abuse it.

But how do I then specifically call out the numerator and denominator of fraction first & second? For instance how will I say multiply numerator of fraction fist by numerator of fraction second.
I think it would be easier to separate them now since for adding and subtracting I will be changing them to get common denominators, correct?

Last one: then how do I make the answer go through the previous steps? ie. putting the negative sign on the numerator or will it automatically do that.
THanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.