Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class RationalFraction {
  4.     private int m; //numerator
  5.     private int n; //denominator
  6.  
  7.     public RationalFraction (int M, int N){
  8.         m = M;
  9.         n = N;
  10.     }
  11.     public int getM(){
  12.         return m;
  13.     }
  14.     public int getN(){
  15.         return n;
  16.     }
  17.     public int setM(int val){
  18.         m = val;
  19.     }
  20.     public int setN(int val){
  21.         n = val;
  22.     }
  23.     public double value(){
  24.         return (double)m/n;
  25.     }
  26.     public void write(){
  27.         System.out.println(m + '/' + n);
  28.     }
  29.     public static RationalFraction sum(RationalFraction a, RationalFraction b){
  30.         int numerator = a.getM() * b.getN() + a.getN() * b.getM();
  31.         int denominator = a.getN() * b.getN();
  32.         return new RationalFraction(numerator, denominator);
  33.     }
  34.     public RationalFraction simplifyFraction(RationalFraction num, RationalFraction den){
  35.         int t = nod(num, den);
  36.         num /= t;
  37.         den /= t;
  38.         return new RationalFraction(num, den);
  39.     }
  40.     private int nod(int a, int b){
  41.         while(a != b){
  42.             if(a > b)
  43.                 a -= b;
  44.             else
  45.                 b -= a;
  46.         }
  47.         return a;
  48.     }
  49. }
  50.  
  51.  
  52. //------------------------------------------------------------------------
  53. package com.company;
  54.  
  55. public class Main {
  56.  
  57.     public static void main(String[] args) {
  58.         //First task
  59.         RationalFraction fraction = new RationalFraction(1, 0);
  60.         fraction.setN(2);
  61.  
  62.         System.out.println("Input data Rational fraction^:");
  63.         fraction.write();
  64.  
  65.         System.out.println("Fraction after reduction:");
  66.         fraction.simplifyFraction();
  67.         fraction.write();
  68.  
  69.         System.out.println("This is value of the fraction:");
  70.         System.out.println(fraction.value());
  71.  
  72.         System.out.println("The result of the sum of two fractions:");
  73.         fraction.sum();
  74.         fraction.write();
  75.  
  76.         System.out.println("This is value of the new fraction:");
  77.         System.out.println(fraction.value());
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement