# [Homework] Rational Class

• January 13th, 2011, 07:44 PM
burger king
[Homework] Rational Class
Hello, So i am taking a Java class ( I regret it), and I have had alot of labs due, but since i have no clue what is going on in the class, it is really difficult for me to do the labs. I have done most with half-effort, but on this one i am just completly bewildered. I just need to know how to sart this lab off and stuff so that i can go ahead and do it. Once someone explains to me how to do part of it, i should understand the rest. If someone is kind enough to help me with this i would Be so so very hapy!

Thanks!

-------------------------------------------
Actual Lab

This lab assignment continues the Rational class that was started with the Lab08MATH02 assignment. Now comes the times to add, subtract, multiply and divide fractions with your nifty Rational class. This assignment will also be the first unit lab assignment. A unit assignment implies a more challenging assignment that carries greater weight for your average computation. A unit assignment involves bringing together a greater number of computer skills in one assignment. For instance, the last keyword assignment provided complete main methods, which handled all the GUI input and output. For this assignment you will need to write the Rational class and also handle most of the code in the main method.

// Lab08MATH03st.java
// The Rational Class Program II
// This is the student, starting version of the Lab08MATH03 assignment.
// There are 5 return methods in the Ration 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 Lab08MATH03st
{
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();
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

public Rational() { }

public Rational(int n, int d) { }

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() { }

public double getDecimal()
{
return 0.0;
}

public String getRational()
{
return "";
}

public String getOriginal()
{
return "";
}

public int getNum()
{
return 0;
}

public int getDen()
{
return 0;
}

public void multiply(Rational r1, Rational r2) { }

public void divide(Rational r1, Rational r2) { }

public void add(Rational r1, Rational r2) { }

public void subtract(Rational r1, Rational r2) { }

}

80 Point Version Specifics

This lab assignment starts by doing everything that was required for the Lab08A assignment. You need to write methods Rational, getNum, getDen, getDecimal, getRational, and getOriginal. You will also need to write methods multiply and divide. Your fractions do not need to be reduced.

80 Point Version Output 1

The execution output will not show the input dialog windows. They are identical to the windows that were used for the Lab08MATH02 assignment. Four separate input dialog windows are used, as is shown by the provided main method.

Enter these 4 numbers: 2, 5, 5, 7

Note that in the GUI output window above, 4 lines of output are shown. In the 80 and 90 point versions, only the first 2 lines are significant. The sum and difference will appear to be the same as the quotient.

80 Point Version Output 2

Enter these 4 numbers: 6, 10, 20, 35

90 Point Version Specifics

The 90 point version adds the reduce method. As with Lab08MATH02, you are provided with the getGCF method. Not only do you need to write the reduce method, but you also need to call it in the appropriate places so that the product and quotient will be displayed in lowest terms.

90 Point Version Output 1

Enter these 4 numbers: 2, 5, 5, 7

NOTE: As with the 80 point version, only the first 2 lines of output are significant.

90 Point Version Output 2

Enter these 4 numbers: 6, 10, 20, 35

100 Point Version Specifics

The 100-point version completes the Rational class with methods add and subtract. The sum and difference also need to be displayed in lowest terms.

100 Point Version Output 1

Enter these 4 numbers: 2, 5, 5, 7

NOTE: Now all 4 lines of output are significant.
ALSO: In the subtraction answer, the negative sign might show up in the numerator.
It also might show up in the denominator. Either way is fine.

100 Point Version Output 2

Enter these 4 numbers: 6, 10, 20, 35

Any help is appreciated!
• January 13th, 2011, 08:04 PM
javapenguin
Re: [Homework] Rational Class
1.) I've never heard of "temporary return statements".
2.) Please put code in [highlight=java] [/highlight]
3.) Is that class up there provided for you, or is that your work so far?
4.) Your statements where you read in a value and then take the Integer.parseInt() can be combined.
i.e.
valueOne = Integer.parseInt(The JOptionPane stuff);

A factor of number n is a value <= n and >-= 1, unless 0, which will have no common factors unless the other int is also 0,
such that n%factor =0.
Two numbers, n1 and n2, would a common factor if
n1%factor = 0 AND n2%factor = 0.
A factor is a greatest common factor if it is the largest factor that n1 and n2 both have in common(or have as factors).
• January 13th, 2011, 08:15 PM
javapenguin
Re: [Homework] Rational Class
Why does the Rational class need two parameters?

If it only had 1, say
Code java:

```public Rational(int num) { this.num = num; setNum(num); } I could   public int getNum() { return num; }   public void add(Rational r1, Rational r2) { setNum((r1.getNum() + r2.getNum());   }```