Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import javax.swing.JOptionPane;
- public class A21 {
- public static void main(String[] args) {
- LinkedList list = new LinkedList(); // instantiate LinkedList list object
- File file = new File(args[0]); // read file
- String name = new String();
- Integer age = new Integer(0);
- Scanner scan = null;
- try {
- scan = new Scanner(file).useDelimiter("\\s*,\\s*"); // set scanner's delimiting pattern to the specified pattern.
- if(scan != null) {
- while(scan.hasNext() || scan.hasNextInt() || scan.hasNextLine()) {
- name = scan.next(); // search name
- age = scan.nextInt(); // search age
- name = name.replace(",", ""); // replace commas stored in String age
- if(!list.equals("name, age")) {
- list.add(name, age); // add to list
- }
- }
- }
- // A19 component
- JOptionPane.showMessageDialog(null, list.toString()); // print unsorted persons
- // A21 components
- list.bubbleSort(); // sort
- JOptionPane.showMessageDialog(null, list.toString()); // print sorted persons
- } // end of try-catch
- catch(FileNotFoundException fnfe) {
- JOptionPane.showMessageDialog(null, "ERROR: " + fnfe.getMessage());
- }
- catch(InputMismatchException ime) {
- JOptionPane.showMessageDialog(null, "ERROR: " + ime.getMessage());
- }
- catch(NoSuchElementException nsee) {
- JOptionPane.showMessageDialog(null, "ERROR: " + nsee.getMessage());
- }
- catch(NullPointerException npe) {
- JOptionPane.showMessageDialog(null, "ERROR: " + npe.getMessage());
- }
- catch(NumberFormatException nfe) {
- JOptionPane.showMessageDialog(null, "ERROR: " + nfe.getMessage());
- }
- } // end of main()
- } // end
- /**
- * Implementments the PersonNode.
- * @author Jung Bum Lee
- */
- class LinkedList{
- protected PersonNode head;
- protected String output;
- protected Integer size;
- /**
- * Initialize PersonNode head, String output, and Integer size objects.
- */
- public LinkedList() {
- head = null;
- output = new String();
- size = new Integer(0);
- }
- /**
- * Adds String name and Integer age from PersonNode to list
- * @param name is the object that is added to list
- */
- public void add(String name, Integer age) {
- if (head == null) {
- head = new PersonNode(name, age, null); // assign name and age from PersonNode to an empty list
- }
- else {
- PersonNode previous = head; // add to the end of the list
- PersonNode current = head.getNext(); // current getNext() new name and age from PersonNode
- while (current != null) {
- previous = current; // swap previous with current PersonNode object
- current = current.getNext(); // current getNext() name and age from PersonNode
- }
- PersonNode node = new PersonNode(name, age, null); // create a new PersonNode object
- previous.setNext(node); // set list as new PersonNode object
- }
- size++; // increase size
- } //end of add()
- /**
- * Displays the data stored in the data fields of name and age (in years) objects.
- * @return "[object name] is [object age] years old."
- */
- public String toString() {
- String message = new String();
- for(PersonNode current = head; current != null; current = current.getNext()) {
- message = message + " " + current.toString() + "\n";
- }
- return message;
- }
- /**
- * A sorting algorithm that works by repeatedly stepping through the list to be sorted,
- * comparing each pair of adjacent items and swapping them if they are in the wrong
- * order.
- */
- public void bubbleSort() {
- String name = new String();
- Integer age = new Integer(0);
- boolean swap = false; // default swap
- //loop stops when swap not needed, list ordered
- while(!swap) {
- swap = true;
- for(PersonNode current = head; current.getNext() != null; current = current.getNext()){
- if(current.getAge() > current.getNext().getAge()){
- name = current.getName();
- age = current.getAge();
- current.setName(current.getNext().getName());
- current.setAge(current.getNext().getAge());
- current.getNext().setName(name);
- current.getNext().setAge(age);
- swap = false;
- }
- }
- }
- }
- } // end of LinkedList()
- /**
- * Stores the reference to Data and the next Node.
- */
- class PersonNode {
- protected String name; // instantiate String name data field
- protected Integer age; // instantiate Integer age data field
- protected PersonNode next; // instantiate PersonNode next data field
- /**
- * Constructor that is used to create each object and
- * initialize data fields.
- * @param name2 initializes the name reference variable.
- * @param age2 initializes the age reference variable.
- * @param next2 initializes the next reference variable.
- */
- public PersonNode(String name2, Integer age2, PersonNode next2) {
- name = name2;
- age = age2;
- next = next2;
- } // end of PersonNode()
- /**
- * Displays the data stored in the data fields of name and age (in years) objects.
- * @return "[object name] is [object age] years old."
- */
- public String toString() {
- return name + " is " + age + " years old.";
- } // end of toString()
- /**
- * Accessor method that is used to get data fields.
- * @return the address to the next node
- */
- public PersonNode getNext() {
- return next;
- } // end of getNext()
- /**
- * Mutator method that is used to set data fields.
- * @param next2 is a pointer to the next node.
- */
- public void setNext(PersonNode next2) {
- next = next2;
- } // end of setNext()
- /**
- * Accessor method that is used to get data fields.
- * @return the address to the next node
- */
- public String getName() {
- return name;
- } // end of getName()
- /**
- * Mutator method that is used to set data fields.
- * @param next2 is a pointer to the next node.
- */
- public void setName(String name2) {
- name = name2;
- } // end of setName()
- /**
- * Accessor method that gets the state of an object to be
- * accessed (retrieved) from other parts of a program.
- * @return String age
- */
- public Integer getAge() {
- return age;
- } // end of getAge()
- /**
- * Mutator method that is used to set data fields.
- * @param next2 is a pointer to the next node.
- */
- public void setAge(Integer age2) {
- age = age2;
- } // end of setAge()
- } // end of PersonNode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement