COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Assignment 3 – Uno

Posted by on September 25th, 2011 · Assignments

Due Date: Sunday week 12 (23:59:59 16 October 2011)

Your final assignment is to complete an implementation of the game of Uno. The focus of the assignment is on using inheritance. You will also be required to write unit tests for you code to demonstrate its correctness.

[Read more →]

Tut 10

Posted by on September 25th, 2011 · Lab

The following exercises are taken from chapter 8 of the textbook.

Exercise 8.4 Open the project dome-v2. This project contains a version of the DoME application rewritten to use inheritance, as described above. Note that the class diagram displays the inheritance relationship. Open the source code of the DVD class and remove the ‘extends Item‘ phrase. Close the editor. What changes do you observe in the class diagram? Add the ‘extends Item‘ phrase again.

 

Exercise 8.5 Create a CD object. Call some of its methods. Can you call the inherited methods (for example, setComment)? What do you observe about the inherited methods?

 

Exercise 8.6 In order to illustrate that a subclass can access non-private elements of the superclass without any special syntax, try the following slightly artificial modification to the CD and Item classes. Create a method called printShortDetails in the CD class. Its task is to print just the artist and the title of a CD on a line by itself. However, because the titIe field is private in the Item class, it will be necessary to add a public getTitIe method to Item. Call this method from printShortDetails to access the title for printing. Remember that no special syntax is required when a subclass calls a superclass method. Try out your solution by creating a CD object. Implement a similar method in the DVD class to print just the director and title.

 

Exercise 8.8 Open the dome-v2 project. Add a class for games and a subclass for video games to the project. Create some video game objects and test that all methods work as expected.

Database inheritance diagram

 

Exercise 8.12 Assume we have four classes: Person, Teacher, Student and PhDStudent. Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student.

a. Which of the following assignments are legal, and why or why not?

Person p1 = new Student();
Person p2 = new PhDStudent();
PhDStudent phd1 = new Student();
Teacher t1 = new Person();
Student s1 = new PhDStudent();

b. Suppose that we have the following legal declarations and assignments

Person p1 = new Person();
Person p2= new PerSon ();
PhDStudent phd1 = new PhDStudent();
Teacher t1 = new Teacher () ;
Student s1 = new Student();

Based on those just mentioned, which of the following assignments are legal and or why not?

s1 = p1;
s1 = p2;
p1 = s1;
t1 = st;
s1 = phd1;
phd1 = s1;

 

Exercise 8.13 Test your answers to the previous question by creating bare-bones versions of the classes mentioned in that exercise and trying it out in BlueJ

 

Tutorial 9

Posted by on September 15th, 2011 · Lab

JUnit Testing

For the exercises this week, download the diary-testing-junit-v2 project. These examples and exercises are based on chapter 6 of the textbook.

  1. Using the diary-testing-junit-v2 project, create a method in DayTest to check that findSpace returns the value of 10 for a one-hour appointment if a day already has a single one-hour appointment at 9 a.m. You must first create the 9 a.m. appointment, then the 10 a.m appointment. Specify assertions for the results of both calls.
  2. Create a test to check that findSpace returns a value of -1 if an attempt is made to find an appointment In a day that is already full.
  3. Create a test class that has Appointment as its reference class. Record separate test methods within It that check that the description and duration flelds of an Appointment object are initialised correctly following its creation.
  4. Create the following negative test in the DayTest class. Create a Day object, a one-hour Appointment object and a two-hour Appointment object. Make the one-hour appointment at 10 a.m. and then try to make the two-hour appointment at 9 a.m. Since this call to makeAppointment should fail, the value to put into the assertion is false. Now run the test. What is shown in the test results window?

Lab 7

Posted by on September 11th, 2011 · Uncategorized

This week’s tutorial is taken from the textbook, exercises 5.25 – 30 on page 149. They are reproduced below.

The task is to implement a simple phone book where numbers are associated with names, both represented as strings. Your phone book should be able to add a new name/number pair and you should be able to lookup a number, given a name.


HashMap phoneBook= new HashMap();

initialises the phone book. The following statements add new entries:

phoneBook.put("Charles Nguyen", "(02) 9392 4587");
phoneBook.put("Lisa Jones", "(03) 4536 4674");
phoneBook.put("William H. Smith", "(07) 5488 0123");

Next, we look up an entry:

String number = phoneBook.get("Lisa Jones");
System.out.println(number);

We refer to the name as the key because it is used for the lookup. Given the above, answer the following questions. You will need to use the Java API documentation.

Create a class MapTester in a new project. In it, use a HashMap to implement a phone book. You will have to import java.util.HashMap. In this class, implement two methods:

public void enterNumber(String name, String number)

and

public String lookupNumber(String name)

The methods should use put and get methods of the HashMap class to implement their functionality.

  • What happens when you add an entry to a map with a key that already exists in the map?
  • What happens when you add an entry to a map with two different keys?
  • How do you check whether a given key is contained in a map? Try it in Java.
  • What happens when you try to look up a values and the key does not exist in the map?
  • How do you check how many entries are contained in the map?

Ass 1 collection

Posted by on September 5th, 2011 · Uncategorized

Assignment 1 should now be available for collection. To collect your assignment:

  1. Go to the give webage.
  2. Enter COMP1400 as the subject code.
  3. Select ‘ass1’ from the list and click ‘Collect my assignment’

Lab 7 – debugging

Posted by on August 25th, 2011 · Lab

For this project you will be debugging a simple ‘games portal’ program. You can download the program as a Bluej project here. The code is complete and compiles correctly but it has a number of bugs in it that mean it doesn’t work properly. It is your job to find them.

There are two classes Player and Game. A Player has a credit balance with which they can buy games. A player can play any game they own, generating a score. The game keeps track of the best score for every player who has ever played it.

The methods on Player are:

  • getName – returns the player’s name
  • getCredit – returns the player’s current credit
  • addCredit – adds credit to the player’s account
  • buyGame – allows a player to buy a game if they have enough credit (and do not already own the game)
  • ownsGame – returns whether the player owns the game

The methods on Game are:

  • getName – returns the game’s name
  • getCredit – returns the game’s purchase price
  • play – The specified player plays the game and gets a random score. If it is better than the player’s best score so far, it is added to the high-score list
  • getHighScore – get the best score for the specified player
  • getBestPlayer – return the player who has the top score for this game,

There are at least seven bugs in the code. Three on Player and four on Game. You need to:

  1. Read the code and try to understand what it is doing.
  2. Invent test cases to check that it works the way it is supposed to.
  3. When you find something wrong, try to find the nearest possible cases that work and fail.
  4. Step through the different cases in the Debugger to work out what is going wrong.
  5. Work out how to fix them.

Some of the bugs are trickier to find than others. Test the simpler parts of the code first, then work up to more difficult cases.

Plagiarism

Posted by on August 23rd, 2011 · Uncategorized

Marking assignment 1, I have noticed a fair amount of code that has quite clearly been copied and pasted from the same source. I will remind you that the University has some quite stiff penalities for plagiarism, including immediate failure of the assignment or the whole course.

Now I don’t want to have to play policeman. I am more concerned that you actually learn stuff, which you won’t do if you are just blindly copying another’s code. I am happy for you to help each other out on assignments, but plagiarism will only hurt you in the end. Remember that the Prac Exam is worth 40% of your mark in this subject. If you copy and coast now, you will find yourself high and dry in Week 13.

Assignment 2

Posted by on August 23rd, 2011 · Assignments

Your second assignment will test your ability to use lists. Your task will be to implement a class representing a playlist from a music program like iTunes.

Due Date: Sunday week 8 (23:59:59 18 September 2011)

Download the ass2.zip file to get you started. It contains three classes:

  • Track – This class represents a single track with a name, a genre (from a list of constants on the class) and a duration (in seconds). Do not modify this file.
  • PlayList – This is the class you are to complete. I have given you the headers for all the methods you need to implement.
  • PlayListTest – This is a JUnit testing harness for your code. At the moment it provides only three tests. You should add more tests to this class to make sure your code works correctly.

Note: You will only be marked on your modifications to the PlayList class. The PlayListTest class is there to provide you with some example tests to check your code is working. You should add your own tests to this class, but you will not be marked on them.
[Read more →]

lab 6

Posted by on August 18th, 2011 · Lab

It seems that most classes spent week 5 working on the assignment, so my advice is to use this week to go back to lab 5 and continue working on it, or do revision from any of the earlier labs.

Assignment 1 – sample solution

Posted by on August 11th, 2011 · Assignments

I have made a sample solution to assignment 1 (with the source code removed of course) so you can check whether your solution does the same thing as mine. You can download it here.