CS 1054: Lab 5
Lab meeting 5: Grouping Objects
This lab is supposed to help you understand grouping of objects in Java and
using looping to traverse through the items in the collection. The predefined
classes that we will use in this lab include ArrayList and arrays.
Using the ArrayList class
-
Open the BlueJ environment. This can be done by clicking on Start -> BlueJ
or Start -> All Programs -> BlueJ -> BlueJ.
-
Create a new project. Go to the Project menu and select "New Project...."
-
You might want to have a copy of the
Chapter 4: Grouping objects
notes open in another window (Shift+Click to open in another window).
-
Add a new class to the project by clicking on the "New Class" button on the
left. Name this class "CollectionsLab".
-
Add a new private variable called
numsgroup
of type
ArrayList.
Refer to your notes to "import" the appropriate package to use this class.
-
To get more information about this predefined class, include what methods it
supports and what the parameters for these methods are, refer to the
standard Java API documentation
(Shift+Click to open in another window). In the bottom-left frame, find the
link corresponding to ArrayList and click on it. The details about that class
should be revealed in the right frame.
-
Your task is to implement a few methods within this class.
-
CollectionsLab()
– this is the constructor where you create the new object "numsgroup" that you
defined earlier.
-
addNumber()
- this method accepts an
int
and adds it to the numsgroup collection. You can use the
add
method of the ArrayList class to do this. However, if you refer to the Java
documentation for ArrayList, you see that the add() method requires an
Object. int is a primitive data-type and not an Object.
However, Java provides an Integer class corresponding to the int primitive data
type. You can use this class to create an Integer object like this:
numsgroup.add(new
Integer(x));
where x is the integer to be added to the collection.
-
getNum()
- this method accepts an
int
which is the index number of the item to be retrieved from the ArrayList. It
should return an int. We can use the
get()
method of ArrayList to retrieve the item from the collection. However, it
returns the item of type Object. We need to extract an int from this. To do
this, you need to cast the Object as an Integer and then use the
intValue()
method of the Integer class to extract an int.
-
printWhileInts()
- this method should iterate through all elements in numsgroup and print all
elements to the screen using a
while loop.
-
printIterInts()
- this method should iterate through all elements in numsgroup and print all
elements to the screen using the Iterator class. Refer to your notes to see how
to use this class.
-
printForInts
- this method should iterate through all elements in numsgroup and print all
elements to the screen using a
for loop.
Remember that when a method returns a value, the prototype must show the return
type and the last line of the method should be a return statement. Methods that
set variables or print values do not return any value and should have a return
type of “void.”
-
Test your class by creating one object, entering approximately 10 numbers and
then applying the various methods.
-
Show your work to the GTA.
Using arrays
-
Create a new project. Go to the Project menu and select "New Project...."
-
Add a new class to the project by clicking on the "New Class" button on the
left. Name this class "ArraysLab".
-
We saw in class one way of declaring an array. For example
int[] nums;
declares an int array called nums. To create the actual array, we use
the following type of syntax:
nums =
new int[10]
to create an array of 10 elements. One other way of declaring and
initializing arrays is the following
int[] nums = {1, 2,
3, 4, 5};
This stores the numbers given in the parentheses into the array
nums.
-
In your ArraysLab class, declare and initialize a private int array called
firstten
with the numbers {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
-
In the constructor method (without any parameters), write a for loop to access
and print out these numbers from the array one by one.
-
Show your work to the GTA.
-
Do the following tasks if there is still time remaining.
-
Declare a new private ArrayList variable called
al.
-
Write a method,
copyArraytoAL
in which you extract each item from the array and insert it into al.
-
Use the
printIterInts
method from the previous class to print the elements from the al object.
-
Show your work to the GTA.
© Mir Farooq Ali 2003.