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.