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

Programming Steps: ( 1-3, then 4-5 at the end) (Make a class to do steps 1-3 and

ID: 3862258 • Letter: P

Question

Programming Steps: ( 1-3, then 4-5 at the end) (Make a class to do steps 1-3 and another class for steps 4-5)

1) Use the attached Name.Java and Comparable.java and the input file "p6Name.txt" to write a short program to sort the input file so that the output will be sorted on LastName, then FirstName. (use collections.sort)

2) Write the sorted Output to the output file "p6NameOut.txt".

3) HINTS: There are spaces between the first name and the last name fields, so your delimiter needs to take care of the case for one or more spaces.

The spacial character for one space is "\s" and "\s*" means 0 or more spaces. So, to refer to 1 or more spaces, you will need to use "\s\s*".

Note: '*' character in a pattern means '0 or more'.

Name.java:

/**
A mutable class that represents a person's name.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public class Name implements Comparable
{
   private String first; // First name
   private String last; // Last name

   public Name()
   {
       this("", "");
   } // end default constructor

   public Name(String firstName, String lastName)
   {
       first = firstName;
       last = lastName;
   } // end constructor

   public void setName(String firstName, String lastName)
   {
       setFirst(firstName);
       setLast(lastName);
   } // end setName

   public String getName()
   {
       return toString();
   } // end getName

   public void setFirst(String firstName)
   {
       first = firstName;
   } // end setFirst

   public String getFirst()
   {
       return first;
   } // end getFirst

   public void setLast(String lastName)
   {
       last = lastName;
   } // end setLast

   public String getLast()
   {
       return last;
   } // end getLast

   public void giveLastNameTo(Name aName)
   {
       aName.setLast(last);
   } // end giveLastNameTo

   public String toString()
   {
       return first + " " + last;
   } // end toString

public int compareTo(Name other)
{
int result = last.compareTo(other.last);
  
// If last names are equal, check first names
if (result == 0)
result = first.compareTo(other.first);

//return result;
return (first + last).compareTo(other.getFirst() + other.getLast());
} // end compareTo
} // end Name

Comparable.java:

public interface Comparable
{
   public static int compareTo(Object o1, Object o2){
       Name p1 = (Name) o1;
Name p2 = (Name) o2;
int res = p1.getLast().compareToIgnoreCase(p2.getLast());
if (res != 0)
return res;
return p1.getFirst().compareToIgnoreCase(p2.getFirst());
   }
}

p6Name.txt:

4) Now, Create a program using Doubly Linked List to sort the same input file "p6Name.txt" in Ascending (Alphabetical Order).

5) Write the output to the output file "p6NameOut2.txt"

Make a class for Doubly Linked List for implementation of Doubly Linked List, and then write required code to do programming steps 4 and 5.

Explanation / Answer

1,2,3.

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class SortName {

   public static void main(String[] args)
   {
       try
       {
           File f = new File("p6Name.txt");
           Scanner sc = new Scanner(f);
          
           List<Name> nameList = new ArrayList<>();
          
           while(sc.hasNextLine())
           {
               String line = sc.nextLine();
               String[] name = line.split("\s+");
              
               Name n = new Name(name[0], name[1]);
               nameList.add(n);
           }
           Collections.sort(nameList);
          
           FileWriter fw = new FileWriter("p6NameOut.txt");
           for(Name n: nameList)
           {
               fw.write(n.toString());
               fw.write(" ");
           }
           fw.close();
       }
       catch (Exception e)
       {
           System.out.println(e);
       }
   }
}