COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Assignment 2 Submission now available

Posted by on September 18th, 2014 · Assignments

The Give Website has been updated to allow submissions for assignment 2

Assignment 1 Marks & Tutor Feedback

Posted by on September 17th, 2014 · Assignments

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

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

You will be presented with your assignment log. The log contains:

  • Your submitted code
  • Comments within the code
  • A summary of the tests and results
  • Output from incorrect tests
  • Your marks

Carefully read any comments that may have added to your log.

Week 8 Lab – ArrayLists and Classes

Posted by on September 14th, 2014 · Lab

The self-test quiz for week 8 is here.

ArrayLists and Classes

Download the BlueJ project club.zip for this week’s exercises. There are three classes in the project that help you to represent a club and its members.

The Member class is fully defined and contains all the methods you need. Note a couple of things we haven’t covered in lectures. The constructor uses a throw statement to create a Java runtime error. Try entering a month with a number outside the range 1 .. 12 and see what happens.

The Member class also defines a method called toString. This returns a string representation of the object. If you try to print the object using System.out.println, the system will used toString to convert your object into a string for printing.

The ClubDemo class is the “main” class that we use to create a club and members and test the other classes.

The Club class is incomplete. The methods in the class are empty and your job is to write the code inside to make them work.

Question 1

The club class needs a way of storing a collection of members. You should use an ArrayList to do that. The class includes two constructors. Let’s start with the first one. It should initialise the ArrayList and leave it empty.

Question 2

The Join method is passed a new member as an argument. You should add this member to the ArrayList.

Question 3

The numberOfMembers should return the number of elements in the ArrayList.

Question 4

The listMembers method should go through the ArrayList of members and print each one, one per line.

Question 5

The removeMember method should remove a member from the ArrayList when a member decides to leave the club. The argument to the method is a string, which is the member’s name. You have to search the ArrayList to find the member with that name. Use a simple algorithm, just go through the list, one-by-one until you find the member with the given name and remove it from the ArrayList.

Question 6

The second constructor in the class allows you to pass an array of members. Initialise the club membership with the members in the array.

 

Assignment 2 – ArrayList

Posted by on September 8th, 2014 · 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 21 September 2014)

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.

Requirements

A PlayList is a container for Tracks. It provides the following functionality:

  • Two constructors:
    1. The first constructor simply asks for a name for the list and creates an empty playlist
    2. The second constructor asks for a name and a list of tracks. It creates a playlist and copies the tracks from the list (in order) onto the playlist.
  • Accessor methods:
    1. getName – returns the name of the playlist
    2. getDuration – returns the total duration of all tracks on the playlist
    3. getNumberOfTracks – returns the number of tracks on the playlist
    4. getTrack – returns the specified track. Tracks should be numbered starting atone.
    5. filterByGenre – get the list of tracks from the playlist (in order) that match a given genre.
  • Mutator methods:
    1. addTrack – add a track to the playlist
  • Display methods:
    1. printListing – print out the name of the playlist and a numbered listing of all tracks on the playlist

The formatting of the printListing method should look like the following:


Queen's Greatest Hits
1: Bohemian Rhapsody - 5:54
2: Another One Bites The Dust - 3:34
3: Killer Queen - 3:01
4: Fat Bottomed Girls - 3:22

You can use the toString() method on the Track class to get the title and duration in a appropriately formatted string.

Sample solution

There is a sample solution (with source code removed) available to download. If any part of this specification is unclear, check the sample solution and reproduce its behaviour as closely as possible.

Marking

Marks will be awarded as follows:

  • Constructors and accessor methods (except filterByGenre) – 25%
  • addTrack – 15%
  • filterByGenre – 15%
  • printListing – 15%
  • Style – 30%

If the assignment is submitted late, 10% will be taken off the maximum mark per day, to a maximum of 5 days.

Submission instructions

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

  1. From within your assignment 2 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 “Assignment2.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 “assignment2″ from the next drop down menu and press “Upload my Assignment”
  9. Accept the student declaration
  10. Press “Choose File” and select the “Assignment2.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 PlayList.java
    PlayList.java Compiled Successfully
    Checking for Track.java
    Track.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 7 Mid-Semester Practical Exam

Posted by on September 3rd, 2014 · Announcements

In next week’s lab period, we will conduct a practical exam. This worth 10% of your final mark and is intended to give you an idea of how well you are understanding the material in this course. Your tutor’s will set up a special exam environment for you to login to. Once logged in, you will be given instructions on how to proceed. There will be three programming exercises that you will submit in much the same way as you did the first assignment. When you go to the lab, wait outside until the tutor has finished the setup.

You are allowed to bring one A4 page (may be double sided) of your own notes into the exam. It is not open book.

The exam will only be one hour long, so you can use the remaining lab time to catch up with previous tutorials.

Update:
The Mid-semester exam will cover course content up to and including Week 5, and contain three questions.
* Question 1 will be on loops
* Questions 2 will be on arrays
* Questions 3 will required you to create a simple class

Week 6 Lab – Catch Up and More on Classes and Objects

Posted by on August 31st, 2014 · Lab

The self-test quiz for this week is here

Task 1: Catch Up

If you haven’t completed exercises from previous labs, you may use this week’s lab to catch up on unfinished work.

If you are up-to-date, you can carry on with the following exercises.

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 should 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 (if time permits)

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.

 

Update to Assignment 1

Posted by on August 27th, 2014 · Announcements

Please note the clarification to question 3 in assignment 1 that we discussed in the Monday lecture. Both input arrays must be the same length, so you should check that and output an error message if they are not equal.

Change of Lab Locations

Posted by on August 25th, 2014 · Announcements

Due to an issue in the CSE labs in Old Main Building (piano, organ, clavier), classes scheduled in those labs have to be relocated. Although COMP1400 labs are not scheduled there, we have to move two lab classes to accommodate the relocations.

Please take note of the following changes:

  • sanhu tue 16:00 18:00 -> viola
  • sanhu wed 16:00 18:00 -> viola

Viola is located in Scientia Lower Ground.

Lab Week 5 – Designing Classes

Posted by on August 24th, 2014 · Lab

The self-test quiz for this week is here.

Assignment 1

  1. If you haven’t started working on assignment 1, follow the assignment instructions to download the skeleton project.
  2. Make sure you understand what is required. If not ask your tutor.
  3. Assignment submission will be enabled later in the week.

Investigating the TicketMachine:

  1. Download the TicketMachine project we’ve started looking at in lectures. Unzip it on your desktop and open the project with BlueJ.
  2. Create an instance of the TicketMachine class.
  3. Experiment with the addMoney() method. What happens if you:
    • Add 0 cents?
    • Add an negative amount?

    Is this behaviour correct?

  4. Experiment with the purchaseTicket() method. Does it work correctly? Try a number of different tests to make sure.
  5. Open up the source and place a breakpoint at the beginning of the purchaseTicket() method by clicking in the grey bar on the left. A little stop sign should appear.
  6. Call the method again and use the Debugger to step through the code.
  7. Try this with different balances to see how the execution varies.
  8. There is a bug in this method. Can you find it?

Extending the TicketMachine

Let us extend the ticket machine to keep track of the total amount of cash collected and to allow us to change ticket prices.

  1. Add a new field to the class to store the total cash collected.
  2. When does this value change? What methods will you need to modify to track these changes?
  3. Add a new accessor method getTotal() to return the value.
  4. Test your code with a variety of method calls to make sure it does the right thing.
  5. Add a new mutator method setPrice() to allow you to change the ticket price.
  6. What happens if you set the ticket price to zero? a negative number?
  7. Add a conditional statement to set-price to stop the price from being set to a negative value.

Design a Class to represent an E-Book.
It should record the following information:

  • An author
  • An title
  • The number of pages
  • The current page being read.

Implement your class in the following steps:

  1. Create a class definition with an appropriate name.
  2. Add appropriate fields. What types will they have? What names will you give them?
  3. Create a constructor to initialise these fields. What starting values will you use?
  4. Create accessor methods to access these fields.
  5. Create mutator methods to allow the use to:
    • turn pages forwards and backwards
    • jump to a specific page
  6. What should happen if they try to access a page outside of the sensible range? How can you implement this?
  7. Create several different books to test your code. What happens if:
    • Your book has only one page?
    • Your book has no pages?
    • Your book has 1000000000000000000000 pages?
  8. Comment your class to describe what it is for and what each of your methods does.

 

Lab Week 4 – Loops and Arrays

Posted by on August 16th, 2014 · Lab

In this lab, we’ll practice creating a new BlueJ project from scratch. Use the “New” menu item to make a new class. Call it Week4. All the exercises below can be done in the same class file. You may not be able to finish all the questions during the lab time. Make sure that you do at least one question from each section in the lab and finish the rest as homework.

The self-test quiz for this week is here.

Basic Output

1. Write a method that prints the string “Hello World” 8 times. The output should be:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

2. Write a method that prints your name, age and favourite number, using integers for the numbers. For example:

My name is Fred, I am 18 years old, my favourite number is 2.

Simple Calculations

1. Write a method, which takes one parameter n and returns the number squared.

2. Write a method, which takes three parameters a, b and c to calculate the area of a triangle using Heron’s formula: area = sqrt(s(s-a)(s-b)(s-c)). Where s is the “semi-perimeter” given by s = (a+b+c)/2. The sqrt can be calculated by Math.sqrt()

3. Write a method that performs a calculation that you learnt in high school mathematics. The method should take as parameters any variables of the calculations and return the result of the calculation.

4. Write a method that takes a 4 digit number and then prints the number of units, tens, hundreds and thousands. For example:

Number: 1234
4 units
3 tens
2 hundreds
1 thousands

Using conditions

1. Write a method that takes in one parameter year and prints “This is a leap year”, if the year is a leap year or “This is not a leap year” if it is not. Recall that years are leap years if they are divisible by 4. Except years divisible by 100 are not leap years unless they are also divisible by 400.
HINT: the code x % y returns the remainder when x is divided by y

2. Write a method that takes three integers and prints them out in increasing order.

3. Write a method which takes in three real numbers a, b and c representing the coefficients of a quadratic equation ax2 + bx + cand uses the quadratic formula to determine one solution of the equation ax2 + bx + c = 0. Use an if statement to ensure that the program works correctly for all numeric input.

4. Write a method that takes a single integer, n between 0 and 9 and prints out the word for that number. For example

Number: 0
zero
Number: 1
one
Number: 9
nine

Using Loops

1. Write a method, which takes two parameters n and pow and returns the value of npow

2. Write a method with a while loop that prints out the numbers from 1 to 10 and their squares.

3. Change your method in question 1 to use a for loop.

4. Modify your method in question 2 so that is prints “Number is even”, if the number (or its square) is even. Otherwise print “Number is odd”.

5. Write a method that takes one parameter n and outputs an n x n square of asterisks. For example

Square: 5
*****
*****
*****
*****
*****

6. Write a method that takes one parameter n and outputs an n x n triangle. For example

Triangle: 5
    *
   **
  ***
 ****
*****

7. Write a method, which takes one parameter, a positive integer n and prints all the factors of n. For example:

n: 1001
The factors of 1001 are:
1
7
11
13
77
91
143
1001

Using Arrays

1. Write the method, void stars(int[] values) which, for each element of values prints out the number of asterisks corresponding to the integer. For example:

 values: {3, 2, 1, 5, 5, 3, 1, 0, 2}
***
**
*
*****
*****
***
*

**

2. Write the method, boolean isIncreasing (int[] values) which returns true if the values in the array are in increasing order. It should return false otherwise