Advertisement
andoird213

Main.java

Jul 29th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Author: Andrew Wagner
  5.  * Project: ExploratoryProject
  6.  * Date: 7/29/13
  7.  * Time: 9:19 PM
  8.  */
  9. public class RunMain {
  10.  
  11.     public static void main(String args[]) {
  12.  
  13.         //Question/Command Strings
  14.         String enterName = "What is your name?";
  15.         String enterGender = "Are you male or female? (Type 1 for Male, or 2 for Female)\n1. Male\n2. Female";
  16.         String enterAge = "How old are you?";
  17.         String enterColors = "What are three of your favorite colors?";
  18.         String enterColorOne = "Color One: ";
  19.         String enterColorTwo = "Color Two: ";
  20.         String enterColorThree = "Color Three: ";
  21.         String enterColorFour = "Color Four: ";
  22.         String enterColorFive = "Color Five: ";
  23.         String queryQuestion = "Would you like to perform a search? Type \"y\" for yes, or \"n\" for no.";
  24.         String queryPrompt =  "Search for user-inputted color: ";
  25.  
  26.         //Return data Strings
  27.         String nameIs = "Your name is ";
  28.         String genderIs = "You are a ";
  29.         String ageIs = "You are ";
  30.         String yrs = " yrs old";
  31.         String colorsAre = "Your favorite colors are: ";
  32.         String yourQueriedColor = "Your queried color, ";
  33.         String colorFoundAtPos = ", was found at position ";
  34.  
  35.         //Error messages
  36.         String noGender = "Your gender could not be determined... o.O";
  37.         String noColor = ", was not found";
  38.  
  39.         //Loading messages
  40.         String genderSorting = "Sorting favorite colors alphabetically...";
  41.  
  42.         //Misc
  43.         String boy = "boy";
  44.         String girl = "girl";
  45.         String period = ".";
  46.         String exclaimation = "!";
  47.  
  48.         String termProgram = "Ok, terminating program";
  49.  
  50.         Scanner scanner;
  51.  
  52.         //Input values
  53.         String name;
  54.         int genderInput;
  55.         int age;
  56.         String colorOne;
  57.         String colorTwo;
  58.         String colorThree;
  59.         String colorFour;
  60.         String colorFive;
  61.  
  62.         //Output values
  63.         String gender;
  64.  
  65.         //Sorting vars
  66.         int index;
  67.         boolean swapped = false;
  68.  
  69.         //Search vars
  70.         String queryConfirm;
  71.         String colorQuery;
  72.         int min = 0;
  73.         int max;
  74.         int med;
  75.         int returnIndex = -1;
  76.  
  77.         scanner = new Scanner(System.in);
  78.  
  79.         //Ask for user input
  80.         System.out.println(enterName);
  81.         name = scanner.nextLine();
  82.         System.out.println(enterGender);
  83.         genderInput = scanner.nextInt();
  84.         switch(genderInput) {
  85.             case 1:
  86.                 gender = boy;
  87.                 break;
  88.             case 2:
  89.                 gender = girl;
  90.                 break;
  91.             default:
  92.                 gender = "";
  93.                 break;
  94.         }
  95.         System.out.println(enterAge);
  96.         age = scanner.nextInt();
  97.         System.out.println(enterColors);
  98.         System.out.println(enterColorOne);
  99.         scanner.nextLine();
  100.         colorOne = scanner.nextLine();
  101.         System.out.println(enterColorTwo);
  102.         colorTwo = scanner.nextLine();
  103.         System.out.println(enterColorThree);
  104.         colorThree = scanner.nextLine();
  105.         System.out.println(enterColorFour);
  106.         colorFour = scanner.nextLine();
  107.         System.out.println(enterColorFive);
  108.         colorFive = scanner.nextLine();
  109.         String[] colors = {colorOne, colorTwo, colorThree, colorFour, colorFive};
  110.  
  111.         //Return user input
  112.         System.out.println(nameIs + name + period);
  113.         if(!gender.equals("")) {
  114.             System.out.println(genderIs + gender + period);
  115.         } else {
  116.             System.out.println(noGender);
  117.         }
  118.  
  119.         //Color sorting alphabetically
  120.         System.out.println(genderSorting);
  121.  
  122.         do {
  123.             swapped = false;
  124.  
  125.             for(index = 0; index < (colors.length - 1); index++) {
  126.                 if(colors[index].charAt(0) > colors[index + 1].charAt(0)) {
  127.                     String replaced = colors[index + 1];
  128.  
  129.                     colors[index + 1] = colors[index];
  130.                     colors[index] = replaced;
  131.                     swapped = true;
  132.                 }
  133.             }
  134.         }  while (swapped);
  135.  
  136.         //Return alphabetically sorted array
  137.         System.out.println(colorsAre);
  138.         for(index = 0; index < colors.length; index++) {
  139.             System.out.println(colors[index]);
  140.         }
  141.  
  142.         //Ask for query
  143.         System.out.println(queryQuestion);
  144.         queryConfirm = scanner.nextLine();
  145.  
  146.         //If yes, search for inputted color
  147.         if(queryConfirm.toLowerCase().equals("y")) {
  148.             System.out.println(queryPrompt);
  149.             colorQuery = scanner.nextLine();
  150.  
  151.             max = colors.length;
  152.             //TODO Check second, third, fourth, etc. letters + compare
  153.             while(max >= min) {
  154.                 med = ((min + max) / 2);
  155.                 if(colors[med].charAt(0) > colorQuery.charAt(0)) {
  156.                     max = (med - 1);
  157.                 } else if (colors[med].charAt(0) < colorQuery.charAt(0)) {
  158.                     min = (med + 1);
  159.                 } else {
  160.                     max = -1;
  161.                     returnIndex = med;
  162.                 }
  163.             }
  164.  
  165.             if(returnIndex != -1) {
  166.                 System.out.println(yourQueriedColor + colorQuery + colorFoundAtPos + (returnIndex + 1) + period);
  167.             } else {
  168.                 System.out.println(yourQueriedColor + colorQuery + noColor + exclaimation);
  169.             }
  170.  
  171.             //If no, print termination of program to console
  172.         } else {
  173.             System.out.println(termProgram + period);
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement