The goal of this lab is to practice the Java features for input and output, for arithmetic and type casting, and String manipulation, and to discover Java's mechanisms for flow of control.
So far in class we have seen the following features of Java: variables and their declarations; how to manipulate Strings; arithmetic operations on ints and doubles; and type casting. To solving interesting problems using these tools, it is also helpful to have decision-making and repetition in a program; we have not yet seen how to do this in Java, but we have written algorithms that use them, so the basic Java constructs for this are easy to specify.
Loops can be written using the while
statement,
using parentheses around the condition and curly braces around the
code that should be repeated:
while ( ... some condition ... ) { ... some code ... }
Decisions can be performed similarly using if
:
if ( ... some condition ... ) { ... some code ... }
an if
/else
combination:
if ( ... some condition .... ) { ... code option 1 ... } else { ... code option 2 ... }
or a multi-way sequence of if
and else if
:
if ( ... some condition .... ) { ... code option 1 ... } else if ( ... another condition ... ) { ... code option 2 ... } else if ( ... another condition ... ) { ... code option 3 .... } else { ... default code option ... }
To compare two values, use < and > for less than and greater than; <= and >= for less than or equal to and greater than or equal to; and == for equals and != for not equals. It is important to note that == is for comparison. A single = is used only for assignment.
You will probably not need operations like and and or, but if you would like to use them in your solution, ask the instructor or TA about how they work in Java.
I have prepared a skeleton of the program for you. Make a new directory for this lab, cd into it, and copy the file from the course directory.
cd cs235 mkdir lab3 cd lab3 cp /homeemp/tvandrun/pub/235/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.
First, input a String from the user, using keyboard.nextLine()
and display it back to the screen.
Compile and run the program.
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)?
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.
Now 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.
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 directory in the TA's account.
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
.)
rm *.class
.
('*' is a wildcard character; *.class
means "all the files that end in
.class
.) This is so that the version of the program you
run in the script is the current version.
cat Convert.java
.
javac Convert.java
)
java Convert
)
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.