Programming for Designers

Class announcements for COMP1400

Programming for Designers random header image

Lab 7

Posted by malcolmr 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 malcolmr 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 malcolmr 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 malcolmr 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 malcolmr 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 malcolmr 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 malcolmr 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.

Assignment 1 correction

Posted by malcolmr on August 11th, 2011 · Assignments

It has pointed out to me that in the Assignment 1 spec, I wrote:

The player’s initial health (which is also their starting health).

This should read:

The player’s initial health (which is also their maximum health).

So if the player starts with 30 health points then this is the maximum amount of health they can have.

lab 5 – lists

Posted by malcolmr on August 11th, 2011 · Lab

Your task today is to implement a program to represent decks and hands of playing cards.

  1. Download the cards.zip file and uncompress it. It contains an incomplete BlueJ project called cards.
  2. Open the project and inspect the classes:
    • Card – this represents a single playing card.
    • Deck – this represents a deck of cards. The constructor has been written but the methods need to be completed.
    • Hand – this represents a hand of cards drawn from the deck. The fields, constructor and methods all need to be implemented.
  3. Complete these classes following the comments. Do the Deck class first then the Hand class.

To complete the Deck.draw() method you will need to use the Random class in the Java API. This class provides a random number generator to pick random numbers.

To use this class you will have to import it, by putting the line:

import java.util.Random

at the top of your code.

Read the documentation to find out what constructors and methods the Random class provide.

To see what the class does, try typing the following into the Codepad:

import java.util.Random;
Random rng = new Random();
rng.nextInt(6)
rng.nextInt(6)
rng.nextInt(6)
rng.nextInt(20)
rng.nextInt(20)

lab 4 – object interaction

Posted by malcolmr on August 5th, 2011 · Lab

Task 1: Universities

Suppose we were writing a program to administer a university. Discuss:

  • What are the different levels of abstraction we might use to represent the university?
  • Choose an object at the mid-level of your hierarchy. What kind of public interface might it provide? What private implementation details might it hide?

Task 2: Return to the Ticket Machine

Go back to the ticket machine project you worked on in last week’s tut.

  1. Create a new class Ticket which represents a ticket.
  2. Create appropriate fields. What information shoud a Ticket carry?
  3. Create a constructor to initialise these fields.
  4. Now modify purchaseTicket on the TicketMachine class so that it creates a new Ticket object and returns it, rather than printing it.

Let’s suppose now that a ticket has 10 charges. Each time we use it, one charge is lost.

  1. Change your Ticket class to track charges. What changes do you need to make to do this?

Now let’s add a recharge method to the TicketMachine. It should take a Ticket as a parameter and reset its charges to 10 but at the cost of 1/10th of the ticketPrice for every charge added (if the user has enough balance). Implement this in stages:

  1. Make the method just recharge the Ticket for free.
  2. Compute how much the recharge will cost. You will need to use an accessor method on the Ticket to find out how much charge it has.
  3. Add a test to see if the user has enough balance.
  4. If there is not enough balance print an error message and do not recharge.
  5. Subtract the charge from the balance.

Task 3: Clock

Take a look at the clock project from Chapter 3 of the textbook.

  1. Investigate the NumberDisplay class first. How does it work?
  2. Look at the setValue method. What does the && operator do? This is a boolean operator. Try experimenting with it in the Code Pad.
  3. Look at the increment method. What does the % operator do? Try experimenting with it in the BlueJ Code Pad.
  4. Now take a look at the ClockDisplay class. Try to understand how it works.
  5. ClockDisplay has two constructors. How do they differ?
  6. The second constructor does not call updateDisplay. Is this a bug? Why/why not?
  7. Change this class to have a seconds field as well as hours and minutes.
  8. Change the class to show a 12 hour clock with AM/PM.