diff --git a/ATM.java b/ATM.java new file mode 100644 index 0000000..267501d --- /dev/null +++ b/ATM.java @@ -0,0 +1,47 @@ +/** + * When you use an automated teller machine (ATM) with + * your bank card, you need to use a personal identification + * number (PIN) to access your account. If a user fails more + * than three times when entering the PIN, the machine will + * block the card. Assume that the user’s PIN is “1234” and + * write a program that asks the user for the PIN no more than + * three times, and does the following: + * • If the user enters the right number, print a message saying, “Your PIN is + * correct”, and end the program. + * • If the user enters a wrong number, print a message saying, “Your PIN is + * incorrect” and, if you have asked for the PIN less than three times, ask for + * it + * again. + * • If the user enters a wrong number three times, print a message saying “Your + * bank card is blocked” and end the program. + */ + +public class ATM { + private final String pin = "1234"; + private int attempts = 0; + + public ATM() { + } + + public void checkPin(String pin) { + if (this.pin.equals(pin)) { + System.out.println("Your PIN is correct"); + System.exit(0); + } else { + System.out.println("Your PIN is incorrect"); + if (attempts < 2) { + attempts++; + } else { + System.out.println("Your bank card is blocked"); + System.exit(0); + } + } + } + + public static void main(String... args) { + ATM atm = new ATM(); + atm.checkPin("1235"); + atm.checkPin("1236"); + atm.checkPin("1237"); + } +} diff --git a/Magpie/Activity_2/Magpie2.java b/Magpie/Activity_2/Magpie2.java new file mode 100644 index 0000000..1973950 --- /dev/null +++ b/Magpie/Activity_2/Magpie2.java @@ -0,0 +1,104 @@ +package Magpie.Activity_2; + +/** + * A program to carry on conversations with a human user. + * This is the initial version that: + * + * This version uses a nested if to handle default responses. + * + * @author Laurie White + * @version April 2012 + */ +public class Magpie2 { + /** + * Get a default greeting + * + * @return a greeting + */ + public String getGreeting() { + return "Hello, let's talk."; + } + + /** + * Gives a response to a user statement + * + * @param statement + * the user statement + * @return a response based on the rules given + */ + public String getResponse(String statement) { + String response = ""; + if (statement.indexOf("no") >= 0) { + response = "Why so negative?" ; + } else if (statement.indexOf(" mother ") >= 0 + || statement.indexOf(" father ") >= 0 + || statement.indexOf(" sister ") >= 0 + || statement.indexOf(" brothe r") >= 0) { + response = "Tell me more about your family."; + } else if (index(statement, " dog ") || index(statement, " cat ")) { + response = "Tell me more about your pets."; + } else if (index(statement, " Mr. ") || index(statement, " Mrs. ") || index(statement, " Ms. ")) { + response = "They sound like a good teacher."; + } else if (statement == "") { + response = "Say something, please."; + } else if (index(statement, " food ")) { + response = "What's your favorite food?"; + } else if (index(statement, " color ")) { + response = "What's your favorite color?"; + } else if (index(statement, " movie ")) { + response = "What's your favorite movie?"; + } else if (index(statement, " book ")) { + response = "What's your favorite book?"; + } else if (index(statement, " sport ")) { + response = "What's your favorite sport?"; + } else { + response = getRandomResponse(); + } + return response; + } + + /** + * Pick a default response to use if nothing else fits. + * + * @return a non-committal string + */ + private String getRandomResponse() { + final int NUMBER_OF_RESPONSES = 4; + double r = Math.random(); + int whichResponse = (int) (r * NUMBER_OF_RESPONSES); + String response = ""; + + if (whichResponse == 0) { + response = "Interesting, tell me more."; + } else if (whichResponse == 1) { + response = "Hmmm."; + } else if (whichResponse == 2) { + response = "Do you really think so?"; + } else if (whichResponse == 3) { + response = "You don't say."; + } + + return response; + } + + /** + * Search for one word in phrase. The search is not case sensitive. + * @param string1 full statement + * @param string2 substring to search for + * @return true if the substring is found, false otherwise + */ + private boolean index(String string1, String string2) { + if (string1.indexOf(string2) >= 0) { + return true; + } else { + return false; + } + } +} \ No newline at end of file diff --git a/Magpie/Activity_2/MagpieRunner2.java b/Magpie/Activity_2/MagpieRunner2.java new file mode 100644 index 0000000..e43c4bf --- /dev/null +++ b/Magpie/Activity_2/MagpieRunner2.java @@ -0,0 +1,31 @@ +package Magpie.Activity_2; + +import java.util.Scanner; + +/** + * A simple class to run the Magpie class. + * + * @author Laurie White + * @version April 2012 + */ +public class MagpieRunner2 { + + /** + * Create a Magpie, give it user input, and print its replies. + */ + public static void main(String[] args) { + Magpie2 maggie = new Magpie2(); + + System.out.println(maggie.getGreeting()); + Scanner in = new Scanner(System.in); + String statement = in.nextLine(); + + while (!statement.equals("Bye")) { + System.out.println(maggie.getResponse(statement)); + statement = in.nextLine(); + } + + in.close(); + } + +} \ No newline at end of file diff --git a/Magpie/Activity_3/Magpie3.java b/Magpie/Activity_3/Magpie3.java new file mode 100644 index 0000000..c697aee --- /dev/null +++ b/Magpie/Activity_3/Magpie3.java @@ -0,0 +1,152 @@ +package Magpie.Activity_3; + +/** + * A program to carry on conversations with a human user. + * This version: + * + * + * @author Laurie White + * @version April 2012 + */ +public class Magpie3 { + /** + * Get a default greeting + * + * @return a greeting + */ + public String getGreeting() { + return "Hello, let's talk."; + } + + /** + * Gives a response to a user statement + * + * @param statement + * the user statement + * @return a response based on the rules given + */ + public String getResponse(String statement) { + String response = ""; + if (statement.length() == 0) { + response = "Say something, please."; + } else if (findKeyword(statement, "no") >= 0) { + response = "Why so negative?"; + } else if (findKeyword(statement, "mother") >= 0 + || findKeyword(statement, "father") >= 0 + || findKeyword(statement, "sister") >= 0 + || findKeyword(statement, "brother") >= 0) { + response = "Tell me more about your family."; + } else { + response = getRandomResponse(); + } + return response; + } + + /** + * Search for one word in phrase. The search is not case + * sensitive. This method will check that the given goal + * is not a substring of a longer string (so, for + * example, "I know" does not contain "no"). + * + * @param statement + * the string to search + * @param goal + * the string to search for + * @param startPos + * the character of the string to begin the + * search at + * @return the index of the first occurrence of goal in + * statement or -1 if it's not found + */ + private int findKeyword(String statement, String goal, + int startPos) { + String phrase = statement.trim(); + // The only change to incorporate the startPos is in + // the line below + int psn = phrase.toLowerCase().indexOf( + goal.toLowerCase(), startPos); + + // Refinement--make sure the goal isn't part of a + // word + while (psn >= 0) { + // Find the string of length 1 before and after + // the word + String before = " ", after = " "; + if (psn > 0) { + before = phrase.substring(psn - 1, psn) + .toLowerCase(); + } + if (psn + goal.length() < phrase.length()) { + after = phrase.substring( + psn + goal.length(), + psn + goal.length() + 1) + .toLowerCase(); + } + + // If before and after aren't letters, we've + // found the word + if (((before.compareTo("a") < 0) || (before + .compareTo("z") > 0)) // before is not a + // letter + && ((after.compareTo("a") < 0) || (after + .compareTo("z") > 0))) { + return psn; + } + + // The last position didn't work, so let's find + // the next, if there is one. + psn = phrase.indexOf(goal.toLowerCase(), + psn + 1); + + } + + return -1; + } + + /** + * Search for one word in phrase. The search is not case + * sensitive. This method will check that the given goal + * is not a substring of a longer string (so, for + * example, "I know" does not contain "no"). The search + * begins at the beginning of the string. + * + * @param statement + * the string to search + * @param goal + * the string to search for + * @return the index of the first occurrence of goal in + * statement or -1 if it's not found + */ + private int findKeyword(String statement, String goal) { + return findKeyword(statement, goal, 0); + } + + /** + * Pick a default response to use if nothing else fits. + * + * @return a non-committal string + */ + private String getRandomResponse() { + final int NUMBER_OF_RESPONSES = 4; + double r = Math.random(); + int whichResponse = (int) (r * NUMBER_OF_RESPONSES); + String response = ""; + + if (whichResponse == 0) { + response = "Interesting, tell me more."; + } else if (whichResponse == 1) { + response = "Hmmm."; + } else if (whichResponse == 2) { + response = "Do you really think so?"; + } else if (whichResponse == 3) { + response = "You don't say."; + } + + return response; + } + +} \ No newline at end of file diff --git a/Magpie/Activity_3/MagpieRunner3.java b/Magpie/Activity_3/MagpieRunner3.java new file mode 100644 index 0000000..9309e70 --- /dev/null +++ b/Magpie/Activity_3/MagpieRunner3.java @@ -0,0 +1,30 @@ +package Magpie.Activity_3; + +import java.util.Scanner; + +/** + * A simple class to run the Magpie class. + * + * @author Laurie White + * @version April 2012 + */ +public class MagpieRunner3 { + + /** + * Create a Magpie, give it user input, and print its replies. + */ + public static void main(String[] args) { + Magpie3 maggie = new Magpie3(); + + System.out.println(maggie.getGreeting()); + Scanner in = new Scanner(System.in); + String statement = in.nextLine(); + + while (!statement.equals("Bye")) { + System.out.println(maggie.getResponse(statement)); + statement = in.nextLine(); + } + in.close(); + } + +} \ No newline at end of file diff --git a/Magpie/Activity_3/StringExplorer.java b/Magpie/Activity_3/StringExplorer.java new file mode 100644 index 0000000..9aa0a41 --- /dev/null +++ b/Magpie/Activity_3/StringExplorer.java @@ -0,0 +1,28 @@ +package Magpie.Activity_3; + +/** + * A program to allow students to try out different + * String methods. + * + * @author Laurie White + * @version April 2012 + */ +public class StringExplorer { + + public static void main(String[] args) { + String sample = "The quick brown fox jumped over the lazy dog."; + + // Demonstrate the indexOf method. + int position = sample.indexOf("quick"); + System.out.println("sample.indexOf(\"quick\") = " + position); + + // Demonstrate the toLowerCase method. + String lowerCase = sample.toLowerCase(); + System.out.println("sample.toLowerCase() = " + lowerCase); + System.out.println("After toLowerCase(), sample = " + sample); + + // Try other methods here: + + } + +} \ No newline at end of file diff --git a/Magpie/Activity_4/Magpie4.java b/Magpie/Activity_4/Magpie4.java new file mode 100644 index 0000000..6b356b6 --- /dev/null +++ b/Magpie/Activity_4/Magpie4.java @@ -0,0 +1,221 @@ +package Magpie.Activity_4; + +/** + * A program to carry on conversations with a human user. + * This version: + * + * @author Laurie White + * @version April 2012 + * + */ +public class Magpie4 +{ + /** + * Get a default greeting + * @return a greeting + */ + public String getGreeting() + { + return "Hello, let's talk."; + } + + /** + * Gives a response to a user statement + * + * @param statement + * the user statement + * @return a response based on the rules given + */ + public String getResponse(String statement) + { + String response = ""; + if (statement.length() == 0) + { + response = "Say something, please."; + } + + else if (findKeyword(statement, "no") >= 0) + { + response = "Why so negative?"; + } + else if (findKeyword(statement, "mother") >= 0 + || findKeyword(statement, "father") >= 0 + || findKeyword(statement, "sister") >= 0 + || findKeyword(statement, "brother") >= 0) + { + response = "Tell me more about your family."; + } + + // Responses which require transformations + else if (findKeyword(statement, "I want to", 0) >= 0) + { + response = transformIWantToStatement(statement); + } + + else + { + // Look for a two word (you me) + // pattern + int psn = findKeyword(statement, "you", 0); + + if (psn >= 0 + && findKeyword(statement, "me", psn) >= 0) + { + response = transformYouMeStatement(statement); + } + else + { + response = getRandomResponse(); + } + } + return response; + } + + /** + * Take a statement with "I want to ." and transform it into + * "What would it mean to ?" + * @param statement the user statement, assumed to contain "I want to" + * @return the transformed statement + */ + private String transformIWantToStatement(String statement) + { + // Remove the final period, if there is one + statement = statement.trim(); + String lastChar = statement.substring(statement + .length() - 1); + if (lastChar.equals(".")) + { + statement = statement.substring(0, statement + .length() - 1); + } + int psn = findKeyword (statement, "I want to", 0); + String restOfStatement = statement.substring(psn + 9).trim(); + return "What would it mean to " + restOfStatement + "?"; + } + + + + /** + * Take a statement with "you me" and transform it into + * "What makes you think that I you?" + * @param statement the user statement, assumed to contain "you" followed by "me" + * @return the transformed statement + */ + private String transformYouMeStatement(String statement) + { + // Remove the final period, if there is one + statement = statement.trim(); + String lastChar = statement.substring(statement + .length() - 1); + if (lastChar.equals(".")) + { + statement = statement.substring(0, statement + .length() - 1); + } + + int psnOfYou = findKeyword (statement, "you", 0); + int psnOfMe = findKeyword (statement, "me", psnOfYou + 3); + + String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim(); + return "What makes you think that I " + restOfStatement + " you?"; + } + + + + + + /** + * Search for one word in phrase. The search is not case sensitive. + * This method will check that the given goal is not a substring of a longer string + * (so, for example, "I know" does not contain "no"). + * @param statement the string to search + * @param goal the string to search for + * @param startPos the character of the string to begin the search at + * @return the index of the first occurrence of goal in statement or -1 if it's not found + */ + private int findKeyword(String statement, String goal, int startPos) + { + String phrase = statement.trim(); + // The only change to incorporate the startPos is in the line below + int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos); + + // Refinement--make sure the goal isn't part of a word + while (psn >= 0) + { + // Find the string of length 1 before and after the word + String before = " ", after = " "; + if (psn > 0) + { + before = phrase.substring (psn - 1, psn).toLowerCase(); + } + if (psn + goal.length() < phrase.length()) + { + after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase(); + } + + // If before and after aren't letters, we've found the word + if (((before.compareTo ("a") < 0 ) || (before.compareTo("z") > 0)) // before is not a letter + && ((after.compareTo ("a") < 0 ) || (after.compareTo("z") > 0))) + { + return psn; + } + + // The last position didn't work, so let's find the next, if there is one. + psn = phrase.indexOf(goal.toLowerCase(), psn + 1); + + } + + return -1; + } + + /** + * Search for one word in phrase. The search is not case sensitive. + * This method will check that the given goal is not a substring of a longer string + * (so, for example, "I know" does not contain "no"). The search begins at the beginning of the string. + * @param statement the string to search + * @param goal the string to search for + * @return the index of the first occurrence of goal in statement or -1 if it's not found + */ + private int findKeyword(String statement, String goal) + { + return findKeyword (statement, goal, 0); + } + + + + /** + * Pick a default response to use if nothing else fits. + * @return a non-committal string + */ + private String getRandomResponse() + { + final int NUMBER_OF_RESPONSES = 4; + double r = Math.random(); + int whichResponse = (int)(r * NUMBER_OF_RESPONSES); + String response = ""; + + if (whichResponse == 0) + { + response = "Interesting, tell me more."; + } + else if (whichResponse == 1) + { + response = "Hmmm."; + } + else if (whichResponse == 2) + { + response = "Do you really think so?"; + } + else if (whichResponse == 3) + { + response = "You don't say."; + } + + return response; + } + +} \ No newline at end of file diff --git a/Magpie/Activity_4/MagpieRunner4.java b/Magpie/Activity_4/MagpieRunner4.java new file mode 100644 index 0000000..8434524 --- /dev/null +++ b/Magpie/Activity_4/MagpieRunner4.java @@ -0,0 +1,32 @@ +package Magpie.Activity_4; + +import java.util.Scanner; + +/** + * A simple class to run the Magpie class. + * @author Laurie White + * @version April 2012 + */ +public class MagpieRunner4 +{ + + /** + * Create a Magpie, give it user input, and print its replies. + */ + public static void main(String[] args) + { + Magpie4 maggie = new Magpie4(); + + System.out.println (maggie.getGreeting()); + Scanner in = new Scanner (System.in); + String statement = in.nextLine(); + + while (!statement.equals("Bye")) + { + System.out.println(maggie.getResponse(statement)); + statement = in.nextLine(); + } + in.close(); + } + +} \ No newline at end of file