Question
Please help me this JAVA problem: Given the code below, Please include a main method that tests all of your constructors and methods, including if
Please help me this JAVA problem:
Given the code below, "Please include a main method that tests all of your constructors and methods, including if the denominator == 0
And please correct the indentation.
//Code:
public class RationalNumber { public static void main(String[] args) { //Testings are placed here so that i can change the numbers easier RationalNumber number1 = new RationalNumber(1, 4); System.out.println(number1); RationalNumber number2 = new RationalNumber(3, 5); System.out.println(number2); System.out.println(number1.add(number2)); System.out.println(number1.subtract(number2)); System.out.println(number1.multiply(number2)); System.out.println(number1.divide(number2)); } private int numerator, denominator;
public RationalNumber() { this(0, 1); }
public RationalNumber(int n, int d) { //throw an IllegalArgumentException if (d == 0) throw new IllegalArgumentException(); if (d < 0) { n = n * -1; d = d * -1; } numerator = n; denominator = d; reduce(); } //get numerator public int getNumerator() { return numerator; } //get denominator public int getDenominator() { return denominator; } //Returns a String public String toString() { String result; if (numerator == 0) result = "0"; else if (denominator == 1) result = numerator + ""; else result = numerator + "/" + denominator; return result; }
public boolean equals(Object other) { if (this == other) return true; RationalNumber that = (RationalNumber) other; return numerator == that.numerator && denominator == that.denominator; }
public RationalNumber add(RationalNumber op2) { int commonDenominator = denominator * op2.getDenominator(); int numerator1 = numerator * op2.getDenominator(); int numerator2 = op2.getNumerator() * denominator; int sum = numerator1 + numerator2; return new RationalNumber(sum, commonDenominator); }
public RationalNumber subtract(RationalNumber op2) { int commonDenominator = denominator * op2.getDenominator(); int numerator1 = numerator * op2.getDenominator(); int numerator2 = op2.getNumerator() * denominator; int difference = numerator1 - numerator2; return new RationalNumber(difference, commonDenominator); }
public RationalNumber multiply(RationalNumber op2) { int numer = numerator * op2.getNumerator(); int denom = denominator * op2.getDenominator(); return new RationalNumber(numer, denom); }
public RationalNumber divide(RationalNumber op2) { return multiply(new RationalNumber(op2.denominator, op2.numerator)); }
private void reduce() { if (numerator != 0) { int common = gcd(Math.abs(numerator), denominator); numerator = numerator / common; denominator = denominator / common; } }
private int gcd(int num1, int num2) { while (num1 != num2) if (num1 > num2) num1 = num1 - num2; else num2 = num2 - num1; return num1; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started