Exercise 1 (List ADT – Array-based list implementation) Make a new Java Project called Lab7. Then create a new class cal

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Exercise 1 (List ADT – Array-based list implementation) Make a new Java Project called Lab7. Then create a new class cal

Post by answerhappygod »

Exercise 1 (List ADT – Array-based list implementation) Make a
new Java Project called Lab7. Then create a new class called
ArrayIntList that implements an array-based list data structure
that supports adding and removing int-typed elements at the end of
the list. Test your implementation in the main method. Also, state
the time complexity of your method implementations.
Explanation of the List ADT Concept An Abstract Data Type (ADT)
is a set of data and the particular operations that are allowed on
that data. We call it abstract, because it does not specify the
implementation of the operations or the underlying structures used
for containing the data. By defining the data type abstractly, we
can develop algorithms using these types, without having to settle
on a particular implementation. In general, data types may have
different types of implementations and each one has different
advantages and disadvantages. One common ADT is the List ADT. It is
defined as an ordered collection of data, supporting operations of
adding and removing elements from the list at different positions.
Commonly, List ADTs are defined as allowing for addition and
removal of elements from the end of the list, although more types
of operations may be defined as well. A concrete implementation of
a List ADT in Java would have to specify the built-in structures to
use for the implementation, and how the methods will implement the
operations of the list. In general, there are two main data
structures to use for implementing various ADTs: arrays and linked
structures. In this exercise, you will explore the use of
array-based implementation of the List ADT. Our implementation will
be limited to storing only int types in the list, but it is easy to
extend this to support any types by the use of the Java generics
features, which is explained in the textbook.
Implementation steps:
a) Declare two instance variables: one int array that will store
the data collection (call it list), and another int that will keep
track of the last element in the list (call it rear).
b) Add a constructor method that initializes the list array to a
new int array of size 10, and initializes rear to zero.
c) Make a method called add, which should take a single int
value as a formal parameter, and store this value at the rear index
in the list array. Increment the rear variable afterwards to make
sure the next time add gets called, the subsequent value gets
stored in the following index.
d) Make a method called remove, which decrements rear, and
returns the value of the list array at that current rear index.
e) Add a toString method that returns a String, composed of the
concatenation of all array elements from beginning to the last
element (up to rear index). Make sure to separate the values by a
space.
f) Test the functionality of the implementation in the main
method. First, create a new ArrayIntList object. Then, call its add
values. Afterwards, print out the list (call method multiple times
to fill the list with some println actual parameter). Then, do call
its remove with the ArrayIntList reference as an method a few times
and print out the list aga
Afterwards, answer the following questions:
1) What is the time complexity of the add and remove
methods?
2) What happens when we try to add more than 10 values to the
list? How would you address the problem that could result?
3) If the problem in 2 is fixed, how would that change the time
complexity?
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply