The goal of this lab is to practice the Java features for input and output, for arithmetic and type casting, and String manipulation.
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 lab2 cd lab2 cp /homes/tvandrun/Public/cs235/lab2/Convert.java .
The program you are writing will eventually do the following:
nextInt
,
use nextLine
and convert the String to an integer.
This lab will walk you through the steps.
A. First, input a String from the user, using keyboard.nextLine()
and display it back to the screen.
Compile and run the program.
B. Next, suppose that the first character of the String is a digit, and you want
to pick off the character and treat it as a int.
The type of expression that results from charAt(0)
is
a char
.
What happens if you explicitly cast it to an int
(or, store it in an int
variable, forcing an automatic cast)?
C. What you should notice is that it will turn the character into an
int
, but not the one you want
(for example, the digit 1 will turn into the int
49).
This is because when Java converts from a char
to an int
it simply looks at the bit sequence and reinterprets them.
"49" happens to be an integer interpretation of the bit sequence standing
for the character "1".
Figure out how to convert a digit character to an appropriate int
.
There is more than one way to do this.
Compile and test your program before you move on.
Your program should input the String, generate the int, and print it to the screen.
Now you want to convert more than just the first character. Assume that all the characters in the String are digits (and, when you test your program, input only digits). Modify your program so that instead of merely processing the first character, it loops through the String to produce the entire int. Store this result in an int variable and print the result on the screen. Compile and test.
Now add a second part to your program (that is, keep the previous part intact, and add something coming after it). As before, you should input a String. This time 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.
Finally, add a final part to your program.
Multiply the int from part 4 and the double from part 5 together.
The result will be a double, which you will display to the screen.
However, you do not want simply to use System.out.println
;
you want to interpret this as an amount of money, so you should round this
to the nearest hundredth and display exactly two decimal places.
Write an algorithm for doing this.
A palindrome is a word, number, phrase, or sentence that is the same backwards and forwards. If we ignore spaces, punctuation, and capitalization, some interesting palindromes are
Your task is to write a program that lets the user input a string and tests if the string is a palindrome. As in Part I, I am providing a stub program.
cp /homes/tvandrun/Public/cs235/lab2/Palindrome.java .
There are two parts to this. First, you will want to pre-process the string---since we want to ignore punctuation, spaces, and the case of letters, you want to create a string like the string the user gave except with the non-letter characters stripped out and the letters all of one case. Second, you want to do the actually testing of the new string to see if it is a palindrome.
Your program should print a message like "This string is a palindrome" or "This string is not a palindrome".
Sometimes you will be asked to turn in an electronic version of your
program by copying the .java
files that you have written
or modified to a grading directory.
Today you will turn in a hard copy.
Here are the procedures for generating something to turn in which
demonstrates you have complete the lab.
script somefilename
will
start recording everything you do in the command shell to a a file called
somefilename
; just script
will record it
to a file called typescript
.)
cat Convert.java
.
javac Convert.java
)
java Convert
)
Palindrome.java
exit
)
a2ps
instead of
lpr
-- this make the file print in "two-up" form, that is, two pages
per page. (a2ps -P sp typescript
-- or whatever the name of your
script file is)
Make sure you have exited the script file before you print, or else it will print forever!
Then turn in the hard copy to me.