Complete the implementation of the (static) class method String findAndReplace(String in, String what, String with) of the class Utils (Utils.java). The method returns a copy of the array in where each word occurring in the array what has been replaced by the word occurring at the corresponding position in the array with. The array designated by in must remain unchanged.
Later in the semester, we will learn about exceptions. Exceptions provide tools for handling error situations. In the meantime, the method findAndReplace returns null whenever the preconditions of the methods are violated. In particular, the formal parameters cannot be null. For all three arrays, none of the elements can be null. The query and replacement arrays must be of the same length.
Although “testing shows the presence, not the absence of bugs”1 , testing is never the less an important activity of software development. JUnit is a Java framework to assist you with this important task.
Your teaching assistant will now present a brief introduction and tour of JUnit. She/he will lead a discussion around each test case found in TestFindAndReplace.java: what is the test case testing, what are the strengths and weaknesses of the test set, what cases are missing, etc.
Ah, you are back! Make all the necessary changes to your method findAndReplace so that it passes all the tests.
Implement a class to represent rational numbers. Each rational number consists of a numerator and a denominator, both of type int. Since each rational number has its own numerator and denominator, these must be instance variables. Furthermore, good object-oriented programming suggests that the visibility of the variables should be private.
The class Rational has two constructors. One of them has two formal parameters, which provide the initial values for both instance variables. The other constructor has a single parameter, which provides the initial value for the numerator; the denominator is assumed to be 1.
Implement access methods that return the numerator and denominator of this rational, but no setter methods. An object that has no setter methods, and no other methods for transforming the state of the object, is said to be immutable. Immutable is a great property. Do you see why? Discuss this with your neighbors and TA.
Implement the instance method plus. The method has a single formal parameter, of type Rational. The method returns a new Rational number that is the sum of this number and that of the parameter.
Implement a class method plus. The method has two formal parameters, both of type Rational. The method returns a new Rational number that is the sum of the two numbers.
Implement a private class method for calculating the greatest common divisor of two integers, which are the formal parameters of the method.
Implement a private instance method called reduce that transforms this number into its reduced form.
Make all the necessary changes so that a rational number is always stored in reduced form.
Implement the instance method public boolean equals(Rational o) that returns true if this fraction and the one designated by o represent the same fraction (content equality).
Add a method public String toString() that returns a String representation of this fraction with the numerator followed by the symbol “/”, followed by the denominator. If the denominator is 1 then the method returns a String consisting of the numerator only.
Implements the instance method int compareTo( Rational o ). It compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Another important aspect of software development is the documentation. JavaDoc is a format for your Java comments, and a set of tools for producing Web pages automatically. In ITI1121, we are asking you to document your code (variables, methods, etc.) using JavaDoc. Your teaching assistant will now present a brief introduction of JavaDoc.
You must add JavaDoc comments for the class Rational.
You can now produce HTML files automatically, either using your favorite IDE (DrJava, Eclipse, Netbeans, etc.) or by running the javadoc command in a shell, the parameters are -d doc, followed by the name(s) of the Java file(s) to be processed
When several files need to be processed simultaneously, use * in place of the names of the files.
Submit your answer on Blackboard Learn:
Last Modified: May 15, 2015