Advertisement
16112

Курсова Работа 2 - 4.5 Калкулатор за обикновени дроби

Mar 27th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class program {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(sc.nextLine());
  9.         int d = Integer.parseInt(sc.nextLine());
  10.  
  11.         fraction drob = new fraction(n, d);
  12.         System.out.println("Drob1 = " + drob);
  13.  
  14.         fraction drob2 = new fraction(7, 5);
  15.         System.out.println("Drob2 = " + drob2);
  16.  
  17.         fraction drob3 = drob.multiply(drob2);
  18.         System.out.println("Drob3 = " + drob3);
  19.  
  20.         fraction drob4 = drob.sum(drob3);
  21.         System.out.println("Drob4 = " + drob4);
  22.  
  23.         fraction drob5 = drob.substract(drob4);
  24.         System.out.println("Drob5 = " + drob5);
  25.     }
  26.  
  27. }
  28.  
  29. -----------------------------------------------------
  30.  
  31.  
  32. public class fraction {
  33.     private int numerator;
  34.     private int denominator;
  35.  
  36.     public fraction(int n, int d) {
  37.         this.numerator = n;
  38.         this.denominator = d;
  39.     }
  40.  
  41.     public String toString() {
  42.         String str = this.numerator + "/" + this.denominator;
  43.         return str;
  44.     }
  45.  
  46.     public fraction multiply(fraction other) {
  47.         int n = this.numerator * other.numerator;
  48.         int d = this.denominator * other.denominator;
  49.         fraction fr = new fraction(n, d);
  50.         return fr;
  51.     }
  52.  
  53.     public fraction sum(fraction other) {
  54.         int n = this.numerator * other.denominator + other.numerator * this.denominator;
  55.         int d = this.denominator + other.denominator;
  56.         fraction fr = new fraction(n, d);
  57.         return fr;
  58.     }
  59.  
  60.     public fraction substract(fraction other) {
  61.         int n = this.numerator * other.denominator - other.numerator * this.denominator;
  62.         int d = this.denominator + other.denominator;
  63.         fraction fr = new fraction(n, d);
  64.         return fr;
  65.     }
  66.  
  67.     public int gcd(int a, int b) {
  68.         for (int i = Math.min(a, b); i >= 1; i--) {
  69.             if (a % 1 == 0 && b % 1 == 0) {
  70.                 return i;
  71.             }
  72.         }
  73.         return 1;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement