Question

In: Computer Science

The purpose of this assignment is to develop your ability to code and understand ArrayLists. We...

The purpose of this assignment is to develop your ability to code and understand ArrayLists. We will do this by taking a do-it-yourself (DIY) approach by building our own ArrayList-Like data structure called "ArrayBox". You must:

Create a generic class called ArrayBox that uses an ARRAY to store its objects. Assume the initial size of the array is two(2). Your ArrayBox must automatically double the size of your elements array when it becomes full as described in class (see slides). Your ArrayBox declaration should look similar to this:

public class ArrayBox<E>
{
private E[] elements = (E[])(new Object[2]);
private int end_idx = 0;
}

within this class implement the following public methods declared EXACTLY AS:

public int size()

public boolean add(E e)

public E get(int index)

public E set(int index, E element)

public E remove(int index)

You may add additional methods as you wish to modularize the code better but the above methods must be in your class.

Create a Driver class called ArrayBoxDriver (this will contain your main method). Have this class create an instance of ArrayBox of data type <String> and call a mainMenu() method:

then produces a "menu" that looks similar to this:

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>>

Testing Script (Total 10 marks)

This output script will test:

public boolean add( E item )

Dynamic Array resizing

List the contents of the ArrayBox (option 4)

OUTPUT:

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 1
Enter an String you want to add to the ArrayBox >>1st

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 1
Enter an String you want to add to the ArrayBox >>2nd

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 1
Enter an String you want to add to the ArrayBox >>3rd
(ATTN: array capacity has been resized to hold a maximum of 4)      [6 MARKS]
Note: for the full 6 marks this message must be displayed saying what the size of the new array is (i.e: 4). If it only states "array has been resized" without specifying the new array size then only 5 marks given.

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 4       [ 4 MARKS ]
INDEX VALUE
0 1st
1 2nd
2 3rd


=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 2
Enter the INDEX of the item you want removed from the box >>0
The element 1st was removed from the box.   
[ 2 MARKS]


=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 4
INDEX VALUE
0   2nd   
[1 MARK]

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

For Marking (Total 2.5 marks)

This script tests error handling. Make sure you throw and catch exceptions.


=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 2
Enter the INDEX of the item you want removed from the box >>1000


ERROR! The box is currently empty.   [0.5 marks]

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 1
Enter an String you want to add to the ArrayBox >>test


=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 2
Enter the INDEX of the item you want removed from the box >>1000


ERROR! Index must be between 0 and 0 [ 0.5 marks]


=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 2
Enter the INDEX of the item you want removed from the box >>-1


ERROR! Index must be between 0 and 0 [0.5 marks]

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 3
Enter the INDEX of the item you want changed from the box >>1000
Enter the String for the item >>test


ERROR! Index is out-of-bounds [0.5 marks]

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program

>> 3
Enter the INDEX of the item you want changed from the box >>-1
Enter the String for the item >>test


ERROR! Index is out-of-bounds [0.5 marks]

Solutions

Expert Solution


public class ArrayBox<E> {
    private E[] elements = (E[])(new Object[2]);
    private int end_idx = 0;
  
    // method to get current size of the list
    public int size(){
        return end_idx;
    }

    // method to add an element to the list
    public boolean add(E e){
        if(end_idx<elements.length){
            elements[end_idx++]=e;
            return true;
        }
        E[] newElements = (E[])(new Object[2*elements.length]);
        for(int i=0; i<elements.length; i++){
            newElements[i]=elements[i];
        }
        elements = newElements;
        elements[end_idx++]=e;
        System.out.println("Array has been resized: "+elements.length);
        return true;
    }

    // method to return an element in the given index
    public E get(int index){
        if(size()==0){
            System.out.println("ERROR! List is empty ");
            return null;
        }
        if(index>=size()){
            System.out.println("ERROR! Index must be between 0 and "+size());
            return null;
        }
        try{        
            return elements[index];
        }catch(IndexOutOfBoundsException e){
            System.out.println("ERROR! Index must be between 0 and "+size());
            return null;
        }
    }

    // method to replace the element in the given index by given element
    public E set(int index, E element){
        if(get(index)==null)
            return null;  
        try{
            elements[index] = element;
            return elements[index];
        }catch(IndexOutOfBoundsException e){
            System.out.println("ERROR! Index must be between 0 and "+size());
            return null;
        }
    }

    // method to remove element in the index
    public E remove(int index){
        E el = get(index);
        if(el==null){
            return el;
        }
        // all the elements from index shifted to left
        for(int i=index; i<end_idx-1; i++){
            elements[i] = elements[i+1];
        }
        end_idx--;
        return el;
    }
}


-----------------------------------------------------------------------------------------------------------------------


import java.util.Scanner;


public class ArrayBoxDriver {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayBox<String> list = new ArrayBox<>();
        while(true){
            mainMenu();
            System.out.print(">> ");
            String choice = in.nextLine();
            if(choice.equals("5"))
                break;
            String input;
            switch(choice){
                case "1":
                    System.out.print("Enter an String you want to add to the ArrayBox >> ");
                    input = in.nextLine();
                    list.add(input);
                    break;
                case "2":
                    System.out.print("Enter the INDEX of the item you want removed from the box >> ");
                    int index = Integer.parseInt(in.nextLine());
                    list.remove(index);
                    break;
                case "3":
                    System.out.print("Enter the INDEX of the item you want replaced from the box >> ");
                    index = Integer.parseInt(in.nextLine());
                    System.out.print("Enter an String you want to add to the ArrayBox >> ");
                    input = in.nextLine();
                    list.set(index, input);
                    break;
                case "4":
                    System.out.println("INDEX\tVALUE");
                    for(int i=0; i<list.size(); i++){
                        System.out.println(i+"\t"+list.get(i));
                    }
                  
                  
            }
        }
      
      
    }

    private static void mainMenu() {
        System.out.println("\n=[==[==[=[=================================]=]==]==]=");
        System.out.println("=[==[==[=[          Welcome to ArrayBox    ]=]==]==]=");
        System.out.println("=[==[==[=[=================================]=]==]==]=");
        System.out.println("\nChoose");
        System.out.println("1. Add an element(String) to our box.");
        System.out.println("2. Remove an element(String) from our box.");
        System.out.println("3. Replace(set) an element(String) from our box.");
        System.out.println("4. List the contents of the box.");
        System.out.println("5. Exit program");
    }
}


Sample output


Related Solutions

PURPOSE The purpose of this assignment is to develop learners’ ability to analyse data and discuss...
PURPOSE The purpose of this assignment is to develop learners’ ability to analyse data and discuss the differences between the types of statistics. REQUIREMENT The following data represent the marks of the Statistic subject obtained by 25 students in mid semester examination. Use this data to answer the following questions: 1 2 4 4 5 6 7 9 9 12 5 12 15 17 20 21 23 23 25 26 27 27 28 29 29 Based on the information from...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability to identify emerging ethical issues in business, interpret the multitude of perspectives inherent in your case study, and model appropriate behaviour by recommending specific solutions. How to Proceed Select a case. It can be one of the textbook cases that we have not discussed during the course. It can also come from the outside world, perhaps a case you have been following in the...
Purpose of Assignment  The purpose of this assignment is to allow the students to understand and...
Purpose of Assignment  The purpose of this assignment is to allow the students to understand and practice the measurement of present value, future value, and interest rate using Microsoft® Excel®.  Assignment Steps  Resources: Microsoft® Office® 2013 Accessibility Tutorials, Microsoft® Excel®, Time Value of Money Calculations Template Calculate the following time value of money problems using Microsoft® Excel®: If we place $8,592.00 in a savings account paying 7.5 percent interest compounded annually, how much will our account accrue to in 9.5 years? What is the present value of...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data sets to apply the concepts of sampling distributions and confidence intervals to make management decisions. Assignment Steps Resources: Microsoft Excel®, The Payment Time Case Study, The Payment Time Case Data Set Review the Payment Time Case Study and Data Set. Develop a 700-word report including the following calculations and using the information to determine whether the new billing system has reduced the mean bill...
The purpose of this assignment is to understand the terms import and export, and then explain...
The purpose of this assignment is to understand the terms import and export, and then explain the advantages or disadvantages of buying imports rather than buying domestic products. You could, for instance, write about an imported automobile, stereo, or household appliance that you bought or considered buying. Include all of the following points in your discussion. If you were a retailer, would you want to sell domestically made goods or imported items? Please explain why you made this choice. If...
The purpose of this assignment is to provide students with an opportunity to develop a framework...
The purpose of this assignment is to provide students with an opportunity to develop a framework for the development of a database that would support a Post-operative Follow-Up Module in an Ambulatory Surgery Center. Please read the article contained in this link and answer the following questions: [Article about Structured Vs. Unstructured Data]. What are the critical issues that Post-Anesthesia Care Unit (PACU) staff must address when contemplating the development of a Post-operative Follow-Up Module? When considering the issue of...
The purpose of this assignment is to read and understand the difference between front end and...
The purpose of this assignment is to read and understand the difference between front end and back end design of a database application. Steps: Search for articles describing front end and back end design of any application (example, a website). Summarize the concepts and elements for each one. Does the database that holds all the data exist at the front end or the back end? Include the answer in your report.
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
The purpose of this assignment is to develop a security program aligned with regulatory compliance and...
The purpose of this assignment is to develop a security program aligned with regulatory compliance and security control frameworks. Select a company for the focus of your assignment. Using the following, map the standard controls to the regulatory compliance that would be appropriate for the organization: The "Security Controls Mapping Template." Regulatory compliance information, such as HIPAA, PCI, SOX, GLBA, etc. Security control frameworks, such as NIST, CIS, COBIT, COSO, ITIL, etc. On the template, map the regulatory rules (one...
Module 06 Assignment – Designing a Care Map Purpose of Assignment Assist students to develop a...
Module 06 Assignment – Designing a Care Map Purpose of Assignment Assist students to develop a care plan that includes safe discharge information for a client with musculoskeletal trauma. Course Competency Explain components of multidimensional nursing care for clients with musculoskeletal disorders. Instructions Mr. Harry Roost is a 78-year old male being discharge after a fracture of his right tibia and fibula. He has a long leg cast that he will need to wear for the next 8 weeks. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT