Second Summer 2002


Lab Notes Seven: Loops, Strings, StringTokenizers.

The practical exam is coming, 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!

Solution

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:
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%
Of course, you could change or enhance it in many ways, so go ahead and experiment with it!


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:

After each coin is entered (into the vending machine, that is, the program) the program prints back the amount that is still needed to purchase the item. 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 (called Vendor) starts by greeting the user, then accepting money. Your vendor accepts the following monies only: Your vendor accepts money one line at a time, and should allow more than one coin to be entered on a line. After that, the program calculates and prints the amount that is still needed to purchase the item. When the amount finally exceeds 340 cents the program prints the change (if any) and thanks the user for using the program. In the example below what the customer enters is rendered in blue.

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:

Good luck and let us know if we can be of more help.


Last updated: Jun 16, 2002 by Adrian German for A201