Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be writing a simple Java program that implements a few basic ArrayList manipulations. For the first exercise, you will write code that takes

You will be writing a simple Java program that implements a few basic ArrayList manipulations. For the first exercise, you will write code that takes two lists of words as input from the command line and stores them in separate ArrayLists. You will also write a method that creates a new list from these input lists by appending the elements of each list together. For the second exercise, you will write a method that extends on the first exercise by interleaving the elements of the two lists together instead of appending them.

Exercise 1 Description

Your code will take a list of Strings as input from the console and store them in an ArrayList. It will then take a second list of Strings from the console and store them in an ArrayList. Finally it will display the two original lists, as well as two new lists created by appending the two lists together in different orders. In the skeleton file above, complete the methods getList(), displayList(), and appendLists() to complete this exercise.

Exercise 1 Sample Output

This is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program. Enter the first wordlist: Enter a word ('XXX' to quit): the Enter a word ('XXX' to quit): quick Enter a word ('XXX' to quit): brown Enter a word ('XXX' to quit): fox Enter a word ('XXX' to quit): jumped Enter a word ('XXX' to quit): xxx Enter the second wordlist: Enter a word ('XXX' to quit): over Enter a word ('XXX' to quit): the Enter a word ('XXX' to quit): lazy Enter a word ('XXX' to quit): dog Enter a word ('XXX' to quit): xxx Wordlist 1 ---------- 0: the 1: quick 2: brown 3: fox 4: jumped Wordlist 2 ---------- 0: over 1: the 2: lazy 3: dog List 2 appended to the end of List 1 ------------------------------------ 0: the 1: quick 2: brown 3: fox 4: jumped 5: over 6: the 7: lazy 8: dog List 1 appended to the end of List 2 ------------------------------------ 0: over 1: the 2: lazy 3: dog 4: the 5: quick 6: brown 7: fox 8: jumped Note that the 'XXX' given to end input should be case insensitive (i.e. the code should terminate whether the user enters capital XXX or lowercase xxx). Another transcript with different inputs appears below: Enter the first wordlist: Enter a word ('XXX' to quit): A Enter a word ('XXX' to quit): B Enter a word ('XXX' to quit): C Enter a word ('XXX' to quit): D Enter a word ('XXX' to quit): E Enter a word ('XXX' to quit): F Enter a word ('XXX' to quit): G Enter a word ('XXX' to quit): xxx Enter the second wordlist: Enter a word ('XXX' to quit): a Enter a word ('XXX' to quit): b Enter a word ('XXX' to quit): c Enter a word ('XXX' to quit): d Enter a word ('XXX' to quit): e Enter a word ('XXX' to quit): xxx Wordlist 1 ---------- 0: A 1: B 2: C 3: D 4: E 5: F 6: G Wordlist 2 ---------- 0: a 1: b 2: c 3: d 4: e List 2 appended to the end of List 1 ------------------------------------ 0: A 1: B 2: C 3: D 4: E 5: F 6: G 7: a 8: b 9: c 10: d 11: e List 1 appended to the end of List 2 ------------------------------------ 0: a 1: b 2: c 3: d 4: e 5: A 6: B 7: C 8: D 9: E 10: F 11: G

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab10b.java in the same ClosedLab10 folder. For this exercise, you should extend the code that you wrote in Exercise 1 with a new method. This new method should use the following method header: private static ArrayList mergeLists(ArrayList list1, ArrayList list2) It should take two lists of Strings as input. The program will merge the two lists together, alternating between elements of list1 and list2 until all values from both lists have been copied into the new list. If list1 is shorter than list2, the remaining elements of list2 will be appended to the end of the merged list. Likewise, if list2 is shorter than list1, the remaning elements of list1 will be appended to the end of the merged list. The original lists should be intact when your method ends (i.e. make no changes to list1 and list2 in the body of your method).

Exercise 2 Sample Output

This is a sample transcript of what your program should do. Note that the prompts for the user have changed from "store" to "sort" and your prompts should be changed accordingly: Enter the first wordlist: Enter a word ('XXX' to quit): the Enter a word ('XXX' to quit): quick Enter a word ('XXX' to quit): brown Enter a word ('XXX' to quit): fox Enter a word ('XXX' to quit): jumped Enter a word ('XXX' to quit): xxx Enter the second wordlist: Enter a word ('XXX' to quit): over Enter a word ('XXX' to quit): the Enter a word ('XXX' to quit): lazy Enter a word ('XXX' to quit): dog Enter a word ('XXX' to quit): xxx Wordlist 1 ---------- 0: the 1: quick 2: brown 3: fox 4: jumped Wordlist 2 ---------- 0: over 1: the 2: lazy 3: dog List 1 merged with List 2 ------------------------- 0: the 1: over 2: quick 3: the 4: brown 5: lazy 6: fox 7: dog 8: jumped List 2 merged with List 1 ------------------------- 0: over 1: the 2: the 3: quick 4: lazy 5: brown 6: dog 7: fox 8: jumped A second run of the same program might produce the following output: Enter the first wordlist: Enter a word ('XXX' to quit): A Enter a word ('XXX' to quit): B Enter a word ('XXX' to quit): C Enter a word ('XXX' to quit): D Enter a word ('XXX' to quit): xxx Enter the second wordlist: Enter a word ('XXX' to quit): a Enter a word ('XXX' to quit): b Enter a word ('XXX' to quit): c Enter a word ('XXX' to quit): d Enter a word ('XXX' to quit): e Enter a word ('XXX' to quit): f Enter a word ('XXX' to quit): g Enter a word ('XXX' to quit): xxx Wordlist 1 ---------- 0: A 1: B 2: C 3: D Wordlist 2 ---------- 0: a 1: b 2: c 3: d 4: e 5: f 6: g List 1 merged with List 2 ------------------------- 0: A 1: a 2: B 3: b 4: C 5: c 6: D 7: d 8: e 9: f 10: g List 2 merged with List 1 ------------------------- 0: a 1: A 2: b 3: B 4: c 5: C 6: d 7: D 8: e 9: f 10: g

Use the skeleton below:

/* * Lab10a.java * * A program that asks the user for two lists of Strings and stores them in two separate * ArrayLists. The program then creates new lists consisting of the first two lists * appended to each other in different orders. * Used to practice using ArrayLists and breaking code up into methods, and as a first * step to implementing Lab12b.java - interleaving the lists in different ways. * * @author ENTER YOUR NAMES HERE * */ package osu.cse1223; import java.util.Scanner; import java.util.ArrayList; public class Lab10a { public static void main(String[] args) { //Fill in the body } // Given a Scanner as input creates a new empty list of Strings and then prompts the // user to enter Strings on the console. If the string entered by the user is XXX or // xxx the method returns the list of Strings it created to the calling program. Otherwise // it adds the String entered by the user to the end of its list and asks for another // String private static ArrayList getList(Scanner inScanner) { // Fill in the body } // Given a ArrayList object, display the contents of the list to the console. The // list should be displayed as the index of the item in the list, followed by a colon // then by the item itself. private static void displayList(ArrayList myList) { // Fill in the body } // Given two ArrayList objects, create a new ArrayList object that contains // duplicates of the Strings in the first list followed by duplicates of the Strings // in the second list. The original ArrayList objects should not be changed by // this method. private static ArrayList appendLists(ArrayList list1, ArrayList list2) { // Fill in the body } } 

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

More Books

Students also viewed these Databases questions