CSC-117 Computer Programming II
Notes on Chapter 12 - CALCULATIONS

Preview
 

Much of the stuff in this chapter you have seen before or perhaps I have mentioned before, but we did not discuss these ideas in great detail. Now is the time to get a better handle on these things. Mainly we are going to deal with
-data types
- assignment statements
- calculations (how they look inside the computer)
- input and output of numbers
- operators
- some useful library functions
- how to define constants
- how to handle mathematical iterations
- problems that occur when mixing data types
- money - how do you display a real number with 2 decimal places
- how to draw mathematical graphs
- exceptions - another Java word to remember
Integer Types
You have been using these since we started, at least the "int" version of them. But there are actually several ways to define an integer.

byte, short, int, long

These various ways tell Java how many computer bits to use to store the integer in the computer. Sometimes, when working with a large amount of data, it is important to make data items like integers as small as possible (taking up the least amount of space inisde the computer as possible.

However, the smaller you make an integer in bits the smaller the numbers in can store. Here are the ranges of values that can be stored in the various sizes.

Type     Size         Minimum         Maximum

byte        8 bits       -128                 127
short     16 bits       -32768             32767
int         32 bits       -2147483648   2147483647
long      64 bits       -9.22 x 1019      9.22 x 1019


Even real numbers have two sizes

    float 32 bits
    double 64 bits

You really don’t have to remember how big or small of values the real numbers are.

Integers inside the computer

We should discuss what integers look like inside the computer. We will use the byte form of integer for simplicity. A byte is 8 bits long. Each bit can have either a zero or a one in it but nothing else. Each bit in a byte corresponds to a certain power of 2 starting on the right and moving to the left, as below:

          0     0     1     1     0     0     1    1
        ___ ___ ___ ___ ___ ___ ___ ___

bit #    7     6     5     4     3     2     1     0
value   27   26    25   24    23   22   21   20
         sign   64   32   16   8     4    2     1

Therefore the above bit pattern, 00110011, is really the integer

32 + 16 + 2 + 1 = 51

We’ll do more examples of this in class.


Declaration of variables and assigning values

You know how to do this already. There are just a few things you need to be careful of.:

With integers you can do just about anything, like

int age = 52;
which is equivalent to the two statements:
int age;
age = 52;
With floats it is the same sort of thing:
float width = 13.54f;
There is only one thing to ve wary of here. Unless you specifically put the "f" after the number, Java will treat the 13.54 as a "double" real number (see above) and you’ll get an error trying to put a double real number into a regular float real number. Java likes to keep its data types VERY distinct.

Some people like to create very large or very small real numbers, like 0.000000017 or 111122222. They usually do this by specifying a the number in what’s called scientific notation. The two numbers above, when written in scientific notation are

1.7 x 10-8 and 1.11122222 x 108
But you cannot write powers like that in a Java program, so Java (and most other programming languages) allow you to specify scientific notation as follows:
1.7e-8f and 1.11122222e8f


Input and output

You have already used input of integers (from a textfield) and some of you ahve used input of real numbers.

In the integer case you wrote something like this:

int number = Integer.parseInt( inputField.getText( ));
The action here is broken down as follows:
- go to the inputField TextField and get whatever was typed there
- convert what was typed there into an integer
- place that integer into the data item called number
In order to understand this completely we need to discuss the difference between numbers and text characters and how they look in the computer. We will do this is class.

Converting a text input into a real number is a little more complex. I really don't know why Java makes you do this. I think it is kind of silly. But the rule is that you cannot directly convert a text string into a type "float" data item. You have to use another thing called "Float" first. I know that is confusing, "Float" is a class in Java, but "float" is an internal data type. And since Java does not like mixing types of things, then we have to go about it a little different. The proper way to get a real number from a textfield is this:

Float temp = Float.valueOf( inputField.getText( ));
float value = temp.floatValue( );
Weird, but it works. Just be careful to remember that!

When you output a number you can use the drawString method to do it. You know that already. And you are used to writing things like:

g.drawString( "The answer is " + answer, 50,50);
However, sometimes you need to output a number without some beginning phrase, you just need the number all by itself. You can do this by writing:
g.drawString ( "" + answer, 50, 50); // a blank string and a number
but a more elegant way of doing it is to convert the number back into a String before outputting it. Like this:
g.drawString( toString(answer), 50,50);
 
End of first part of Chapter 12. This is all you are responsible for for the Midterm Exam. We will finsish Chapter 12 after Spring Break.