Spring Semester 2005


Lecture Notes Five: More Practice for Homework One.

So What Do We Need to Know?

  1. What is the simplest program?
  2. What are statements?
  3. How do we print and what can we print?
  4. How do we read and what can we read?
  5. How do we store and what can we store (and why)?
  6. How do we concatenate Strings?
  7. How do we take the square root of a number?
  8. What are types and why do we care?
  9. How do we refer to a portion of a string?
  10. How do you find out how long a string is?
  11. What is casting and why do we care?
  12. How do we round and what kind of number do we get then?
  13. Is there a difference between a character and a number?
  14. Is there a difference between a character and a string of one character?

Your Basic Program

Your programs will ALL have this structure:
class _____________ {
  public static void main(String[] args) {

    // your code here 

  } 
} 
So that much is known.

What Are Statements?

Statements are sentences in Java.

In English after each sentence we put a period. In Java we put a semicolon.

So your programs will ALL look like this:

class _____________ {
  public static void main(String[] args) {
    
    <instruction in Java>;
    <instruction in Java>;
    <instruction in Java>;
    <instruction in Java>;

  } 
}
You can have none, one or more instructions in a program.

The types of instructions we have learned:

  1. print statements (System.out.print__(...))
  2. declarations of variables (int n;, double a;, String u;)
  3. assignment statements (n = n + 1;, a = Math.sqrt(2);, u = u.substring(1);)

Your programs for Homework One are all composed of these kinds of instructions.

Printing

You already know the difference between

The arguments to these methods are expressions.

(Arguments are like ingredients in a recipe).

So we can have

Reading Values

For that we need a specialized kind of object.

(To make a phone call you need a phone, to bake a cake you need an oven, etc.)

So here's how we read an integer:

ConsoleReader craig; // choose a name 
craig = new ConsoleReader(System.in); // create an actual console reader (and start calling it craig) 
int n; 
n = craig.readInt(); 
So I decided to refer to my console reader as craig.

Then I prepare a location (call it n) where I will put my integer.

And then I assign to n the value of an expression (craig.readInt()).

The expression retrieves the value from the keyboard and makes it available to us.

So we assign (put/place) that value to (into) n.

Reading doubles is slightly different yet very similar:

double a; 
a = craig.readDouble(); 
And here's how we read Strings:
String u; 
u = craig.readLine(); 

Storing Values in Variables

We've seen how we do that already: assignment statements.

But we need to be careful: expressions have types and so have variables.

Thus the following are for your practice and review:

Again, by and large this is all you need.

Concatenating Strings. Why?

Assume the following code:
int x = 3; 
int y = 4; 
System.out.print("The sum of"); 
System.out.print(x);
System.out.print(" and ");  
System.out.print(y); 
System.out.print(" is "); 
System.out.println(x + y); 
Isn't it so much better (easier) to write the code as such:
int x = 3, y = 4; 
System.out.println("The sum of " + x + " and " + y + " is " + (x + y))
Why do we need to put the x + y in parentheses?

Taking the Square Root

Just like
System.out.println(2)
magically prints 2 in your command prompt window so does
Math.sqrt(2)
magically amount to the square root of 2 in an expression.

Types (Again!)

Math.sqrt(2) amounts to a double.

So don't try to store that value in an int variable.

Can you determine what Math.round(...) does and the type it returns?

Here's a link to class java.lang.Math that has all these functions.

So does this work:

int n; 
long m = 2; 
n = m + 1; 
Why or why not? (How do you find out?)

frilled.cs.indiana.edu%cat Alpha.java
class Alpha {
  public static void main(String[] args) {
    int n; 
    long m = 2; 
    n = m + 1; 
  } 
} 
frilled.cs.indiana.edu%javac Alpha.java
Alpha.java:5: possible loss of precision
found   : long
required: int
    n = m + 1; 
          ^
1 error
frilled.cs.indiana.edu%
What do you do about it?

Slicing Strings

This amounts to knowing the difference between the two substring methods.

If a is a String how many characters does it have?

Measuring Strings

If a is a String it has a.length() characters.

What is the index of the first one? (Answer: 0)

What is the index of the last one? (Answer: a.length() - 1)

When Do We Need Casting?

For us, for now, only when we want to truncate.

Here's an example:

frilled.cs.indiana.edu%cat Alpha.java
class Alpha {
  public static void main(String[] args) {
    double amount = 4.35; // in dollars  
    amount = 4.35 * 100; // in cents (has type double) 
    System.out.println(amount); // does it print 435.0? 
    int cents = (int) amount; 
    System.out.println(cents); // have we lost a cent? 
  } 
} 
frilled.cs.indiana.edu%javac Alpha.java
frilled.cs.indiana.edu%java Alpha
434.99999999999994
434
frilled.cs.indiana.edu%
So things are not as simple as they should be.

frilled.cs.indiana.edu%cat Alpha.java
class Alpha {
  public static void main(String[] args) {
    double amount = 4.35; // in dollars  
    amount = 4.35 * 100; // in cents (has type double) 
    System.out.println(amount); // does it print 435.0? 
    int cents = (int) (Math.round(amount)); 
    System.out.println(cents); // have we lost a cent? 
  } 
} 
frilled.cs.indiana.edu%javac Alpha.java
frilled.cs.indiana.edu%java Alpha
434.99999999999994
435
frilled.cs.indiana.edu%
Now things are OK, but why do we still need the (int)?

Rounding. How?

By now this should be the old hat. (See above).

The Meaning of char

A char is a numeric code that prints like a character.

So for example

Take a look at this program:

frilled.cs.indiana.edu%cat cat Alpha.java
cat: cat: No such file or directory
class Alpha {
  public static void main(String[] args) {
    char a = 97; 
    System.out.println(a); 
  } 
} 
frilled.cs.indiana.edu%javac Alpha.java
frilled.cs.indiana.edu%java Alpha
a
frilled.cs.indiana.edu%
How do you print the 20 characters that follow the character with code 45?

Oh, that's just like the square root approximation (or the loan calculation).

The Chare of chars.

frilled.cs.indiana.edu%cat Alpha.java
class Alpha {
  public static void main(String[] args) {
    char a = 97; // change to 45                 [1] 
    System.out.println(a);                    // [2] 
    // get code of next char, store it in a   
    a = (char) (a + 1); // a + 1 is an int!      [3] 
    System.out.println(a); // print next char    [4] 
    
    // perform [3] and [4] over and over 
    //(for as many times as you need or want to) 

  } 
} 
frilled.cs.indiana.edu%javac Alpha.java
frilled.cs.indiana.edu%java Alpha
a
b
frilled.cs.indiana.edu%

More Examples?

In class we will also go over the MilitaryFormat.java program as well.


Last updated by Adrian German for A201/A597/I210 on Mon Jan 24 20:16:41 EST 2005