Guest User

Untitled

a guest
Jun 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. /* Name: George Muller
  2.     ID: n00461436
  3.     Date: 2-10-2012
  4.     Class: COP3530 Assignment 3
  5. */
  6.  
  7. import java.util.*;
  8. import java.text.*;
  9. import javax.swing.JOptionPane;
  10.  
  11. public class n00461436 {
  12.     public static void main(String[] args) {
  13.         // Variables
  14.         String DataIn;
  15.         String STpeople = " ";
  16.         String STnumber = " ";
  17.         String STleft = " ";
  18.         String stop = "Stop";
  19.         boolean program = true;
  20.        
  21.     // do Loop
  22.         do {
  23.             // Try/Catch Loop
  24.             try {  
  25.            
  26.             //Get information
  27.             DataIn = JOptionPane.showInputDialog(null, "Enter Number of People, Number for Counting off, Number to Left where counting starts (or stop to exit): ", "Program", JOptionPane.QUESTION_MESSAGE);
  28.  
  29.             //Find out when to stop
  30.             int input = DataIn.compareToIgnoreCase(stop);
  31.                 if (input == 0)
  32.                     {
  33.                     JOptionPane.showMessageDialog(null, "Thank You!");
  34.                     System.exit(0);
  35.                     }
  36.                    
  37.             // Break up inputed String
  38.             StringTokenizer st = new StringTokenizer(DataIn, "= ");
  39.                 while (st.hasMoreTokens()) {
  40.                
  41.                     STpeople = st.nextToken();
  42.                     STnumber = st.nextToken();
  43.                     STleft =   st.nextToken();
  44.                    
  45.                     }
  46.  
  47.             // Convert string to doubles
  48.             int people = Integer.parseInt(STpeople);
  49.             int number = Integer.parseInt(STnumber);
  50.             int left =   Integer.parseInt(STleft);
  51.            
  52.            
  53.             if (people <= 0 || number <= 0 || left <= 0)
  54.                 JOptionPane.showMessageDialog(null, "Data can not be 0 or less");
  55.             else {
  56.            
  57.                     CircularList list = new CircularList();
  58.                     for(int i = 1; i < people+1; i++)
  59.                         list.insert(i);
  60.                     Link start = list.find(left);
  61.                     while(start.next != start){    
  62.                         start = list.deleteAfter(start, number);
  63.                         }
  64.                        
  65.                     JOptionPane.showMessageDialog(null, "With Inputed data: " +
  66.                              "\nNumber of People: " + people +
  67.                              "\nNumber for Counting off: " + number +
  68.                              "\nNumber to Left where counting starts: " + left +
  69.                              "\nLast man standing is = " + list.toString());
  70.                    
  71.                   }
  72.  
  73.              
  74.             }//Try
  75.             // Catch Errors
  76.             catch (NoSuchElementException ex)
  77.             {
  78.                 JOptionPane.showMessageDialog(null, "Not Valid input, Try Again");
  79.             }
  80.  
  81.             catch (NullPointerException ex)
  82.             {
  83.                 JOptionPane.showMessageDialog(null, "Not Valid input, Try Again");
  84.             }
  85.            
  86.             catch (NumberFormatException ex)
  87.             {
  88.                 JOptionPane.showMessageDialog(null, "Not Valid input, Try Again");
  89.             }
  90.  
  91.         } while (program);
  92.     }
  93.  
  94. }// End
  95.  
  96. class Link {
  97.     public int data;
  98.     public Link next;
  99.    
  100.     public Link(int item){
  101.         data = item;
  102.         next = null;
  103.     }
  104. }
  105.  
  106. class CircularList{
  107.     private Link first;
  108.     private Link last;
  109.    
  110.     public CircularList (){
  111.         first = null;}
  112.    
  113.     public void insert (int item) {
  114.         Link newLink = new Link (item);
  115.         if(first == null){
  116.             first = newLink;
  117.             first.next = first;
  118.             last = first;
  119.         }
  120.         else {
  121.             newLink.next = first;
  122.             last.next = newLink;
  123.             last = newLink;
  124.         }
  125.     }
  126.    
  127.     public Link find (int key) {
  128.         Link current = first;
  129.         while (current.data != key){
  130.             if (current.next == null)
  131.                 return null;
  132.             else
  133.                 current = current.next;
  134.         }
  135.         return current;
  136.     }
  137.  
  138.     public Link delete (int key) {
  139.         Link current = first;
  140.         Link previous = last;
  141.         while (current.data != key){
  142.             if (current.next == null)
  143.                 return null;
  144.             else {
  145.                 previous = current;
  146.                 current = current.next;
  147.             }}
  148.         if (current == first){
  149.             first = first.next;
  150.             last.next = first;
  151.             }
  152.         if(current == last)
  153.             last = previous;
  154.            
  155.             previous.next = current.next;
  156.         return current.next;       
  157.         }
  158.     public Link deleteAfter (Link start, int n){
  159.         Link current = start;
  160.         for (int i = 1; i <= n; i++){      
  161.             current = current.next;
  162.        
  163.         }
  164.         return delete(current.data);
  165.     }
  166.    
  167.     public String toString (){
  168.         StringBuffer buf = new StringBuffer();
  169.         int count = 1;
  170.  
  171.         buf.append ( first.data + " " );
  172.         Link current = first.next;
  173.         while (current != first) {
  174.             if (count == 4){
  175.                 buf.append (current.data + "\n");
  176.                 count = 0;
  177.             }
  178.             else {
  179.                 buf.append (current.data + " ");
  180.                 count++;
  181.             }  
  182.             current = current.next;
  183.         }
  184.         return buf.toString(); 
  185.         }
  186.  
  187.  
  188. } //End CircularList
Add Comment
Please, Sign In to add comment