COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Assignment 3 Marks

Posted by on December 5th, 2014 · Announcements

Assignment 3 marks should now be viewable on the Give website

Practical Exam Reminder

Posted by on October 27th, 2014 · Announcements

A reminder that the Final Practical Exam will be held during labs this week. The exam duration is 2 hours, so make sure you arrive on time. Tutors will not wait for late students before starting the exam.

Like the mid-sem, you may bring 1 A4 page of notes into the exam.

Mid-semester Practical Exam Marks

Posted by on October 21st, 2014 · Announcements

The marks for the mid-sem exam are now available on the Sturec Page

The mark will appear in the “exam_midsem” field.
You should also be able to see your assignment marks and lab marks (if your tutor has uploaded them)

Update: The marked transcript with comments can also be viewed on the Give Website by selecting “exam_midsem” from the menu.

Week 12 Lab – HashMaps

Posted by on October 19th, 2014 · Lab

The self-test quiz for this week is here.

This week’s tutorial is taken from the textbook, exercises 5.25 – 32 on page 174-175. 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<String, String> phoneBook= new HashMap<String, String>();

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.

  1. 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.

  2. What happens when you add an entry to a map with a key that already exists in the map?
  3. What happens when you add an entry to a map with two different keys?
  4. How do you check whether a given key is contained in a map? Try it in Java.
  5. What happens when you try to look up a values and the key does not exist in the map?
  6. How do you check how many entries are contained in the map?
  7. How do you print out all keys currently stored in a map?

 

Assignment 2 Marks

Posted by on October 16th, 2014 · Announcements

Marks for Assignment 2 are now available on the Give website.

On the Assignment Management page select COMP1400. Then select “assignment2″ and press “Collect my Assignment”.

Week 11 Lab – Inheritance

Posted by on October 12th, 2014 · Lab

The self-test quiz for this week is here.

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 classdiagram 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 printShortDetailsin the CDclass. 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: PersonTeacherStudent andPhDStudentTeacher and Student are both subclasses of PersonPhDStudent 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

 

Assignment 3 – The Game of Life

Posted by on October 6th, 2014 · Assignments

DUE DATE: 11:59pm Sunday 26 October.

WARNING: This is a draft only. These specifications will be updated.

For this assignment you will write a program that implements Conway’s Game of Life. The board is a rectangular array in which a cell is designated as either live or dead. A few simple rules tell you how to create a new generation (i.e. a new board) based on the old one.

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

You can see an animation of the game at this web site. Two supporting classes are provided for you to display the game. You don’t have to write any graphics code. Your task will only be to implement the rules above.

You will find a project file for the assignment here and an executable sample here.

The project has three classes: Canvas, which handles the primitive drawing on the screen; Grid, is a subclass of canvas and handles drawing the board; and Life, which is the file that you must modify to play the Game of Life. It also has the constructor method that plays the game. Do not, under any circumstances, change the other two classes and do not change the Life constructor method!

A board is represented as a 2-dimensional boolean array:

boolean[][] board = new boolean[numberOfRows][numberOfColumns];

You are required to three methods.

Task 1

The first method you must write is to create a new board with a random number of cells set to true. The chances of a cell being alive (i.e. true) should be 10%.

private boolean[][] newBoard(int height, int width)

The method has two parameters: the width and height of the board. It must return a 2-dimensional boolean array in which approximately 10% of the cells are set to true (i.e. alive).

Task 2

The second method you must write is to count the live neighbours of a cell.

private int countNeighbours(boolean[][] board, int row, int col)

Given a board (i.e. a 2D boolean array) and a row and column position, count the number of immediately surrounding cells that contain the value true. The table below shows the index values of the neighbours of the cell where i is the row number and j is the column number (starting from zero).

i-1, j-1 i-1, j i-1, j+1
i, j-1 i, j i, j+1
i+1, j-1 i+1, j i+1, j+1

Your method should visit each of the surrounding cells and accumulate a count of the cells that contain true, i.e, the cell is live. Note that you do not count the cell itself. If a cell is on the edge of the array (i.e. some of its neighbours are out of bounds), only count the cells that are within the array.

Task 3

The third method you must write is:

private boolean[][] nextGeneration(boolean[][] board1)

The only parameter to the method is a board (i.e. a 2D boolean array). For each cell in the array (i.e. for each column position in each row), apply the four reproduction rules, given above, to create a new board representing the next generation. To do this, you must declare a new array that is the same size as the old one. As you apply the rules to a cell in the old array, you update the corresponding cell in the new array. That is, you must not change the original array.

WARNING: The Game of Life is a popular programming exercise and you will find many versions of it on the web. While you may use these to get an idea of how to write your own program, your code must be your own. Plagiarism is not allowed and will be heavily penalised.

Submission instructions

You will submit your assignment to the Give automated marked system.

  1. From within your assignment 3 BlueJ project, select Project -> Create Jar File…
  2. In the Dialog Box that appears:
    1. Set “Main Class” to none
    2. Ensure “Include Source” is checked
    3. Leave “Include Bluej project files” unchecked
  3. Press “Continue”
  4. Save the filename as “Assignment3.jar”
  5. Open a web browser and go to the give web site
  6. Log-in with you Z-Pass
  7. Either enter “COMP1400″ in for the course, or select COMP1400 from the drop down menu and press “Search for Assignments”.
  8. Select “assignment3″ from the next drop down menu and press “Upload my Assignment”
  9. Accept the student declaration
  10. Press “Choose File” and select the “Assignment3.jar” file that you saved in step 4
  11. Press “Submit my Files”
  12. Wait for the page to completely load. Then carefully read the output. If the assignment is successful you should see the following lines:
    ================================================
    Checking your submission...
    
    Checking for Life.java
        Life.java Compiled Successfully
    Compiling against supplied file Canvas.java
        Canvas.java Compiled Successfully
    Compiling against supplied file Grid.java
        Grid.java Compiled Successfully
    
    All files are OK
    ================================================
  13. If the page doesn’t load properly, that is OK just submit the assignment file again.
  14. If you don’t get the above output check the output to see what went wrong and resubmit.

Submission notes:

  1. You can submit as many times as you wish, even after the deadline. Only your last submission will be marked.
  2. Make sure you name everything the as instructed, including the classes and the submission file or the submission will not work.
  3. If you have any problems with submission, email the class account

 

Week 10 Lab – Random Numbers

Posted by on October 6th, 2014 · Lab

The self-test quiz for week 10 is here.

These exercises are taken from Chapter 5 of the text book. You should also use this week to catch up with previous labs.

Exercise 5.14 Write some code (in BlueJ) to test the generation of random numbers. To do this, create a new class called RandomTester. You can create this class in a new project. In class RandomTester, implement two methods: printOneRandom (which prints out one random number) and printMultiRandom(int howMany) (which has a parameter to specify how many numbers you want, and then prints out the appropriate number of random numbers).

Exercise 5.15 Find the nextInt method in class Random that allows the target range of random numbers to be specified. What are the possible random numbers that are generated when you call this method with 100 as its parameter?

Exercise 5.16 Write a method in your RandomTester class called throwDice that returns a random number between 1 and 6 (inclusive).

Exercise 5.17 Write a method called getResponse that randomly returns one if the strings “yes”, “no”, or “maybe”.

Exercise 5.18 Extend your getResponse method so that it uses an ArrayList to store an arbitrary number of responses and randomly returns one of them. to Store

Exercise 5.19 Add a method to your RandomTester class that takes a parameter max and generates a random number in the range 1 to max (inclusive).

Exercise 5.20 Add a method to your RandomTester class that takes two parameters, min and max, and generates a random number in the range min to max (inclusive). Rewrite the body of the method you wrote for the previous exercise so that it now calls this new method to generate its result. Note that it should not be necessary to use a loop in this method.

 

Week 9 Lab – Classes and ArrayLists

Posted by on September 22nd, 2014 · Lab

The self-test quiz for week9 can be found here.

Classes

Consider a database of student records for the university. What kind of data would be associated with each student?

  • Download the Lab9 BlueJ project. It defines a new class Student which contains some student information
  • Create a few different students in BlueJ. Use the Inspector to examine their fields.
  • Use the getName() method to access the student’s name.
  • Write your own accessor methods to get the address and year enrolled.
  • Add a field to record the student’s gender. Update the constructor to initialise this value appropriately using a parameter. Add an accessor method.
  • Add a field to record the student’s age. Update the constructor to initialise the age to the default value of 18.
  • Add a second constructor that allows you to specify an age other than the default.
  • Add a static final int constant to the class for the default age, to avoid using 18 as a magic number in your code.

Consider designing a class representing a car in a database for a used car lot.

  • What kinds of data would be recorded about each car?
  • What are the types of this data?
  • Write a Car class representing an entry in this database. Give it:
    1. Fields recording the above data.
    2. A public constructor.
    3. Public accessor methods to read the data.

ArrayLists

  • Create a new class Course, that contains an ArrayList, called students, that stores all the students in a class. It should also have fields for the class id, like COMP1400 and a String type for the course name.
  • Call the size method to check that the list is initially empty.
  • Call the add method to add some students to the list.
  • Use the inspector to see how the fields have changed.
  • How does the elementData field change as you add names to the list? What happens if you add more than 10 names?
  • Use the get method to read items off the list. What happens if you try to read an item beyond the end of the list? Explain the message you receive.

Create a new class called StringUtils with the following methods:
public String findLongest(ArrayList)

This method should search through a list of strings to find the longest one. You can use the length() method on the String class to find this value. Test this on the ArrayList of students.

public ArrayList findStartsWith(String prefix)
This method should search through the ArrayList and collect all strings that start with the given prefix. You can use the startsWith() method to help you.

 

Extension for Assignment 2

Posted by on September 19th, 2014 · Assignments

The deadline for submitting assignment 2 will be extended to 11:59pm Wednesday 24 September.