![]() |
| CS Home » My Home Page » Teaching Archive 2005/6 » COMP1008 » Exercises 3 |
COMP1008 Object-Oriented Programming 2006Programming Notes and Exercises 3Recommend finishing date: Friday 17th February 2006Purpose: Practice writing classes. 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. Core questionsNote: Answers to all the questions involve writing programs consisting of two or more classes. The BlueJ programming environment would be a good tool to use when writing your classes. Q2.1 Write a class StringQueue to represent a queue of Strings. The class should have the following methods: public StringQueue() // A constructor to create an empty queue You will need to use a data structure like an ArrayList to store the queue contents. The StringQueue class should have a private instance variable to reference the data structure and only the methods declared in the class should directly access the data structure. Write a second class that tests the StringQueue class to confirm that it works. Note that the StringQueue class should not do any input or output (no reading from the keyboard or writing to the screen using System.out.print). Q2.2 Write a class IntSet to represent a set of integers. It should have the following methods: publi2 IntSet() // Create an empty set public void add(int n) // Add an integer to the set, or do // nothing if the integer is already in the set Remember that a set should not contain any duplicates. You will need a data structure to store the integers in the set and a way of checking for duplicates. Write a second class to test that your set class works correctly. When you have a working IntSet class, add methods to find the intersection and union of two sets: public IntSet intersection(IntSet set) // Return a new set representing the // intersection of the set with the set // given as the parameter. Note that both these methods should return a new IntSet object holding the result. The orginal sets should be left unchanged. Q2.3 Write a class ComplexNumber to represent complex numbers. Include methods to add, subtract and multiply complex numbers. public ComplexNumber(double real, double imaginary) // Create a new ComplexNumber object One ComplexNumber object should represent one complex number. Add, subtract and multiply should create and return new objects to store the result. It should not be possible to modify a complex number object once it has been created. If you are don't know or have forgotten about complex numbers do a web search - there are many sites that explain what they are. Q2.4 Create a modified version of your IntSet class from question 2.2 called ComplexSet to represent a set of complex numbers. Write a test class to confirm that your ComplexSet works correctly. Challenge questionQ2.5 Write a class to represent dates (i.e., day/month/year). Don't use any of the Java library classes that represent time or dates (such as Date, Calendar, etc.). As well as representing dates, methods should be provided to perform operations such as determining the number of days between two dates and determining the day of the week that a particular date falls on. Write a test class to confirm that your date class works.
|
Last updated:
September 2, 2006 |
Computer Science Department - University College London - Gower Street - London - WC1E 6BT -
+44 (0)20 7679 7214 - Copyright 1999-2006 UCL