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 2 of 2

Thread: The Rational Class Program [HOMEWORK HELP]

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

    Default The Rational Class Program [HOMEWORK HELP]

    This is for my AP Computer Science class that I am struggling with. Please help me.

    Assignment Purpose:

    The primary purpose of this lab is to demonstrate knowledge of creating a class with object methods, instantiate multiple objects of the created class, and then call the object methods from the main program method.

    Write a program with a Rational class. The purpose of the Rational class is to manipulate rational number operations. A rational number is a number that can be expressed in the form A / B where A and B are both whole numbers (no fractions or decimals) and B  0.

    This program will utilize GUI input and output windows. For this assignment you are not expected to create this GUI code yourself. Your main concern is to create and use the Rational class. The Rational class is quite involved and will be developed over two separate assignments. This first assignment will just get the ball rolling.

    The main method is provided for you and needs to be used as shown. You are also provided with a getGCF method of the Rational class which will return the Greatest Common Factor of 2 integers. You will find this useful in writing other methods of the Rational class. Your mission is to complete the Rational class that is used by the Lab08MATH02java program.


    Here is the Code that they give you;
    // MathLab02st.java
    // The Rational Class Program I
    // This is the student, starting version of the MathLab02 assignment.
     
     
    import javax.swing.JOptionPane;
     
     
    public class Lab08MATH02st
    {
    	public static void main (String args[])
    	{   
    		String strNbr1 = JOptionPane.showInputDialog("Enter Numerator "); 
    		String strNbr2 = JOptionPane.showInputDialog("Enter Denominator ");
     
    		int num = Integer.parseInt(strNbr1);
    		int den = Integer.parseInt(strNbr2);
    		Rational r = new Rational(num,den);
     
    		JOptionPane.showMessageDialog(null,r.getNum()+"/"+r.getDen()+" equals "+r.getDecimal());
     
    		System.exit(0);
    	}
    }
     
     
    class Rational
    {
     
    //	Rational
     
    //	getNum
     
    //	getDen
     
    //	getDecimal
     
    //	getRational 
     
    //	getOriginal
     
    //	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;
    	} 
    }


    80 Point Version Specifics

    Your Rational class needs to declare two data attributes: num for numerator and den for denominator. Only one constructor is required, which uses two parameters entered at the keyboard. The first parameter is the numerator and the second parameter is the denominator. The Rational class requires three additional methods, which are getNum, getDen and getDecimal. Method getNum returns the integer numerator, getDen returns the integer denominator and the getDecimal method returns a real number decimal value of the fraction. For example, if the numerator is 3 and the denominator is 4, getDecimal will return 0.75






    80 (and 90) Point Version Output 1

    The GUI windows appear one after the other. They do not all show up simultaneously as shown below. The windows will display on top of the current desktop and they are smaller than the windows shown for this sample execution.










    80 (and 90) Point Version Output 2












    90 Point Version Specifics

    The 90-point version adds the getRational method. This method returns a String representation of the fraction. For example, if the numerator is 3 and the denominator is 4, getRational will return 3/4




    Concatenation Hint:

    You probably know that String variables/values can be concatenated together.

    Example: "John" + "Smith" = "JohnSmith"

    What you may not know is that other data types can be concatenated with Strings as well.

    Example: "John" + 19 = "John19"

    This shows an int being concatenated to the end of a String.




    Even though the output of the 90 point version is identical to the 80 point version (see previous page), the showMessageDialog statement will need to be changed in the main method for the 90 point version to work properly. (See below.) Now a single call to getRational replaces the 2 calls to methods getNum and getDen.


    90 Point Version

    public static void main (String args[])
    	{   
    		String strNbr1 = JOptionPane.showInputDialog("Enter Numerator "); 
    		String strNbr2 = JOptionPane.showInputDialog("Enter Denominator ");
     
    		int num = Integer.parseInt(strNbr1);
    		int den = Integer.parseInt(strNbr2);
     
    		Rational r = new Rational(num,den);
     
    		JOptionPane.showMessageDialog(null,r.getRational() + " equals " + r.getDecimal());
     
    		System.exit(0);
    	}


    100 Point Version Specifics

    The 100-point version adds the getOriginal and reduce methods as well as firstNum and firstDen variable attributes. The constructor also needs to be changed. This version of the lab assignment reduces the fraction, if possible. The output displays something like 15/20 reduces to 3/4. Without additional variables, the original values of the numerator and denominator will be lost. You need to achieve the following sequence. The Rational constructor initializes all variables and then reduces the fraction. The reduce method needs getGCF to insure maximum reduction.

    As with the 90 point version, the showMessageDialog statement will need to be changed in the main method for this program to work properly. (See below.)


    public static void main (String args[])
    	{   
    		String strNbr1 = JOptionPane.showInputDialog("Enter Numerator 1"); 
    		String strNbr2 = JOptionPane.showInputDialog("Enter Denominator 2");
    		int num = Integer.parseInt(strNbr1);
    		int den = Integer.parseInt(strNbr2);
    		Rational r = new Rational(num,den);
     
    		JOptionPane.showMessageDialog(null,r.getOriginal() + " equals " + 
    										r.getDecimal() + "\n and reduces to " + r.getRational());
     
    		System.exit(0);
    	}

    Attachment 797 Here si the original Word Doc that describes the lab.

    Here I the Java file I am provided with.Attachment 798

    If you can help me figure this out I would be most appreciative.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: The Rational Class Program [HOMEWORK HELP]

    Please do not multi-post the same question - your other post has been removed.

    And I recommend reading the getting help link in my signature, and the following:
    http://www.javaprogrammingforums.com...e-posting.html
    Your post is very long, and with the number of posts here many (including myself) don't have the time to immerse themselves with it.

Similar Threads

  1. Replies: 7
    Last Post: July 12th, 2013, 01:43 AM
  2. [SOLVED] Could someone help me find my errors on this program?? (homework)
    By r19ecua in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2011, 10:25 PM
  3. [Homework] Rational Class
    By burger king in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2011, 09:15 PM
  4. need help with homework program...thanks
    By robertsbd in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 5th, 2010, 03:12 PM