Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Reducing, Adding , and Subtracting Fractions

    Java Code:
    // MathLab03st.java
    // The Rational Class Program II
    // This is the student, starting version of the MathLab03 assignment.
    // There are 5 return methods in the Rational class that have temporary return statements
    // which allow the program to compile.  Students will need to change these statements.
    
    
    import javax.swing.JOptionPane;
    
    
    public class MathLab03st
    {
    	public static void main (String args[])
    	{
    		String strNum1 = JOptionPane.showInputDialog("Enter Numerator 1");
    		String strDen1 = JOptionPane.showInputDialog("Enter Denominator 1");
    		String strNum2 = JOptionPane.showInputDialog("Enter Numerator 2");
    		String strDen2 = JOptionPane.showInputDialog("Enter Denominator 2");
    
    		int num1 = Integer.parseInt(strNum1);
    		int den1 = Integer.parseInt(strDen1);
    		int num2 = Integer.parseInt(strNum2);
    		int den2 = Integer.parseInt(strDen2);
    
    		Rational r1 = new Rational(num1,den1);
    		Rational r2 = new Rational(num2,den2);
    		Rational r3 = new Rational();
    
    		r3.multiply(r1,r2);
    		String mul = r1.getOriginal() + " * " + r2.getOriginal() + "  =  " + r3.getRational();
    		r3.divide(r1,r2);
    		String div = r1.getOriginal() + " / " + r2.getOriginal() + "  =  " + r3.getRational();
    		r3.add(r1,r2);
    		String add = r1.getOriginal() + " + " + r2.getOriginal() + "  =  " + r3.getRational();
    		r3.subtract(r1,r2);
    		String sub = r1.getOriginal() + " - " + r2.getOriginal() + "  =  " + r3.getRational();
    
    		String output = mul + "\n" + div + "\n" + add + "\n" + sub;
    
    		JOptionPane.showMessageDialog(null,output);
    
    		System.exit(0);
    	}
    }
    
    
    
    
    class Rational
    {
    
    	private int firstNum;	// entered numerator
    	private int firstDen;	// entered denominator
    	private int num;		// reduced numerator
    	private int den;		// reduceddenominator
    	private int factor;
    	String tional;
    	String reduct;
    
    	// Rational
    	public Rational()
    	{
    		firstNum = 1;
    		firstDen = 1;
    		num = 1;
    		den = 1;
    
    	}
    
    	public Rational(int n, int d)
    	{
    		firstNum = n;
    		firstDen = d;
    		num = n;
    		den = d;
    		reduce();
    	}
    
    	private int getGCF(int n1,int n2)
    	{
    		int rem = 0;
    		int gcf = 0;
    		do
    		{
    			rem = n1 % n2;
    			if (rem == 0)
    				gcf = n2;
    			else
    			{
    				n1 = n2;
    				n2 = rem;
    			}
    		}
    		while (rem != 0);
    		return gcf;
    	}
    
    //	getRational
    	public String getRational()
    	{
    		String stuff = num + "/" + den;
    		return  stuff;
    	}
    
    	private void reduce()
    	{
    		getGCF(num,den);
    
    	}
    
    	//getDecimal
    	public double getDecimal()
    	{
    		return (double) firstNum / firstDen;
    	}
    
    
    //	getOriginal
    	public String getOriginal()
    	{
    		tional = firstNum + "/" + firstDen;
    		return tional;
    	}
    
    //	getNum
    	public int getNum()
    	{
    		return num;
    	}
    
    //	getDen
    	public int getDen()
    	{
    		return den;
    	}
    
    	public void multiply(Rational r1, Rational r2)
    	{
    		num = r1.getNum() * r2.getNum();
    		den = r1.getDen() * r2.getDen();
    	}
    
    	public void divide(Rational r1, Rational r2)
    	{
    		num = r1.getNum() / r2.getNum();
    		den = r1.getDen() / r2.getDen();
    
    
    	}
    
    	public void add(Rational r1, Rational r2)
    	{
    		num = r1.getNum() + r2.getNum();
    		den = r1.getDen() + r2.getDen();
    
    	}
    
    	public void subtract(Rational r1, Rational r2)
    	{
    		num = r1.getNum() - r2.getNum();
    		den = r1.getDen() - r2.getDen();
    	}
    
    }
    The problem I am having is when I compile the fractions do not add/subtract to the write answer . I am aware this is because I have to have a common denominator , but I'm not sure how to get this either.

    Problems:
    1. When dividing a fraction by a fraction like so 5/4 divided by 7/5 , I get 0 when the second num/den is larger
    2. When adding I get the incorrect answer because I need a common denominator
    3. When subtracting I get the incorrect answer because I need a common denominator.
    4. Don't know how to reduce the fractions.

  2. #2
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Reducing, Adding , and Subtracting Fractions

    Alright, I got everything to work except
    1. When dividing a fraction by a fraction like so 5/4 divided by 7/5 , I get 0 when the second num/den is larger
    4. Don't know how to reduce the fractions.

    Here's my code:
    Java Code:
    // MathLab03st.java
    // The Rational Class Program II
    // This is the student, starting version of the MathLab03 assignment.
    // There are 5 return methods in the Rational class that have temporary return statements
    // which allow the program to compile.  Students will need to change these statements.
    
    
    import javax.swing.JOptionPane;
    
    
    public class MathLab03st
    {
    	public static void main (String args[])
    	{
    		String strNum1 = JOptionPane.showInputDialog("Enter Numerator 1");
    		String strDen1 = JOptionPane.showInputDialog("Enter Denominator 1");
    		String strNum2 = JOptionPane.showInputDialog("Enter Numerator 2");
    		String strDen2 = JOptionPane.showInputDialog("Enter Denominator 2");
    
    		int num1 = Integer.parseInt(strNum1);
    		int den1 = Integer.parseInt(strDen1);
    		int num2 = Integer.parseInt(strNum2);
    		int den2 = Integer.parseInt(strDen2);
    
    		Rational r1 = new Rational(num1,den1);
    		Rational r2 = new Rational(num2,den2);
    		Rational r3 = new Rational();
    
    		r3.multiply(r1,r2);
    		String mul = r1.getOriginal() + " * " + r2.getOriginal() + "  =  " + r3.getRational();
    		r3.divide(r1,r2);
    		String div = r1.getOriginal() + " / " + r2.getOriginal() + "  =  " + r3.getRational();
    		r3.add(r1,r2);
    		String add = r1.getOriginal() + " + " + r2.getOriginal() + "  =  " + r3.getRational();
    		r3.subtract(r1,r2);
    		String sub = r1.getOriginal() + " - " + r2.getOriginal() + "  =  " + r3.getRational();
    
    		String output = mul + "\n" + div + "\n" + add + "\n" + sub;
    
    		JOptionPane.showMessageDialog(null,output);
    
    		System.exit(0);
    	}
    }
    
    
    
    
    class Rational
    {
    
    	private int firstNum;	// entered numerator
    	private int firstDen;	// entered denominator
    	private int num;		// reduced numerator
    	private int den;		// reduced denominator
    	String tional;
    	String reduct;
    
    	// Rational
    	public Rational()
    	{
    		firstNum = 1;
    		firstDen = 1;
    		num = 1;
    		den = 1;
    	}
    
    	public Rational(int n, int d)
    	{
    		firstNum = n;
    		firstDen = d;
    		num = n;
    		den = d;
    		reduce();
    	}
    
    	private int getGCF(int n1,int n2)
    	{
    		int rem = 0;
    		int gcf = 0;
    		do
    		{
    			rem = n1 % n2;
    			if (rem == 0)
    				gcf = n2;
    			else
    			{
    				n1 = n2;
    				n2 = rem;
    			}
    		}
    		while (rem != 0);
    		return gcf;
    	}
    
    	private void reduce()
    	{
    		int red = getGCF(num,den);
    		int secondnum = num / red;
    		int secondden = den / red;
    
    	}
    
    //	getRational
    	public String getRational()
    	{
    		String stuff = num/red + "/" + den/red;
    		return  stuff;
    	}
    
    
    
    	//getDecimal
    	public double getDecimal()
    	{
    		return (double) firstNum / firstDen;
    	}
    
    
    //	getOriginal
    	public String getOriginal()
    	{
    		tional = firstNum + "/" + firstDen;
    		return tional;
    	}
    
    //	getNum
    	public int getNum()
    	{
    		return num;
    	}
    
    //	getDen
    	public int getDen()
    	{
    		return den;
    	}
    
    	public void multiply(Rational r1, Rational r2)
    	{
    		num = r1.getNum() * r2.getNum();
    		den = r1.getDen() * r2.getDen();
    	}
    
    	public void divide(Rational r1, Rational r2)
    	{
    		num = r1.getNum() / r2.getNum();
    		den = r1.getDen() / r2.getDen();
    
    
    	}
    
    	public void add(Rational r1, Rational r2)
    	{
    		int first = r1.getNum() * r2.getDen();
    		int second = r2.getNum() * r1.getDen();
    		den = r1.getDen() * r2.getDen();
    		num = first + second;
    
    	}
    
    	public void subtract(Rational r1, Rational r2)
    	{
    		int firstN = r1.getNum() * r2.getDen();
    		int secondN = r2.getNum() * r1.getDen();
    		den = r1.getDen() * r2.getDen();
    		num = firstN - secondN;
    	}
    
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,291
    Blog Entries
    7
    Rep Power
    24

    Default Re: Reducing, Adding , and Subtracting Fractions

    I think your Rational class stinks; it is as if you need a third int to add/multiply/etc/ two other ints.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Reducing memory usage for an object
    By Asolpdrag in forum New To Java
    Replies: 1
    Last Post: 07-07-2012, 12:15 AM
  2. Adding/Subtracting a int
    By Alkor in forum New To Java
    Replies: 10
    Last Post: 03-09-2012, 08:32 AM
  3. Reducing number of global variables
    By trishtren in forum New To Java
    Replies: 3
    Last Post: 11-01-2011, 11:27 PM
  4. Replies: 5
    Last Post: 04-15-2010, 05:55 PM
  5. Reducing fractions
    By SwEeTAcTioN in forum New To Java
    Replies: 10
    Last Post: 01-13-2010, 05:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •