Theese are CodeHS modules that are similar to ones on the Internet

Theese are CodeHS modules that are similar to ones on the Internet. But, they are not exactly the same. So please do not just copy paste code from the Internet and say this is the solution while it is a solution of a similar problem. I also know how to copy that problem from the Internet. That is what happened to me before and I don’t want it happening again. Please read the questions carefully and solve if you’re really willing to solve instead of copy-pasting code from the Internet if you truly want to help me. Thank you for your understanding.

Instructions:

Your company is doing some data cleanup, and notices that the list of all users has been getting outdated.
For one, there are some users who have been added multiple times.

Your job is to create a series of methods that can purge some of the old data from the existing list.

Create static methods in the DataPurge class that can do the following:

removeDuplicates This method takes a list of people, and removes the duplicate names. It also prints to the console which duplicate names have been removed.
removeName This method takes a list of people and name for which to search. It removes all names that match the search name (whether the name matches the first name or the last name or both). It should print to the console any names that were removed.
correctlyFormatted This method returns true if all of the data in the list is formatted correctly. Correctly formatted names are made up of a first name and a last name, separated by a single space. Both the first and last names should start with an uppercase letter.
Test your methods out in the DataPurgeTester file. You don’t have to change anything there, but the methods should work accordingly!

Hint: There are many ways to solve these problems, especially correctlyFormatted. You may find indexOf or split or toUpperCase or contains or substring to be useful methods, depending on your algorithm.
One way to check if a letter is uppercase is to compare the letter to the uppercase version of itself.

Existing Files:

DataPurge.java

import java.util.ArrayList;
public class DataPurge
{
public static void removeDuplicates(ArrayList list)
{
ArrayList existing = new ArrayList();

for(int i = 0; i < list.size();=””>
{
String email = list.get(i);
if(existing.indexOf(email) != -1)
{
list.remove(i);
i–;
}
existing.add(email);
}
}
public static void removeName(ArrayList list, String)
{
for(int i = 0; i < list.size();=””>
{
if(list.get(i).indexOf(“@aol.com”) > -1)
{
System.out.println(“removing ” + list.get(i));
list.remove(i);
i–;
}
}
}
public static boolean correctlyFormatted(ArrayList list)
{
for(int i = 0; i < list.size();=””>
{
if(list.get(i).indexOf(“@”) == -1 || list.get(i).indexOf(“.”) == -1) {
return false;
}
}
return true;
}
}

DataPurgeTester.java

import java.util.ArrayList;

public class DataPurgeTester
{
public static void main(String[] args)
{
ArrayList members = new ArrayList();
addMembers(members);
System.out.println(“All names formatted correctly? ” + DataPurge.correctlyFormatted(members));
DataPurge.removeName(members, “Araceli Castaneda”);
DataPurge.removeDuplicates(members);
System.out.println(members);
}

public static void addMembers(ArrayList emails)
{
emails.add(“London Braun”);
emails.add(“Jaslyn Chavez”);
emails.add(“Daphne Kane”);
emails.add(“Aden Brock”);
emails.add(“Jaime Wolf”);
emails.add(“finley Wood”);
emails.add(“Trent maynard”);
emails.add(“Jaiden Krause”);
emails.add(“London Braun”);
emails.add(“Jaiden Krause”);
emails.add(“Davon Mccormick”);
emails.add(“JosieFreeman”);
emails.add(“Jaslyn Chavez”);
emails.add(“Zaiden Harding”);
emails.add(“Araceli Castaneda”);
emails.add(“Jaime Wolf”);
emails.add(“London Braun”);

}
}

The Correct Answer and Explanation is :

To address the data cleanup requirements specified, we need to implement three static methods in the DataPurge class: removeDuplicates, removeName, and correctlyFormatted. Below is the implementation of these methods:

import java.util.ArrayList;
import java.util.HashSet;

public class DataPurge {

    // Removes duplicate names from the list and prints the removed duplicates
    public static void removeDuplicates(ArrayList<String> list) {
        HashSet<String> uniqueNames = new HashSet<>();
        ArrayList<String> duplicates = new ArrayList<>();

        for (String name : list) {
            if (!uniqueNames.add(name)) {
                duplicates.add(name);
            }
        }

        // Remove duplicates from the original list
        list.removeAll(duplicates);

        // Print removed duplicates
        for (String duplicate : duplicates) {
            System.out.println("Removing duplicate: " + duplicate);
        }
    }

    // Removes all occurrences of the specified name from the list and prints the removed names
    public static void removeName(ArrayList<String> list, String name) {
        ArrayList<String> toRemove = new ArrayList<>();

        for (String currentName : list) {
            if (currentName.equals(name)) {
                toRemove.add(currentName);
            }
        }

        // Remove specified names from the list
        list.removeAll(toRemove);

        // Print removed names
        for (String removed : toRemove) {
            System.out.println("Removing: " + removed);
        }
    }

    // Checks if all names in the list are correctly formatted
    public static boolean correctlyFormatted(ArrayList<String> list) {
        for (String name : list) {
            String[] parts = name.split(" ");
            if (parts.length != 2) {
                return false;
            }
            String firstName = parts[0];
            String lastName = parts[1];

            if (!Character.isUpperCase(firstName.charAt(0)) || !Character.isUpperCase(lastName.charAt(0))) {
                return false;
            }
        }
        return true;
    }
}

Explanation:

  1. removeDuplicates Method:
  • Purpose: Removes duplicate names from the list and prints each duplicate removed.
  • Implementation:
    • Utilizes a HashSet to track unique names.
    • Iterates through the list; if a name is already in the HashSet, it’s added to the duplicates list.
    • Removes all duplicates from the original list.
    • Prints each removed duplicate.
  1. removeName Method:
  • Purpose: Removes all occurrences of a specified name from the list and prints each name removed.
  • Implementation:
    • Iterates through the list to find all occurrences of the specified name.
    • Adds each occurrence to the toRemove list.
    • Removes all names in toRemove from the original list.
    • Prints each removed name.
  1. correctlyFormatted Method:
  • Purpose: Checks if all names in the list are correctly formatted (i.e., two words with each starting with an uppercase letter).
  • Implementation:
    • Splits each name by a space.
    • Checks if the split results in exactly two parts.
    • Verifies that both parts start with an uppercase letter.
    • Returns false if any name is not correctly formatted; otherwise, returns true.

Usage in DataPurgeTester:

import java.util.ArrayList;

public class DataPurgeTester {
    public static void main(String[] args) {
        ArrayList<String> members = new ArrayList<>();
        addMembers(members);

        System.out.println("All names formatted correctly? " + DataPurge.correctlyFormatted(members));

        DataPurge.removeName(members, "Araceli Castaneda");
        DataPurge.removeDuplicates(members);

        System.out.println(members);
    }

    public static void addMembers(ArrayList<String> emails) {
        emails.add("London Braun");
        emails.add("Jaslyn Chavez");
        emails.add("Daphne Kane");
        emails.add("Aden Brock");
        emails.add("Jaime Wolf");
        emails.add("Finley Wood");
        emails.add("Trent Maynard");
        emails.add("Jaiden Krause");
        emails.add("London Braun");
        emails.add("Jaiden Krause");
        emails.add("Davon Mccormick");
        emails.add("Josie Freeman");
        emails.add("Jaslyn Chavez");
        emails.add("Zaiden Harding");
        emails.add("Araceli Castaneda");
        emails.add("Jaime Wolf");
        emails.add("London Braun");
    }
}

Explanation of DataPurgeTester:

  • Purpose: Tests the functionality of the DataPurge methods.
  • Implementation:
  • Creates an ArrayList named members and populates it using the addMembers method.
  • Checks if all names are correctly formatted and prints the result.
  • Removes all occurrences of “Araceli Castaneda” and duplicates from the list.
  • Prints the final list of members.

Note: The correctlyFormatted method assumes that names are in the format “First Last” with each part starting with an uppercase letter. If the format varies (e.g., middle names or initials), the method would need to be adjusted accordingly.

This implementation ensures that the data cleanup requirements are met efficiently and correctly.

Scroll to Top