Assignment 3 marks should now be viewable on the Give website
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.
The marks for the mid-sem exam are now available on the
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 by selecting “exam_midsem” from the menu.
The self-test quiz for this week is .
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.
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.
Marks for Assignment 2 are now available on the .
On the Assignment Management page select COMP1400. Then select “assignment2″ and press “Collect my Assignment”.
The self-test quiz for this week is .
The following exercises are taken from chapter 8 of the textbook.
Exercise 8.4 Open the . 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.
Exercise 8.12 Assume we have four classes: Person, Teacher, Student andPhDStudent. 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
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.
You can see an animation of the game at . 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 and an executable sample .
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.
You will submit your assignment to the Give automated marked system.
================================================Checking your submission...Checking for Life.java Life.java Compiled SuccessfullyCompiling against supplied file Canvas.java Canvas.java Compiled SuccessfullyCompiling against supplied file Grid.java Grid.java Compiled SuccessfullyAll files are OK================================================
Submission notes:
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.
The self-test quiz for week9 can be found .
Classes
Consider a database of student records for the university. What kind of data would be associated with each student?
Consider designing a class representing a car in a database for a used car lot.
ArrayLists
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 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 to help you.
The deadline for submitting assignment 2 will be extended to 11:59pm Wednesday 24 September.