-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
645 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package Magpie.Activity_2; | ||
|
||
/** | ||
* A program to carry on conversations with a human user. | ||
* This is the initial version that: | ||
* <ul> | ||
* <li> | ||
* Uses indexOf to find strings | ||
* </li> | ||
* <li> | ||
* Handles responding to simple words and phrases | ||
* </li> | ||
* </ul> | ||
* 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package Magpie.Activity_3; | ||
|
||
/** | ||
* A program to carry on conversations with a human user. | ||
* This version: | ||
* <ul> | ||
* <li> | ||
* Uses advanced search for keywords | ||
* </li> | ||
* </ul> | ||
* | ||
* @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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
Oops, something went wrong.