Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class frac{
- private int num, den;
- public frac(){
- this.num = 0;
- this.den = 1;
- }
- public frac(int num){
- this.num = num;
- this.den = 1;
- }
- public frac(int num, int den){
- this.num = num;
- if (den == 0){
- System.out.println("Error: Zero can not be a denominator");
- }
- this.den = den;
- }
- private int gcd(int a, int b){
- while (b != 0){
- int t = b;
- b = a % b;
- a = t;
- }
- return a;
- }
- private void reduction(){
- int nod = gcd(num, den);
- num /= nod;
- den /= nod;
- }
- public void plus(frac other){
- this.num = this.num * other.den + other.num * this.den;
- this.den *= other.den;
- reduction();
- }
- public void minus(frac other){
- this.num = this.num * other.den - other.num * this.den;
- this.den *= other.den;
- reduction();
- }
- public void mult(frac other){
- this.num *= other.num;
- this.den *= other.den;
- reduction();
- }
- public void div(frac other){
- frac other1 = new frac(other.den, other.num);
- mult(other1);
- reduction();
- }
- public String vyvod(){
- return num + "/" + den;
- }
- }
Add Comment
Please, Sign In to add comment