Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.00 KB | None | 0 0
  1. ///////////////////////// TOP OF FILE COMMENT BLOCK ////////////////////////////
  2. //
  3. // Title: Text Converter
  4. // Course: CS 200, Fall, 2019
  5. //
  6. // Author: Khosti Eshak
  7. // Email: eshak@wisc.edu
  8. // Lecturer's Name: Marc Renault
  9. //
  10. ///////////////////////////////// CITATIONS ////////////////////////////////////
  11. //
  12. // https://cs200-www.cs.wisc.edu/wp/syllabus/#academicintegrity
  13. // Source or Recipient; Description
  14. // Examples:
  15. // Jane Doe; helped me with for loop in reverse method
  16. // https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html;
  17. // counting for loop
  18. // John Doe; I helped with switch statement in main method.
  19. //
  20. /////////////////////////////// 80 COLUMNS WIDE ////////////////////////////////
  21.  
  22. import java.util.Scanner;
  23.  
  24. public class TextConverter {
  25.     /**
  26.      * 1337 - convert the string to leet-speak: Replace each L or l with a 1 (numeral one) Replace
  27.      * each E or e with a 3 (numeral three) Replace each T or t with a 7 (numeral seven) Replace
  28.      * each O or o with a 0 (numeral zero) Replace each S or s with a $ (dollar sign)
  29.      *
  30.      * @param current Original string
  31.      * @return string converted to leet-speak.
  32.      */
  33.     public static String action1337(String current) {
  34.         String leetString = "";
  35.         int i = 0;
  36.  
  37.         for (i = 0; i < current.length(); i++) {
  38.             if (current.charAt(i) == 'L' || current.charAt(i) == 'l') {
  39.                 leetString += "1";
  40.             } else if (current.charAt(i) == 'E' || current.charAt(i) == 'e') {
  41.                 leetString += "3";
  42.             } else if (current.charAt(i) == 'T' || current.charAt(i) == 't') {
  43.                 leetString += "7";
  44.             } else if (current.charAt(i) == 'O' || current.charAt(i) == 'o') {
  45.                 leetString += "0";
  46.             } else if (current.charAt(i) == 'S' || current.charAt(i) == 's') {
  47.                 leetString += "$";
  48.             } else {
  49.                 leetString += current.charAt(i);
  50.             }
  51.         }
  52.  
  53.         return leetString; // FIX ME
  54.     }
  55.  
  56.     /**
  57.      *  tests action1337 method with various cases to ensure it is working
  58.      *  correctly.
  59.      */
  60.     public static void testAction1337() {
  61.         boolean error = false;
  62.  
  63.         String input1 = "LEeTs";
  64.         String expected1 = "1337$";
  65.         String result1 = action1337(input1);
  66.         if (!result1.equals(expected1)) {
  67.             error = true;
  68.             System.out.println("1) testAction1337 with input " + input1 + ", expected: " + expected1
  69.                 + " but result:" + result1);
  70.         }
  71.  
  72.         String input2 = "   1337$   ";
  73.         String expected2 = "   1337$   ";
  74.         String result2 = action1337(input2);
  75.         if (!result2.equals(expected2)) {
  76.             error = true;
  77.             System.out.println("2) testAction1337 with input " + input2 + ", expected: " + expected2
  78.                 + " but result:" + result2);
  79.         }
  80.  
  81.         String input3 = "1010010";
  82.         String expected3 = "1010010";
  83.         String result3 = action1337(input3);
  84.         if (!result3.equals(expected3)) {
  85.             error = true;
  86.             System.out.println("3) testAction1337 with input " + input3 + ", expected: " + expected3
  87.                 + " but result:" + result3);
  88.         }
  89.  
  90.  
  91.  
  92.         // FIX ME
  93.         // add at least 2 other test cases. What edge cases can you think of?
  94.  
  95.  
  96.         if (error) {
  97.             System.out.println("testAction1337 failed");
  98.         } else {
  99.             System.out.println("testAction1337 passed");
  100.         }
  101.     }
  102.  
  103.  
  104.  
  105.     /**
  106.      * This reverses the order of characters in the current string.
  107.      * @param current  Original string to be reversed.
  108.      * @return  The string in reversed order.
  109.      */
  110.     public static String actionReverse(String current) {
  111.         String reverseString = "";
  112.         int i = 0;
  113.         for (i = (current.length() - 1); i >= 0; i--) {
  114.             reverseString += current.charAt(i);
  115.         }
  116.         return reverseString;
  117.     }
  118.  
  119.     /**
  120.      *  tests actionReverse method with various cases to ensure it is working
  121.      *  correctly.
  122.      */
  123.     public static void testActionReverse() {
  124.         boolean error = false;
  125.  
  126.         String input1 = "Abc";
  127.         String expected1 = "cbA";
  128.         String result1 = actionReverse(input1);
  129.         if (!result1.equals(expected1)) {
  130.             error = true;
  131.             System.out.println("1) testActionReverse with input " + input1 + ", expected: "
  132.                 + expected1 + " but result:" + result1);
  133.         }
  134.  
  135.         String input2 = "   hello world   ";
  136.         String expected2 = "   dlrow olleh   ";
  137.         String result2 = actionReverse(input2);
  138.         if (!result2.contentEquals(expected2)) {
  139.             error = true;
  140.             System.out.println("2) testActionReverse with input " + input2 + ", expected: "
  141.                 + expected2 + " but result:" + result2);
  142.         }
  143.  
  144.         String input3 = "\\n\\t\\abc";
  145.         String expected3 = "cba\\t\\n\\";
  146.         String result3 = actionReverse(input3);
  147.         if (!result3.contentEquals(expected3)) {
  148.             error = true;
  149.             System.out.println("3) testActionReverse with input " + input3 + ", expected: "
  150.                 + expected3 + " but result:" + result3);
  151.         }
  152.  
  153.  
  154.         // FIX ME
  155.         // add at least 2 other test cases. What edge cases can you think of?
  156.  
  157.  
  158.         if (error) {
  159.             System.out.println("testActionReverse failed");
  160.         } else {
  161.             System.out.println("testActionReverse passed");
  162.         }
  163.     }
  164.  
  165.     /**
  166.      * Provides the main menu for the text converter and calls methods based
  167.      * on menu options chosen.
  168.      * @param args
  169.      */
  170.     public static void main(String[] args) {
  171.         Scanner sc = new Scanner(System.in);
  172.         String command;
  173.         String userString;
  174.  
  175.         System.out.println("Welcome to the Text Converter.");
  176.         System.out.println("Available Actions:");
  177.         System.out.println("  1337) convert to 1337-speak");
  178.         System.out.println("  rev) reverse the string");
  179.         System.out.println("  quit) exit the program");
  180.         System.out.println("");
  181.  
  182.         System.out.print("Please enter a string: ");
  183.         userString = sc.nextLine();
  184.  
  185.         while (true) {
  186.             System.out.print("Action (1337, rev, quit): ");
  187.             command = sc.nextLine();
  188.  
  189.             if (command.equals("quit")) {
  190.                 System.out.println("See you next time!");
  191.                 break;
  192.             } else if (command.equals("1337")) {
  193.                 userString = action1337(userString);
  194.                 System.out.println(userString);
  195.             } else if (command.equals("rev")) {
  196.                 userString = actionReverse(userString);
  197.                 System.out.println(userString);
  198.             } else {
  199.                 System.out.println("Unrecognized action.");
  200.                 System.out.println(userString);
  201.             }
  202.         }
  203.         // testAction1337();
  204.         // testActionReverse();
  205.  
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement