Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TextLab05st.java
- // The Rational Class Program II
- // This is the student, starting version of the Text Lab 05 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.
- /* Tyler Beaver
- I changed things a little bit.
- Namely I changed the add/multiply/sub/divide
- methods to static methods returning
- new objects as that makes much more sense
- in a higher level language than the former
- assembly style operations.
- Also I added the built-in ascii fraction symbols.
- That's pretty cool I should get bonus points for
- that. srsly tho.
- */
- import java.util.Scanner;
- public class TextLab05st
- {
- static int num1, den1; // numerator and denominator of the 1st rational number
- static int num2, den2; // numerator and denominator of the 2nd rational number
- public static void main (String args[])
- {
- enterData();
- Rational r1 = new Rational(num1,den1);
- Rational r2 = new Rational(num2,den2);
- Rational r3 = null; // I chaned some stuff -
- // It makes more sense for operators to be static.
- // why do we not use the built in features, anyways?
- r3 = Rational.multiply(r1,r2);
- System.out.println("\n\n" + r1.getOriginal() + " * " + r2.getOriginal() + " = " + r3.getRational());
- r3 = Rational.divide(r1,r2);
- System.out.println("\n" + r1.getOriginal() + " / " + r2.getOriginal() + " = " + r3.getRational());
- r3 = Rational.add(r1,r2);
- System.out.println("\n" + r1.getOriginal() + " + " + r2.getOriginal() + " = " + r3.getRational());
- r3 = Rational.subtract(r1,r2);
- System.out.println("\n" + r1.getOriginal() + " - " + r2.getOriginal() + " = " + r3.getRational());
- System.out.println();
- }
- public static void enterData()
- {
- Scanner input = new Scanner(System.in);
- System.out.print("\nEnter the 1st numerator ----> ");
- num1 = input.nextInt();
- System.out.print("\nEnter the 1st denominator --> ");
- den1 = input.nextInt();
- System.out.print("\nEnter the 2nd numerator ----> ");
- num2 = input.nextInt();
- System.out.print("\nEnter the 2nd denominator --> ");
- den2 = input.nextInt();
- }
- }
- class Rational
- {
- protected int _num, _den;
- protected final int firstNum, firstDen;
- public Rational(int numerator, int denom)
- {
- firstNum = numerator;
- firstDen = denom;
- reduce();
- }
- public static Rational multiply(Rational r1, Rational r2)
- {
- return new Rational(r1._num * r2._num, r1._den * r2._den);
- }
- public static Rational divide(Rational r1, Rational r2)
- {
- return new Rational(r1._num * r2._den, r1._den * r2._num);
- }
- public static Rational add(Rational r1, Rational r2)
- {
- int n1=r1._num, n2=r2._num, d1 = r1._den, d2=r2._den;
- int n = n1 * d2 + n2 * d1, d = d1 * d2;
- return new Rational(n,d);
- }
- public static Rational subtract(Rational r1, Rational r2)
- {
- int n1=r1._num, n2=r2._num, d1 = r1._den, d2=r2._den;
- int n = n1 * d2 - n2 * d1, d = d1 * d2;
- return new Rational(n,d);
- }
- public int getNum() { return _num; }
- public int getDen() { return _den; }
- public double getDecimal() { return ((double)_num/(double)_den); }
- public static String format(int _num,int _den){
- String base = "";
- if(_num<0||_den<0){
- if(!(_num<0&&_den<0)){
- base+="-";
- }
- if(_num<0)
- _num*=-1;
- if(_den<0)
- _den*=-1;
- }
- if(_num==1){
- switch(_den){
- case 1:
- return base+"⅟";
- case 2:
- return base+"½";
- case 3:
- return base+"⅓";
- case 4:
- return base+"¼";
- case 5:
- return base+"⅕";
- case 6:
- return base+"⅙";
- case 8:
- return base+"⅛";
- }
- } else if(_num==2){
- switch(_den){
- case 3:
- return base+"⅔";
- case 5:
- return base+"⅖";
- }
- } else if(_num==3){
- switch(_den){
- case 4:
- return base+"¾";
- case 5:
- return base+"⅗";
- case 8:
- return base+"⅜";
- }
- } else if(_num==4&&_den==5) {
- return "⅘";
- } else if(_den==8){
- if(_num==5){
- return base+"⅝";
- } else if(_num==7){
- return base+"⅞";
- }
- }
- return base+ _num + "/" + _den;
- }
- public String getRational() {
- return format(_num,_den);
- }
- public String getOriginal() { return format(firstNum,firstDen); }
- protected void reduce()
- {
- int cden = getGCF(firstNum,firstDen);
- _num = firstNum / cden;
- _den = firstDen / cden;
- }
- public void displayData()
- {
- System.out.println();
- System.out.println(getOriginal() + " equals " + getDecimal());
- System.out.println();
- System.out.println("and reduces to " + getRational());
- System.out.println();
- }
- private int getGCF(int n1,int n2)
- {
- int rem = 0, gcf = 0;
- do
- {
- rem = n1 % n2;
- if (rem == 0)
- gcf = n2;
- else
- {
- n1 = n2;
- n2 = rem;
- }
- }
- while (rem != 0);
- return gcf;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement