Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
HELP! Displaying Fractions
Author Message
Thanos

Posted: Mon Feb 23, 2004 1:48 am   Post subject: HELP! Displaying Fractions

Hi, I wrote a fraction calculator program and i need help in displaying whole numbers as fraction, but everything else works pretty good. For example, 5/1 + 5/1 would be 10, but i need it to display 10/1 instead of just 10, can someone help me with this please? here is this code.

class file:
 code: public class Fraction {                 private int numerator;         private int denominator;         //get/set methods         public void setNumerator(int num) {                 this.numerator = num;         }                 public int getNumerator() {                 return this.numerator;         }                 public void setDenominator(int den) {                 this.denominator = den;         }                 public int getDenominator() {                 return this.denominator;         } //constructors          public Fraction() {                 this.numerator = 4;                 this.denominator = 9;         }                 public Fraction(int num, int den) {                 this.numerator = num;                 this.denominator = den;         } //// static methods         public static Fraction add (Fraction fr1, Fraction fr2) {     int temp;     int rDen, rNum;         rDen = fr1.denominator * fr2.denominator;     rNum = (fr1.numerator*fr2.denominator ) + (fr2.numerator*fr1.denominator);         temp = getGCD (rDen, Math.abs(rNum));         rDen = rDen/temp;     rNum = rNum/temp;         return new Fraction(rNum, rDen);   }         public static Fraction subtract ( Fraction fr1, Fraction fr2) {         int temp;     int rDen, rNum;         rDen = fr1.denominator * fr2.denominator;     rNum = (fr1.numerator*fr2.denominator ) - (fr2.numerator*fr1.denominator);         temp = getGCD (rDen, Math.abs(rNum));         rDen = rDen/temp;     rNum = rNum/temp;         return new Fraction(rNum, rDen);   }         public static Fraction multiply (Fraction fr1, Fraction fr2) {     int rDen, rNum;     int gcd;         rDen = fr1.denominator * fr2.denominator;     rNum = fr1.numerator * fr2.numerator;         gcd = getGCD(rDen, rNum);         return ( new Fraction(rNum/gcd, rDen/gcd) );   }           public static Fraction divide (Fraction fr1, Fraction fr2) {     int rDen, rNum;     int gcd;         rDen = fr1.denominator * fr2.numerator;     rNum = fr1.numerator * fr2.denominator;         gcd = getGCD(rDen, rNum);         return ( new Fraction(rNum/gcd, rDen/gcd) );   }         public static int getGCD (int a, int b) {     if (b == 0)       return a;         return getGCD (b, a % b);   }   public boolean isValid() { return denominator!=0; }       public String toString() {     if (numerator == 0)       return "0";     else if (denominator == 0)       return "Invalid";     else if (denominator == 1)       return ""+numerator;     else       return (numerator + "/" + denominator);   } }

and here's the main program

 code: import javax.swing.*; public class FractionCalculator {     public static void main(String arg[]) {     Fraction fr1, fr2;     int option = 0;     boolean exit = false;         fr1 = getFraction(1);     fr2 = getFraction(2);     while (true) {       try {         option = Integer.parseInt(JOptionPane.showInputDialog("Enter Option:\n\nCurrent Fractions:\n" + fr1.toString() +"\n" + fr2.toString() + "\n\n1.add\n2.multiply\n3.subtract\n4.divide\n5.another fractions\n6.exit"));       } catch (NumberFormatException e) { JOptionPane.showMessageDialog (null,"Invalid input", "Error", 2);       }         switch (option) {           case 1: JOptionPane.showMessageDialog (null,Fraction.add(fr1,fr2), "Result", 1);           break;           case 2: JOptionPane.showMessageDialog (null,Fraction.multiply(fr1,fr2), "Result", 1);           break;           case 3: JOptionPane.showMessageDialog (null,Fraction.subtract(fr1,fr2), "Result", 1);           break;           case 4: JOptionPane.showMessageDialog (null,Fraction.divide(fr1,fr2), "Result", 1);           break;           case 5 : fr1 = getFraction(1);           fr2 = getFraction(2);           break;           case 6: exit = true;         }         if (exit)           break;       }     }     public static Fraction getFraction (int num) {     Fraction fr;     while (true) {       fr = new Fraction(getData("Enter numerator for fraction " + num), getData("Enter denominator for fraction " + num));       if (fr.isValid())         return fr;             JOptionPane.showMessageDialog(null, "Invalid Fraction (Denominator cannot be zero)", "Error", 2);     }   }     private static int getData(String msg) {     while (true) {       try {         int f1 = Integer.parseInt(JOptionPane.showInputDialog( msg ));         return f1;       } catch (NumberFormatException e) {         JOptionPane.showMessageDialog(null, "Invalid input. Must be a number!", "Error", 2);       }     }   } }

da_foz

Posted: Wed Mar 03, 2004 2:20 pm   Post subject: (No subject)

In your toString method take out the following 2 lines:
// else if (denominator == 1)
// return "" + numerator;

This will force everything to display with a denominator.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: