MyArrayList
Lab 4 (CS1101 AY2008/9 Semester 1)
This lab assignment is graded.
Date of release: 14 October 2008, Tuesday, 23:59.
Submission deadline: 22 October 2008, Wednesday, 23:59.
School of Computing, National University of Singapore

0 Introduction

(Return to Labs page.)

This is your fourth graded lab. The final lab will be released on 28th October.

This lab requires you to do ONE exercise. The second exercise is not graded and is therefore optional. You are advised to review the material covered in chapters 1 through 9, and chapter 10 (sections 10.1 through 10.6), and read Programming Style, Design and Marking Guidelines before attempting this lab assignment. You should not use syntax or constructs not covered in lectures; you shouldn't need to and may even be penalized for doing so (if the objective of the assignment is undermined). Unless a template is given, you should start each program afresh (ie. don't cut and paste and modify) so that you get sufficient practice with writing a complete Java program.

A word of advice: Code incrementally. Don't try to finish all parts of a program then compile it. Write your program in bits and pieces and compile frequently. Try to maintain a compilable program even while you are working on it. Submitting a compilable program that partially works is better than submitting an un-compilable one. This last point especially applies to the programming exams. Seriously, code incrementally.

Note that:

For this lab, the number of submissions is set to 10. For subsequent labs, it may be reduced. Only your last submission will be graded.

If you have any questions, you may post your queries in the IVLE forum of your respective lecture group. Do not post your programs (partial or complete) in the forum before the deadline!

Announcements: We have release last year's lab write-ups, the test data for Labs #1 to #3, and the solution for Lab #2 Ex 2 (CodeIndenter.java), all on this website: CS1101X lab page.


1 Exercise 1: MyArrayList (100%)

1.1 Learning objectives

1.2 Task

As you have learned during the previous weeks, arrays allow you to work with an ordered list of related data. Arrays work great for many lists, but if you have a list where the number of elements is hard to predict, they don't work so well.

In this exercise, you are to write a class MyArrayList which allows any number of elements in the list. In your MyArrayList class, you should use an Array of ListItem objects to store elements. The ListItem class is an object which only stores a single attribute "name". You are also provided with a MyArrayListDriver class which contains a main method and provides the input and output to test your MyArrayList class. You should not change the code in MyArrayListDriver.java and ListItem.java.

Like an array, the items in your MyArrayList class are ordered. This means that if there are 10 items in the list, the first item can be retrieved using myList.get(0) and the last item can be retrieved using myList.get(9). If an item is removed using the myList.remove() method, then all items on the right of the item should be shifted to the left. For example, if myList.remove(6) is called, the last item can now be retrieved with myList.get(8).

Before we start we need to define two terms: capacity and size. The capacity of MyArrayList is denoted by the size of the array inside the class (equivalent to array.length). The size of MyArrayList denotes the number of elements currently stored in the list. Naturally at all times, size <= capacity. However, at a point in time, when the size = capacity and the user (of your list) tries to add another ListItem, you should then double the capacity of your MyArrayList class automatically.

(What described above is a prelude to the ArrayList class which you will study in the coming week. We hope you would appreciate it after doing this lab exercise.)

The operations necessary for this task are specified below:

Constructor Summary
MyArrayList() 
          Constructs an empty list with an initial capacity of ten.
MyArrayList(MyArrayList list) 
          Constructs a list containing the elements of the specified collection, in the order they are found in the list.
MyArrayList(int initialCapacity) 
          Constructs an empty list with the specified initial capacity.
 
Method Summary
 void add(int index, ListItem elem) 
          Inserts the specified element at the specified position in this list.
 void add(ListItem elem) 
          Appends the specified element to the end of this list.
 void addAll(MyArrayList list) 
          Appends all of the elements in the specified MyArrayList to the end of this list, in the order they are found in the list.
 void addAll(int index, MyArrayList list) 
          Inserts all of the elements in the specified MyArrayList into this list, starting at the specified position.
 void clear() 
          Removes all of the elements from this list.
 MyArrayList clone() 
          Returns a copy of this ArrayList instance.
 boolean contains(ListItem elem) 
          Returns true if this list contains the specified element testing for equality using the equals method.
 ListItem get(int index) 
          Returns the element at the specified position in this list.
 int indexOf(ListItem elem) 
          Searches for the first occurence of the given argument, testing for equality using the equals method.
 boolean isEmpty() 
          Tests if this list has no elements.
 int lastIndexOf(ListItem elem) 
          Returns the index of the last occurrence of the specified ListItem in this list.
 ListItem remove(int index) 
          Removes the element at the specified position in this list.
 ListItem set(int index, ListItem elem) 
          Replaces the element at the specified position in this list with the specified element.
 int size() 
          Returns the number of elements in this list.

 

Constructor Detail

MyArrayList

public MyArrayList(int initialCapacity)

Constructs an empty list with the specified initial capacity.

Parameters:

initialCapacity - the initial capacity of the list.

Notes:

You may assume that initialCapacity is always a positive integer.

 


MyArrayList

public MyArrayList()

Constructs an empty list with an initial capacity of 10.


MyArrayList

public MyArrayList(MyArrayList list)

Constructs a list containing the elements of the specified collection, in the same order they are found in the list. The MyArrayList instance has an initial capacity of twice the size of the specified collection.

Parameters:

list - the collection whose elements are to be placed into this list.

Notes:

You may assume the list has always at least one element.

 

Method Detail

size

public int size()

Returns the number of elements in this list.

Returns:

the number of elements in this list.


isEmpty

public boolean isEmpty()

Tests if this list has no elements.

Returns:

true if this list has no elements; false otherwise.


contains

public boolean contains(ListItem elem)

Returns true if this list contains the specified element testing for equality using the equals method.

Parameters:

elem - element whose presence in this List is to be tested.

Returns:

true if the specified element is present; false otherwise.
 

Notes:

You may assume that elem is never null.


indexOf

public int indexOf(ListItem elem)

Searches for the first occurence of the given argument, testing for equality using the equals method.

Parameters:

elem - a ListItem.

Returns:

the index of the first occurrence of the argument in this list; returns -1 if the object is not found.

Notes:

You may assume that elem is never null.


lastIndexOf

public int lastIndexOf(ListItem elem)

Returns the index of the last occurrence of the specified object in this list.

Parameters:

elem - the desired element.

Returns:

the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.

Notes:

You may assume that elem is never null.


clone

public MyArrayList clone()

Returns a copy of this MyArrayList instance. (The elements themselves are not copied.).

Returns:

a clone of this ArrayList instance.


get

public ListItem get(int index)

Returns the element at the specified position in this list.

Parameters:

index - index of element to return.

Returns:

the element at the specified position in this list.

Notes:

You may assume index is always within range (index >= 0 && index < size()).


set

public ListItem set(int index, ListItem elem)

Replaces the element at the specified position in this list with the specified element.

Parameters:

index - index of element to return.

element - element to be stored at the specified position.

Returns:

the element previously at the specified position.

Notes:

You may assume index is always within range (index >= 0 && index < size()).


add

public void add(ListItem elem)

Appends the specified element to the end of this list.

Parameters:

elem - element to be appended to this list.

Notes:

You may assume that elem is never null.


add

public void add(int index, ListItem elem)

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:

index - index at which the specified element is to be inserted.

elem - element to be appended to this list.
 

Notes:

You may assume index is always within range (index >= 0 && index <= size()).

You may assume that elem is never null.


remove

public ListItem remove(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Parameters:

index - the index of the element to removed.

Returns:

the element that was removed from the list.

Notes:

You may assume index is always within range (index >= 0 && index < size()).


clear

public void clear()

Removes all of the elements from this list. The list will be empty after this call returns.


addAll

public void addAll(MyArrayList list)

Appends all of the elements in the specified MyArrayList to the end of this list, in the order that they are found in the specified list.

Parameters:

list - the elements to be inserted into this list.

Notes:

You may assume the list has always at least one element.


addAll

public void addAll(int index, MyArrayList list)

Inserts all of the elements in the specified MyArrayList into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the same order they are found in the specified list.

Parameters:

index - index at which to insert first element from the specified collection.

list - elements to be inserted into this list.

Notes:

You may assume index is always within range (index >= 0 && index <= size()).


Before you read further, note that the input and output statements have already been coded for you in MyArrayListDriver.java.

The input consists of 1 or more lines which MyArrayListDriver will read in and perform the specified operations on your MyArrayList class. The last line is a quit command with no parameters. Once the quit command is read in, the driver will print out the final state of the MyArrayList class by using the toString method in the MyArrayList class (which has already been written for you). Each item is a single word (without any spaces). The commands are as follows:

new <size> If a parameter is specified (e.g. new 5) this command would create a new MyArrayList instance with a capacity of 5. Otherwise (e.g. new) it creates a new MyArrayList instance with a default capacity.
add <item1> This will add a single item.
addi <index> <item> This adds the item which is defined by item to the position at index.
addm <item1> .... <item n> This adds the items which are defined by item 1 to item n at the end of the list.
addmi <index> <item 1> ... <item n> This adds the items which are defined by item 1 to item n to the position at index.
remove <index> This removes the item specified at the index.
set <index> <item> This replaces the existing item specified at the index.
clear This empties the list.
clone This creates a duplicate of the list.
contains <item> This checks if the item is present in the list.
indexOf <item> This returns the first occuring position of the item
lastIndexOf <item> This returns the last occuring position of the item
quit This terminates the program

Every single command in the program produces a single line of output. The output consists of the return value (if any) and the contents of the array separated by a space. Again, the output has already been coded for you, so you only have to worry about the correctness of your implementation. This is better explained in the sample output below.

You are advised to look at the main method implementation if you have any doubts about the commands listed above.

In summary, take note of these points:

 

1.3 Sample run

Sample run using interactive input (user's input shown in blue; output shown in bold purple).
(The first 2 statements are commands you issue to compile and run your programs in command-line environment, so they are not part of the output.)

$ javac MyArrayListDriver.java
$ java MyArrayListDriver
add 111
[111] 
add 222
[111,222] 
add 333
[111,222,333] 
add 444
[111,222,333,444] 
quit 
[111,222,333,444]

Another sample run is shown below

$ javac MyArrayListDriver.java
$ java MyArrayListDriver
addm The cow jumped over the moon
[The,cow,jumped,over,the,moon] 
addi 1 pink
[The,pink,cow,jumped,over,the,moon] 
addmi 0 Without a doubt
[Without,a,doubt,The,pink,cow,jumped,over,the,moon] 
remove 4
pink [Without,a,doubt,The,cow,jumped,over,the,moon] 
quit 
[Without,a,doubt,The,cow,jumped,over,the,moon] 

Another sample run is shown below

$ javac MyArrayListDriver.java
$ java MyArrayListDriver
addm hello world
[hello,world] 
set 0 goodbye
hello [goodbye,world] 
new 5
[] 
add the
[the] 
add cow
[the,cow] 
add jump
[the,cow,jump] 
add over
[the,cow,jump,over] 
add the
[the,cow,jump,over,the] 
add moon
[the,cow,jump,over,the,moon] 
quit
[the,cow,jump,over,the,moon]

Another sample run is shown below

$ javac MyArrayListDriver.java
$ java MyArrayListDriver
addm hey diddle diddle
[hey,diddle,diddle] 
addm the cat played the fiddle
[hey,diddle,diddle,the,cat,played,the,fiddle] 
addm the cow jumped over the moon
[hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
contains donkey
false [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
contains cat
true [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
contains dog
false [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
indexof donkey
-1 [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
indexof cat
4 [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
indexof dog
-1 [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
indexof the
3 [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
lastindexof the
12 [hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon] 
quit 
[hey,diddle,diddle,the,cat,played,the,fiddle,the,cow,jumped,over,the,moon]

You can try your own sample runs here:

1.4 Submission

Submit your program through CourseMarker.

1.5 Important notes


2 Exercise 2: BetterShoppingCart (0%)

2.1 Learning objectives

2.2 Task

In lab 1, you were tasked to create a simple shopping cart that could only accept three sets of input. Now that you have learnt OOP and Java constructs such as Arrays, it is time to apply what you have learnt to improve your Shopping Cart.

The SimpleShoppingCart is unrealistic and not applicable in real situations.  We now want to improve our application so that it can now take in an unlimited number of items. The user should now also be able to edit or delete any items they choose.

To accomodate the changes required, we are required to change a line of output. Instead of prompting the user "Do you wish to add more items? (y/n):" we shall now prompt "Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): ". The user will select his option by typing in 'a' or 'e' or 'd' or 'q' then pressing enter which corresponds to add, edit or delete or quit respectively.

You may assume that a user will only edit or delete items that are in the shopping cart.

The program should then query the user as follows for the different options:

a Enter item name: Ruler
Enter cost for each item: 100
Enter quantity: 1
e Enter item name: Ruler
Enter new cost: 100
Enter new quantity: 1
d Enter item name: Ruler
Item deleted
q Total: $x.yz

Your output should be a single line printing the total cost of all items (as in lab 1).

Again, the input and output statements in the main method have already been coded for you and you are not required to change it. You are only required to implement the static methods defined in ShoppingCartDriver.java and code the ShoppingCart and CartItem class.

As in exercise 1, you should not be using any of the Java Collection classes other then Arrays. However since this exercise is ungraded, we can't penalize you if you choose to do otherwise.

 

2.3 Sample run

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ javac ShoppingCartDriver.java
$ java ShoppingCartDriver
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): a 
Enter item name: Ruler 
Enter cost for each item: 10 
Enter quantity: 1 
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): a 
Enter item name: Pencil 
Enter cost for each item: 100 
Enter quantity: 1 
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): q 
Total: $1.10

Another sample run is shown below:

$ javac ShoppingCartDriver.java
$ java ShoppingCartDriver
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): a 
Enter item name: Ruler 
Enter cost for each item: 10 
Enter quantity: 1 
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): e 
Enter item name: Ruler 
Enter cost for each item: 100 
Enter quantity: 1 
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): q 
Total: $1.00 

Another sample run is shown below:

$ javac ShoppingCartDriver.java
$ java ShoppingCartDriver
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): a 
Enter item name: Pencil 
Enter cost for each item: 100 
Enter quantity: 100 
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): a 
Enter item name: Ruler 
Enter cost for each item: 200 
Enter quantity: 1
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): d
Enter item name: Pencil 
Please state your choice ([a]dd item, [e]dit item, [d]elete item, [q]uit): q 
Total: $2.00

You can try your own sample runs here:

Note that the input are the text in blue! So if you want to add a pencil which cost 100 cents with 100 quantity, to generate an output your should type your input in the textbox as follows:

a
pencil
100
100
q

2.4 Submission

Submit your program through CourseMarker. Note that this exercise will not be graded.

2.5 Important notes


3 Deadline

The deadline for handing in all programs is 22 October 2008, Wednesday, 23:59. Late submissions will NOT be accepted.




Mon Oct 20 18:50:28 SGT 2008