Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program: (Name this Lab14_Problem1) This problem has you populate parts of

ID: 3770060 • Letter: J

Question

Java Program: (Name this Lab14_Problem1)

This problem has you populate parts of an array list. It's a partial list of states that could be used in an application as a drop-down list, etc. Use a main() method to manage the array list operations. Create a separate method fvDisplay to display the array; you'll use it a lot.

Create an array list constructor to store the US postal abbreviations of the 50 states (e.g., MD, DE, VA, etc.).

Add Vermont, Texas, Louisiana, Arkansas, and California. Use the add method with no index.

Call the fvDisplay method to output the array, as follows (include the Index in your output as shown):

Index 0: VT

Index 1: TX

Index 2: LA

Index 3: AR

Index 4: CA

Before your fvDisplay statement, insert Utah before Vermont, this time using the add method with an index to make sure it falls before Vermont.

Run the program. Your list should now look like:

Index 0: UT

Index 1: VT

Index 2: TX

Index 3: LA

Index 4: AR

Index 5: CA

Sort the array in ascending order, then display the array. It should look like this now:

Index 0: AR

Index 1: CA

Index 2: LA

Index 3: TX

Index 4: UT

Index 5: VT

Now, sort it in descending order by reversing it. You should see:

Index 0: VT

Index 1: UT

Index 2: TX

Index 3: LA

Index 4: CA

Index 5: AR

California had an earthquake. It fell into the ocean and disappeared. Remove it from the list using its index number, then re-sort the array in ascending order and display it. It should look like this:

Index 0: AR

Index 1: LA

Index 2: TX

Index 3: UT

Index 4: VT

Display the number of states in the array after California's tragic demise. Your output should look like:

Index 0: AR
Index 1: LA

Index 2: TX

Index 3: UT

Index 4: VT

Array size: 5

Comment out the line that shows the array size.

Now add the Peachtree State—not once but twice. Use the add method with no index. Move the fvDisplay statement below your add methods. It should look like:

Index 0: AR

Index 1: LA

Index 2: TX

Index 3: UT

Index 4: VT

Index 5: GA

Index 6: GA

Try to remove the duplicate GA entry using listStates.remove("GA");

Display the list. What happened? Describe this behavior.

Finally, empty the list completely and display it. What do you see? What did it do?

Explanation / Answer

package current;

import java.util.ArrayList;
import java.util.Collections;

public class PostalCodeArrayListTest {

   public static void main(String[] args) {
       PostalCodesArrayList postalCodes = new PostalCodesArrayList();
       postalCodes.fvDisplay();
      
       postalCodes.insert(0,"UT");
       System.out.println("Inserted UT (Utah) at start");
       postalCodes.fvDisplay();
      
       ArrayList<String> sortArray = postalCodes.sort();
       System.out.println("Sorted array in ascending order");
       postalCodes.fvDisplay(sortArray);
      
       System.out.println("Removing Calfornia..");
       postalCodes.remove(4);
       postalCodes.fvDisplay(sortArray);
      
       System.out.println("Now add the Peachtree State—not once but twice");
       postalCodes.insert("GA");
       postalCodes.insert("GA");
       postalCodes.fvDisplay();
      
       System.out.println("Try to remove the duplicate GA entry using list.remove(GA)");
       postalCodes.remove("GA");
       postalCodes.fvDisplay();
      
   }
}

class PostalCodesArrayList {
   private ArrayList<String> codes = null;
  
   public PostalCodesArrayList() {
       codes = new ArrayList<String>();
       codes.add("VT");
       codes.add("TX");
       codes.add("LA");
       codes.add("AR");
       codes.add("CA");
   }
  
   public ArrayList<String> sort() {
       ArrayList<String> sortArray = new ArrayList<String>();
       for(int i = 0; i < codes.size(); i++)
           sortArray.add(codes.get(i));
       Collections.sort(sortArray);
       return sortArray;
   }
  
   public void insert(String item) {
       codes.add(item);
   }
  
   public void insert(int index, String item) {
       codes.add(index, item);
   }
  
   public void remove(int index) {
       codes.remove(index);
   }
  
   public void remove(String item) {
       codes.remove(item);
   }

   public void fvDisplay() {
       for(int i = 0; i < codes.size(); i++)
           System.out.println("Index "+i+": "+codes.get(i));
   }
  
   public void fvDisplay(ArrayList<String> array) {
       for(int i = 0; i < array.size(); i++)
           System.out.println("Index "+i+": "+array.get(i));
   }
  
}

----------------output---------------------

Index 0: VT
Index 1: TX
Index 2: LA
Index 3: AR
Index 4: CA
Inserted UT (Utah) at start
Index 0: UT
Index 1: VT
Index 2: TX
Index 3: LA
Index 4: AR
Index 5: CA
Sorted array in ascending order
Index 0: AR
Index 1: CA
Index 2: LA
Index 3: TX
Index 4: UT
Index 5: VT
Removing Calfornia..
Index 0: AR
Index 1: CA
Index 2: LA
Index 3: TX
Index 4: UT
Index 5: VT
Now add the Peachtree State—not once but twice
Index 0: UT
Index 1: VT
Index 2: TX
Index 3: LA
Index 4: CA
Index 5: GA
Index 6: GA
Try to remove the duplicate GA entry using list.remove(GA)
Index 0: UT
Index 1: VT
Index 2: TX
Index 3: LA
Index 4: CA
Index 5: GA

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote