import java.util.NoSuchElementException; import java.lang.StringBuffer; // Original Source: OpenDSA Data Structures and Algorithms Modules Collection, CHAPTER 9 LINEAR STRUCTURES: https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/ListArray.html // Array-based list implementation class AList implements List { private E listArray[]; // Array holding list elements private static final int DEFAULT_SIZE = 10; // Default size private int maxSize; // Maximum size of list private int listSize; // Current # of list items private int curr; // Position of current element // Constructors // Create a new list object with maximum size "size" @SuppressWarnings("unchecked") // Generic array allocation AList(int size) { maxSize = size; listSize = curr = 0; listArray = (E[])new Object[size]; // Create listArray } // Create a list with the default capacity AList() { this(DEFAULT_SIZE); // Just call the other constructor } public void clear() { // Reinitialize the list listSize = curr = 0; // Simply reinitialize values } // Insert "it" at current position public boolean insert(E it) { if (listSize >= maxSize) { return false; } for (int i=listSize; i>curr; i--) { // Shift elements up listArray[i] = listArray[i-1]; // to make room } listArray[curr] = it; listSize++; // Increment list size return true; } // Append "it" to list public boolean append(E it) { if (listSize >= maxSize) { return false; } listArray[listSize++] = it; return true; } // Remove and return the current element public E remove() throws NoSuchElementException { if ((curr<0) || (curr>=listSize)) { // No current element throw new NoSuchElementException("remove() in AList has current of " + curr + " and size of " + listSize + " that is not a a valid element"); } E it = listArray[curr]; // Copy the element for(int i=curr; i listSize)) { return false; } curr = pos; return true; } // Return true if current position is at end of the list public boolean isAtEnd() { return curr == listSize; } // Return the current element public E getValue() throws NoSuchElementException { if ((curr < 0) || (curr >= listSize)) {// No current element throw new NoSuchElementException("getvalue() in AList has current of " + curr + " and size of " + listSize + " that is not a a valid element"); } return listArray[curr]; } //Tell if the list is empty or not public boolean isEmpty() { return listSize == 0; } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

import java.util.NoSuchElementException; import java.lang.StringBuffer; // Original Source: OpenDSA Data Structures and Algorithms Modules Collection, CHAPTER 9 LINEAR STRUCTURES: https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/ListArray.html // Array-based list implementation class AList<E> implements List<E> { private E listArray[]; // Array holding list elements private static final int DEFAULT_SIZE = 10; // Default size private int maxSize; // Maximum size of list private int listSize; // Current # of list items private int curr; // Position of current element // Constructors // Create a new list object with maximum size "size" @SuppressWarnings("unchecked") // Generic array allocation AList(int size) { maxSize = size; listSize = curr = 0; listArray = (E[])new Object[size]; // Create listArray } // Create a list with the default capacity AList() { this(DEFAULT_SIZE); // Just call the other constructor } public void clear() { // Reinitialize the list listSize = curr = 0; // Simply reinitialize values } // Insert "it" at current position public boolean insert(E it) { if (listSize >= maxSize) { return false; } for (int i=listSize; i>curr; i--) { // Shift elements up listArray[i] = listArray[i-1]; // to make room } listArray[curr] = it; listSize++; // Increment list size return true; } // Append "it" to list public boolean append(E it) { if (listSize >= maxSize) { return false; } listArray[listSize++] = it; return true; } // Remove and return the current element public E remove() throws NoSuchElementException { if ((curr<0) || (curr>=listSize)) { // No current element throw new NoSuchElementException("remove() in AList has current of " + curr + " and size of " + listSize + " that is not a a valid element"); } E it = listArray[curr]; // Copy the element for(int i=curr; i<listSize-1; i++) {// Shift them down listArray[i] = listArray[i+1]; } listSize--; // Decrement size return it; } public void moveToStart() { // Set to front curr = 0; } public void moveToEnd() { // Set at end curr = listSize; } public void prev() { // Move left if (curr != 0) { curr--; } } public void next() { // Move right if (curr < listSize) { curr++; } } public int length() { // Return list size return listSize; } public int currPos() { // Return current position return curr; } // Set current list position to "pos" public boolean moveToPos(int pos) { if ((pos < 0) || (pos > listSize)) { return false; } curr = pos; return true; } // Return true if current position is at end of the list public boolean isAtEnd() { return curr == listSize; } // Return the current element public E getValue() throws NoSuchElementException { if ((curr < 0) || (curr >= listSize)) {// No current element throw new NoSuchElementException("getvalue() in AList has current of " + curr + " and size of " + listSize + " that is not a a valid element"); } return listArray[curr]; } //Tell if the list is empty or not public boolean isEmpty() { return listSize == 0; } }

import
java.util.NoSuchElementException;
// Source: OpenDSA Data Structures and Algorithms Modules Collection, CHAPTER 9 LIN
// List class ADT. Generalize the element type using Java Generics.
public interface List<E> { // List class ADT
// Remove all contents from the list, so it is once again empty
public void clear();
}
// Insert "it" at the current location
// The client must ensure that the list's capacity is not exceeded
public boolean insert (E it);
// Append "it" at the end of the list
// The client must ensure that the list's capacity is not exceeded
public boolean append(E it);
// Remove and return the rrent element
public E remove() throws NoSuchElementException;
// Set the current position to the start of the list
public void moveToStart();
// Set the current position to the end of the list
public void moveToEnd();
// Move the current position one step left, no change if already at beginning
public void prev();
// Move the current position one step right, no change if already at end
public void next();
// Return the number of elements in the list
public int length();
// Return the position of the current element
public int currpos();
// Set the current position to "pos"
public boolean moveToPos (int pos);
// Return true if current position is at end of the list
public boolean isAtEnd();
// Return the current element
public E getValue () throws NoSuchElementException;
// Tell if the list is empty or not
public boolean isEmpty();
Transcribed Image Text:import java.util.NoSuchElementException; // Source: OpenDSA Data Structures and Algorithms Modules Collection, CHAPTER 9 LIN // List class ADT. Generalize the element type using Java Generics. public interface List<E> { // List class ADT // Remove all contents from the list, so it is once again empty public void clear(); } // Insert "it" at the current location // The client must ensure that the list's capacity is not exceeded public boolean insert (E it); // Append "it" at the end of the list // The client must ensure that the list's capacity is not exceeded public boolean append(E it); // Remove and return the rrent element public E remove() throws NoSuchElementException; // Set the current position to the start of the list public void moveToStart(); // Set the current position to the end of the list public void moveToEnd(); // Move the current position one step left, no change if already at beginning public void prev(); // Move the current position one step right, no change if already at end public void next(); // Return the number of elements in the list public int length(); // Return the position of the current element public int currpos(); // Set the current position to "pos" public boolean moveToPos (int pos); // Return true if current position is at end of the list public boolean isAtEnd(); // Return the current element public E getValue () throws NoSuchElementException; // Tell if the list is empty or not public boolean isEmpty();
Array-Based Lists
Write Java code in a new driver class that uses an array-based list to complete the following:
Prompt the user for the total number of characters in their first name, their middle initial and last name (including spaces) (for the the maximum size of the list)
• Prompt the user for their first name and insert every character of their first name into a list and display a representation of the list using sequence notation (in part by calling toString())
• Prompt the user for their last name and insert a space and then every character of their last name into a list and display a representation of the list using sequence notation (in part by calling
toString())
Prompt the user for their middle initial and insert a space and then their middle initial, then a ' . ' in the list after their first name and display a representation of the list using sequence notation (in part
by calling toString())
• Remove all but the first letter of their first name, middle name and last name and display a representation of the list using sequence notation (in part by calling toString())
●
Use the List interface and the AList class for this assignment. Also, add a toString method into the AList class that returns a sequence representation of the list. Do not change the
interface and you do not need to make any other adjustments to the class. Note, you do not need to upload List.java.
Examples
Note, user input is in bold face blue and underlined text is required for test cases.
Please enter the number of characters for your first name, a space, your middle initial (and a
You entered 12
Please enter your first name: Joe
You entered Joe
Just the first name: ( J, o, e )
Please enter your last name: Biden
You entered Biden
With first and last names: ( | 1, o, e, _,- B, i, d, e, n )
Please enter just your middle initial: R
You entered R
Full name: (1, 0, e, _| R,. .,. B, i, d, e, n )
Just initials: J, R, B)
---
.'), a space and your last name: 12
Transcribed Image Text:Array-Based Lists Write Java code in a new driver class that uses an array-based list to complete the following: Prompt the user for the total number of characters in their first name, their middle initial and last name (including spaces) (for the the maximum size of the list) • Prompt the user for their first name and insert every character of their first name into a list and display a representation of the list using sequence notation (in part by calling toString()) • Prompt the user for their last name and insert a space and then every character of their last name into a list and display a representation of the list using sequence notation (in part by calling toString()) Prompt the user for their middle initial and insert a space and then their middle initial, then a ' . ' in the list after their first name and display a representation of the list using sequence notation (in part by calling toString()) • Remove all but the first letter of their first name, middle name and last name and display a representation of the list using sequence notation (in part by calling toString()) ● Use the List interface and the AList class for this assignment. Also, add a toString method into the AList class that returns a sequence representation of the list. Do not change the interface and you do not need to make any other adjustments to the class. Note, you do not need to upload List.java. Examples Note, user input is in bold face blue and underlined text is required for test cases. Please enter the number of characters for your first name, a space, your middle initial (and a You entered 12 Please enter your first name: Joe You entered Joe Just the first name: ( J, o, e ) Please enter your last name: Biden You entered Biden With first and last names: ( | 1, o, e, _,- B, i, d, e, n ) Please enter just your middle initial: R You entered R Full name: (1, 0, e, _| R,. .,. B, i, d, e, n ) Just initials: J, R, B) --- .'), a space and your last name: 12
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

can you modify it please so it can print the first name initial, followed by middle name initial, followed by last name initial: [J, R, B].

thank you

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Depth First Search
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education