Lab 03
Lab 03
Lab Assignment 03
2020 – 2021 Spring, CMPE 211 Fundamentals of Programming II
In this lab, you are going to complete classes that use an array of integers dynamically. You are asked to do
the following tasks:
File IntegerList.java contains a Java class representing a list of integers. The following public
methods are already provided:
IntegerList(int size): creates a new list of size elements. Elements are initialized to 0.
void randomize(): fills the list with random integers between 1 and 100, inclusive.
It is often necessary to add items to or remove items from a list. A more common strategy is to choose an
initial size for the array and add elements until it is full, and then increase its size and continue adding
elements until it is full, and so on.
Add this capability to the IntegerList class. You will need to add a new increaseSize() method for
the purpose of increasing the capacity of the list by 1 element. Also, you will need to add instance variables
to hold current number of integers in the list and the current size of the array. However, you are not allowed
to use any available methods from Java libraries.
Add a method void addElement(int newVal) to the IntegerList class that adds an element to
the list. At the beginning of addElement, you should check to see if the array is full. If so, call
increaseSize() before you do anything else.
Add a method void removeAll(int newVal) to the IntegerList class that removes all
occurrences of a value from the list. If the value does not appear in the list, it should do nothing (but it’s not
an error). If the duplicate values appear in the list, all occurrences of them should be removed. Note that the
array values do need to remain contiguous, so when you remove a value you will have to shift/copy
everything after it down to fill up its space. Also remember to decrement the necessary variables that keep
track of the number of elements and current capacity. Hint: You can make use of another reduced size array
and your own extra helper function to find the index/indices of given removable integer value.
1
Computer Engineering Department
File IntegerListTest.java contains a Java program that provides menu-driven testing for the
IntegerList class. Add extra options to the menu in IntegerListTest to test your two new methods
(addElement and removeAll) from Task 1.
You can see expected program run (demo) from the link below:
https://asciinema.org/a/nzI5Z3L7RGB8e1VnpoI9nubP9
Submission
You need to implement all the required tasks above and upload your Java source files (.java) as a single Zip
file into the available Moodle submission.