More from Chp 3: I/O, Java Statements


Input/Ouput


Console Output Briefly

To write output to a console window:

String name = "Dr. Seuss";
int age = 65;
System.out.print("My name is " + name + 
	         " and my age is " + age);

Here the symbol + is used as a concatenation operator and not numerical addition. We say + is an overloaded operator. Java knows how to interpret the + symbol by the context.

In Visual Cafe, this program will cause the output to appear in the console window.

Console output is easy but console input is a mess!


GUI I/O using the javabook Package

Class for reading input: InputBox

Class for writing output: OutputBox (see page 112 for list of methods)

For example, click here.


Making Change Example

Suppose you are asked to convert some number of cents into the correct change. Assume you have dollar bills, quarters, dimes, nickels, and pennies. How would you do it?


Java Statements

<variable> = <expression>;

For Example:

int height;
height = 34;


The Java Assignment Statement

CORRECT: <variable> = <expression>;

int width, height;
width = 476;
height = 23;

INCORRECT: <expression> = <variable>;

2 = width;

width + 2 = height;

width + height = 43


Expressions


Initial Values

You must always set the value of a variable before using it. This is referred to as initializing the variable. This can be done at declaration time for primitives or objects:

  Player p = new Player();
  int width = 23;

Or later

  int width;
  width = 23;

What happens if you forget to initialize?

  Player p;
  p.takeTurn(); // p not initialized

Here, p doesn't point to anything so the compiler will give a Null pointer exception.

  int area, height, width=4;
  area = width * height;

Here, height is not initialized. Most compilers (but don't count on it!) will probably initialize height to 0. Some compilers will complain and not even let you compile. Bottom line: you can't predict what will happen - this is bad programming!


Arithmetic Expressions

7 / 2 - takes only integer part
7 % 2 - remainder after dividing

27.5 + -27.2


Precedence

Which Operator Goes First? see page 94 in text

High Precedence (performed first)

()

unary -, +

* / %

+ -

Low Precedence (performed last)

Examples:

int number = 5 * 7 - 15 % 6 + 3/4*5 + 3/(4*5) ;


Some More Arithmetic Expressions


Java is (sort-of) Strongly Typed

This means that if you create variables of different types, you can't always use them together.

Often numbers can be combined but be careful - the results may not always be what you expect!

Example 1:

  int age = 45;
  char letter = 'k';
  letter = age; // WON’T WORK 

Example 2: Mixing numerical types

int age = 45;
double fraction = 0.4;
fraction = age; // WILL WORK
age = fraction; // WON’T WORK (info can be lost)

Can force type change by type casting.

age = (int) fraction; // WILL WORK

Example 3: Mixing apples and oranges

int person = new Player(); // WON’T WORK
Dice mydice = new Player(); // WON’T WORK 


Constants


The Math class

(see table on page 100-101)

Contains methods for standard math functions.

Note the format for message passing is a little different than what we have seen before because these methods are class methods and not instance methods.

Instance methods:

<object>.<method name>(<arguments>)

Class methods:

<class name>.<method name>(<arguments>)

Why make them class methods?? It is more convenient because we need not create a Math object in order to use these methods.


next lecture