Advertisement
Vermiculus

Untitled

Nov 1st, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Queue;
  3.  
  4. public class PriorityProject {
  5.     //a large ArrayList to hold the information once it has been prioritized
  6.     ArrayList<String> mrah = new ArrayList<String>();
  7.    
  8.     //constructor
  9.     public PriorityProject(ArrayList moose){
  10.         mrah = moose;
  11.     }
  12.  
  13.     //precondition: mrah exists, and a string name and an int priority exist
  14.     //postcondition: the string and int are combined into a string and added to the ArrayList mrah
  15.     public void enqueue(String name, int priority) {
  16.         String stuffs = (name + priority);
  17.         mrah.add(stuffs);
  18.     }
  19.  
  20.     //preconditon: mrah exists
  21.     //postcondition: returnes what is about to be removed, before it is removed
  22.     public String dequeuereturn() {
  23.         return mrah.get(0);
  24.     }
  25.  
  26.     //precondition: mrah exists
  27.     //postcondition: the first element in the ArrayList is removed
  28.     public void dequeueremove() {
  29.         mrah.remove(0);
  30.     }
  31.  
  32.     //precondition: mrah exists
  33.     //postcondition: if mrah has any elements it returns true, otherwise (since it is empty) it returns false
  34.     public boolean isEmpty() {
  35.         if (mrah.size() > -1) {
  36.             return true;
  37.         } else {
  38.             return false;
  39.         }
  40.     }
  41.  
  42.     //precondition: mrah (an ArrayList) exists
  43.     //postcondition: returns the first element inputted into the ArrayList
  44.     public String front() {
  45.         return mrah.get(0);
  46.     }
  47.  
  48.     public static void main(String[] args) {
  49.         //An ArrayList to hold the city's name
  50.         ArrayList<String> words = new ArrayList<String>();
  51.        
  52.         //an ArrayList that takes in the name and priority number as one string and holds it until the String and int can be separated
  53.         ArrayList<String> everything = new ArrayList<String>();
  54.        
  55.         //an ArrayList to be passed to the constructor.  Does the same thing as ArrayList mrah
  56.         ArrayList <String> thing = new ArrayList <String> ();
  57.  
  58.         //the information is added
  59.         everything.add("Columbus , 3");// 1
  60.         everything.add("New York , 5");// 2
  61.         everything.add("Baltimore , 4");// 3
  62.         everything.add("Greencastle , 1");// 4
  63.         everything.add("Washington , 5");// 5
  64.         everything.add("Atlanta , 4");// 6
  65.         everything.add("Seneca , 1");// 7
  66.         everything.add("Frederick , 3");// 8
  67.         everything.add("Columbia , 3");// 9
  68.         everything.add("Dallas , 4");// 10
  69.         everything.add("Los Angeles , 5");// 11
  70.  
  71.         // separates the string from the priority level
  72.         for (int i = 0; i < everything.size(); i++) {
  73.            
  74.             //takes in the first line and divides it into an array of Characters
  75.             char[] array = everything.get(i).toCharArray();
  76.            
  77.             //goes through the arrya of characters
  78.             for (int j = 0; j < array.length; j++) {
  79.                 //when a comma is seen...
  80.                 if (array[j] == (',')) {
  81.                     //the array of Characters is turned back into a String
  82.                     String str = new String(array);
  83.                     //then, the string is reduced to everything BEFORE the comma, and held in ArrayList words
  84.                     str = str.substring(0, j);
  85.                     words.add(str);
  86.                 }
  87.             }
  88.         }
  89.         PriorityProject p = new PriorityProject(thing);
  90.        
  91.         // separates the priority level from the string
  92.         for (int c = 5; c > 0; c--) {
  93.             //does essentially the same thing as the above for loop
  94.             for (int y = 0; y < everything.size(); y++) {
  95.                 char[] arrays = everything.get(y).toCharArray();
  96.                 for (int j = 0; j < arrays.length; j++) {
  97.                     if (arrays[j] == (',')) {
  98.                         //except here it takes the character two spaces away from the comma(which is the number)
  99.                         Character ch = arrays[j + 2];
  100.                         //turns that character into a string
  101.                         String s = ch.toString();
  102.                         //parses that string into an int (there doesnt seem to be a way to go directly from Char to Int)
  103.                         int x = Integer.parseInt(s);
  104.                         if (x == c) {
  105.                             p.enqueue(words.get(y), x);
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.  
  112.         //prints out the now prioritized ArrayList
  113.         for (int h =0; h<thing.size(); h++){
  114.             System.out.println(thing.get(h));
  115.            
  116.         }
  117.  
  118.  
  119.  
  120.     }
  121.  
  122. }
  123.  
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement