Question: in java script 2 Write a Rational Number Class a In the second part of today's lab, we're going to practice writing another class. Your


2 Write a Rational Number Class a In the second part of today's lab, we're going to practice writing another class. Your task today is to create a class, called Rational, for storing and computing on Rational numbers. Recall that 4 CSCI 1933 LAB 2 2. WRITE A RATIONAL NUMBER CLASS rational numbers are defined as numbers that can be expressed as the fraction of two integeirs, a numerator, p, and and a non-zero denominator, q P 9 All of these are examples of rational numbers 1.5 10 3'8' 1 If you want to read more about rational numbers, this is a good resource. Remember that Java clone should be defined in their own Java files. Also, your Rational class will need to include a constructor that requires the numerator and denominator as input. It will need to have get and set methods for the numerator and denominator attributes. Finally, it will also need to include methods that perform basic arithmetic operations (add, subtract, multiply, divide). To summarize, your class should include all the following functioning methods: public Rational(int numerator, int denominator) public int gotNumerator public int getDenominator) public void setNumerator(int numerator) public void set Denominator (int denominator) public void addRational (Rational r) public void subRational (Rational r) public void mulRational (Rational r) public void divational Crational r) Reflection: What should the access modifiers for your data members and methods be? As a reminder, remember that when you add two rational numbers, ad + bc d bd and when you multiply two rational numbers, a c ac OD - bd Recall the corresponding operations for subtracting and dividing rational numbers. For this lab, you do NOT need to reduce the results of arithmetic operations to the lowest terms (eg a rational number 6/10 is OK). 5 CSCI 1933 LAB 2 2. WRITE A RATIONAL NUMBER CLASS Once you've finished writing your Rational class, you'll need to write tests that check whether it's working. In later labs, we'll discuss more sophisticated ways to write code for testing, but today, you'll simply use conditional statements to check whether we're getting the expected result for calls to our methods. For example, if I had a method int mult(int a, int b), to test if it worked, I could write a statement comparing the actual output of the method to the expected output as follows: //test: mult(2,3); expected result = 6 boolean result - (mult(2,3) -- 6); System.out.println("Tost Mult: " + result); Doing this allows for quicker evaluation of the tests, since you don't need to inspect the results any further than looking for any test that returned false In a main method, write at least one test for each of these Rational methods: addrational, subRational, mulRational, and divRational. Milestone 2: Show a TA your completed Rational class and run the accompanying tests for them to demonstrate that it works
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
