Lab 3 (100 pts)
due Monday, January 28 at 9:20am on Canvas


Pair Programming Required (Or No Credit)!

  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your Lab to Canvas.
  • Only ONE partner submits the lab assignment on Canvas. Please make sure both your names are on the source file(s).


Removing Duplicate Names
  • A common task when working with lists of data (and a common technical interview question), is to remove duplicates from the list.
  • Two values may get mistakenly entered twice into a list - maybe a customer responds twice to a survey, or accidentally creates two accounts using two different email addresses, or a person performing data entry might accidentally enter the same value twice.
  • For this assignment, assume you have the following values stored in an input file:
Jiming Wu
Leanna Perez
Xing Li
Stacey Cahill
James Brown
Mohammed Abbas
Kumari Chakrabarti
Shakil Smith
James Brown
Jung Ahrin
Pedro Martinez
Ally Gu
Tamara White
Alvin Ngo
Pedro Martinez
Abir Fadel
Brad Feinman
Xiaohang Yue
  • Your task is to write a program to count up the number of duplicate names and remove the duplicates from the list.
  • To get full credit, you must do the following:
    • Read the names from the input file into an array of Strings (you can assign the array an initial length of 25)
    • Sort the the array using the insertionSort algorithm
    • Count up and remove the duplicates
    • Write the sorted, and duplicate-free, data into an output file.
  • Here is the starter code for this assignment. Please name this file Duplicates.java
/**
 * Duplicates.java
 * @author
 * CIS 36B
 */
import java.io.*;
import java.util.Scanner;

public class Duplicates {

    public static void main(String[] args) throws IOException{
        Scanner input = new Scanner(System.in);
        int duplicates = 0, numElements = 0;
        String fileName = "";
        String[] roster = new String[25]; //leaving extra room at end of array
        
        System.out.print("Welcome!\n\nPlease enter the name of your file: ");       
       
        System.out.println("\nI found " + numElements + " names in " + fileName
                + " and " + duplicates + " duplicate names.");

        System.out.print("\nPlease enter the name of the output file: ");
       
        System.out.println("\nThe updated roster has now been written to " + fileName + ".");
        System.out.println("\nGoodbye!");
    } //end of main
   
    /**
     * Writes the contents of the array to the specified file
     * with each element on its own line
     * @param array the list of Strings
     * @param numElements the current number of elements in the array
     * @param fileName the file name to write the data into
     */
    public static void printArray(String[] array,
            int numElements, String fileName) throws IOException {
       
    }
   
    /**
     * Reads in the contents of a file and stores it as an array
     * Also counts the number of elements in the file and returns
     * the count
     * @param array the list of String values
     * @param fileName the name of file to read from
     * @return the number of elements in the file
     */
    public static int readFileArray(String[] array, String fileName) throws IOException{
        return -1;
    }
   
    /**
     * Sorts an array of Strings from smallest to largest
     * using the insertion sort algorithm
     * @param array the list of String values
     * @param numElements the current number of elements
     */
   
    public static void insertionSort(String array[], int numElements) {
       
    }
   
    /**
     * Detects and removes duplicate values inside an array
     * Calls the remove method each time it finds a duplicate
     * @param array the list of String values containing duplicates
     * @param numElements the current number of elements stored
     * @return the total number of duplicates found
     */
    public static int removeDuplicates(String[] array, int numElements) {
        return -1;
    }
   
    /**
     * Removes an element from an array at a specified index
     * @param array the list of String values
     * @param numElements the current number of elements stored
     * @param indexToRemove where in the array to remove the element
     */
   
    public static void remove(String array[], int numElements, int indexToRemove) {
       
    }
}

  • Complete the method bodies for each method (including main)
  • Please do not alter the method signatures in any way or use any additional methods or you will not receive credit for this assignment.
  • When you are finished, your output should look identical to the sample output(s).
  • Upload Duplicates.java to Canvas once your program is working properly.

Sample Output:

Welcome!

Please enter the name of your file: roster.txt

I found 18 names in roster.txt and 2 duplicate names.

Please enter the name of the output file: outfile.txt

The updated roster has now been written to outfile.txt.

Goodbye!


Corresponding Output File:

Abir Fadel
Ally Gu
Alvin Ngo
Brad Feinman
James Brown
Jiming Wu
Jung Ahrin
Kumari Chakrabarti
Leanna Perez
Mohammed Abbas
Pedro Martinez
Shakil Smith
Stacey Cahill
Tamara White
Xiaohang Yue
Xing Li

Another Example Input File:

Jiming Wu
Leanna Perez
Xing Li
Stacey Cahill
James Brown
Mohammed Abbas
Kumari Chakrabarti
Shakil Smith
Tamara White
Jung Ahrin
Alvin Ngo
Pedro Martinez
Ally Gu
Tamara White
Kumari Chakrabarti
Alvin Ngo
Pedro Martinez
Abir Fadel
Xing Li
Brad Feinman
Xiaohang Yue

Sample Output

Welcome!

Please enter the name of your file: students36B.txt

I found 21 names in
students36B.txt and 5 duplicate names.

Please enter the name of the output file: sortedStudents.txt

The updated roster has now been written to
sortedStudents.txt.

Goodbye!

Corresponding Output File:

Abir Fadel
Ally Gu
Alvin Ngo
Brad Feinman
James Brown
Jiming Wu
Jung Ahrin
Kumari Chakrabarti
Leanna Perez
Mohammed Abbas
Pedro Martinez
Shakil Smith
Stacey Cahill
Tamara White
Xiaohang Yue
Xing Li