Spring Semester 2003


Lab Notes Two: Some simple programs, some sample problems.
Goals for this lab: This is a long lab, meant to give you enough work to really get you started.

Your in-lab assignment is indexed 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.

frilled.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%
Here's the actual 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 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
    } 
}
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, and to slowly become familiar with it, and Java code and I/O (which will be covered at some point in class later).
/*******************************************************
   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 
}
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:

Reference types (using Rectangle)

Rectangle a = new rectangle(10, 20, 30, 40); 
Rectangle b = a; 
The picture looks as follows:

Now if you have

a.translate(3, 3); 
System.out.println(b);
you will be able to see the change. Both 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)

int a = 3;
int b = a; 
The picture looks as follows:

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

a = 10; 
System.out.println(b);
you will see that the value of 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.

  1. Here's the document of warm-up questions.
    (http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/chapTwoReview.htm)

  2. Here's the same document with answers.
    (http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/chapTwoRevSols.html)

  3. Here are the problems that you should think about.
    (http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/chapTwoProbs.html)

  4. Here are the solutions to these problems.
    (http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/chTwoPSols.html)

  5. Here's your lab assignment no. 2, due at the begining of the next lab.
    (http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/labAssignTwo.html)

  6. And here, to remind us of the intrinsic fun, is the note of Monday on solving problems.
    (http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/RPF.html)


Last updated: Jan 21, 2003 by Adrian German for A201