|
Spring Semester 2004 |
Strings,
StringTokenizers.
The practical exam is coming, let's work out some problems.
Problem 1 Write a program stenographer that
For the purposes of this exercise
Good evening, and welcome to Minneapolis!
would be
Gd vnng, nd wlcm t Mnnpls! Write a program stenographer
class Stenographer {
public static void main(String[] args) {
}
}
that accepts lines of text from the user
class Stenographer {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Please type a line of text: ");
String line = c.readLine();
}
}
and translates them (character by character)
class Stenographer {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Please type a line of text: ");
String line = c.readLine();
for (int i = 0; i < line.length(); i++) {
String temp = line.substring(i, i+1);
System.out.print(temp);
}
System.out.println();
}
}
in shorthand. For our purposes shorthand means
removing the vowels everywhere.
class Stenographer {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Please type a line of text: ");
String line = c.readLine();
for (int i = 0; i < line.length(); i++) {
String temp = line.substring(i, i+1);
if (temp.equalsIgnoreCase("A") ||
temp.equalsIgnoreCase("E") ||
temp.equalsIgnoreCase("I") ||
temp.equalsIgnoreCase("O") ||
temp.equalsIgnoreCase("U")) {
// skip that letter
} else {
System.out.print(temp);
}
}
System.out.println();
}
}
Here's running it twice:
Of course, you could change or enhance it in many ways, so go ahead and experiment with it!school.cs.indiana.edu%java Stenographer Please type a line of text: Good evening, and welcome to Minneapolis! Gd vnng, nd wlcm t Mnnpls! school.cs.indiana.edu%java Stenographer Please type a line of text: The Stenographer program works pretty well! Th Stngrphr prgrm wrks prtty wll! school.cs.indiana.edu%
Problem 2 Take a look at this problem, and become clear how it works, first.
Your lab assignment seven (below) is based on this and the previous question.
Your task is to write a program that simulates a vending machine that sells items which are worth 65 cents.
Your program
(called Vending) starts by greeting the user, then
accepting coins.
The machine accepts one coin at a time and understands the following coins:
nickel
dime and
quarter
When the amount exceeds 65 cents the program will print the change (if any) and thanks the user for making use of it. See below.
Example run:
prompt> java Vending Welcome. Please enter coins: coin> nickel 60 cents remaining coin> quarter 35 cents remaining coin> quarter 10 cents remaining coin> quarter Thank you. Your change is: 15 cents. Thanks for using this program.
Here's a possible solution:
class Vending {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Welcome. Please enter coins:");
int amount = 0;
final int PRICE = 65;
while (amount < PRICE) {
String coin;
System.out.print("coin> ");
coin = console.readLine();
if (coin.equals("nickel")) {
amount += 5;
} else if (coin.equals("dime")) {
amount += 10;
} else if (coin.equals("quarter")) {
amount += 25;
}
if (amount < 65) {
System.out.println((PRICE - amount) + " cents remaining.");
} else {
System.out.println("Thank you. Your change is: " +
(amount - PRICE) + " cents.");
}
}
System.out.println("Thanks for using this program.");
}
}
Now we can state the lab assignment.
A201/A597 LAB ASSIGNMENT SEVEN
Write a program that simulates a vendor that sells books of stamps which are worth 3 dollars and 40 cents each. (The vendor only sells whole books, each book is $3.40). Your program (calledVendor) starts by greeting the user, then
accepting money. Your vendor accepts the following monies only:
cent
nickel
dime
quarter and
dollar
Example run:
prompt> java Vendor Welcome. We sell stamps ($3.40) Please enter money: enter> nickel nickel dollar dollar quarter Thanks. Your credit is $2.35 I need $1.05 more. enter> cent cent Thanks. Your credit is $2.37 I need $1.03 more. enter> dollar quarter Thanks. Your credit is $3.62 The stamps are yours. Your change is: $0.22 Thanks for using this program.
Hints and Advice.
Your strategy should be simple:
StringTokenizer.