Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some assistance with my java assignment. Comments would be appreciated. In this assignment you will implement a GroceryList class that allows a person to

Need some assistance with my java assignment. Comments would be appreciated.

In this assignment you will implement a GroceryList class that allows a person to create and manipulate a grocery list, which is an implementation of the concept of a list. At any given time the list may be partially filled, and your program must keep track of how many values are ACTUALLY in the grocery list, versus the maximum number of elements that the list can hold. This project will be implemented and delivered in 2 parts.

Part 1: The GrocerList class & Class GroceryListException

Class GroceryList is an implementation of the concept of a list. It does not adhere to the List interface that is part of the Java API. The UML on the following page gives the specification for the class.

In Part 1, you will implement the GroceryList class which MUST encapsulate and manipulate an array of Strings which holds the individual elements of the list.

Additionally you will download and use the GroceryListException class to throw exceptions if at any time an invalid operation is performed on the list. Any violations to stated preconditions should result in an exception

GroceryList

- elements[]: array of Strings - currentSize: int - maxSize: int

+ GroceryList(in maxSize: int): constructor + getSize(): int

+ getMaxSize(): int + isEmpty(): boolean + isFull() : boolean + isValidPositionToInsert(in position: int): boolean + isValidPositionToDelete(in position: int): boolean + add( in newItem: String): void + add( in position: int, in newItem: String):void + remove(): void + remove( in position: int): void + get( in position: int): String + toString(): String + sort ():

public GroceryList( int maxElements): [constructor] : Creates a new instance of the GroceryList with no elements.

/**PreConditions: maxElements > 0 **/ /** Postconditions: maxSize = maxElements, elements = new String[maxElements],

currentSize =0 **/ /** Exceptions: maxElements <=0 **/

public int getSize(): This method returns the number of items currently in the list. /**PreConditions: none **/ /** Postconditions: none **/ /** Exceptions: none **/

public int getMaxSize(): this method returns the maximum number of items the list can hold. /**PreConditions: none **/ /** Postconditions: none **/ /** Exceptions: none **/

public void add( String newItem): This method will add an item to the end of an existing list.

/**PreConditions: isFull() == false **/ /** Postconditions: elements[currentSize] = newItem, currentSize += 1 **/ /** Exceptions: isFull() == true **/

public void add( String newItem, int position) This method will add a new value to an existing list at a specified position. /**PreConditions: isFull() == false **/ /** Postconditions: elements[position] = newItem, (all elements at index > position are shifted up one elements) , currentSize += 1 **/

/** Exceptions: isFull() == true OR position < 0 OR position > currentSize **/

remove() This method removes the item at the end of the list. /**PreConditions: isEmpty() == false **/ /** Postconditions: elements[currentSize-1] = null, currentSize -= 1 **/ /** Exceptions: isEmpty == true **/

remove( int position) This method removes and item at a specified position. /**PreConditions: isEmpty() == false AND position >=0 AND position < currentSize**/ /** Postconditions: (Elements at an index greater than position are shifted down one index) , elements[currentSize-1] = null, currentSize -= 1 **/ /** Exceptions: isEmpty == true **/

public String get( int position) This method will retrieve and return the item at a specific position.

/**PreConditions: isEmpty() == false AND position >=0 AND position < currentSize**/ /** Postconditions: return value = elements[position] **/ /** Exceptions: none **/

public String toString(): This method returns a string containing the value stored in every elements. This MUST be produced using the method Arrays.toString /**PreConditions: none **/ /** Postconditions: none **/

/** Exceptions: none **/

public void sort(): This method performs a bubble sort on the contents of the array to sort the array contents in ascending order . YOU MAY NOT USE THE SORT METHOD FROM CLASS ARRAYS TO PERFORM THIS OPERATION. /**PreConditions: isEmpty == false **/

/** Postconditions: elements sorted in ascending order **/ /** Exceptions: isEmpty == true **/

public boolean isFull(): this method returns true if every element in the list is filled. /**PreConditions: none **/ /** Postconditions: returns true if currentSize == maxSize **/ /** Exceptions: none **/

public boolean isEmpty(): this method returns true if every element in the list is empty /**PreConditions: none **/ /** Postconditions: returns true if currentSize == 0 **/ /** Exceptions: none **/

13. public boolean isValidPositionToInsert(int position): validates if a new element can be inserted at a specified position. /**PreConditions: none **/ /** Postconditions: returns true if 0 <= position <= currentSize **/

/** Exceptions: none **/

14. public boolean isValidPositiontoDelete(): validates if an element can be removed from a specified position /**PreConditions: none **/ /** Postconditions: returns true if 0 <= position < currentSize **/

/** Exceptions: none **/

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

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions