Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am programming the following in Java: a) class MyApp : - Use the provided class to start your project. - Prepare the method main

I am programming the following in Java:

a) class MyApp:

- Use the provided class to start your project.

- Prepare the method main to use methods to create, change and show the following data structures:

vector;

linked list;

stack;

queue;

set; and

map.

- Use the first and the second arrays of Strings to create the data structures.

- You must use at least once:

regular for to traverse the collection;

enhanced for to traverse the collection;

iterator to traverse the collection; and

comparator to sort a collection.

- protected attributes (member-variables):

String[ ] firstArray

String[ ] secondArray

ArrayList arrayList

Vector vector

LinkedList linkedList

Stack stack

ConcurrentLinkedQueue queue

TreeSet set

Hashtable map

- public constructor:

MyApp ( )

- public methods (member functions):

main ( )

createFirstArray ( )

changeFirstArray ( )

showFirstArray ( )

createSecondArray ( )

changeSecondArray ( )

showSecondArray ( )

createArrayList ( )

changeArrayList ( )

showArrayList ( )

createVector ( )

changeVector ( )

showVector ( )

createLinkedList ( )

changeLinkedList ( )

showLinkedList ( )

createStack ( )

changeStack ( )

showStack ( )

createQueue ( )

changeQueue ( )

showQueue ( )

createSet ( )

changeSet ( )

showSet ( )

createMap ( )

changeMap ( )

showMap ( )

The program I have so far:

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Hashtable; import java.util.LinkedList; import java.util.Stack; import java.util.TreeSet; import java.util.Vector; import java.util.concurrent.ConcurrentLinkedQueue; public class MyApp { /****************************************************************/ // Fields /** First array of Strings. */ protected String[] firstArray = null; /** Second array of Strings. */ protected String[] secondArray = null; /** ArrayList of Strings. */ protected ArrayList arrayList = null; /** Vector of Strings. */ protected Vector vector = null; /** LinkedList of Strings. */ protected LinkedList linkedList = null; /** Stack of Strings. */ protected Stack stack = null; /** Queue of Strings. */ protected ConcurrentLinkedQueue queue = null; /** Set of Strings. */ protected TreeSet set = null; /** Map of Strings. */ protected Hashtable map = null; /** Dashed line. */ private static final String dashedLine = "----------------------------------------------------------"; /**************************************************************** * Main function to start the application. * * @param args array of arguments to start the application. */ public static void main (String args[]) { // reference MyApp myApp; // instantiates an object of Class MyApp myApp = new MyApp(); /************************************/ // First Array // creates and shows information System.out.println(dashedLine); myApp.createFirstArray(); myApp.showFirstArray(); // changes and shows information System.out.println(dashedLine); myApp.changeFirstArray(); myApp.showFirstArray(); /************************************/ // Second Array // creates and shows information System.out.println(dashedLine); myApp.createSecondArray(); myApp.showSecondArray(); // changes and shows information System.out.println(dashedLine); myApp.changeSecondArray(); myApp.showSecondArray(); /************************************/ // ArrayList // creates and shows information System.out.println(dashedLine); myApp.createArrayList(); myApp.showArrayList(); // changes and shows information System.out.println(dashedLine); myApp.changeArrayList(); myApp.showArrayList(); System.out.println(); } /**************************************************************** * First Array ****************************************************************/ /**************************************************************** * Creates the first array of Strings. */ public void createFirstArray () { // instantiates the array firstArray = new String[6]; // sets elements firstArray[0] = "red"; firstArray[1] = "green"; firstArray[2] = "blue"; firstArray[3] = "white"; firstArray[4] = "black"; firstArray[5] = "yellow"; } /**************************************************************** * Changes the first array of Strings. */ public void changeFirstArray () { // consistency: if array is null, create a new one if (firstArray == null) createFirstArray(); // shows information System.out.println("Sorting first array..."); // sorts the array Arrays.sort(firstArray); } /**************************************************************** * Shows the first array of Strings. */ public void showFirstArray () { // consistency: if array is null, creates a new one if (firstArray == null) createFirstArray(); // shows information System.out.println("First array of Strigs, " + firstArray.length + " elements: "); // shows each element of the array for(String element : firstArray) System.out.println(element); System.out.println(); } /**************************************************************** * Second Array ****************************************************************/ /**************************************************************** * Creates the second array of Strings. */ public void createSecondArray () { // instantiates the array secondArray = new String[6]; // sets elements secondArray[0] = "black"; secondArray[1] = "yellow"; secondArray[2] = "cyan"; secondArray[3] = "gray"; secondArray[4] = "orange"; secondArray[5] = "brown"; } /**************************************************************** * Changes the second array of Strings. */ public void changeSecondArray () { // consistency: if array is null, creates a new one if (secondArray == null) createSecondArray(); // shows information System.out.println("Sorting only the first half of the second array..."); // sorts the array Arrays.sort(secondArray, 0, secondArray.length/2); } /**************************************************************** * Shows the second array of Strings. */ public void showSecondArray () { // consistency: if array is null, creates a new one if (secondArray == null) createSecondArray(); // shows information System.out.println("Second array of Strings, " + secondArray.length + " elements: "); // shows each color in the array for(String element : secondArray) System.out.println(element); System.out.println(); } /**************************************************************** * Array List ****************************************************************/ /**************************************************************** * Creates the ArrayList of Strings. */ public void createArrayList () { // instantiates arrayList arrayList = new ArrayList(); // adds elements of the first array for(String element : firstArray) arrayList.add(element); // adds elements of the second array for(String element : secondArray) arrayList.add(element); } /**************************************************************** * Changes the ArrayList of Strings. */ public void changeArrayList () { // consistency: if arrayList is null, creates a new one if (arrayList == null) createArrayList(); // shows information System.out.println("Sorting arrayList in descending order..."); // sorts the arrayList in descending order Collections.sort(arrayList, Collections.reverseOrder()); // shows information System.out.println("Removing the first element of arrayList..."); // removes the first element arrayList.remove(0); } /**************************************************************** * Shows the ArrayList of Strings. */ public void showArrayList () { // consistency: if arrayList is null, creates a new one if (arrayList == null) createArrayList(); // shows information System.out.println("ArrayList of Strings, " + arrayList.size() + " elements: "); // shows each element of arrayList for(int i=0; i < arrayList.size(); i++) { String element = arrayList.get(i); System.out.println(element); } System.out.println(); } /**************************************************************** * Vector ****************************************************************/ /**************************************************************** * Linked List ****************************************************************/ /**************************************************************** * Stack ****************************************************************/ /**************************************************************** * Queue ****************************************************************/ /**************************************************************** * Set ****************************************************************/ /**************************************************************** * Map ****************************************************************/ }

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

How do you want me to help you?

Answered: 1 week ago

Question

Which is the invalid variable name in MATLAB?

Answered: 1 week ago