Re: Java problem

Correction, my program did not subtract the two methods as it was suppose to. 
So the program did not run fine. Sorry for the confussion.

Gilbert Neiva

  ----- Original Message ----- 
  From: Gilbert Neiva 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Monday, November 15, 2010 11:39 AM
  Subject: Java problem


  Ok, in my last post I had mensioned that I would add more methods. I added a 
subtract method that would subtract two fractions. I get an error when running 
the program with J2SDK. When I run the program with Eclipse, it runs fine, 
except for the error that it mensions after it has finished running. Here is my 
Fraction class.

  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; }

  // Method returns printed formatted string

  public String toString()

  {

  return numerator + "/" + denominator; 

  }

  // Method adds two fractions together

  public Fraction add(Fraction frac1)

  {

  Fraction fraction_1 = new Fraction();

  Fraction fraction_2 = new Fraction();

  Fraction result = new Fraction();

  Fraction printResult = 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 = denominator * frac1.denominator;

  printResult = new Fraction(result.numerator, result.denominator);

  return printResult;

  }

  // Method subtracts two fractions

  public Fraction subtract(Fraction frac1)

  {

  Fraction fraction_1 = new Fraction();

  Fraction fraction_2 = new Fraction();

  Fraction result = new Fraction();

  Fraction printResult = 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 = denominator * frac1.denominator;


  // If both numerator and denominator are negative

  if (result.numerator < 0 && result.denominator < 0)

  {

  printResult = new Fraction(Math.abs(result.numerator), 
Math.abs(result.denominator));

  return printResult;

  }

  if (result.numerator > 0 && result.denominator < 0)

  {

  printResult = new Fraction(result.numerator*-1, 
Math.abs(result.denominator)); 

  return printResult;

  }

  }

  } // End of class



  Here is my source code for the driver class.



  import java.util.Scanner; // Import Scanner class

  // Class FractionDriver inputs two fractions and adds and subtracts them

  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();

  Fraction answer = 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 + ".");

  answer = frac1.add(frac2);

  System.out.println(frac1 + " + " + frac2 + " = " +

  answer);

  answer = frac2.subtract(frac1);

  System.out.println(frac2 + " - " + frac1 + " = " +

  answer); 

  }

  }



  When I run the program with Eclipse, this is what I get.



  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

  Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

  This method must return a result of type Fraction


  at Fraction.subtract(Fraction.java:49)

  at FractionDriver.main(FractionDriver.java:33)



  When I run the program with J2SDK, I get the following.



  C:\Users\user\Desktop\COMP268\Practice\Fractions\src>javac *.java
  Fraction.java:74: missing return statement
  }
  ^
  1 error
  C:\Users\user\Desktop\COMP268\Practice\Fractions\src>



  What am I doing wrong?



  Gilbert Neiva


Other related posts: