![]() |
![]() Spring Semester 2002 |
Goals for this lab:
int
and
double
types and the overflow and roundoff
errors that can result
String
type to define and manipulate character
strings
Your in-lab assignment is posted at the end of these lab notes.
Here now is the interactive conversion program announced at the end of Lecture Notes Four.
First a sample session of working with the program (on Unix). I marked the user's answers with blue, but this just for illustration purposes in these lab notes. Don't expect the program to actually print in color when you compile and run it, of course.
Here's the actualfrilled.cs.indiana.edu%javac Conversion.java frilled.cs.indiana.edu%java Conversion Hi my name is Hal. What is your name? Dave Hello, Dave! How many dollars do you want to convert? (Please type an integer value, no decimal part) 40 I see, you want to convert 40 dollars in British pounds. Very well... What is the conversion rate today? 0.65 Well, Dave for 40 dollars you can get: 26.0 pounds. Thank you for using Hal! Good-bye. frilled.cs.indiana.edu%
Conversion
class. I placed all the comments in
light grey to make the reading of the code a bit
easier. (To better read the comment just highlight it with your mouse.)
And here's the/* A conversion program that illustrates how one can read input from the keyboard using the textbook's ConsoleReader class */ class Conversion { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); // now we can read from 'console' System.out.println("Hi my name is Hal. What is your name?"); // greet the user String name = console.readLine(); // wait for input and collect it (a String, for the name) System.out.print("Hello, " + name + "! "); // echo the name to the user, raising user's confidence in us System.out.println("How many dollars do you want to convert?"); // approach the user directly, offer your services System.out.println("(Please type an integer value, no decimal part)"); // instruct the user what limitations you have (accepting only int's) int amount = console.readInt(); // wait for the user to provide the sum to be converted System.out.println("I see, you want to convert " + amount + " dollars in British pounds. Very well..."); // talk to the user, be friendly, echo information frequently System.out.println("What is the conversion rate today?"); // ask the user for the last piece of input double rate = console.readDouble(); // wait for the conversion rate, which can have a fractional part System.out.println("Well, "+ name + " for " + amount + " dollars " + "you can get: " + rate * amount + " pounds."); // do the calculation and report it to the user System.out.println("Thank you for using Hal! Good-bye."); // thank the user for interest and say good-bye } }
ConsoleReader
class. This class is
given to you for the purpose of doing keyboard input. You don't
need to understand the code right now, it will become clearer
at the end of chapter 3. However we think it's good for you to
be exposed to what it looks like, and to slowly become familiar
with it, and Java code and I/O (which will be covered at some point
in class).
In your programs, from now on, keep using this class for user input from the keyboard, as it will provide a simple, uniform way of doing keyboard input. Here now is also a diagrammatic description of the fundamental difference between primitive and reference types, that we looked at in the notes:/******************************************************* ConsoleReader class is used for keyboard input. Just keep the source code of this class (the file) in the same directory in which your other program (the one that needs user input) resides. In this particular case keep it in the same folder with Conversion.java that has been presented above. ********************************************************/ import java.io.*; // I/O package needed class ConsoleReader { ConsoleReader(InputStream inStream) { // constructor reader = new BufferedReader( new InputStreamReader( inStream)); } String readLine() { // instance method String inputLine = ""; try { inputLine = reader.readLine(); } catch (IOException e) { System.out.println(e); System.exit(1); } return inputLine; } int readInt() { // instance method String inputString = readLine(); int n = Integer.parseInt(inputString); return n; } double readDouble() { // instance method String inputString = readLine(); double x = Double.parseDouble(inputString); return x; } private BufferedReader reader; // instance method }
Reference types (using Rectangle
)
The picture looks as follows:Rectangle a = new rectangle(10, 20, 30, 40); Rectangle b = a;
Now if you have
you will be able to see the change. Botha.translate(3, 3); System.out.println(b);
a
and
b
point to one and the same thing, an essentially
anonymous object, to which we refer as both a
and
b
. So changes made using the a
name
can be seen by looking at the object using the other name, that
is, b
.
Primitive types (using int
)
The picture looks as follows:int a = 3; int b = a;
The big difference is that the primitive value is copied into the storage location. So each location has its own copy. It just works that way with the numbers (as primitive types) but doesn't with objects (reference types). That's how it works. With reference types, what the variable holds is a reference, the arrow. With primitive types, the variable holds a value, a copy of the actual value.
Now if you have
you will see that the value ofa = 10; System.out.println(b);
b
has not been updated. Each location has its own (copy of the) value, and changing one does not affect the other. This is an important difference, we refer to it as the by value vs. by reference contrast, and it will be useful for you to remember it from now on when you reason about and design programs.
Now, let's move on to exercises. First, a set of warm-ups.
Here, to remind you of the intrinsic fun, is the note of Monday on solving problems.