Advertisement
Tyler_Elric

TextLab05st.java

Dec 4th, 2013
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. // TextLab05st.java
  2. // The Rational Class Program II
  3. // This is the student, starting version of the Text Lab 05 assignment.
  4. // There are 5 return methods in the Rational class that have temporary return statements
  5. // which allow the program to compile.  Students will need to change these statements.
  6.  
  7.  
  8. /* Tyler Beaver
  9.  
  10. I changed things a little bit.
  11. Namely I changed the add/multiply/sub/divide
  12. methods to static methods returning
  13. new objects as that makes much more sense
  14. in a higher level language than the former
  15. assembly style operations.
  16.  
  17. Also I added the built-in ascii fraction symbols.
  18. That's pretty cool I should get bonus points for
  19. that. srsly tho.
  20.  
  21. */
  22.  
  23. import java.util.Scanner;
  24.  
  25.  
  26. public class TextLab05st
  27. {
  28.     static int num1, den1;   // numerator and denominator of the 1st rational number
  29.     static int num2, den2;   // numerator and denominator of the 2nd rational number
  30.  
  31.     public static void main (String args[])
  32.     {
  33.         enterData();
  34.  
  35.         Rational r1 = new Rational(num1,den1);
  36.         Rational r2 = new Rational(num2,den2);
  37.         Rational r3 = null; // I chaned some stuff -
  38.                             // It makes more sense for operators to be static.
  39.                             // why do we not use the built in features, anyways?
  40.  
  41.         r3 = Rational.multiply(r1,r2);
  42.         System.out.println("\n\n" + r1.getOriginal() + " * " + r2.getOriginal() + "  =  " + r3.getRational());
  43.         r3 = Rational.divide(r1,r2);
  44.         System.out.println("\n" + r1.getOriginal() + " / " + r2.getOriginal() + "  =  " + r3.getRational());
  45.  
  46.         r3 = Rational.add(r1,r2);
  47.         System.out.println("\n" + r1.getOriginal() + " + " + r2.getOriginal() + "  =  " + r3.getRational());
  48.         r3 = Rational.subtract(r1,r2);
  49.         System.out.println("\n" + r1.getOriginal() + " - " + r2.getOriginal() + "  =  " + r3.getRational());
  50.         System.out.println();
  51.     }
  52.  
  53.     public static void enterData()
  54.     {
  55.         Scanner input = new Scanner(System.in);
  56.         System.out.print("\nEnter the 1st numerator ----> ");
  57.         num1 = input.nextInt();
  58.         System.out.print("\nEnter the 1st denominator --> ");
  59.         den1 = input.nextInt();
  60.         System.out.print("\nEnter the 2nd numerator ----> ");
  61.         num2 = input.nextInt();
  62.         System.out.print("\nEnter the 2nd denominator --> ");
  63.         den2 = input.nextInt();
  64.     }
  65. }
  66.  
  67. class Rational
  68. {
  69.   protected int _num, _den;
  70.   protected final int firstNum, firstDen;
  71.  
  72.     public Rational(int numerator, int denom)
  73.     {
  74.         firstNum = numerator;
  75.         firstDen = denom;
  76.         reduce();
  77.     }
  78.  
  79.     public static Rational multiply(Rational r1, Rational r2)
  80.     {
  81.         return new Rational(r1._num * r2._num, r1._den * r2._den);
  82.     }
  83.  
  84.     public static Rational divide(Rational r1, Rational r2)
  85.     {
  86.         return new Rational(r1._num * r2._den, r1._den * r2._num);
  87.     }
  88.  
  89.     public static Rational add(Rational r1, Rational r2)
  90.     {
  91.         int n1=r1._num, n2=r2._num, d1 = r1._den, d2=r2._den;
  92.         int n = n1 * d2 + n2 * d1, d = d1 * d2;
  93.         return new Rational(n,d);
  94.     }
  95.  
  96.     public static Rational subtract(Rational r1, Rational r2)
  97.     {
  98.         int n1=r1._num, n2=r2._num, d1 = r1._den, d2=r2._den;
  99.         int n = n1 * d2 - n2 * d1, d = d1 * d2;
  100.         return new Rational(n,d);
  101.     }
  102.  
  103.     public int getNum() { return _num; }
  104.  
  105.     public int getDen() { return _den; }
  106.  
  107.     public double getDecimal() { return ((double)_num/(double)_den); }
  108.  
  109.   public static String format(int _num,int _den){
  110.     String base = "";
  111.     if(_num<0||_den<0){
  112.       if(!(_num<0&&_den<0)){
  113.         base+="-";
  114.       }
  115.       if(_num<0)
  116.         _num*=-1;
  117.       if(_den<0)
  118.         _den*=-1;
  119.     }
  120.     if(_num==1){
  121.       switch(_den){
  122.         case 1:
  123.           return base+"⅟";
  124.         case 2:
  125.           return base+"½";
  126.         case 3:
  127.           return base+"⅓";
  128.         case 4:
  129.           return base+"¼";
  130.         case 5:
  131.           return base+"⅕";
  132.         case 6:
  133.           return base+"⅙";
  134.         case 8:
  135.           return base+"⅛";
  136.       }
  137.     } else if(_num==2){
  138.       switch(_den){
  139.         case 3:
  140.           return base+"⅔";
  141.         case 5:
  142.           return base+"⅖";
  143.       }
  144.     } else if(_num==3){
  145.       switch(_den){
  146.         case 4:
  147.           return base+"¾";
  148.         case 5:
  149.           return base+"⅗";
  150.         case 8:
  151.           return base+"⅜";
  152.       }
  153.     } else if(_num==4&&_den==5) {
  154.       return "⅘";
  155.     } else if(_den==8){
  156.       if(_num==5){
  157.           return base+"⅝";
  158.       } else if(_num==7){
  159.           return base+"⅞";
  160.       }
  161.     }
  162.     return base+ _num + "/" + _den;
  163.   }
  164.  
  165.     public String getRational() {
  166.     return format(_num,_den);
  167.   }
  168.  
  169.     public String getOriginal() { return format(firstNum,firstDen); }
  170.  
  171.     protected void reduce()
  172.     {
  173.         int cden = getGCF(firstNum,firstDen);
  174.         _num = firstNum / cden;
  175.         _den = firstDen / cden;
  176.     }
  177.  
  178.     public void displayData()
  179.     {
  180.         System.out.println();
  181.         System.out.println(getOriginal() + " equals " + getDecimal());
  182.         System.out.println();
  183.         System.out.println("and reduces to " + getRational());
  184.         System.out.println();
  185.  
  186.     }
  187.  
  188.     private int getGCF(int n1,int n2)
  189.     {
  190.         int rem = 0, gcf = 0;
  191.         do
  192.         {
  193.             rem = n1 % n2;
  194.             if (rem == 0)
  195.                 gcf = n2;
  196.             else
  197.             {
  198.                 n1 = n2;
  199.                 n2 = rem;
  200.             }
  201.         }
  202.         while (rem != 0);
  203.         return gcf;
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement