Anda di halaman 1dari 17

The Art of Programming in Java

Professor Nathaniel Hepburn January 17, 2011

2 Preface

My goal of writing this book is to discuss some interesting techniques to write ecient Java programs with the assumptions that you knew one of the procedural languages. Nathaniel Hepburn Sunnyvale, California

Contents
1 Object Oriented Programming in Java 1.1 Classes, Objects, and Constructors . . 1.2 Methods . . . . . . . . . . . . . . . . . 1.2.1 Recursion . . . . . . . . . . . . 1.2.2 Static Factory Method . . . . . 1.2.3 Overloading Methods . . . . . 1.3 Interface . . . . . . . . . . . . . . . . . 1.4 Inheritance . . . . . . . . . . . . . . . 1.5 Polymorphisms . . . . . . . . . . . . . 1.6 Serializations . . . . . . . . . . . . . . 1.7 Reections . . . . . . . . . . . . . . . . 2 Garbage Collection in Java 3 Threaded Programming 4 Graphics 5 JOpenGL 5 5 7 7 9 9 9 9 9 9 9 11 13 15 17

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

CONTENTS

Chapter 1

Object Oriented Programming in Java


1.1 Classes, Objects, and Constructors

A class is the detailed information from which individual objects are created. An instance of the class is an object. A class contains states and methods. A static method is like a function in C with the static keyword. Method has a signature (which species its return type and the types and names of its parameters) and a body (which consists of a sequence of statements, including a return value back to the client). When a client invokes a method, the parameters are initialized with client values, the lines of code are executed until a return value is computed, and the value is returned to the client, with the same eect as if the method invocation in the client were replaced with that value. All of this action is the same as for static methods, but there is one critical distinction for instance methods: they can perform operations on instance values.this is the keyword and is the reference of the current object whose method or constructor is being called. A constructor is a method that has the same name with the class, has no return type, and initializes the object. Example 1.1: public class Student { public String rst name; public String last name; public String email; public Student(String rst, String last, String email) { this.rst = rst; this.last = last; this.email = email; 5

6 } ... }

CHAPTER 1. OBJECT ORIENTED PROGRAMMING IN JAVA

Example 1.2: public class Square { public int x; public Square() this(0, 0, 0); } public Squase(int side) { this(0, 0, side); public Square(int x,int y, int side) { this.x = x; this.y = y; this.side = side; } ... } Example 1.3: public class Point { public int x = 0; public int y = 0; // a constructor public Point(int a, int b) { x = a; y = b; }

public class Rectangle { public int width = 0; public int height = 0; public Point origin; public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) {

1.2. METHODS origin = new Point(0, 0); width = w; height = h;

} public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for translating the rectangle public void translate(int x, int y) origin.x = x; origin.y = y; } // a method for computing the area of the rectangle public int getArea() { return width * height; } } // end of Rectangle. Exercise 1.1: Extend the class student in example 1.1 with age, sex, major, gpa, and methods to access them. Exercise 1.2: Write and run a demo class containing main to test methods in example 1.3. Exercise 1.3: Write and run a class card containing 13 ranks 2-10, ace, jack, queen, king and 4 suits diamonds, spades, hearts, and clubs. Methods are isValidRank, isValidSuit return true or false, constructor to make a card, getRank to get the rank, getSuite to get a suit of the card. Write a main to test methods and print the card out.

1.2
1.2.1

Methods
Recursion

A recursive method is a method that calls itself. A recursive method has the base case to stop the recursion and the general case. The way to thinking recursively is to see the solution to the problem as a smaller version of the same problem. If we forget the base case, then we have innite recursion. This is the case of stack overow. Recursive methods are very short and elegant but it is hard to debug, slow, and inecient. Example 1.2.1.1: Factorial function. We dene factorial(n) = n. n-1...3. 2. 1 and factorial(0) = 1. Therefore factorial(n) = n * factorial(n-1) public int factorial(int n) {

CHAPTER 1. OBJECT ORIENTED PROGRAMMING IN JAVA

if n <= 0 return 1; else return factorial(n) = n * factorial(n-1); }

Tail recursive functions are functions in which all recursive calls are tail calls. Unfortunately, the Java Virtual Machine does not optimize tail calls itself. For each recursive call a new stack frame is built, that will slow down the calculation and will eventually lead to a StackOverowError for large n. Example 1.2.1.2: tail recursion public static int ifact(int n, int accumulator) { if (n == 1) return accumulator; else return ifact(n - 1, accumulator * n); } Example 1.2.1.2: tail recursion public String reverse(String str) { if ((null == str) || (str.length() <= 1)) return str; { reverse (str, ); } private String reverse(String str, String accumulatoion) { if (str.length() == 0)return accumulation; else return reverse(str.substring(1), str.charAt(0) + accumulation); }

Example 1.2.1.3: non tail recursion version.

1.3. INTERFACE public String reverse(String str) { if ((null == str) || (str.length() = 1 return str; else return reverse(str.substring(1)) + str.charAt(0); }

Exercise 1.4: Write and run a program to reverse an integer. Exercise 1.5: Write and run a program to check if the word is a palindrome. A palindrome is a word or sentence that reads the same forward as backward.

1.2.2 1.2.3

Static Factory Method Overloading Methods

1.3 1.4 1.5 1.6 1.7

Interface Inheritance Polymorphisms Serializations Reections

10

CHAPTER 1. OBJECT ORIENTED PROGRAMMING IN JAVA

Chapter 2

Garbage Collection in Java

11

12

CHAPTER 2. GARBAGE COLLECTION IN JAVA

Chapter 3

Threaded Programming

13

14

CHAPTER 3. THREADED PROGRAMMING

Chapter 4

Graphics

15

16

CHAPTER 4. GRAPHICS

Chapter 5

JOpenGL

17