The Java programming language

(ε-faster than slow motion)

Type casting numbers

Last class we saw what happens if we do things like

Type casting numbers

We can force our program to treat a number as having a different type

(TYPE)variable_name

Forcing java to treat a double as an int


Forcing java to treat a int as an double

Visualizing conditional statements

A simple conditional statement

Visualizing conditional statements

Chained conditional statements

Loops and Arrays!

The while loop

while ( logical_expression )
do something;

The while loop


while ( there are apples left in the box )
do something;

The while loop


The while loop


while ( there are apples left in the box ){
if ( the apple is not rotten )
then it goes in one of the shelves;
else
then it goes in the garbage bin;
}

The while loop


Visualizing the while loop

Do you think you understand?

Write a program that prints the numbers from 1 to 100.
Then change it so that:
  • for multiples of 3 it prints “Fizz” instead of the number
  • for multiples of 5 it prints “Buzz”
  • for numbers which are multiples of both 3 and 5 it prints “FizzBuzz”


Hint: Think of integer division.

See the story behind this problem: http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/

Working with more data

Imagine we want to write a program that uses a calendar...



A friend wants to come visit Montreal

Your friend wants to come when the weather is nice

We will use a computer program to help us decide the best time

Declaring variables for September


Declaring variables for January


Declaring variable for months


This is becoming VERY ineffective!

Arrays

Arrays provide a way of holding more than one value with the same variable name

The values in the arrays are of the same type


Visualizing Arrays

Visualizing Arrays

Visualizing Arrays

Declaring Arrays

If we know the size but not the content of the array

TYPE[] variable_name = new TYPE[N];


If we know the content of the array

TYPE[] variable_name = {VALUE_1,VALUE_2, ..., VALUE_N};

Breaking down the declaration of an Array - 1

int: the type of the array

[]: this variable is an array

days_per_month: the variable name

{31,28,31,30,31,30,31,31,30 31,30,31}: The content of the array

Breaking down the declaration of an Array - 2

int: the type of the array

[]: this variable is an array

days_per_month: the variable name

new int: Allocating memory on the computer for this array

[12]: The length of the array (number of elements)

Accessing Arrays


Processing data using Arrays

The usual way of using arrays is to do something for every element in the array

Remember: positions in the array start at 0. The last element of an array with n elements is at position n-1

The for each loop

for (TYPE element_name: array_name ){
do something with element_name;
}

The for each loop


The indexed for loop

for (initialize_index; logical_expression; change_index_value){
do something;
}

initialize_index;
while ( logical_expression ){
do something;
change_index_value;
}

The indexed for loop


Try something by yourself

In the code for this class, the is a WeatherData.java file

The file Example6_Calendar.java shows how to use it, to get monthly temperature data

Using loops only:

Resources

/