COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Week 9 Quiz – what to revise

Posted by on September 21st, 2013 · Announcements

Many students have been asking what they should focus their study on for each quiz.

This week’s quiz is based on ArrayLists. Having completed and understood all of last week’s lab work you should be able to achieve ‘satisfactory’ on this week’s quiz. You may also want to revise the methods in your first assignment, the ‘Returning to Ticket Machine’ lab and do some practice using arrays and loops.

Wk8 Lab Solutions and Extra Questions

Posted by on September 19th, 2013 · Uncategorized

Here are my solutions to your Week 8 ArrayList and Classes lab. I have added two extension questions. I added comments so that you may follow along with the logic.

I strongly suggest that you try these two questions yourself before checking my solutions. The questions are:

Question 1

Write a method ‘newVIPList’ that takes in an array of strings of VIP member names in the club. The method should take all of the members in the existing club with matching names and return a new array list of members containing only these VIP members.

Question 2

Write a method ‘removeOldMembers’ that takes in a year cut off and removes all the members in the club who joined in a year earlier than the cut off year. This method should not return anything but manipulate the array list field of the club itself.

Please remember these are extension questions that are good practice to test your understanding of ArrayLists and Arrays.

Please Note: I have also included a test method that creates an array of members given a specified size. Feel free to use this and do not worry about the line: String.valueOf(i). It has not been covered in the course thus far and you are not expected to know it.

 

DOWNLOAD FILES HERE:

http://www.cse.unsw.edu.au/~tlen803/infs1609/

Week 8 Lab – ArrayLists and Classes

Posted by on September 15th, 2013 · Lab

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 – Classes and Objects

Posted by on September 5th, 2013 · Assignments

NOTE: check this web site regularly for updates and clarifications to the assignment specification.

Due Date: Sunday week 8 (23:59:59 22 September 2013)

ChangeLog:

  • 11-09-2013: Submission for Assignment 2 enabled

Task

Suppose you are designing a program for an automated teller machine. The ATM generates transactions that are sent to the banks central computer for processing.

In this assignment, you will create two classes, “Account” and “Transaction”. An Account object should have a unique account number, which can be represented by an integer, and a balance. Initially the balance is zero. A Transaction object should have an amount being transacted and a reference to the Account class associated with the transaction.

Here is an example of the two classes being invoked:

/**
 * This class contains a main method that calls methods in the classes you will write.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class RunTransactions
{
    static void main()
    {
       // Create two new accounts with the given account numbers
       Account fred = new Account(1234);
       Account jim = new Account(6778);

       // Provide accessor methods for account information.
       int accountNumber = fred.getAccountNumber();
       float balance = fred.getBalance();

       // Transactions consist of an account reference and an amount
       Transaction t1 = new Transaction(fred, 20);
       Transaction t2 = new Transaction(jim, 10);
       Transaction t3 = new Transaction(jim, -20);

       // Transactions must contain a "process" method that is called to
       //  actually perform the transaction.
       // A transaction should not be allowed if it results in a negative balance.
       t1.process();
       t2.process();
       t3.process();

       // Print out a report of the account balance.
       // The format should be like this: Account 6778 has balance $20.0
       fred.report();
       jim.report();
    }
}

Download this file. It contains a BlueJ project with just the RunTransactions class. You must create your own Account and Transaction classes. You can create whatever fields and methods you think appropriate but you must provide methods that can be called exactly as above.

The report method should print out a simple message giving the account number and balance. The format should be

       Account <account number> has balance $<account balance>

For example, the output of the reports from the above sample code should be:

       Account 1234 has balance $20.0
       Account 6778 has balance $10.0

You will learn about main methods and the static qualifier in later lectures. For now, you only need to know that when you right click on the RunTransactions class, you will see an item in the menu void main(). Clicking on this will run the main method, which will call your code.

You may change the main method to test your code with different combinations of accounts and transactions. Only remember that you must use exactly the same names of classes and methods and the types of arguments are return values must be identical.

When you submit your assignment, we test your code with different main methods.

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”. Note COMP1400 students may need to enter “COMP1400” instead of selecting COMP1400 if the assignment is not listed
  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 Account.java
Account.java Compiled Sucessfully
Checking for Transaction.java
Transaction.java Compiled Sucessfully
Sucessfully compiled against sample RunTransactions.java

All files are OK
  1. If the page doesn’t load properly, that is OK just submit the assignment file again.
  2. If you see the final line, everything is good. Otherwise 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 (“Account” and “Transaction”) the submission file (“assignment1.jar”) or the submission will not work.
  3. If you have any problems with submission, email the class account

Marking Scheme

The maximum mark for this assignment is 10. Marks will be awarded as follows:

Performance (6 marks)
– for a program that produces the correct results under all tests
Style (2 marks)
– for the structure of your classes, layout and readability
Comments (2 marks)
– for a well commented program

Having trouble with this week’s lab?

Posted by on September 2nd, 2013 · Uncategorized

If you’re having trouble with this week’s lab, take a look at the following example and step through the ‘main’ method inside the ‘Main’ class.

Using Objects Demo

Mid-Term Practical Exam in Week 7

Posted by on August 30th, 2013 · Announcements

A practical exam will be held in the first hour of next weeks lab. You will be given several programming exercises that you will do on the computer and submit using the same give system that you used to submit the first assignment.

The full specification will be given in the lab for next week.

Week 6 Lab – More Classes and Objects

Posted by on August 30th, 2013 · Lab

Task 1: Universities

This first task should be attempted before coming to the lab.

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

 

Extra Exercises on Loops and Arrays

Posted by on August 26th, 2013 · Uncategorized

If you’d like some extra practice writing methods involving loops and arrays, take a look at the following two links:

Loops Exercises

Arrays Exercises

 

Lab Week 5 – Arrays and Classes

Posted by on August 22nd, 2013 · Uncategorized

If you didn’t finish last week’s exercises, continue on with those. In particular, make sure you understand how to use loops and arrays (i.e. the last two exercises).

Classes

Investigating the TicketMachine:

  1. Download the TicketMachine project we have been building 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?
  4. Is this behaviour correct?
  5. Experiment with the purchaseTicket() method. Does it work correctly? Try a number of different tests to make sure.
  6. 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.
  7. Call the method again and use the Debugger to step through the code.
  8. Try this with different balances to see how the execution varies.
  9. 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.

 

Assignment 1 – Loops and Arrays

Posted by on August 15th, 2013 · Assignments

Due: 11:59pm Sunday 1 September (i.e. the end of week 5)

This assignment is about loops arrays. You will write three methods to manipulate arrays. The first two are easy. The third one will require more thought about the design. You will submit the assignment using the CSE give system (see below).

Download the BlueJ project, Assignment1.zip. It contains three methods. You have to write the code that goes inside them.

Question 1

The first method finds the average of the elements of an integer array:

public double average(int[] data)

That is, given an integer array, data, calculate what the average of its elements are and return the average value. For example, the average of {1, 3, 2, 5, 8} is 3.8.

Question 2

The second method is:

public int countInRange(int[] data, int lo, int hi)

For this, you have to count the number of elements of the array, data, that lie in the range lo to hi inclusive, and return the count. For example, if data is the array {1, 3, 2, 5, 8} then the call

countInRange(data, 2, 5)

should return 3 because there are three elements, 3, 2 and 5 that lie in the range 2 .. 5.

Question 3

The third method, arrayAdd, takes two arrays as parameters and returns a new array that is the sum of the first two.

public int[] arrayAdd(int[] R1, int[] R2)

The addition of two arrays yields an array whose elements are the sum of the corresponding elements in the parameter arrays, i.e.

R1[0] + R2[0], R1[1] + R2[1], R1[2] + R2[2], ....

Submission instructions

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

  1. From within your assignment 1 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 “Assignment1.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”. Note INFSi1609/2609 students may need to enter “COMP1400″ instead of selecting the course form the dropdown
  8. Select “assignment1″ from the next drop down menu and press “Upload my Assignment”
  9. Accept the student declaration
  10. Press “Choose File” and select the “Assignment1.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 Assignment1.java
    Assignment1.java Compiled Sucessfully
    
    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