Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2018



ASSIGNMENT #8 - Testing

In this assignment, you will be you will practice writing and testing methods. You will be given some code that implements a fraction class, and you are to complete some of the methods. The most important part, though, is using the main method to test the code that you have written. Remember what we talked about in class about white box and black box testing. You should develop test cases for both processes for each of your methods.

Fractions. In the lectures coming up, we will be talking about building your own data types by using classes. Fractions are a great example of why you would want to do this. Once you have defined a data type called Fraction and implemented its methods, you can create variables of that type and do fraction math on them. For this assignment, you are to fill in the missing methods and then thoroughly test them. The code that you should start from is in Fraction.java.

Below is a list of the methods that should be in your Fraction class. (This list, by the way, is called the API, or Application Programming Interface. The first column shows the return type of the method, the second, the name of the method and parameters passed in - the signature- and the third column is a brief description of what the method should do.) The methods that are in red are the ones you have to complete: add, subtract, multiply and divide. In the code you will find that these methods have been set up for you with a /**** YOUR CODE HERE ****/ comment in them. Each of these contains a "return" statement so that the program compiles as-is, but as you complete each method, you should return the appropriate result.

For this assignment, assume that fractions like x/0 are acceptable and that your code deals with them properly. If you need a refresher on fraction math, you can find the rules here.

It is VERY important that the methods are implemented exactly as stated in the API. If we run test code that, for example, expects to call your multiply method, and you have named it Multiply, have changed the number or type of parameters, or the return type, the code will fail.

class Fraction
-----------------------------------------------------------------------------------------
         Fraction(int n, int d)  // create a fraction with numerator x and denominator y at maze coordinates x, y 
         Fraction()              // create a fraction with numerator 1 and denominator 1
String   toString()              // return a printable version of the fraction
boolean  equals(Fraction f)      // is one fraction equal to another?
Fraction reduce()                // reduce the fraction to its lowest terms
Fraction reciprocal()            // return the reciprocal of a fraction
Fraction multiply(Fraction f)    // multiply two fractions
Fraction divide(Fraction f)      // divide the first fraction by the second
Fraction add(Fraction f)         // add two fractions
Fraction subtract(Fraction f)    // subtract the second fraction from the first

The main method in your program should create fractions and test the various methods. It has been started for you with tests for the methods already written. You must write the tests for the methods that you have written. We, of course, will be running our own tests to make sure that your Fraction methods work correctly. Be sure to look through the grading criteria below to make sure you've completed everthing.

Be sure to check for troublesome fraction values in your tests to make sure that your methods work as expected.

Good luck -- and have fun!


Grading
Grade ItemPoints PossiblePoints Earned
Program Compiles
2
Program Runs
2
Header Comment
2
multiply Method
3
divide Method
3
add Method
3
subtract Method
3
multiply Tests
3
divide Tests
3
add Tests
3
subtract Tests
3
Total
30

Submission. Submit your code, Fraction.java, using Moodle. Be sure your submitted code has the required header with your name and a description of the program.

Page last updated: August 15, 2019