221 In-Lab Page
Fall 2005
Lecture Schedule
In-Lab 1--September 12, 2005
Design a class to work with time
Your class should allow you to:
- set the value of a Time object
- add time to the value of the Time object (in hours and minutes, or in minutes only)
- output the value of the Time object
- compare two time objects, to see which is earlier
- distinguish between am and pm
Output might look something like this
8:40am + 30 minutes is 9:10am
11:15am +3 hours and 30 minutes is 2:45pm
8:40am is earlier than 2:45pm
Your Design should include:
- An interface that the class itself will implement.
Remember that an interface in Java is a way to specify the names,
parameters, and return values of the methods that the class will use
without specifying how the methods perform their operations and
without specifying how the data is internally represented.
- Constructors are not included in an interface, but I would like to
see the signature of the constructors you intend to use. You will
need to comment them out when you try to compile your interface.
- Since this is a review exercise as well as a design exercise, I am
going to specify that two of the methods to be included are:
- the toString( ) method which returns a String and has no argument, and
- the compareTo( ) method which returns an int and takes an argument of type Object.
Both of these methods are routinely included in user-defined classes,
and are inherited from the class Object, then overridden in the user-defined
class.
To Hand in:
Everyone should work individually on this assignment, but I certainly
encourage you to discuss different design options and possibilities with
others in the class.
Turn in the Java interface for this problem, written with Java syntax, with comments as described in the text.
In-Lab 2--September 19, 2005
Arrays, Exceptions and File I/O
Start with this Fraction class.
Get #1 below working before modifying your program for #2.
Then when #2 is working, modify it for #3.
- Write a main( ) program that obtains an arbitary number of
fractions from the user (max 20), stores them in an
array of Fractions, averages them, and displays the the fractions and their average.
- Catching Exceptions: Add try and catch blocks to your program in case
of exceptions thrown by erronous user input. Input errors from the user
should not cause your program to crash. Test this by entering incorrect
data.
- Modify your program so that it takes the input from this
data file instead of the user, then outputs
the results to a file called results.txt.
Hand in: Hand in your source code. William will tell you what he wants you to demo.
In-Lab 3--September 26, 2005
Test a Class
Due to the problems with esus, this lab will be due October 3, 2005
The class DVD_Collection is a class designed to keep track of a DVD collection.
Your job is to write a driver that tests each and every method in the
DVD_Collection class.
What to do
- Download and compile the files you need. These include
DVD_Entry.java and
DVD_Collection.java.
- Note that this class is much like the PhoneDirectory class that we looked
at during our classtime.
- Because time is limited in an in-lab, I have written part of the
driver for you. You are welcome to use as much or little of it as you want.
- The driver as written so far has three methods:
- The main( ) method which calls a menu method to find out
what the user wants to do.
- The menu( ) method which outputs to the screen the user's options,
then returns their choice as an int.
Since this method is not
called with an object, it is a static method, as are all the
methods written in main.
- The loadFile( ) method, was written mostly to help you with the Java
syntax of writing static methods in main.
- The menu offers the user eight choices. Implementing these choices will
test the methods in the DVD_Collection class. Be sure you take a look at
the output to be sure everything is working correctly every time you test
one method.
- Note that the comments are very terse. This is because I want you to
be able to tell what the code is doing, and how it is doing it
by reading the code. In a real
enviroment, the comments should give the reader more help with what
the code is doing.
Hand in: Hand in the source code
from your driver. William will tell you what he wants you to demo.
In-Lab 4--October 3, 2005
Inheritance and Abstract Classes
The basic assignment is to do the programming exercises on #2 on page 143
and #2 on page 154 but I will add a few comments/instructions here.
- Use the class Computer and
Laptop we talked about in class.
- As the exercise mentions, there are many differetnt kinds of
computers. An organization may have servers, mainframes, destopPCs,
and laptops. There are also personal data assistants and game computers.
So it may be more appropriate to declare class Computer as an
abstract class that has an actual subclass for each category of computer.
- Write an abstract class Computer that defines all the methods shown
above and declares an abstract method with the signature costBenefit(double)
that returns the cost-benefit(type double) for each category of computer.
- Besides the Laptop subclass, design at least three or four other
subclasses, but you may leave all but one of the abstract.
(This means the methods in the class will not have to be implemented.)
- You must make one other subclass besides the Laptop class
concrete. (So you must end up with two concrete classes.)
- You can devise any parameters you want to get values for CostBenefit( ).
- You will need to test your classes. Do this in a driver that declares an
array of type Computer, then puts several instances of of your
subclasses into the array (put them in any way you want, hard-code,
interactive input, or file input).
- Your driver should call the overridden methods of both the subclasses to
test them, and see that they actually do different things, even if it is
just figure cost-benefit and output.
Extra Credit: Write a clone method for class Laptop.
In-Lab 5--October 10, 2005
Building a Hierarchy
The basic assignment is to do the programming exercises on #4 on page 189.
I am reproducing the assignment here in case you do not have your books.
I will also add a few notes at the end.
You will do the design work and start on the implementation in class today.
The entire program will be due in two weeks. It is your next outlab.
Write a banking program that simulates the
operation of your local bank.
You should declare the following collection of classes.
- Class Account
- Data fields: customer (type Customer), balance, accountNumber, transactions array (type Transaction[ ])
- Methods: getBalance( ), getCustomer( ), toString( ), setCustomer( ), setBalance( ), clone( )
- Class SavingsAccount extends Account
- Methods: deposit( ), withdraw( ), addInterest( )
- Class CheckingAccount extents Account
- Methods: deposit( ), withdraw( ), addInterest( )
- Class Customer
- Data fields: name, address, age, phone custNumber
- Methods: Accessors and modifiers for data fields
- Classes Senior, Adult, Student all these classes extend Customer
- Each has constant data fields SAVING_INTEREST, CHECK_INTEREST, CHECK_CHARGE, and OVERDRAFT_PENALTY, that define these values for customers of that type.
- Class Bank
- Data fields: accounts array (type Account[ ])
- Methods: addAccount( ), makeDeposit( ), makeWithdrawal( ), getAccount( )
- Class Traction
- Data fields: customerNumber, tranactionType, amount, fees (a string describing unusual fees)
- Methods: processTrans( )
Notes:
- You should draw a diagram to indicate the hierarchy. The diagram should include a box for each class with the instance variables and method headers. Include arrows from a subclass to a superclass.
- Start implementing the classes highest in your hierarchy first. You can stub in methods that you don't need immediately.
- Decide which, if any of your methods should be abstract.
- You should include toString and clone methods in all classes
where you think they would be useful.
- You will needto write an allplication class that interacts with the user.
In this application you should first open several accounts and then enter several transactions.
Hand in: William will tell you what he wants
handed in today.
In-Lab 6--October 17, 2005
Introduction to Linked Lists
During today's in-lab, you are going to work with some
linked list classes. Before beginning the lab, download the following two
files and put them into a project called Lab6. List.java, and UseList.java, Open them in
the BlueJ compiler. You will need to look at them to do the lab.
Familiarize yourself with the code in these files and then do as many of
the following as you can in the time permitted. (No one is expected to get
them all done) The first is mandatory, you may choose to do the others in
any order you wish. You must turn in your code at the end of two hours.
What to Do
- On a sheet of paper, walk through the code, starting with main, and
drawing the new nodes as they are created. Add three nodes using the addToEnd( ),
method, reassigning the pointers as directed by the code. Any local variables should be shown,
and then crossed out as the method returns.
- Write an addToFront( ) method.
- Write an addUnique( )method so that it does not allow an element
that is already in the linked list to be inserted again. In this case an
appropriate error message should be printed. Add some statements to
UseList to check your changed method. Note that the String class includes
a compareTo method which you should use.
- Write an method called remove ( ) that removes an element (if it
exists) from the linked list. Remove should return a boolean so the client
knows whether
or not the removal was successful. It should be tested in UseList
- Write an addSorted( ) method so that it keeps the elements in
sorted order as they are inserted.
Hand in:
- A printout of your modified code.
- A printout of runs of your changed methods showing that they do work.
In-Lab 7--October 24, 2005
Using the Linked List class
This lab has several parts: the first two parts should take no
more than 30 minutes or so. The majority of your time should be
spent on the last part
What to do
- Study the documentation for a LinkedList class in the Java API.
Pay special attention to the methods that are available to you.
- Write an simple application program that uses a LinkedList. Your application
should:
- Create a LinkedList to hold Strings
- Put the following String items in the list (unordered for now)
Tom Dick Harry Jane Mary Julie (You can hard code this if you wish)
- Output all the items in the list
- Output every other item in the list
- Remove Jane from the list; output the list to test
- The KWListTester code we looked at in class should help you do
this quickly and easily.
- Implement a class that uses the LinkedList class to store lists
that are always in order. This is the class you designed in lecture
on Friday.
- After you have shown the above class to the TA or consultant
you can modify it to test your new class.
- Extra Credit: Create a linked list of DirectoryEntries that are always in order.
- This class will have to implement the comparable interface for it
to work with your OrderedList. So you will have to add a compareTo
method to it.