Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance

Similar presentations


Presentation on theme: "Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance"— Presentation transcript:

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 http://home.comcast.net/~glance44/microhard/java2.html

2 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 2 Your Objective Your Name

3 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 3 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.

4 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 4 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”

5 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 5 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.

6 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 6 Integer Primitives NOTE: 8 bits = 1 byte TypeWidth in bits (bytes) MIN_VALUEMAX_VALUE byte8 (1)-2 7 (-128)2 7 -1 (127) short16 (2)-2 15 (-132768)2 15 -1 (32,767) int32 (4)-2 31 (-2,147,483,648)2 31 -1 (2,147,483,647) long64 (8)-2 63 (-9,223,372,036,854,775,808L) 2 63 -1 (9,223,372,036,854,775,807L)

7 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 7 Other Primitives TypeWidth in bits (bytes) MIN_VALUEMAX_VALUE float32 (4)1.401298e-453.40282e+38 double64 (8)4.940656e-3241.797693e+308 char16 (2)0x00xffff booleanN/AIs either true or false

8 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 8 Default Values for Member Variables TypeDefault Value booleanfalse char‘\u0000’ integers0 floating points0.00F or 0.00 object referencesnull

9 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 9 Problems What is wrong with these assignment statements? char e = -29; float d = 25.9; int i = 50000; short s = i;

10 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 10 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

11 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 11 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.

12 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 12 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’.

13 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 13 Arrays, ctd To get the number of elements that can be stored in the array ‘keys’: keys.length This 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.

14 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 14 Initializing Arrays of Primitives int[] 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 creation float[] floatArray;// declare a reference floatArray float f1 = 1.1, f2 = 3.3, f3 = 5.5; floatArray = new float[]{f1, f2, f3};

15 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 15 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(); }

16 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 16 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 immediately int[][] twoDeeArray1 = new int[10][5]; // The above is really a short way to do the following int[][] twoDeeArray2 = new int[10][]; for(int i = 0; i < twoDeeArray2.length; i++) twoDeeArray2[i] = new int[5];

17 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 17 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]?

18 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 18 Person Class class Person { private String firstName; private String lastName; // CONSTRUCTOR public Person(String firstName, String lastName){ setFirstName(firstName); setLastName(lastName); } // ACCESSORS public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } // MUTATORS public 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...

19 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 19 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”);

20 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 20 Person Array Populate person2 with anonymous Person objects. Person[] person2 = { new Person(“a”, “b”), new Person(“c”, “d”) };

21 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 21 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).

22 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 22 Inheritance class Vehicle { protected float weight;// protected means subclasses have copies protected int numDoors;// of these variables for their data. You should not protected int numTires;// redefine them in subclasses. // Constructor public 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; // Constructor public Bike(int numSpokes, float weight, int numDoors, int numTires){ super(weight, numDoors, numTires);// calls constructor of superclass this.numSpokes = numSpokes; }

23 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 23 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?

24 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 24 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

25 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 25 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.

26 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 26 Vehicle[] vehicles = new Vehicle[2]; // weight, numDoors, numTires vehicles[0] = new Vehicle(1000, 4, 4); // numSpokes, weight, numDoors, numTires vehicles[1] = new Bike(200, 75, 0, 2); Constructors

27 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 27 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 + ")"; }

28 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 28 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 + ")"; }

29 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 29 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] );

30 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 30 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.

31 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 31 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 object String s = b.getTitle();// s = _____? String t = s.toLowerCase();// Is this an error? }

32 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 32 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(); }

33 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 33 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 = “”;... }

34 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 34 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 }

35 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 35 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]); }

36 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 36 What is the Output? java Test java Test a java Test a “b” java Test 1 2 c

37 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 37 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.

38 Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 38 End of Chapter 1


Download ppt "Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance"

Similar presentations


Ads by Google