Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Exam { public static void main(String[] args) { System.out.println(

ID: 3731796 • Letter: P

Question

public class Exam {

   public static void main(String[] args) {

       System.out.println(args.length); //1.

       System.out.printf("*%-6s%6s* ",200,3.0); //2.

      

       //Susin g the attached Rational Numebr Class (fractions)

       RationalNumber dog = new RationalNumber();

       RationalNumber cat = new RationalNumber(2,8);

       RationalNumber pet = new RationalNumber(1,4);

      

       System.out.println(dog); //3.

       System.out.println(cat); //4.

      

       System.out.println(dog.getNumerator()); //5.

       System.out.println(dog.getDenominator()); //6.

      

       System.out.println(cat.equals(pet)); //7.

       System.out.println(cat == pet); //8.

      

       dog=cat.add(pet); //9. write an add method

       //this will add two fractions, for example (1/3) +(1/8) yields (11/24)

      

      

      

      

      

       dog.reduce(); //10. write a reduce fraction method

       //this must reduce your fraction by the greatest comon divisor,

       //for example (11/24) would not change, but (12/24) should become (1/2)

      

      

      

      

      

   }

   // This class can be used to stroe rational numbers: numbers that can be

   // expressed as a/b where a and b are integers and b is not 0.

   // Building Java Programs, Chapter 8, Project #1

   // by Stuart Regers and Marty Stepp

  

   public class RationalNumber {

       private int numerator;

       private int denominator;

      

       //constructs 0

       public RationalNumber() {

           this (0,1);

       }

       public RationalNumber (int numerator, int denominator) {

           if (denominator == 0) {

               throw new IllegalArgumentException();

           }

           this.numerator = numerator;

           this.denominator = denominator;

       }

       public int getNumerator() {

           return numerator;

       }

       public int getDenominator() {

           return denominator;

       }

      

       //returns a string representation of the object

       public String toString() {

           if (denominator == 1) {

               return "" + numerator;

           } else {

               return numerator + "/" + denominator;

           }

       }

   }

}

Explanation / Answer

RationalNumber.java

//This class can be used to stroe rational numbers: numbers that can be

// expressed as a/b where a and b are integers and b is not 0.

// Building Java Programs, Chapter 8, Project #1

// by Stuart Regers and Marty Stepp

public class RationalNumber {

private int numerator;

private int denominator;

  

//constructs 0

public RationalNumber() {

this (0,1);

}

public RationalNumber (int numerator, int denominator) {

if (denominator == 0) {

throw new IllegalArgumentException();

}

this.numerator = numerator;

this.denominator = denominator;

}

public int getNumerator() {

return numerator;

}

public int getDenominator() {

return denominator;

}

  

//returns a string representation of the object

public String toString() {

if (denominator == 1) {

return "" + numerator;

} else {

return numerator + "/" + denominator;

}

  

}

public boolean equals(RationalNumber other) {

if (denominator != other.denominator)

return false;

if (numerator != other.numerator)

return false;

return true;

}

  

// -----------------------------------------------------------------

// Adds this rational number to the one passed as a parameter.

// A common denominator is found by multiplying the individual

// denominators.

// -----------------------------------------------------------------

public RationalNumber add(RationalNumber op) {

int num1 = getNumerator();

int denom1 =getDenominator();

int num2 = op.getNumerator();

int denom2 = op.getDenominator();

int num3 = (num1 * denom2) + (num2 * denom1);

int denom3 = denom1 * denom2;

RationalNumber r1 = new RationalNumber(num3, denom3);

return r1;

}

// -----------------------------------------------------------------

// Reduces this rational number by dividing both the numerator

// and the denominator by their greatest common divisor.

// -----------------------------------------------------------------

public void reduce() {

if (numerator != 0) {

int common = gcd(Math.abs(numerator), denominator);

numerator = numerator / common;

denominator = denominator / common;

}

}

// -----------------------------------------------------------------

// Computes and returns the greatest common divisor of the two

// positive parameters. Uses Euclid's algorithm.

// -----------------------------------------------------------------

private int gcd(int num1, int num2) {

// % is modulus which is the remainder of a division

// base case

if ((num1 % num2) == 0) {

return num2;

}

// recursive case

else {

return gcd(num2, num1 % num2);

}

}

}

_______________________

Exam.java

public class Exam {

public static void main(String[] args) {

System.out.println(args.length); //1. 0

System.out.printf("*%-6s%6s* ",200,3.0); //2. *200 3.0*

  

//Susin g the attached Rational Numebr Class (fractions)

RationalNumber dog = new RationalNumber();

RationalNumber cat = new RationalNumber(2,8);

RationalNumber pet = new RationalNumber(1,4);

  

System.out.println(dog); //3. 0

System.out.println(cat); //4. 2/8

  

System.out.println(dog.getNumerator()); //5. 0

System.out.println(dog.getDenominator()); //6. 1

  

System.out.println(cat.equals(pet)); //7. false

System.out.println(cat == pet); //8. false

  

dog=cat.add(pet); //9. write an add method 16/32   

//this will add two fractions, for example (1/3) +(1/8) yields (11/24)

System.out.println(dog);

dog.reduce(); //10. write a reduce fraction method 1/2

//this must reduce your fraction by the greatest comon divisor,

//for example (11/24) would not change, but (12/24) should become (1/2)

System.out.println(dog);

}
}

___________________

Output:

0
*200 3.0*
0
2/8
0
1
false
false
16/32
1/2

_______________Thank You