Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Just need some help, code is in java using intellij, thanks Task 3/4: Checkpoint 34 For this task you will define and add another class

Just need some help, code is in java using intellij, thanks

Task 3/4: Checkpoint 34

For this task you will define and add another class

Change the main method to contain the following code:

 public class ArrayTask { public static void main (String[] args) { IntList list = new IntList(); list.printList(); list.insertAtEndofList(42); list.printList(); list.insertAtEndofList(2); list.printList(); System.out.println(list.getElementAt(3)); System.out.println(list.getElementAt(-1)); System.out.println(list.getElementAt(1)); list.insertAtEndofList(7); list.insertAtEndofList(9); list.printList(); list.insertAtEndofList(100); } } 

You are now going to complete a new class called IntList (skeleton provided). The aim of the class is to represent a list. It is initially empty but can have elements inserted at the end of it. Since arrays are not extensible, one way to represent the list is to create an array which is long enough for all foreseeable circumstances and to keep track of how much of the array is being used with another variable. Initially this variable will be zero and each time a value is inserted it will become one larger. The class should include the following:

An instance variable named list which refers to an array containing 4 ints.

An instance variable called listLength to keep track of how many elements of the array are being used. It should be initialised to 0.

A method called printList which prints out all the elements in the list. If the list does not contain any elements then "List is empty" should be printed (see example output below).

A method called insertAtEndofList which inserts a value (provided as a formal parameter) at the end of the list. To do this it should use listLength to index the array and assign the value to that element of the array. listLength should then be increased by 1 and a message produced (see example output below). Prior to performing the insertion the method should check that there are unused elements in the array (make use of the length attribute of the array and listLength). If there are not, the insertion should not be performed and an appropriate message should be output (see the last line in the example below).

A method called getElementAt which returns the value at a given index (similar to array indexing). It should check that the index passed as a parameter is that of an element which is being used. For example, after adding two elements to a list (calling insertAtEndofList twice), the only valid indexes will be 0 and 1. If the index is not valid, a message should be printed and 0 returned (see the example output below).

A good strategy to adopt in developing this class is to write and test each method one at a time. To do this you will need to comment out those lines in the main method which call methods which have not been written yet. You may also want to set up the contents of the list manually so, for example, you could write and test printList. e.g.

private int[] theList = {42,2,0,0}; private int listLength = 2; 

Once you are convinced printList is performing correctly you will need to change the above declarations to represent an empty list with a maximum length 4 (instead of explicitly initialising the array). Note: The main method should not be changed from that given above. The final output of your program should be: List is empty 42 inserted Printing list... Element 0 = 42 2 inserted Printing list... Element 0 = 42 Element 1 = 2 Invalid index: 3 0 Invalid index: -1 0 2 7 inserted 9 inserted Printing list... Element 0 = 42 Element 1 = 2 Element 2 = 7 Element 3 = 9 Insertion of 100 failed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions