The computer will store integers or whole numbers (type int in Java) in a manner different from that of decimal numbers with fractions (double). Numbers with decimal points are stored using scientific notation. For example,
3.0 might be stored in the form 0.3 X 101 ==> 0 011 0 001
where the first 0 represents the sign of the number (+), then next three represent the number 3, the next digit 0 represents the sign of the exponent (+) and the final three digits, 001, represent the power of 10.
On the other hand, the number 3 would be stored simply as: 0000 0011
It should now be clear that int's and double's are NOT interchangeable. For example, as we learned last time, the following statements produce an error message when compiled:
int x;
double y = 5.3;
x = y: //The integer variable, x, is not
"big" enough to accept a double value
We began by discussing the inherent limitations of the primitive data types. Since the computer's memory locations are finite, it seems obvious that the size of numbers (both int and double) must be limited. We saw this dramatically in the following example:
//File: IntPrecision.java
public class IntPrecision
{
public static void main (String[] args)
{
int x = 100000 * 100000;
System.out.println(x);
}
}
E:\>javac IntPrecision.java
E:\>java IntPrecision
1410065408
Of course, the "right" answer is 10,000,000,000, NOT 1,410,065,408. We spent some time trying to deduce the number of bits allocated for the storage of int's and concluded that Java stores int variables in a memory location 32 bits long. This allows a number as big as +/- 1111111111111111111111111111111 binary. That's 31 1's strung together. After some class discussion, we concluded that this is equal to 231-1 = 2,147,483,647. In fact, if you were try to compile the following program:
//File: IntPrecision2.java
public class IntPrecision2
{
public static void main (String[] args)
{
int x = 2147483648;
System.out.println(x);
}
}
You will get the following error message:
E:\>javac IntPrecision2.java
IntPrecision2.java:5: integer number too large: 2147483648
int x = 2147483648;
^
1 error
While compiling with x = 2147483647 (the maximum possible int value) presents no difficulties at all.
A similar phenomenon holds with double's. The following straightforward program gives very peculiar results:
//File: FloatPrecision.java
public class FloatPrecision
{
public static void main (String[] args)
{
double x = 1.0/5.0 + 1.0/5.0 +
1.0/5.0 - 0.6;
System.out.println(x);
}
}
E:\>java FloatPrecision
1.1102230246251565E-16
The "right" result of course is 0, but because of the limitation of the computer's memory, a very small error results from the inability of the computer to represent numbers like 1.0/5.0 (0.2) exactly as binary numbers.
We then spent some time reviewing the Temperature class because of it's relevance to the homework and indeed to much of what we'll be doing. Remember that when we create a class like Temperature, we're creating a form or template with which we can construct actual objects. It's rather like the difference between the recipe for chocolate chip cookies and the actual construction of a chocolate chip cookie (yummie!). So,
//File: Temperature.java
import CSLib.*;
public class Temperature
{
// Convert temperature from Fahrenheit to Centigrade
// Author: Samuel N. Kamin, June 1, l996
public void compute ()
{
double temperature; // The
Fahrenheit temperature.
InputBox in;
OutputBox out;
in = new InputBox();
in.setPrompt ("Please type the temperature
(deg F): ");
temperature = in.readDouble();
out = new OutputBox();
out.println ("\tDegrees
Fahrenheit\t\tDegrees Celcius\n\n\n\n");
out.println ("");
//Create a new memory location in which to
store the converted Fahrenheit temperature
double celcius = 5.0 *
(temperature - 32.0) / 9.0;
out.println ("\t" + temperature +
"\t\t\t\t" + celcius);
//out.print(temperature);
//out.print(" deg F is ");
//out.print((5.0 * (temperature - 32.0) /
9.0));
//out.print(" deg C.");
}
}
the above class gives the details about how to construct a Temperature object and indicates what kinds of messages it will respond to. In this case, a Temperature object will respond only to the message compute(). If we attempt to compile this class, we get:
E:\>javac Temperature.java
E:\>
No problems at all. On the other hand, trying to execute Temperature leads to the following familiar error message:
E:\>java Temperature
Exception in thread "main" java.lang.NoSuchMethodError: main
Remember, only classes with the main() method can be executed! Therefore, we must create a "client" class with a main() to actually execute the Temperature code.
//File: TemperatureClient.java
public class TemperatureClient
{
public static void main (String[] args)
{
Temperature test = new
Temperature();
test.compute();
}
}
Execution of the TemperatureClient code gives the following interaction with the user:


If you examine the code in Temperature2.java carefully, you'll see that the \t (tab)
and \n (new line) control codes in the output strings explain all the white space you see
in the OutputBox.
Finally, we observed that the computer could care less about the pretty formatting of our source code. In fact, the following version of the Temperature class is perfectly acceptable to the computer (BUT NOT TO ME).
//File: TemperatureCrammed.java
import CSLib.*;public class TemperatureCrammed{public
void compute(){double
temperature;InputBox in;OutputBox out;in=new InputBox();in.setPrompt(
"Please type the temperature (deg F): ");temperature=in.readDouble();out=
new OutputBox();out.print(temperature);out.print(" deg F is
");out.
print((5.0*(temperature-32.0))/9.0);out.println(" deg C");}}
Try changing your TemperatureClient class to construct a TemperatureCrammed object and you'll see that it will execute perfectly fine.
The String class is a very usefull class if you want to manipulate string. You can find all you need to know about Strings at the Java documentation site. To save time, just click String. Among all the methods available, we chose a select few to examine more carefully. The following program and its output reveals how a few of these String methods work.
//File: StringPlay.java
public class StringPlay
{
public static void main (String [] args)
{
String phrase = "I wonder what this
does.";
System.out.println (phrase);
System.out.println (phrase.toUpperCase());
System.out.println (phrase.toLowerCase());
System.out.println ("The length of this
phrase is: " + phrase.length());
System.out.println ("The tenth character
in this phrase is: " + phrase.charAt(10-1));
System.out.print ("The substring from
character 5 to character 10 is "
+ phrase.substring (4, 10));
}
}
E:\>java StringPlay
I wonder what this does.
I WONDER WHAT THIS DOES.
i wonder what this does.
The length of this phrase is: 24
The tenth character in this phrase is: w
The substring from character 5 to character 10 is nder w
It's important to note that Java counts from 0, not 1, so it's sometimes necessary to subtract 1. In particular, in order to retrieve the 10th character in the phrase "I wonder what this does.", it was necessary to write phrase.charAt(10-1). This idiosynchracy of the language will no doubt be the source of future frustrations.