Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- public class RationalFraction {
- private int m; //numerator
- private int n; //denominator
- public RationalFraction (int M, int N){
- m = M;
- n = N;
- }
- public int getM(){
- return m;
- }
- public int getN(){
- return n;
- }
- public int setM(int val){
- m = val;
- }
- public int setN(int val){
- n = val;
- }
- public double value(){
- return (double)m/n;
- }
- public void write(){
- System.out.println(m + '/' + n);
- }
- public static RationalFraction sum(RationalFraction a, RationalFraction b){
- int numerator = a.getM() * b.getN() + a.getN() * b.getM();
- int denominator = a.getN() * b.getN();
- return new RationalFraction(numerator, denominator);
- }
- public RationalFraction simplifyFraction(RationalFraction num, RationalFraction den){
- int t = nod(num, den);
- num /= t;
- den /= t;
- return new RationalFraction(num, den);
- }
- private int nod(int a, int b){
- while(a != b){
- if(a > b)
- a -= b;
- else
- b -= a;
- }
- return a;
- }
- }
- //------------------------------------------------------------------------
- package com.company;
- public class Main {
- public static void main(String[] args) {
- //First task
- RationalFraction fraction = new RationalFraction(1, 0);
- fraction.setN(2);
- System.out.println("Input data Rational fraction^:");
- fraction.write();
- System.out.println("Fraction after reduction:");
- fraction.simplifyFraction();
- fraction.write();
- System.out.println("This is value of the fraction:");
- System.out.println(fraction.value());
- System.out.println("The result of the sum of two fractions:");
- fraction.sum();
- fraction.write();
- System.out.println("This is value of the new fraction:");
- System.out.println(fraction.value());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement