H0_0H

Untitled

Jan 29th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class frac{
  4.     private int num, den;
  5.  
  6.     public frac(){
  7.         this.num = 0;
  8.         this.den = 1;
  9.     }
  10.  
  11.     public frac(int num){
  12.         this.num = num;
  13.         this.den = 1;
  14.     }
  15.  
  16.     public frac(int num, int den){
  17.         this.num = num;
  18.         if (den == 0){
  19.             System.out.println("Error: Zero can not be a denominator");
  20.         }
  21.         this.den = den;
  22.     }
  23.  
  24.     private int gcd(int a, int b){
  25.         while (b != 0){
  26.             int t = b;
  27.             b = a % b;
  28.             a = t;
  29.         }
  30.         return a;
  31.     }
  32.  
  33.     private void reduction(){
  34.         int nod = gcd(num, den);
  35.         num /= nod;
  36.         den /= nod;
  37.     }
  38.  
  39.     public void plus(frac other){
  40.         this.num = this.num * other.den + other.num * this.den;
  41.         this.den *= other.den;
  42.         reduction();
  43.     }
  44.  
  45.     public void minus(frac other){
  46.         this.num = this.num * other.den - other.num * this.den;
  47.         this.den *= other.den;
  48.         reduction();
  49.     }
  50.  
  51.     public void mult(frac other){
  52.         this.num *= other.num;
  53.         this.den *= other.den;
  54.         reduction();
  55.     }
  56.  
  57.     public void div(frac other){
  58.         frac other1 = new frac(other.den, other.num);
  59.         mult(other1);
  60.         reduction();
  61.     }
  62.  
  63.     public String vyvod(){
  64.         return num + "/" + den;
  65.     }
  66.  
  67. }
  68.  
  69.  
Add Comment
Please, Sign In to add comment