Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////// TOP OF FILE COMMENT BLOCK ////////////////////////////
- //
- // Title: Text Converter
- // Course: CS 200, Fall, 2019
- //
- // Author: Khosti Eshak
- // Email: eshak@wisc.edu
- // Lecturer's Name: Marc Renault
- //
- ///////////////////////////////// CITATIONS ////////////////////////////////////
- //
- // https://cs200-www.cs.wisc.edu/wp/syllabus/#academicintegrity
- // Source or Recipient; Description
- // Examples:
- // Jane Doe; helped me with for loop in reverse method
- // https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html;
- // counting for loop
- // John Doe; I helped with switch statement in main method.
- //
- /////////////////////////////// 80 COLUMNS WIDE ////////////////////////////////
- import java.util.Scanner;
- public class TextConverter {
- /**
- * 1337 - convert the string to leet-speak: Replace each L or l with a 1 (numeral one) Replace
- * each E or e with a 3 (numeral three) Replace each T or t with a 7 (numeral seven) Replace
- * each O or o with a 0 (numeral zero) Replace each S or s with a $ (dollar sign)
- *
- * @param current Original string
- * @return string converted to leet-speak.
- */
- public static String action1337(String current) {
- String leetString = "";
- int i = 0;
- for (i = 0; i < current.length(); i++) {
- if (current.charAt(i) == 'L' || current.charAt(i) == 'l') {
- leetString += "1";
- } else if (current.charAt(i) == 'E' || current.charAt(i) == 'e') {
- leetString += "3";
- } else if (current.charAt(i) == 'T' || current.charAt(i) == 't') {
- leetString += "7";
- } else if (current.charAt(i) == 'O' || current.charAt(i) == 'o') {
- leetString += "0";
- } else if (current.charAt(i) == 'S' || current.charAt(i) == 's') {
- leetString += "$";
- } else {
- leetString += current.charAt(i);
- }
- }
- return leetString; // FIX ME
- }
- /**
- * tests action1337 method with various cases to ensure it is working
- * correctly.
- */
- public static void testAction1337() {
- boolean error = false;
- String input1 = "LEeTs";
- String expected1 = "1337$";
- String result1 = action1337(input1);
- if (!result1.equals(expected1)) {
- error = true;
- System.out.println("1) testAction1337 with input " + input1 + ", expected: " + expected1
- + " but result:" + result1);
- }
- String input2 = " 1337$ ";
- String expected2 = " 1337$ ";
- String result2 = action1337(input2);
- if (!result2.equals(expected2)) {
- error = true;
- System.out.println("2) testAction1337 with input " + input2 + ", expected: " + expected2
- + " but result:" + result2);
- }
- String input3 = "1010010";
- String expected3 = "1010010";
- String result3 = action1337(input3);
- if (!result3.equals(expected3)) {
- error = true;
- System.out.println("3) testAction1337 with input " + input3 + ", expected: " + expected3
- + " but result:" + result3);
- }
- // FIX ME
- // add at least 2 other test cases. What edge cases can you think of?
- if (error) {
- System.out.println("testAction1337 failed");
- } else {
- System.out.println("testAction1337 passed");
- }
- }
- /**
- * This reverses the order of characters in the current string.
- * @param current Original string to be reversed.
- * @return The string in reversed order.
- */
- public static String actionReverse(String current) {
- String reverseString = "";
- int i = 0;
- for (i = (current.length() - 1); i >= 0; i--) {
- reverseString += current.charAt(i);
- }
- return reverseString;
- }
- /**
- * tests actionReverse method with various cases to ensure it is working
- * correctly.
- */
- public static void testActionReverse() {
- boolean error = false;
- String input1 = "Abc";
- String expected1 = "cbA";
- String result1 = actionReverse(input1);
- if (!result1.equals(expected1)) {
- error = true;
- System.out.println("1) testActionReverse with input " + input1 + ", expected: "
- + expected1 + " but result:" + result1);
- }
- String input2 = " hello world ";
- String expected2 = " dlrow olleh ";
- String result2 = actionReverse(input2);
- if (!result2.contentEquals(expected2)) {
- error = true;
- System.out.println("2) testActionReverse with input " + input2 + ", expected: "
- + expected2 + " but result:" + result2);
- }
- String input3 = "\\n\\t\\abc";
- String expected3 = "cba\\t\\n\\";
- String result3 = actionReverse(input3);
- if (!result3.contentEquals(expected3)) {
- error = true;
- System.out.println("3) testActionReverse with input " + input3 + ", expected: "
- + expected3 + " but result:" + result3);
- }
- // FIX ME
- // add at least 2 other test cases. What edge cases can you think of?
- if (error) {
- System.out.println("testActionReverse failed");
- } else {
- System.out.println("testActionReverse passed");
- }
- }
- /**
- * Provides the main menu for the text converter and calls methods based
- * on menu options chosen.
- * @param args
- */
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String command;
- String userString;
- System.out.println("Welcome to the Text Converter.");
- System.out.println("Available Actions:");
- System.out.println(" 1337) convert to 1337-speak");
- System.out.println(" rev) reverse the string");
- System.out.println(" quit) exit the program");
- System.out.println("");
- System.out.print("Please enter a string: ");
- userString = sc.nextLine();
- while (true) {
- System.out.print("Action (1337, rev, quit): ");
- command = sc.nextLine();
- if (command.equals("quit")) {
- System.out.println("See you next time!");
- break;
- } else if (command.equals("1337")) {
- userString = action1337(userString);
- System.out.println(userString);
- } else if (command.equals("rev")) {
- userString = actionReverse(userString);
- System.out.println(userString);
- } else {
- System.out.println("Unrecognized action.");
- System.out.println(userString);
- }
- }
- // testAction1337();
- // testActionReverse();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement