Part 1

Prerequisite material

Objectives

  • Understanding when to use “==” or “equals”
  • Manipulating arrays and references

1 The method equals

As you know, the class Object defines a method equals.

                  
public class Object {

    // ...

    public boolean equals(Object obj) {
        return this == other;
    }

}
                

Since in Java, every class inherits from the class Object, every class inherits the method boolean equals(Object obj). This is true for the predefined classes, such as String and Integer, but also for any class that you will define.

The method equals is used to compare references variables. Consider two references, a and b, and the designated objects.

  • If you need to compare the identity of two objects (do a and b designate the same object?), use "==" (i.e. a == b );
  • If you need to compare the content of two objects (is the content of the two objects the same?), use the method "equals" (i.e. a.equals(b)).

2 Manipulating arrays - FindAndReplace

Complete the implementation of the (static) class method String[] findAndReplace(String[] in, String[] what, String[] with) of the class Utils. 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. If there are duplicate words in the array what, simply use the first instance you find.

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 one of the preconditions of the methods is 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.

You will hand this exercise in. Download and complete the starter file here: Utils.java.

 

Part 2

Using tests to validate programs

Objectives

  • Introduction to unit testing

Although "testing shows the presence, not the absence of bugs"1, testing is never the less an important activity of software development.

Tests are an essential part of software development. It is therefore important for you to know how to use them. Although it is possible to manually test simple programs, as your program gets more and more complex this becomes impossible, and automated tests are needed. The basic idea of an automatic test is to create a block of code – a test – that interacts with your program by doing one to many possible scenarios where we know the logical answer that we are supposed to get, and then make sure that it matches the answer that the programs returns. There exist many tools that can be used to help you test your code such as JUnit.

Use the test class TestFindAndReplace.java to test your program.

Make all the necessary changes to your method findAndReplace so that it passes all the tests.

Part 3

More objects

Objectives

  • Further understanding of object-oriented programming
  • Problem solving using Java

Rational

You will hand in this exercise. Download and complete the starter file here: Rational.java.

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.

2.1 Constructors

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.

2.2 getters

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.

2.3 plus

Implement the instance method plus. The method has a single formal parameter, of type Rational. The method returns a new Rational object that represents the sum of this number and that of the parameter.

2.4 plus (part 2)

Implement a class method plus. The method has two formal parameters, both of type Rational. The method returns a new Rational object that represents the sum of the two numbers.

2.5 gcd

Implement a private class method for calculating the greatest common divisor of two integers, which are the formal parameters of the method.

2.6 reduce

Implement a private instance method called reduce that transforms this number into its reduced form. (ex: 18/4 -> 9/2)

2.7 reduce (part 2)

Make all the necessary changes so that a rational number is always stored in reduced form.

2.8 equals

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).

2.9 toString

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.

2.10 compareTo

Implements the instance method int compareTo( Rational o ). It compares this object with the specified object for order, by computing the difference. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

 

Part 4

Documentation: JavaDoc

Objectives

  • Explain JavaDoc in your own words
  • Represent your comments with the help of JavaDoc
  • Produce Web pages from JavaDoc specifications

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.

The JavaDoc documentation is a series of comments using a special syntax. These comments are a description of a class or a method. They start with “/**”, after which the following lines must start with “*”, and lastly the comment block is closed using “*/”. If we want to change the paragraph, we use “<p>”.

Once the description is done, we use the “block tags”. These tags start with the symbol “@”. There are many tags but we will focus on these:

  • @author : Indicates who the author of the code is.
  • @param : Has to be followed by the name of a parameter from the class to which it is related, and then a description of that parameter.
  • @return : It is followed by a description of what the method returns.

For more information, see the JavaDoc documentation.

Example :


/**
* this class represents a person
* <p>
* Note: Giving proper comments in your program makes it more
* user friendly and it is assumed as a high quality code.
*
* @author  Bob Smith
*/
public class Person {
       private String firstName;
       private String lastName;
       private String username;
       [...]

        /**
        * Constructor method initializing the first and last name.
        * Sets the default value for the username as "firstName.lastName".
        *@param firstName The first name of the person
        *@param lastName The last name of the person
        */
  		public Person(String firstName, String lastName){
					//no code before this call
					this(firstName, lastName, firstName + "." + lastName);
					//possibly more code here
      }
        /**
        * Constructor method initializing the first name, last name and the username.
        *@param firstName The first name of the person
        *@param lastName The last name of the person
        *@param username The preferred username of the person
        */
			public Person(String firstName, String lastName, String username){
				this.firstName = firstName;
				this.lastName = lastName;
				this.username = username;
			}
        /**
        *this is a getter method
        *it is used to get the value of the variable firstName
        *@return String this returns the person's first name
        */
  		public String getFirstName(){
        	return firstName; //Here we do not need to use "this"
        }
        /**
        *this is a setter method
        *it is used to change the value of the variable firstName
        *@param firstName The value to assign to the instance variable firstName
        */
        public void setFirstName(String firstName){
        	this.firstName = firstName; //Here we need to use the keyword "this"
        }
  }
  

You must add JavaDoc comments for the file Utils.java.

  1. Add JavaDoc comments to the method findAndReplace. Each comment should include a brief description of what the method does and descriptions of the parameters and the return value using JavaDoc format.
  2. Add a brief description of the class Utils, using the JavaDoc syntax, making sure to include the name of the author of the class (you).

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

> javadoc -d doc Utils.java

When several files need to be processed simultaneously, use * in place of the names of the files.

> javadoc -d doc *

 

5th part

Create and submit a zip folder (1 point)

Instructions

  • Create a directory lab3_123456, where 123456 is replaced by your student number.
  • Inside this directory, place the .java files for Utils and Rational. Include only your source code. Do not include any class files or any other files; only Java files.
  • In this directory, also create a file README.txt which is a text file containing your name, student number and a brief description of the content of the directory:
    
    Student name: Jane Doe
    Student number: 123456
    Course code: ITI1121
    Lab section: B-2
    
    This archive contains the 3 files of the lab 3, that is, this file (README.txt),
    plus the files Utils.java and Rational.java.
  • Create a zip file lab3_123456.zip containing the directory lab3_123456 and all of the java files.
  • Verify that your archive is correct by uncompressing it somewhere and making sure that all the files and the directory structure are there.
  • Submit the archive using https://uottawa.brightspace.com/

Important remarks!

We are using automated scripts to test and grade your lab submission. Therefore, you must follow these instructions exactly. In particular:
  • Your naming of all the files and methods must be exact. Copy/paste the starter code provided during the lab to avoid problems.
  • You MUST submit a .zip; not individual files; not a .rar, not a .7z, or anything else.
  • All the required java files must be present and must compile (that is, running javac *.java must not produce any errors), even if you were unable to complete an exercise.
  • Even if you could not solve an exercise's solution completely, you have to hand in a file with the methods that compiles (you can put "return 0" if you gave up).
  • Your submission is due by Tuesday 11:59PM the week after the lab. You can submit as often as you want before then, but not after. Only your last submission will be considered.

JUnits

To help you making sure that your code is correct, we have prepared some JUnit tests. Refer to the Readme.txt in the zip file for some additional information.

Resources

Table of Contents