UCL Logo

COMP1008 Object-Oriented Programming 2006

Programming Notes and Exercises 2

Recommend finishing date: Friday 3rd February 2006


Purpose: To work with and modify a small object-oriented Java program.

Goal: Complete as many of the exercise questions as you can. If you are keeping up, you need to do at least the core questions. The additional questions are more challenging and are designed to stretch the more confident programmers. Don't worry if you can't do them now, but be prepared to come back and try them later on.

Feedback: It is important that you get feedback on your exercise answers so that you know they are correct, that you are not making common mistakes, that the program code is properly presented and that you are confident you have solved the problem properly. To do this, get your answers reviewed by a lab demonstrator during lab sessions.

NOTE: You must keep all exercise answers as they form a record of your progress. After the exams you may be required to hand-in all exercises and coursework answers, as part of the course assessment process.

 


Getting Started

These exercise questions require you to work with the SimpleOrderSystem program introduced in the lectures. To get the source code do the following:

Either:

  1. Download the zip file containing the source code by using this URL: http://www.cs.ucl.ac.uk/staff/G.Roberts/courses2005_6/1008/SimpleOrderSystem.zip (link also available from COMP1008 web page). Save the zip file in a suitable directory in your filestore (if your web browser attempts to unzip the file, either force it to save the file instead or make sure it unzips the files where you want them).
  2. Unzip the zip file using the command 'unzip SimpleOrderSystem.zip'. This will create a directory called SimpleOrderSystem containing the .java files.
  3. Change to the SimpleOrderSystem directory and type the command 'javac *.java'. Note, you must be running Java 5 (the default on CS machines).
  4. Run the application using the command 'java SimpleOrderSystem'.

Or for use with BlueJ:

  1. Download the file: http://www.cs.ucl.ac.uk/staff/G.Roberts/courses2005_6/1008/SimpleOrderSystem.bluej.zip. Unzip the file to get the SimpleOrderSystem directory.
  2. Start BlueJ (use the command 'bluej') and select Open Project from the Project menu to display the file dialog. Select the directory SimpleOrderSystem and click Open (or Choose). BlueJ should load the project and you will see the class icons appear in the BlueJ window.
  3. Compile all the code by selecting the Compile item on the Tools menu, or one class at a time by right clicking on an icon and selecting Compile. Double click an icon to open an editor window to edit the code.
  4. You can run the program via the main method by right clicking on the SimpleOrderSystem class icon and selecting main. A terminal window will appear, as though you were running the program from an xterm window.

Exercise Questions

Each of these questions involves working with, modifying and/or extending the SimpleOrderSystem application.

Q1. Spend some time looking through the code, so that you understand how it works. Use BlueJ to run the code, and experiment with creating and using objects.

Q2. Add a command to display the total value of all orders for all customers. You will need to add a menu item, and add a method called overallTotal that will be called from the doOption method. OverallTotal will need to iterate through each customer adding up their total for all their orders.

Q3. You need to test your code to make sure it works. We haven't looked at testing yet, or loading/saving data to a file, and it is tedious having to type in customer and order information every time you run the program. Add an initialise method to class SimpleOrderSystem that creates a collection of example objects that you can then use to see if your code works.

For example:

public void initialise() {

  Product p1 = new Product(1,"Widget",100);

  products.add(p1);
  ... // Repeat for more Products
  LineItem i1 = new LineItem(2,p1);
  ... // Repeat for more LineItems
  Order o1 = new Order();
  o1.addI(i1); 
  ... // Repeat for more orders

  Customer c1 = new Customer("Arthur","Dent","Earth","1233556","arthur@earth.com");
  customers.add(c1);
  c1.addOrder(o1);
}

Q4. Add a command to edit a customers details (name, address, phone or email). Do this by creating a new Customer object and transferring the orders from the old customer to the new customer (use the getOrders method and add each order to the new Customer).

Q5. Customers have mobile phones! Add a mobile phone instance variable to class Customer and modify the class and the rest of the program to work with mobile phone numbers as well as ordinary phone numbers.

Q6. Add the ability to display the list of all orders for a Customer, showing each item in the order.

Q7. Add the ability to display a list of each order that includes a selected product. For each order, the customer name should be displayed.

 

Last updated: September 2, 2006

Computer Science Department - University College London - Gower Street - London - WC1E 6BT - Telephone: +44 (0)20 7679 7214 - Copyright 1999-2006 UCL


 Search by Google