Dr Marina De Vos
Department of Computer Science
Tuesday, 20-Nov-2018 19:58:25 GMT

## Programming I (CM10134 - CM50147): Lab 4

Carry out the following exercises from the BlueJ book, chapter 4 "Grouping Objects".

You will need to type up your answers to exercises 4.2, 4.4, 4.23, 4.42, 4.43, 4.44, 4.45, 4.47, and 4.53 (brief answers are sufficient).
You will also need to show your code to 4.3, 4.5, 4.8, 4.11 , 4.15, 4.16, 4.17, 4.24, 4.46, 4.48, 4.51, 4.52 and the Grid and CircleList exercises.

• Using the notebook1 project carry out exercises 4.2 - 4.5, 4.8, and 4.15 - 4.17

• Using the auction project carry out exercises 4.23 and 4.24

• Exercises 4.42, 4.42, 4.44

• Using the weblog-analyzer project carry out exercises 4.45 - 4.48 and 4.51 - 4.53 (LogAnalyzer class)

• Look up do-while loops and switch-case statements in the section Control Flow Statements at http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html

• Multi-dimensional arrays are arrays with more than one subscript e.g., the following array has three dimensions: private int [][][] 3Dgraph;

Create a class called Grid that contains a 2-dimensional array of rows and columns. Write a method that takes a row and a column as a parameter and enters a String into that position. Print out the grid so that the Strings are laid out as a 2D grid.

• Arrays and ArrayLists are two examples of collections objects. A Vector object is another example. The Vector class is defined in the java.util package, so you need to include the import statement import java.util.Vector; to use the Vector class.

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.

Important methods are:
Vector(): Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
Vector(int initialCapacity, int capacityIncrement): Constructs an empty vector with the specified initial capacity and capacity increment.

void clear(): Removes all of the elements from this Vector.
Object elementAt(int index): Returns the component at the specified index.
boolean isEmpty(): Tests if this vector has no components.
void add(int index, Object element): Inserts the specified element at the specified position in this Vector.
void addElement(Object obj): Adds the specified component to the end of this vector, increasing its size by one.
void removeElementAt(int index): Deletes the component at the specified index.
int size(): Returns the number of components in this vector.

Write a toString() method for the Circle class from chapter 1 that prints out the state of a circle object. Create a new project that contains the Circle class and a new class, CircleList, that will use a Vector to store circles. You will also need the Canvas class from the Shapes project if you want to use the makeVisible() method.

CircleList should have a constructor that creates a vector using Vector(). It should have a method that adds the circles to the end of the vector. Another method should add a circle object to a specified index. A third method should remove a circle from a specified index. A final method should clear the vector. At the end of each method print out the details (state) of each of the objects in the vector using the toString() method as well as their index (this should be a separate printDetails() method). Make use of each of the above methods in your code. For more information on the Vector class go to http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html