Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Name: George Muller
- ID: n00461436
- Date: 2-10-2012
- Class: COP3530 Assignment 3
- */
- import java.util.*;
- import java.text.*;
- import javax.swing.JOptionPane;
- public class n00461436 {
- public static void main(String[] args) {
- // Variables
- String DataIn;
- String STpeople = " ";
- String STnumber = " ";
- String STleft = " ";
- String stop = "Stop";
- boolean program = true;
- // do Loop
- do {
- // Try/Catch Loop
- try {
- //Get information
- 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);
- //Find out when to stop
- int input = DataIn.compareToIgnoreCase(stop);
- if (input == 0)
- {
- JOptionPane.showMessageDialog(null, "Thank You!");
- System.exit(0);
- }
- // Break up inputed String
- StringTokenizer st = new StringTokenizer(DataIn, "= ");
- while (st.hasMoreTokens()) {
- STpeople = st.nextToken();
- STnumber = st.nextToken();
- STleft = st.nextToken();
- }
- // Convert string to doubles
- int people = Integer.parseInt(STpeople);
- int number = Integer.parseInt(STnumber);
- int left = Integer.parseInt(STleft);
- if (people <= 0 || number <= 0 || left <= 0)
- JOptionPane.showMessageDialog(null, "Data can not be 0 or less");
- else {
- CircularList list = new CircularList();
- for(int i = 1; i < people+1; i++)
- list.insert(i);
- Link start = list.find(left);
- while(start.next != start){
- start = list.deleteAfter(start, number);
- }
- JOptionPane.showMessageDialog(null, "With Inputed data: " +
- "\nNumber of People: " + people +
- "\nNumber for Counting off: " + number +
- "\nNumber to Left where counting starts: " + left +
- "\nLast man standing is = " + list.toString());
- }
- }//Try
- // Catch Errors
- catch (NoSuchElementException ex)
- {
- JOptionPane.showMessageDialog(null, "Not Valid input, Try Again");
- }
- catch (NullPointerException ex)
- {
- JOptionPane.showMessageDialog(null, "Not Valid input, Try Again");
- }
- catch (NumberFormatException ex)
- {
- JOptionPane.showMessageDialog(null, "Not Valid input, Try Again");
- }
- } while (program);
- }
- }// End
- class Link {
- public int data;
- public Link next;
- public Link(int item){
- data = item;
- next = null;
- }
- }
- class CircularList{
- private Link first;
- private Link last;
- public CircularList (){
- first = null;}
- public void insert (int item) {
- Link newLink = new Link (item);
- if(first == null){
- first = newLink;
- first.next = first;
- last = first;
- }
- else {
- newLink.next = first;
- last.next = newLink;
- last = newLink;
- }
- }
- public Link find (int key) {
- Link current = first;
- while (current.data != key){
- if (current.next == null)
- return null;
- else
- current = current.next;
- }
- return current;
- }
- public Link delete (int key) {
- Link current = first;
- Link previous = last;
- while (current.data != key){
- if (current.next == null)
- return null;
- else {
- previous = current;
- current = current.next;
- }}
- if (current == first){
- first = first.next;
- last.next = first;
- }
- if(current == last)
- last = previous;
- previous.next = current.next;
- return current.next;
- }
- public Link deleteAfter (Link start, int n){
- Link current = start;
- for (int i = 1; i <= n; i++){
- current = current.next;
- }
- return delete(current.data);
- }
- public String toString (){
- StringBuffer buf = new StringBuffer();
- int count = 1;
- buf.append ( first.data + " " );
- Link current = first.next;
- while (current != first) {
- if (count == 4){
- buf.append (current.data + "\n");
- count = 0;
- }
- else {
- buf.append (current.data + " ");
- count++;
- }
- current = current.next;
- }
- return buf.toString();
- }
- } //End CircularList
Add Comment
Please, Sign In to add comment