|
CSCI A201/A597 and I210
|
int and double
types and the overflow and roundoff errors
that can result
String type to define and manipulate character strings
The minute paper is exercise 2.17 (p. 94) in your book:
You can turn this in on a piece of paper, that I will come and pick up from you during the lab.
Minute Paper for A201/A597/I210 Week Two (R2.17, p. 94) What are the values of the following expressions? In each line, assume that
double x = 2.5; double y = -1.5; int m = 18; int n = 4; String s = "Hello"; String t = "World";
x + n * y - (x + n) * y
m / n + m % n
5 * x - n / 5
Math.sqrt(Math.sqrt(n))
(int)Math.round(x)
(int)Math.round(x) + (int)Math.round(y)
s + t
s + n
1 - (1 - (1 - (1 - (1 - n))))
s.substring(1, 3)
s.length() + t.length()
Here now is the interactive conversion program announced in Lecture Notes Four.
First a sample session of working with the program (on Unix). I marked the user 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.
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.)
/* A conversion program for the illustration of reading input with
the ConsoleReader class */
public class Conversion {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
// now we can read from the 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 ints)
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 and echo the info 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
}
}
And here's the 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, to slowly slowly grow familiar
with it and Java code and I/O.
/* 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
(that needs user input) resides. */
import java.io.*; // I/O package needed
public class ConsoleReader {
public ConsoleReader(InputStream inStream) { // constructor
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() { // instance method
String inputLine = "";
try {
inputLine = reader.readLine();
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() { // instance method
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() { // instance method
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
private BufferedReader reader; // instance method
}
In your programs keep using this class for user input from the keyboard. Here is also a diagrammatic description of the fundamental difference between primitive and reference types.
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, 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.
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 difference, and it will be useful for you to remember it from now on when you reason about and design programs.