Lab 2: Designing Classes
Part A: Getting Started
Download Lab2.exe, exctract it, open the project.
In this part you will add test cases to an existing class
and its test suite.
The handout has the code for the Class
ManhattanPoint and its test suite ManhattanPointTest.
- Define
aManhattanPoint with
coordinates x = 29
and y = 17.
- Add
test for the method distanceTo0()
- Add
test for the method isWithin(...)
- Add
test for the method isCloserThan(...)
- Add
test for the method getCloserPoint(...)
- Save
and print the results of your tests, and the source code for your extended
test suite.
Part B: Building Your Own Project (omit - download and open Lab2Book.exe instead)
- In File
menu select New
- Select
Java2SE Stationery
- Type
in the name for your project and hit return
- Expand
the Generic item and select Java Application
- When
the project opens, right-click next to its name and select MDI child
This gives you more
space to work with - the project floats in the window.
- In the
Project menu select Create Group and name it CodeBase
- In Project
menu select Add Files and select the file Base.jar
- The
window asks where to add files - select Java Application Release
only
- If
needed, move Base.jar into
the CodeBase folder (not important, just nice)
- Expand
the Sources tab in the project file list.
- Delete
TrivialApplication.
- In File
menu select New
- Select
File tab, then Text File - make sure you see where is it
stored.
- Give
the file the name of the class you will write: Book.java for Class Book .
- Now
you can develop your class.
- Copy
the skeleton of the test class (e.g. ExerciseSet1B.java) and re-name it BookTest.java.
- In
three places change the Exerciseset1B
to BookTest.
- In Edit menu select Java Application Release Settings near the bottom
- On the left select Java Target and replace TrivialApplication with the name of your test class Booktest.
- On the left select Java Output and replace TrivialApplication with the name of your test class Booktest.
- Make
sure the output type is again Java Application
- Develop
the test suite and run your program
Part C: Designing-Building a Class
- Define
the class Book
which contains as member data the name of the author, the title of the
book, the book id, the year published.
- Build
the skeleton of the test suite for this class.
- Define
the constructor for this class.
- Define
the toString method for
this class and test the constructor.
- Develop
the method howOld()
which determines how old is the book.
Part D: Class Composition
- Design-Build
the class LibRecord
which contains lending record for a book in the library. The record
contains a boolean
value available, the day it is due (make it just an
integer), and the reference to a Book
object. Add it to your project.
- Develop
a method takeOut, which records
that the book has been taken out for three weeks.
- Develop
the method returnBook,
which records the return of the book.
- Draw
UML diagram of this collection of classes.
--------------------------------------------------------------------------------
Rules for writing the toString
method for a class (thanks aLJaFP):
·
if the class doe not contain any member data fields,
use
public
String toString(){
return "new" +
getClass().getName() + "()"; }
·
if the class has one member data field, say x, use
public
String toString(){
return "new" +
getClass().getName() + "(" + x + ")"; }
·
if the class has two member data fields, say x and y,
use
public
String toString(){
return "new" +
getClass().getName() + "(" + x + ", " + y + ")"; }
To define an object to experiment with:
SomeClass y = new
SomeClass(.....);
println(y);
expected(...);
actual(y.methodName());