Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

magic8ball1

By: ace on Oct 13th, 2010  |  syntax: None  |  size: 2.35 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package Magic8Ball2;
  2.  
  3. import java.io.BufferedReader;
  4.         import java.io.IOException;
  5.         import java.io.InputStreamReader;
  6.         import java.util.ArrayList;
  7.         import java.util.Random;
  8.  
  9.  
  10.         public class Magic8Ball {
  11.  
  12.                 private ArrayList<String> foo;
  13.                
  14.                 public Magic8Ball(){
  15.                        
  16.                 }
  17.                
  18.                 public static void main(String[] args){
  19.                        
  20.                 }
  21.                
  22.                 /**
  23.                  * Adds an array list of answers to the magic8ball.
  24.                  * @param al an ArrayList of Strings
  25.                  */
  26.                 public void addArrayList(ArrayList<String> al){
  27.                         foo = al;
  28.                         populateArray();
  29.                 }
  30.                
  31.                 public ArrayList<String> getArrayList(){
  32.                         return foo;
  33.                 }
  34.                
  35.                 /**
  36.                  * Prints the answer corresponding with a given number.
  37.                  * @param number
  38.                  */
  39.                 public void printAnswer(String answer){
  40.                         System.out.println(answer);
  41.                 }
  42.  
  43.                 public String getAnswer(int number){
  44.                         return foo.get(number);
  45.                 }
  46.                
  47.                
  48.                 /**
  49.                  * Prints the answer corresponding with a number, entered as a string.
  50.                  * @param input
  51.                  */
  52.                 public void answerByNumber(String input){
  53.                         int number = Integer.parseInt(input);
  54.                         printAnswer(number);
  55.                 }
  56.                
  57.                 /**
  58.                  * Gives a random answer.
  59.                  */
  60.                 public void randomAnswer(){             Random rnd = new Random();
  61.                         int n = rnd.nextInt(8);
  62.                         printAnswer(n);
  63.                 }
  64.                
  65.                 /**
  66.                  * You may not change this method.
  67.                  * Populates the Answers with a default answer set.
  68.                  */
  69.                 private void populateArray(){
  70.                         foo.add("Things look rosy from here.");
  71.                         foo.add("Only when the sun is shining.");
  72.                         foo.add("You have the power of the prophets behind you.");
  73.                         foo.add("I would tell you, but then I'd have to kill you.");
  74.                         foo.add("I wouldn't if I were you. But you wouldn't if you were an 8 ball.");
  75.                         foo.add("On a clear day I can see the answer. Today's forecast is cloudy.");
  76.                         foo.add("Snowball's chance in hell.");
  77.                         foo.add("I'm ashamed at you for asking.");
  78.                 }
  79.                
  80.                 /**
  81.                  * You may not change this method.
  82.                  * This method accepts user input from the command line.
  83.                  */
  84.                 private void getInput(){
  85.                         System.out.print("Enter a number from 1 to 8:");
  86.                         String input = null;
  87.                         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  88.                         try {
  89.                          input = br.readLine();
  90.                       } catch (IOException ioe) {
  91.                          System.out.println("IO error trying to read input!");
  92.                          System.exit(1);
  93.                       }
  94.                     answerByNumber(input);
  95.                 }
  96.        
  97. }