Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: [Homework] Rational Class

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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).

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: [Homework] Rational Class

    Why does the Rational class need two parameters?

    If it only had 1, say
    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());
     
    }

Similar Threads

  1. MORE java homework help.
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: October 13th, 2009, 07:15 PM
  2. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM
  3. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM
  4. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  5. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM