Lecture Notes, class 7 for csc211.

Prepared by: Anthony Larrain

These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.


Classes

Recall all Java programs are built from classes.

In Java a class is used in several way's.


Stack and the Heap


Writing Class or Static Methods

Exercises

For the following exercises I will place all the method definitions in a class called ClassMethods. This is strictly for convenience. Typically methods should be grouped together under class names when they are related in some way. Like the Math functions.


Arrays In Java

Consider the problem, suppose we wanted to find the average of a collection of scores and print out a table showing each score and its distance from the mean.

int [] data // declares a reference to an array of integers data = new int [3] // creates an array object with capacity 3. // assign each cell a value data[0] = 2; data[1] = 4; data[2] = 6; System.out.println(data[0]); // outputs 2 if(data[1] == 4) { System.out.println("Yes it is 4 "); } data[0] = data[2]; System.out.println(data[0]); // outputs 6 System.out.println(data[2]); // outputs 6; System.out.println( data.length ) // outputs 3 data[3] = 1000; // ERROR: not in bounds, arrays start at 0 // can declare, construct and initialize in one step int [] data = {2,4,6}; // can create arrays of other types char [] vowels = {'a','e','i','o','u'}; String [] days = {"sunday", "monday", "tuesday"};