sun certified java programmer, ©2004 gary lance, chapter 1, page 1 sun certified java 1.4...

38
Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance [email protected] http://home.comcast.net/~glance44/ microhard/java2.html

Upload: maximilian-morris

Post on 02-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Sun Certified Java 1.4 ProgrammerChapter 1 Notes

Gary Lance

[email protected]://home.comcast.net/~glance44/microhard/java2.html

Page 2: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Your Objective

Your Name

Page 3: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Getting Java to Run

• Create a folder on the desktop. (Suggestion – its name should be your name, or part of it). My folder is “glance”.

• RMB on “cmd.com” in WINNT/System, drag to your folder, release and select “copy to folder”.

• RMB on MS-DOS, and change properties – “start-in” to name of folder.

Page 4: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Environmental Variables

• Create “User-variable” named “CLASSPATH”, if it doesn’t exist.

• Append your folder name to “value”. If there is already a “CLASSPATH”, at end of string, put “;”, and then the path to your folder.

• For system variables, edit “PATH” or “path” to include path to “lib” folder of the Java jdk (which is in the Sun folder”

Page 5: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Java Conventions

• All variables will always start with a small letter.

• All class names will start with a capital letter.

• All final strings will consist of capital letters and underscores ‘_’ only.

• Variables and class names that are compound will have “camel code”.

• Java is case-sensitive, so ‘Person’ and ‘person’ are not the same.

• There can only be ONE public class per file, and the name of that file MUST be the name of that class, with “.java” appended to its end.

• A Java file can have any number of non-public classes.

• Package names should begin with a lowercase letter.

Page 6: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Integer Primitives

NOTE: 8 bits = 1 byte

Type Width in bits (bytes)

MIN_VALUE MAX_VALUE

byte 8 (1) -27 (-128) 27-1 (127)

short 16 (2) -215 (-132768) 215-1 (32,767)

int 32 (4) -231 (-2,147,483,648) 231-1 (2,147,483,647)

long 64 (8) -263

(-9,223,372,036,854,775,808L)

263-1

(9,223,372,036,854,775,807L)

Page 7: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Other Primitives

Type Width in bits (bytes)

MIN_VALUE MAX_VALUE

float 32 (4) 1.401298e-45 3.40282e+38

double 64 (8) 4.940656e-324 1.797693e+308

char 16 (2) 0x0 0xffff

boolean N/A Is either true or false

Page 8: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Default Values for Member Variables

Type Default Value

boolean false

char ‘\u0000’

integers 0

floating points 0.00F or 0.00

object references null

Page 9: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Problems

What is wrong with these assignment statements?

char e = -29;

float d = 25.9;

int i = 50000;

short s = i;

Page 10: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Statement Basics

Equal “draws from the right”:

int a, b, c;

a = b = c = 10;

1. c is assigned the value 10

2. b is assigned the value held by c

3. a is assigned the value held by b

Page 11: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Arrays

int[] key; // declares a reference ‘key’ that can hold int values

We haven’t discussed references yet, but for now, know that a reference will eventually hold the address of the beginning location of “something”. Here, the “something” is the beginning of the location where the array ‘key’ will eventually be stored.

Page 12: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Arrays

int[] key = new int[100]; // array holding 100 primitive types ‘int’

Recall that ‘=‘ draws from the right.

1. Memory is created that can hold 100 ints. (How many bytes is this?)

• The memory for an array will always be consecutive.• Arrays are 0-based.• The O/S returns an address which is where the start of the array is

in memory.2. Each element of the array has its default value (0, in this case).3. A variable ‘key’ is declared which can old the address of an

array of int.4. The address of the array returned by the O/S becomes the

value of ‘key’.

Page 13: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Arrays, ctd

To get the number of elements that can be stored in the array ‘keys’:

keys.lengthThis is a special Java syntax for arrays. The ‘.’

between ‘keys’ and ‘length’ does not have the same meaning when appending a dot to an object reference.

Note: “length” does not have “()” after it. This will often be confused with the method “length()” of the String class, which returns the number of chars in the String object.

Page 14: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Initializing Arrays of Primitivesint[] intArray = new int[3];intArray[0] = 1;intArray[1] = 89;intArray[2] = 789;

double[] dblArray = {1.2, 3.4, 5.6};

int[] scores;scores = new int[]{1, 89, 789};

What is wrong with the next statements?// Anonymous array creationfloat[] floatArray; // declare a reference floatArrayfloat f1 = 1.1, f2 = 3.3, f3 = 5.5;floatArray = new float[]{f1, f2, f3};

Page 15: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Explain This Code

private static void println( float[] array ){

for(int i = 0; i < array.length; i++){

System.out.print( array[i] );

if( i != array.length - 1)

System.out.print(", ");

}

System.out.println();

}

Page 16: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Multi-Dimensional Arrays

A 2-Dimensional array is just an array of arrays. That is, each first index will hold an array. You can then generalize this concept for higher dimensional arrays.

// Create a 2-dimensional array twoDeeArray1,// specifying all the storage immediatelyint[][] twoDeeArray1 = new int[10][5];

// The above is really a short way to do the followingint[][] twoDeeArray2 = new int[10][];for(int i = 0; i < twoDeeArray2.length; i++)

twoDeeArray2[i] = new int[5];

Page 17: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Initialize Multidimensional Array

int[][] dimArray = { {1,2,3}, {4,5}, {6,7,8,9,10,11} };

1. What is dimArray[0]? It is a ___ to a ___ element array.

2. What is dimArray[1]? It is a ___ to a ___ element array.

3. What is dimArray[2]? It is a ___ to a ___ element array.

4. What is the value of dimArray[2][4]?

Page 18: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Person Classclass Person {

private String firstName;private String lastName;

// CONSTRUCTORpublic Person(String firstName, String lastName){

setFirstName(firstName);setLastName(lastName);

}

// ACCESSORSpublic String getFirstName(){

return firstName;}

public String getLastName(){return lastName;

}

// MUTATORSpublic void setFirstName(String firstName){

this.firstName = firstName;}

public void setLastName(String lastName){this.lastName = lastName;

}}

We need a simple class in order to demonstrate how to create arrays of objects.

We will cover more details about classes soon...

Page 19: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Person[] persons

Create one Person object, and call it ‘joe’.Person joe = new Person(“Joe”, “Smith”);

What if we want to store names of 10 people? (Is the code below ok?)

Person[] person = new Person[10];person[0] = new Person(“Gary”, “Lance”);person[1] = new Person(“Shan”, “Nawaz”);person[2] = new Person(“Abe”, “Lincoln”);...person[10] = new Person(“Doesn’t”, “Exist”);

Page 20: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Person Array

Populate person2 with anonymous Person objects.

Person[] person2 = {

new Person(“a”, “b”),

new Person(“c”, “d”)

};

Page 21: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Basic Inheritance

Everything Is An Object.public class Person {...}

Person implicitly extends Object:public class Person extends Object {...}

Person is thus a “special” Object, having the instance variables and methods of Person, as well as the functionality of Object (through the public methods of Object).

Page 22: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Inheritanceclass Vehicle {

protected float weight; // protected means subclasses have copiesprotected int numDoors; // of these variables for their data. You should notprotected int numTires; // redefine them in subclasses.

// Constructorpublic Vehicle(float weight, int numDoors, int numTires){

this.weight = weight;this.numDoors = numDoors;this.numTires = numTires;

}}

// A BIKE IS A VEHICLE, BUT A VEHICLE IS NOT A BIKE.class Bike extends Vehicle {

private int numSpokes;

// Constructorpublic Bike(int numSpokes, float weight, int numDoors, int numTires){

super(weight, numDoors, numTires); // calls constructor of superclassthis.numSpokes = numSpokes;

}}

Page 23: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Inheritance

A [superclass] reference can hold a reference to either

1. a reference of the same class

2. a reference to a subclass

Vehicle v = new Vehicle(); // holds ref. to Vehicle (ok)

Bike b = new Bike(); // holds ref. to Bike (ok)

v = b; // ok, since v is a superclass ref.

b = v; // runtime error (page 309). Why?

Page 24: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Given the previous definitions of classes Vehicle and Bike, what is wrong with this code?

Vehicle[] vehicles = new Vehicle[2];

vehicles[0] = new Vehicle();

vehicles[1] = new Bike();

Constructors

Page 25: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Constructors

Answer: If you define a constructor that has parameters, and you do not define a no-argument (or default) constructor, then if you create an object with the default constructor, you will get a compiler error.

The Solution: (1) use an existing constructor, (2) define a no-argument constructor, or (3) do not use the no-argument constructor when creating an object of that class.

Page 26: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Vehicle[] vehicles = new Vehicle[2];

// weight, numDoors, numTiresvehicles[0] = new Vehicle(1000, 4, 4);

// numSpokes, weight, numDoors, numTiresvehicles[1] = new Bike(200, 75, 0, 2);

Constructors

Page 27: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

class Vehicle with Default Constructor and toString()

class Vehicle {protected float weight;protected int numDoors;protected int numTires;

public Vehicle(){}

public Vehicle(float weight, int numDoors, int numTires){this.weight = weight;this.numDoors = numDoors;this.numTires = numTires;

}

public String toString(){return "(weight, numDoors, numTires) = (" +

weight + ", " + numDoors + ", " + numTires + ")";}

}

Page 28: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

class Bike with Default Constructor and toString()

class Bike extends Vehicle {private int numSpokes;

public Bike(){}

public Bike(int numSpokes, float weight, int numDoors, int numTires){super(weight, numDoors, numTires);this.numSpokes = numSpokes;

}

public String toString(){return "(numSpokes, weight, numDoors, numTires) = (" +

numSpokes + ", " + weight + ", " + numDoors + ", " + numTires + ")";

}}

Page 29: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

What is Printed?

// Create Vehicle array

Vehicle[] vehicles = new Vehicle[2];

vehicles[0] = new Vehicle();

vehicles[1] = new Bike();

// Print out vehicles[0] and vehicles[1]

System.out.println( vehicles[0].toString() );

System.out.println( vehicles[1] );

Page 30: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Instance (or member) Variables

Defined within a class, represent the data that defines the objects of that class.

For example, class Vehicle has three instance variables: weight, numDoors and numTires.

class Bike has four instance variables: those of Vehicle, and numSpokes.

RECALL: The default value of object references is null.

Page 31: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Don’t Forget To Initialize Objects Before Using Them

‘null’ is a valid value for an object reference, but you can’t send messages to it.

public class Book {private String title;public String getTitle(){ return title; }

public static void main(String[] args){Book b = new Book(); // create Book objectString s = b.getTitle(); // s = _____?String t = s.toLowerCase(); // Is this an error?

}}

Page 32: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Bad Way to Correct main(), in this example.

public class Book {private String title;public String getTitle(){ return title; }

public static void main(String[] args){Book b = new Book();String s = b.getTitle(); if( s != null) // why is this stupid? Because s always is

null.String t = s.toLowerCase();

}}

Page 33: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Other Ways to Correct main()

public class Book {private String title;public Book(){ // default constructor

title = “”; // short for: title = new String(“”);}...

}

public class Book {private String title = “”;...

}

Page 34: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Local Variables do NOT Behave Like Instance Variables

public class Book {private String title;private int numPages;public String getTitle(){ return title; }

public static void main(String[] args){int numPages;System.out.println( numPages ); // compiler error – not

initialized}

}

Page 35: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Command Line Arguments

class Test {public static void main(String[] args){

int length = args.length;System.out.println(length);

// Are the next statements valid?for(int i = 0; i < length; i++)

System.out.println(args[i]);}

}

Page 36: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

What is the Output?

java Test

java Test a

java Test a “b”

java Test 1 2 c

Page 37: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

Problem

1. Create an array ‘person’ of ten Person objects, and store the references to 10 Person objects in that array.

2. Create another array ‘personRev’ that can hold 10 Person objects.

3. Copy the references of person into personRev, but in reverse order. Use a for-loop.

Page 38: Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance glance44@comcast.net glance44/microhard/java2.html

End of Chapter 1