Question
Hi, I need help creating this program for Java, if anyone could help, That would be appreciated. In this assignment, you will write a program
Hi, I need help creating this program for Java, if anyone could help, That would be appreciated.
In this assignment, you will write a program to perform JUnit testing on the RationalNum class in assignment 2. An examples of JUnit test has been discussed in class and it is also available on Blackboard. As demonstrated in class, Eclipse can be used for JUnit test.
You need to write the TestRationalNum class which includes methods to test all the constructors and methods in your RationalNum class. Make sure you also test all the different cases.
The TestRationalNum class should extend the TestCase class that is imported from junit.framework.TestCase
For most of the test methods, you can compare the actual result with the expected result, or to test if a condition is true or false. But for the case that the denominator is zero, you will need to include try/catch exception handling code in your test method. If an exception is thrown, it means that your RationalNum class did not handle the zero denominator case properly.
You may include the setUp method in your TestRationalNum class. The setUp method will be called before EACH test method is executed. Therefore you should only put the necessary code in the setUp method.
This is the code to be tested:
public class RationalNum { private int numerator; //numerator of rational number private int denominator; //denominator of rational number /** * This method creates a rational number with * no parameters. */ public RationalNum() { numerator = 0; denominator = 1; } /** * This method creates a rational number * using only a given numerator. * @param numerator */ public RationalNum(int numerator) { this.numerator = numerator; this.denominator = 1; } /** * This method creates a rational number * using a given numeratot and denominator * @param numerator * @param denominator */ public RationalNum(int numerator, int denominator) { if(denominator > 0) //checks if denominator is larger than 0 { this.numerator = numerator; this.denominator = denominator; } else if(denominator < 0) //checks if denominator is negative { numerator = -1*numerator; denominator = -1*denominator; this.numerator = numerator; this.denominator = denominator; } else if(denominator == 0) //checks if denominator is 0 { System.out.println("Zero can not be in the denominator"); System.exit(0); } } /** * This method converts the rational number to * a string. */ @Override public String toString() { return (this.numerator+"/"+this.denominator); } /** * This method compares two rational numbers and checks * if they are equal. * @param obj * @return true It returns true if they are equal. * @return false It returns false if they are not equal. */ @Override public boolean equals(Object obj) { RationalNum rationalNum = (RationalNum)obj; int numerator1 = this.numerator; int denominator1 = this.denominator; int numerator2 = rationalNum.numerator; int denominator2 = rationalNum.denominator; if (numerator1 == numerator2 && denominator1 == denominator2) { return true; } else { return false; } } /** * This method calls the numerator * of said rational number. * @return the numerator */ public int getNumerator() { return numerator; } /** * This method calls the denominator * of said rational number. * @return the denominator */ public int getDenominator() { return denominator; } /** * This method adds two rational numbers * @param num * @return This returns the added numbers * based on the formula */ public RationalNum add(RationalNum num) { RationalNum reNum = new RationalNum(); int numerator1 = this.numerator; int denominator1 = this.denominator; int numerator2 = num.numerator; int denominator2 = num.denominator; reNum.numerator = (numerator1*denominator2+numerator2*denominator1); reNum.denominator = denominator1+denominator2; return reNum; } /** * This method subtracts two rational numbers * @param num * @return This returns the subtracted * numbers based on the formula */ public RationalNum sub(RationalNum num) { RationalNum reNum = new RationalNum(); int numerator1 = this.numerator; int denominator1 = this.denominator; int numerator2 = num.numerator; int denominator2 = num.denominator; reNum.numerator = (numerator1*denominator2-numerator2*denominator1); reNum.denominator = denominator1*denominator2; return reNum; } /** * This method multiplies two rational numbers * @param num * @return This returns the multiplied numbers * based on the formula */ public RationalNum mul(RationalNum num) { RationalNum reNum = new RationalNum(); int numerator1 = this.numerator; int denominator1 = this.denominator; int numerator2 = num.numerator; int denominator2 = num.denominator; reNum.numerator = numerator1*numerator2; reNum.denominator = denominator1*denominator2; return reNum; } /** * This method divides two rational numbers * @param num * @return This returns the divided numbers * based on the formula */ public RationalNum div(RationalNum num) { RationalNum reNum = new RationalNum(); int numerator1 = this.numerator; int denominator1 = this.denominator; int numerator2 = num.numerator; int denominator2 = num.denominator; reNum.numerator = numerator1*denominator2; reNum.denominator = denominator1*numerator2; return reNum; } /** * This method negates a given rational number * @param num * @return This returns the negated number */ public RationalNum neg() { RationalNum reNum = new RationalNum(); int numerator1 = -1*this.numerator; int denominator1 = this.denominator; reNum.numerator = numerator1; reNum.denominator = denominator1; return reNum; } }
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