I am currently trying to create a Class that involves fractions. The concept is very simple: input the fractions and perform various tasks such as displaying the numerator/denominator, and add, subtract, multiply and divide the fractions via methods (or functions). My knowledge about creating methods and classes in java and programming in general is very limited. And was wondering if anyone could help me out.

Here's the code to the methods for my class Fraction:

//Fraction Project Methods

package Fraction_Project;

public class Fraction_Project_Methods {

//Instance variables

private int numerator1;

private int numerator2;

private int denominator1;

private int denominator2;

private int fraction1;

private int fraction2;

private int newNumerator;

private int newDenominator;

//Constructor method, the purpose of this is to initialize instance variables

public Fraction_Project_Methods(){

//Initialize numerator and denominator to zero

numerator1 = 0;

numerator2 = 0;

denominator1= 0;

denominator2 = 0;

fraction1 = 0;

fraction2 = 0;

newNumerator = 0;

newDenominator = 0;

}

public void setFraction(int num1, int denom1, int num2, int denom2){

//set a student's name

numerator1 = num1;

numerator2 = num2;

denominator1 = denom1;

denominator2 = denom2;

fraction1 = (numerator1/denominator1);

fraction2 = (numerator2/denominator2);

}

//Accessor method to obtain the numerator

public int returnNumerator(int i){

//Return the numerator of the fraction

if(i==1){

return numerator1;

}

else{

return numerator2;

}

}

//Accessor method to obtain the denominator

public int returnDenominator(int j){

//Return the denominator of the fraction

if(j==1)

return denominator1;

else

return denominator2;

}

//Methods: add, subtract, multiply, and divide

public int addFractions(){

newNumerator = (numerator1*denominator2)+(numerator2+denominator1 );

newDenominator = (denominator1*denominator2);

return newNumerator/newDenominator;

}

public int subFractions(){

newNumerator = (numerator1*denominator2) -(numerator2+denominator1);

newDenominator = (denominator1*denominator2);

return newNumerator/newDenominator;

}

public int divideFractions(){

newNumerator = (numerator1*denominator2);

newDenominator = (denominator1*numerator2);

return newNumerator/newDenominator;

}

public int multiplyFractions(){

newNumerator = (numerator1*numerator2);

newDenominator = (denominator1*denominator2);

return newNumerator/newDenominator;

}

public String toString(){

//Construct and return a string representation of a student

String str;

str = "Fraction one: " + fraction1 + "\n" +

"Fraction two: " + fraction2 + "\n";

return str;

}

}

CODE TO MAIN PROGRAM:

//Fraction Project Main Program

package Fraction_Project;

public class Fraction_Project_Main {

public static void main(String[] args) {

Fraction_Project_Methods f; //Create an object f

f = new Fraction_Project_Methods();

f.setFraction(1, 2, 3, 4);

System.out.println("The first fraction's numerator is: ");

f.returnNumerator(1);

System.out.println("The second fraction's numerator is: ");

f.returnNumerator(2);

System.out.println("The first fraction's denominator is: ");

f.returnDenominator(1);

System.out.println("The second fraction's denominator is: ");

f.returnDenominator(2);

System.out.println("These two fractions added together produces: ");

f.addFractions();

System.out.println("These two fractions subtracted from one another produces: ");

f.subFractions();

System.out.println("These two fractions multipled together produces: ");

f.multiplyFractions();

System.out.println("These two fractions divided produces: ");

f.divideFractions();

System.out.println("This method in string form is:");

f.toString();

}

}

This program currently runs error free, but the methods do not return anything.

Output:

The first fraction's numerator is:

The second fraction's numerator is:

The first fraction's denominator is:

The second fraction's denominator is:

These two fractions added together produces:

These two fractions subtracted from one another produces:

Thanks for your help!