Create two arrays: String [] names and Double [] prices of size numItems

Assignment 5:

1. It should have the following data members: a. private T[] arr to store different variables of type T b. private int size to store the number of elements in arr

2. Implement the following constructors:

1. public AnyArray() which sets arr to null and size to 0

2. public AnyArray(T[] input) which sets arr as input and size a s

input.length

3. Implement the following methods:

a. public T getIth(int i) which returns the i-th element in arr (you have to check if i is a valid location, else return null)

b. public int findLocation (T query) which checks each element in arr and returns the location of the query in the arr, if found. Else return -1 (Hint: use equals method)

c. public void printArray() which prints each element of arr on separate lines

d. public String toString() which returns a string with the following format: This list has AA elements with the first item: BB and the last item: CC where AA, BB, and CC represent the actual values. (Hint: use method getIth() )

Create an application class named GroceryCart.java :

1. Ask the user to enter how many items are they planning to buy today. Save this number as an int variable numItems

2. Create two arrays: String [] names and Double [] prices of size numItems

3. Now, using a loop, ask the user to enter the name and price of i-th item in the cart and

store them at appropriate locations in the arrays

4. Once the arrays have been create, create two objects of the generic class AnyArray :

5. a. AnyArray<String> nameList = new AnyArray<String>(names);

6. b. AnyArray<Double> priceList = new AnyArray<Double>(prices);

7. Print the information about nameList and priceList using the toString methods

8. Now, ask the user to enter the name of the item they want to search for and store it in a

string variable search . Pass this search item to the method findLocation using the

nameList.

9. If the item is found in the list, print its price using the getIth ( ) method with priceList .

If the item is not found, print ‘ Item not found ’

Sample Execution 1:

Enter the number of items

3

Enter name of the item 0

Apples

Enter price of the item 0

1.99

Enter name of the item 1

Cookies

Enter price of the item 1

3.5

Enter name of the item 2

Bread

Enter price of the item 2

1

Name list has 3 elements with the first item: Apples and the last item: Bread

Price list has 3 elements with the first item: 1.99 and the last item: 1.0

Enter the name of item to search for

Bread

The price of this item is 1.0

Sample Execution 2:

Enter the number of items

2

Enter name of the item 0

Yogurt

Enter price of the item 0

2.5

Enter name of the item 1

Eggs

Enter price of the item 1

2

Name list has 2 elements with the first item: Yogurt and the last item: Eggs

Price list has 2 elements with the first item: 2.5 and the last item: 2.0

Enter the name of item to search for

Apples

Item not found!

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"FIRST15"

Order Now