|
|
Strings,
StringTokenizers.
import java.util.*;
class Seven {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
String line;
do {
System.out.print("Type> ");
line = console.readLine();
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
String token = st.nextToken();
System.out.println(" ***(" + token + ")*** ");
}
String rev = "";
for (int i = line.length() - 1; i >= 0; i--) {
rev = rev + line.charAt(i);
}
System.out.println(rev);
System.out.println(line);
} while (! line.equals("bye"));
}
}
We developed it in stages: Next, let's work out some problems.
Problem 1 Write a program stenographer that accepts lines of text from the user and translates them (one by one) in shorthand. For the purposes of this exercise let's define shorthand notation as the one in which all vowels are removed from all the words everywhere.
Thus shorthand version of
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
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.