Lab 3: Java types

The goal of this lab is to exercise your understanding of Java types and practice the recent Java constructs you have seen.

I. Converting strings to numbers and back again

First open a terminal window.

I have prepared a stub of the program for you. Make a new directory for this lab, cd into it, and copy the file from the course directory.

mkdir lab3
cd lab3
cp /homes/tvandrun/Public/cs235/lab3/Convert.java .

In the previous lab, you converted a string to an int. This time, you will convert a string representing a real number (a number with a decimal portion) to a double. We'll do this in a couple of steps.

Step 1

Now, in your program, prompt the user and input a String. You may assume that the String contains a representation of a real number---including a whole and decimal part, formatted something like xxxx.yyy, so you can assume that the String has only digits except for one . character. Write an algorithm for converting this String to an appropriate double. Hint: you will probably need two loops, one for handling the characters before the decimal point, the other for after. Store the result in a double variable and print it to the screen.

Compile and test your program. When it's working, move on.

Step 2

Keep the previous part of your program intact, but add another part: Interpret the double as if it were an amount of money. To do this you want to round to the second decimal place and print out exactly two decimal places (even if those decimal places are zero).

Write an algorithm for doing this. Then compile and test.

II. Pangrams

Last time you wrote a program to test whether a string is a palindrome. In exercises II and III, you will be writing programs for two other word games.

A sentence is a pangram if it contains every letter of the alphabet at least once. Examples include

Write a program which prompts the user for a string and determines whether or not that string is a pangram. Ignore spaces, punctuation, and capitalization. Your program should also include an opening documentation block (following the style we've seen in class) and a comment on every variable you declare.

III. Anagrams

Two words or phrases are anagrams of each other if they contain all the same letters (with the same frequency). Examples include

Write a program which prompts the user for two strings. Test whether the two strings are anagrams of each other. Ignore spaces, punctuation, and capitalization. Your program should also include an opening documentation block (following the style we've seen in class) and a comment on every variable you declare.


Thomas VanDrunen
Last modified: Thu Sep 10 08:31:37 CDT 2009