Sie sind auf Seite 1von 75

Why Java?

Object oriented Interpreted Portable Simple yet feature-full Secure and robust Scalable High-performance multi-threaded Dynamic Distributed

Object Oriented Everything in Java is coded using OO principles. This facilitates code modularization, reusability, testability, and performance. Interpreted/Portable Java source is complied into platform-independent bytecode, which is then interpreted (compiled into native-code) at runtime. Javas slogan is "Write Once, Run Everywhere" Simple Java has a familiar syntax, automatic memory management, exception handling, single inheritance, standardized documentation, and a very rich set of libraries (no need to reinvent the wheel). Secure/Robust Due to its support for strong type checking, exception handling, and memory management, Java is immune to buffer- overruns, leaked memory, illegal data access. Additionally, Java comes with a Security Manager that provides a sand-box execution model. Scalable Thanks to its overall design, Java is scalable both in terms of performance/throughput, and as a development environment. A single user can play a Java game on a mobile phone, or millions of users can shop though a Java-based e-commerce enterprise application. High-performance/Multi-threaded With its HotSpot Just-in-Time compiler, Java can achieve (or exceed) performance of native applications. Java supports multi-threaded development out-of-the-box. Dynamic Java can load application components at run-time even if it knows nothing about them. Each class has a run-time representation. Distributed Java comes with support for networking, as well as for invoking methods on remote (distributed) objects through RMI.

A Java Hello World Program


Implementing Hello World in Java

2.1. HelloWorld.java
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } All code is contained within a class, in this case HelloWorld. The file name must match the class name and have a .java extension, HelloWorld.java

for example:

All executable statements are contained within a method, in this case named main(). Use System.out.println() to print text to the terminal.

Classes and methods (including other flow-control structures) are always defined in blocks of code enclosed by curly braces ({ }). All other statements are terminated with a semi-colon (;). Java language is case-sensitive! This means that HelloWorld is not the same as helloworld, nor is String the same as string. There is an generally accepted naming convention in the Java community to capitalize the names of Java classes and use the so-called CamelCase (or CamelHump) notation, where the first letter of each word is capitalized, but the words are joined together (i.e. no dashes or underscores). Note Java source files can be encoded in UTF-8 to support internationalized characters (even for class, method, and variable names). In most situations however, Java sources are stored in US-ASCII character set, and internationalized strings are loaded from external resource files.

2.2. Java Keywords


Table 1. Java Keywords
abstract assert case catch continue default enum extends for goto instanceof int new package boolean break byte char class const do double else final finally float if implements import interface long native private protected public

return short static switch synchronized this transient try void

strictfp throw volatile

super throws while

Keywords goto and const are reserved, but never used. Keyword strictfp was added in Java 1.2. Keyword assert was added in Java 1.4. Keyword enum was added in Java 1.5. In addition to these 50 keywords, Java also defined three special literals: true, false, and null. Keywords in our HelloWorld program are in bold:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }

2.3. Java Identifiers

An identifier is the name of a class, variable, field, method, or constructor. o Cannot be a Java keyword or a literal
o o

Can contain letters, digits, underscores, and dollar signs Cannot start with a digit

Valid identifier examples: HelloWorld, args, String, i, Car, $myVar, employeeList2 Invalid identifier examples: 1st, byte, my-Arg, *z Java naming conventions for identifiers:
o o

Use CamelCase for most identifiers (classes, interfaces, variables, and methods). Use an initial capital letter for classes and interfaces, and a lower case letter for variables and methods. For named constants, use all capital letters separated by underscores. Avoid using $ characters in identifiers.

o o

Identifiers in our HelloWorld program are in bold:

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }

2.4. Compiling Java Programs

The JDK comes with a command-line compiler: javac. o It compiles source code into Java bytecode, which is low-level instruction set similar to binary machine code.
o

The bytecode is executed by a Java virtual machine (JVM), rather than a specific physical processor.

To compile our HelloWorld.java, you could go to the directory containing the source file and execute:
javac HelloWorld.java This produces the file HelloWorld.class,

which contains the Java bytecode.

Use the javac -help option to get a full list of available options:
Usage: javac <options> <source files> where possible options include: -g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath <path> Specify where to find user class files and annotation processors -cp <path> Specify where to find user class files and annotation processors -sourcepath <path> Specify where to find input source files -bootclasspath <path> Override location of bootstrap class files -extdirs <dirs> Override location of installed extensions -endorseddirs <dirs> Override location of endorsed standards path -proc:{none,only} Control whether annotation processing and/or compilation is done. -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process -processorpath <path> Specify where to find annotation processors -d <directory> Specify where to place generated class files -s <directory> Specify where to place generated source files -implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files -encoding <encoding> Specify character encoding used by source files -source <release> Provide source compatibility with specified release -target <release> Generate class files for specific VM version

-version -help -Akey[=value] -X -J<flag>

Version information Print a synopsis of standard options Options to pass to annotation processors Print a synopsis of nonstandard options Pass <flag> directly to the runtime system

You can view the generated byte-code, using the -c option to javap, the Java class disassembler. For example:
javap -c HelloWorld

2.5. Running Java Programs


Figure 2. Compiling and running a Java program

To run the bytecode, execute:


java HelloWorld

Do not include the .class extension. The java command starts the JVM and executes the class bytecode. The JVM abstracts O/S and H/W from the Java application.

It is the Java virtual machine that provides the layer of insulation to Java programs so that they do not depend on the underlying operating system or hardware semantics. This allows compiled Java applications to run on any platform that has a Java virtual machine written for it:

AIX BSD HP-UX Linux

Mac OS X Solaris Windows

2.6. Comments in Java Code


// single-line comment o Comments-out everything /* Multi-line comment */ o o

until a line break

Useful for commenting out a section of code Cannot be nested within other multi-line comments

/** JavaDoc comments */ o

Similar to multi-line comments but used to document Java code (classes, methods, fields) Extracted using javadoc command-line utility

/** * My first class * @author student * @version 1.0 */ public class HelloWorld { /** * Main method that runs when the program is started. * @param args command-line arguments */ public static void main (String[] args) { /* This is how you can print to STDOUT */ System.out.println("Hello World"); // don't forget the semi-colon /* System.out.println("I was also going to say..."); System.out.println("but I changed my mind"); */

} }

2.7. The main() Method

A Java application is a public Java class with a main() method. o The main() method is the entry point into the application.

The signature of the method is always:


public static void main(String[] args)

Command-line arguments are passed through the args parameter, which is an array of Strings

Applets and servlets do not have a main() method because they are run through template methods in the context of a framework. For example, a web container is responsible for instantiating a servlet object. It then invokes a standard set of methods defined by the Servlet class at appropriate points in the servlet lifecycle; for example, init() to initialize the servlet, doGet() to handle an HTTP GET message, destroy() when the web container is about to destroy the object, and so on. The parent class provides default implementations of these methods, but the derived servlet class can override any of the template methods to provide servlet-specific functionality.

Data Types
Primitive Data Types in Java

3.1. Declaring and Assigning Variables

The syntax for declaring a variable is:


DataType variableName[

= expression];

Examples:
float j; int i = 5 + 3;

Java is a strongly typed language o Every variable must have an explicit type
o

Expressions assigned to the variable must be a compatible type

Local variables (declared within methods) must be declared and initialized before they are used Class and instance variables can be declared anywhere and are initialized to defaults:
o o o 0

for numerical typed variables for boolean variables

false null

for object reference variables

In contrast to a language like C, in Java the local variables do not have to be declared at the beginning of program blocks (defined by curly braces). They can be declared when they need to be used.

public static void main(String[] args) { System.out.print("Number of arguments: "); int len = args.length; System.out.println(len); }

Multiple variables can be declared on a singe line: int i, j, k; Multiple variables can be initialized at the same time: i = j = k = 5; There is an generally accepted naming convention in the Java community to start variable names with a lower-case letter and use a so-called CamelCase (or CamelHump) notation, where the first letter of each subsequent word is capitalized, but the words are joined together (i.e. no dashes or underscores).

3.2. Java Primitive Types


Table 2. Java Primitive Types Type
boolean byte short char int long float double

Size 1 bit 8 bits 16 bits 16 bits 32 bits 64 bits 32 bits 64 bits

Range or false [-128, 127] [-32,768, 32,767] ['\u0000', '\uffff'] or [0, 65535] [-2,147,483,648 to 2,147,483,647] [-263, 263-1] 32-bit IEEE 754 floating-point 64-bit IEEE 754 floating-point
true

Default*
false 0 0 '\u0000' 0 0 0.0 0.0

The default value for uninitialized class or instance variables of this type.

Even though Java is a fully object oriented language, for performance reasons it defines eight primitive data types. All of these types also have their respective wrapper classes (for true OO) in the default java.lang package (which we will see later). In addition to the eight primitive types, there is also a concept of a void type, which is only used for a method return type (i.e. to indicate that nothing is returned). Examples:
boolean bln = true; byte b = 0x20; short s = 500; char c = 'A'; char tab = '\t'; // // // // // booleans can only be 'true' or 'false' using hexadecimal notation small integer must use single quotes to denote characters other specials: \n, \r, \f, \b, \\, \', \"

int i = 1000000; // decimal notation int j = 0x3FA0B3; // hexadecimal notation int k = 0777; // octal notation float f = 1.5f; // trailing 'f' distinguishes from double long l = 2000000L; // trailing 'L' distinguishes from int double pi = 3.141592653589793; // doubles are higher precision double large = 1.3e100; // using the exponent notation

3.3. Conversion Between Types

Implicit up-casting: a lower-precision type is automatically converted to a higher precision type if needed:
int i = 'A';

Explicit down-casting: a manual conversion is required if there is a potential for a loss of precision, using the notation: (lowerPrecType) higherPrecValue
int i = (int) 123.456;

Figure 3. Implicit up-casting of primitive types

Note Java has a clear separation between numerical types and booleans. They are not interchangeable. This means that an int cannot be used in place of a boolean expression, for example in an if statement:
int someInteger = 0; /* * This will not compile, because someInteger is not * a boolean type (or a boolean expression) */ if (someInteger) { // Do something } // This will compile if (someInteger != 0) { // Do something }

Note Strings are not converted automatically into integers or other primitive data types they may represent. Strings must be parsed:
int myInt = Integer.parseInt(args[0]);

3.4. Declaring Arrays


An array is a simple data structure to hold a series of data elements of the same type. Declare an array variable in one of two ways:
o o

With [] after the variable type: int[] values; With [] after the variable name: int values[];

Arrays can be single- or multi-dimensional.


o

A two dimensional array could be declared with: double values[][];

Array elements are integer indexed.


o o o

Use arrayName.length to get the array length. Elements are indexed from 0 to arrayName.length - 1 Access individual elements with arrayName[index]

Examples of array declarations:


String[] args; int[] numbers; byte[] buffer; short[][] shorts; // // // // single-dimensional single-dimensional single-dimensional double-dimensional array array array array of of of of String objects ints bytes shorts

To get the array length:


int arrayLength = myNums.length; int nameLength = name.length; int bufLength = buf[2].length; // 5 // 3 // 1024

3.5. Creating and Initializing Array Objects


Arrays are implemented as objects. You can use the new operator to create an array of an indicated type and length, as in:
int[] values = new int[10]; o Elements are initialized automatically

to type-appropriate default values. Alternatively, you can use an array initializer to create an array with a set of initial values.
o

The array initializer consists of a sequence of comma-separated expressions of an appropriate type, enclosed within braces. For example:
int[] values = {5, 4, 3, 2, 1};

The array object is sized automatically based on the number of initial values.

Examples of array initializations:


int[] myNums = { 1, 3, 5, 7, 9 }; // 5 elements char[] name = new char[3]; name[0] = 'T'; name[1] = 'o'; name[2] = 'm'; short[][] matrix = { {1,4,7}, {5,6,3}, {2,8,9} }; byte[][] bufs = new byte[10][1024];

To access an element of an array:


int myNum = myNums[3]; // 7 char firstLetter = name[0]; // 'T' matrix[1][2] = 0; // used to be 3

3.6. Modifying Array Size

Once created, the size of an array cannot change. o If you need to grow an array, you must create a larger array object of the same type, and then copy the elements from the old array to the new array.
o o o o o o o o

The System.arraycopy() method is an efficient way to copy the existing elements to the new array. For example:
int[] values = {5, 4, 3, 2, 1}; // A 5-element int array int[] newValues = new int[10]; // A 10-element int array // Copy all elements from values to newValues System.arraycopy(values, 0, newValues, 0, values.length); // Assign the array back to values values = newValues;

In the example above, the values and newValues variables do not actually contain the arrays, they refer to the array objects. Well examine this concept more in the Object Oriented module.

3.7. Strings

Strings are objects. o In Java, strings are instances of the String class.
o o

Unlike C/C++, you cant treat a String directly as an array of chars. There are methods that allow you to create a String from an array of chars, and vice versa.

Enclosing a sequence of literal characters within double quotes automatically creates a String object: "this is my string" Strings are immutable

o o

Once created, a String cannot be modified. But a String has methods to perform various transformations, each returning a new String object.

The String class has many methods, including: length, replace, substring, indexOf, equals, trim, split, toUpperCase, endsWith, etc.

To get the length of a String do:


String s = "my string"; int len = s.length();

To parse a primitive value from a String, do:


boolean b = Boolean.valueOf(s).booleanValue(); byte b = Byte.parseByte(s); short s = Short.parseShort(s); char c = s.charAt(0); int i = Integer.parseInt(s); float f = Float.parseFloat(s); long l = Long.parseLong(s); double d = Double.parseDouble(s);

We will examine the String API later in class.

Operators
Operators in Java

4.1. Arithmetic Operators


Operator Use Description + x + y Adds x and y x - y Subtracts y from x -x Arithmetically negates x * x * y Multiplies x by y / x / y Divides x by y % x % y Computes the remainder of dividing x by y In Java, you need to be aware of the type of the result of a binary (two-argument) arithmetic operator.

If either operand is of type double, the other is converted to double. Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long. Otherwise, both operands are converted to type int.

For unary (single-argument) arithmetic operators:


If the operand is of type byte, short, or char, the result is a value of type int. Otherwise, a unary numeric operand remains as is and is not converted.

This can result in unexpected behavior for a newcomer to Java programming. For example, the code below results in a compilation error:
short a = 1; short b = 2; short c = a + b;

// Compilation error

The reason is that the result of the addition is an int value, which cannot be stored in a variable typed short unless you explicitly cast it, as in:
short c = (short) (a + b);

Also, there are a couple of quirks to keep in mind regarding division by 0:


A non-zero floating-point value divided by 0 results in a signed Infinity. 0.0/0.0 results in NaN. A non-zero integral value divided by integral 0 results in an ArithmeticException thrown at runtime.

Note In Java, unlike C/C++, it is legal to compute the remainder of floating-point numbers. The result is the remainder after dividing the dividend by the divisor an integral number of times. For example, 7.9 % 1.2 results in 0.7 (approximately, given the precision of a float or double type).

4.2. Shortcut Arithmetic Operators


Operator Use Description x++ y = x++; is the same as y ++ ++x y = ++x; is the same as x x-- y = x--; is the same as y ---x y = --x; is the same as x

= x; x = x + 1; = x + 1; y = x; = x; x = x - 1; = x - 1; y = x;

The location of the shortcut operator is only important if the operator is used in an expression where the operand value is assigned to another variable or returned from a method.

That is to say that:


x++;

is equivalent to:
++x;

But in an assignment:
y = x++;

is not the same as:


y = ++x;

Shortcut operators are often used in for loops to increment the loop index variable (as we will see soon).

4.3. String Concatenation

The plus (+) operator performs string concatenation. o The result is a new String object, for example:
String first = "George"; String last = "Washington"; String name = first + " " + last; // "George Washington" You can concatenate a String with Java primitive types. o Java automatically generates a String representation of the primitive value o o

for

concatenation, for example:


o int count = 8; String msg = "There are " + count + " cows."; // "There are 8 cows." arithmetic + operator has the same order of precedence as the String concatenation

The +.

In a complex expression, they are evaluated from left to right, unless you use parentheses to override, for example:
String test = 1 + 1 + " equals " + 1 + 1; // "2 equals 11"

For other object types used as string concatenation operands, Java automatically invokes the objects toString() method, for example:
Date now = new Date(); String msg = "The time is: " + now; // "The time is: Mon Feb 07 18:04:48 PST 2011"

4.4. Relational Operators

Operator
> >= < <= == !=

Use
x > y x >= y x < y x <= y x == y x != y

Description x is greater than y x is greater than or equal to y x is less than y x is less than or equal to y x is equal to y x is not equal to y

Only numerical primitive types (byte, short, char, int, float, long, and double) can be compared using the ordering operators (>, >=, <, and <=). Objects and booleans can only be tested for [in]equality (==, !=). Due to Javas type safety (booleans are not integers and vice versa), it is not possible to make the common mistake:
int x = 5; int y = 3 + 2; // Does not compile if (x = y) { // Do something } // You must use equality operator if (x == y) { // Do something }

4.5. Logical Boolean Operators


Operator Evaluates to true if && x && y Both x and y are true || x || y Either x or y are true ! !x x is not true All operands to these operators must be boolean values. The logical && and \\ operators are subject to short circuit evaluation. Use

These logical operators accept boolean values only. Attempting to use numerical or other operand types results in a compilation error. As with many modern programming languages, Java uses short circuit evaluation when evaluating logical boolean operators. This means that conditional operators evaluate the second operand only when needed:

In x && y, y is evaluated only if x is true. If x is false, the expression immediately evaluates to false. In x || y, y is evaluated only if x is false. If x is true, the expression immediately evaluates to true.

For example:
int i = 0; int j = 5; if ( (i != 0) && ((j / i) > 1) ) { // Do something }

Because we use a logical AND (&&), the first expression evaluates to false and so the second expression is not tested and the overall condition is false.

4.6. Bitwise Operators


Operator
~ & | ^ >> >>> <<

Evaluates to true if ~x Bitwise complement of x x & y AND all bits of x and y x | y OR all bits of x and y x ^ y XOR all bits of x and y x >> y Shift x right by y bits, with sign extension x >>> y Shift x right by y bits, with 0 fill x << y Shift x left by y bits Use

In general, these bitwise operators may be applied to integral values only. For the ~, &, \|, and ^ operators:

If either operand is of type long, the other is converted to long. Otherwise, both operands are converted to type int.

For the shift operators:


If the value being shifted is of type long, the result is of type long. Otherwise, the result is of type int. Binary Result
00000000000000000000000001000111 00000000000000000000000000000101 11111111111111111111111110111000 00000000000000000000000000000101 00000000000000000000000001000111 00000000000000000000000001000010

Operation Decimal Result


x y ~x x & y x | y x ^ y 71 5 -72 5 71 66

Operation Decimal Result


x >> y x >>> y x << y

Binary Result

2 00000000000000000000000000000010 2 00000000000000000000000000000010 2272 00000000000000000000100011100000

The bitwise operators may also be used with boolean values to produce a boolean result. However, you cannot combine boolean and integral operands in a bitwise expression.

4.7. Assignment Operators


Operator
= += -= *= /= %= &= |= ^= >>= >>>= <<=

Use
x x x x x x = += -= *= /= %= y y y y y y

Shortcut for
x x x x x x = = = = = = y x x x x x + * / % y y y y y

x &= y x |= y x ^= y

x = x & y

(also works for boolean (also works for boolean (also works for boolean

values)
x = x | y

values)
x = x ^ y

values)

x >>= y x = x >> y x >>>= x = x >>> y y x <<= y x = x << y

Actually, a compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E1)), where T is the type of E1. For example, the following code is compiles without error:
short x = 3; x += 4.6;

and results in x having the value 7 because it is equivalent to:


short x = 3; x = (short) (x + 4.6);

4.8. Other Operators


Operator
()

Use
(x + y) * z

Description Require operator precedence

Description ?: z = b ? x : y Equivalent to: if (b) { z = x; } else { z = y; } [] array[0] Access array element . str.length() Access object method or field (type) int x = (int) 1.2; Cast from one type to another new d = new Date(); Create a new object instanceof o instanceof String Check for object type, returning boolean The short-cut conditional operator (?:) (also known as the if-then-else operator or the ternary operator) is used very often to make Java code more compact, so it is worthwhile to get accustomed to it. For example:
int x = 0; int y = 5; // Long way int z1; if (x == 0) { z1 = 0; } else { z1 = x / y; } // Short-cut way int z2 = (x == 0) ? 0 : x / y;

Operator

Use

Well explore the other operators listed on this slide in more depth throughout this class.

4.9. Operator Precedence


1. [], (), . 2. ++, --, ~, !, (type), new, 3. *, /, % 4. +, 5. <<, >>, >>> 6. <, <=, >, >=, instanceof 7. ==, != 8. & 9. ^

10. | 11. && 12. || 13. ? : 14. =, *=, /=, +=, -=, %=, <<=, >>=, >>>=, &=, ^=, |=

Use the () operator to change precedence of operators:


x << (((((x == 0 ? 0 : (x / y)) + 5) * z) > 4) ? 3 : 7)

Other than in the most trivial situations, It is always a good practice to use () to document your intentions, regardless of precedence rules. For example, change:
int x = 5 - y * 2 > 0 ? y : 2 * y;

to:
int x = ((5 - (y * 2)) > 0) ? y : (2 * y);

even though both lines will assign the same value to x

Statements and Flow Control


Controlling the Flow of Java Programs

5.1. Expressions

An expression is a code fragment consisting of variables, constants, method invocations, and operators that evaluates to a single value. Examples:
o o o 2 + 2 a = b + c myMethod()

5.2. Statements and Blocks


A statement is a complete unit of execution. Many types of expressions can be made into statements by terminating them with a semicolon (;).

o o o o

Assignment expressions: answer = 42; Increment or decrement expressions: a++; or b--; Method invocations: System.out.println("Hello, world!"); Object creation: Account client = new Account();

A declaration statement declares a variable, optionally assigning it an initial value. For example: int count; A block is a group of zero or more statements between balanced braces ({ }).
o

A block can be used anywhere a single statement is allowed.

A control flow statement is a structure controlling the execution of other statements. Well explore these through the rest of this module.

Java whitespace characters are the space, tab, carriage return, newline (aka linefeed), and formfeed characters. Java requires one or more whitespace characters between keywords and/or identifiers. Other whitespace characters that do not occur within quoted string literals are not considered significant and serve only to increase the readability of code.

5.3. Local Variable Storage: The Stack

All variables declared within a block are known as local variables, which are stored in a memory location known as the stack. Examples of where blocks are used include: o Method definitions
o o

Flow control statements Other blocks contained within a sequence of statements

The stack grows and shrinks as the program runs.


o o

Entering a block expands the stack, storing local variables in memory. Leaving a block shrinks the stack, flushing local variables from memory.

The scope of a local variablethat is, the context in which is visible and accessible to your programis limited to the block in which it is declared, and all nested blocks. Note Java does not allow you to declare a local variable with the same name as a local variable already in the same scope.

public static void main(String[] args) { int x = 1; // A local scope where args and x are visible

while (x <= 5) { String msg = "x is now " + x; // A local scope where args, x, and msg are visible System.out.println(msg); } // msg is no longer in scope, and has been flushed from memory

As previously mentioned, local variables must be initialized before they are used. A local variable is visible only in the block of code (or sub blocks) where it is declared. Along with the variables value, its name is also flushed as the stack shrinks, allowing the same variable to be re-declared in another block of code. Note Method parameters are also considered local variables, because they too are declared (and initialized automatically by the calling code) on the stack. Tip It is a good practice to limit the scope of local variables as much as possible. This promotes variable name reuse. In the case of objects, limiting local variable scope allows the JVM to quickly reclaim the memory occupied by objects that are no longer in use.

5.4. The return Statement


The return statement breaks out of the entire method. It must return a value of the type specified in the methods signature. It must not return a value if the methods return value is of type void. The value can be an expression:

public static int max(int x, int y) { return (x > y) ? x : y; } public class FindNumber { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: FindNumber <num>"); return; } int[] numbers = {1, 3, 4, 5, 7, 5, 2, 8, 9, 6}; int findNumber = Integer.parseInt(args[0]); System.out.println(find(numbers, findNumber)); } public static int find(int[] nums, int num) { for (int i = 0; i < nums.length; i++) { if (nums[i] == num) { return i; }

} return -1; } }

5.5. The if-else Statement


The if-else statement defines a block of code that is to be executed based on some boolean condition.
if (boolean-expression) { // Run if BE is true } else if (boolean-expression2) { // Run if BE is false and BE2 is true } else { // Run if both BE and BE2 are false } The else if part is optional and can appear many times between the if and else parts. The else part is optional but can appear only once after if and all/any else if parts.

public class LetterGrade { public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: LetterGrade <numeric-score>"); return; } int score = Integer.parseInt(args[0]); char grade; if (score grade } else if grade } else if grade } else if grade } else { grade } } >= 90) = 'A'; (score = 'B'; (score = 'C'; (score = 'D'; = 'F'; { >= 80) { >= 70) { >= 60) {

System.out.println("Grade = " + grade); }

Note In this example, the numerical score is parsed from a string (the first and only command-line argument) into an integer data type.

5.6. The switch Statement

The switch statement is a special-purpose if-else-if-else construct that is easier and shorter to write:

switch(expression) { case const1: /* do X */ break; case const2: /* do Y */ break; default: /* do something else */ } o The switch expression must be an integral type char, or int. o case values must be constants (not variables). o case

other than long: byte, short,

values are tested for equality only (==).

public class MonthFromNumber { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: MonthFromNumber <month>"); return; } int month = Integer.parseInt(args[0]); switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month: " + month); } } }

Note You can also switch on enumerated values defined by enum, which is discussed in Typesafe Enums.

5.7. The switch Statement (cont.)

A case is an entry point into a switch statement, whereas a break acts as an exit point. o Typically each case has a trailing break.
o

Without a break, execution automatically falls through to the statements in the following case.

Tip If you intentionally fall through to the following case, include a comment to alert maintenance programmers of your intent. Otherwise, they might think you simply forgot the break and add it in later. The optional default case cannot break and is executed if no other cases match and break prior to it. To exit both the switch statement as well as the method in which it is defined, use a return in place of a break.

public class DaysInMonth { public static void main(String[] args) { if (args.length != 2) { System.err.println("Usage: DaysInMonth <year> <month>"); return; } int year = Integer.parseInt(args[0]); int month = Integer.parseInt(args[1]); int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: numDays = ( ( (year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) )? 29 : 28; break; default: System.err.println("Invalid month " + month); return; } System.out.println("Number of Days = " + numDays);

} }

5.8. The while Loop Statement


The while loop statement executes a block of statements as long as a condition remains true:
while (boolean-expression) {

// Do something repeatedly // Update condition

To ensure the loop body runs at least once, a do-while variant executes the statement block before evaluating the condition:
do { // Do something at least once } while (boolean-expression);

Note Remember to update the condition to avoid infinite loops.


public class WhileArguments { public static void main (String[] args) { int i = 0; while (i < args.length) { System.out.println(args[i]); i += 1; } } }

5.9. The for Loop Statement


The for loop statement is similar to the while loop, but allows compact initialization of an iterator variable and its increment/decrement:
for (init; condition; update) { // Do something } o The initialization statement runs before anything else. o The condition is evaluated before each repetition of the loop o

body.

The update statement is run after each body repetition. This can consist of multiple expression statements separated by commas (,).

Note You can declare a local variable in the for initialization statement, in which case its scope is the loop body.
for (int i = 1; i <= 10; i++) { // i is local to this scope } public class ForArguments { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } } }

Tip You can omit the initialization and/or update statements from the for loop, but you must

still include the ; separator characters:


// x has been declared and initialized elsewhere for (; x < 5; x++) System.out.println(x);

5.10. Using for to Iterate over Arrays and Collections

Java 5 also introduced an advanced for syntax known as for-each. o It is designed specifically for iterating over collections of data, whether whose collections are arrays, or some predefined collection classes (discussed in the Java Collections Framework module).
o o o o o o o o o o

The for-each statement has the following syntax:


for (Type element: collectionOfType) { // Do something with element }

For example:
public class ForEachArguments { public static void main(String[] args) { for (String arg: args) { System.out.println(arg); } } }

5.11. The break Statement


In addition to defining the exit points from cases in a switch statement, the break statement stops the execution of a loop:

while (someCondition) { // Do something if (someOtherCondition) { break; } // Do something else } public class SearchForNumber { public static void main (String[] args) { int[] nums = {1, 5, 4, 43, -2, 6, 4, 9 }; int search = 4; for (int i = 0; i < nums.length; i++) { if (nums[i] == search) { System.out.println("Found " + search + " at position " + i); break; } } } }

5.12. The continue Statement

Skips one execution of a loops body With continue, this:


while (someCondition) { if (someOtherCondition) { continue; } // Do something }

works the same as this:


while (someCondition) { if (!someOtherCondition) { // Do something } } public class SkipOddNumbers { public static void main(String[] args) { int[] nums = { 0, 3, 4, 5, 7, 2, 6, 9, 8, 7, 1 }; for (int i = 0; i < nums.length; i++) { if (nums[i] % 2 != 0) { continue; } System.out.println(nums[i] + " is even"); } } }

5.13. Nested Loops and Labels

Looping structures can be nested. o By default, a break or continue statement affects the innermost loop in which it is located. You can apply an optional label to a looping structure.
o

The label is an identifier followed by a : placed before the looping structure.

Both the break and the continue statement accept an optional label argument.
o

In that case, the break or continue statement affects the looping structure with that label.

OUTER: for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { if (i == j) continue OUTER; } } public class SearchForNumber2D { public static void main (String[] args) {

int[][] nums = { {1, 3, 7, 5}, {5, 8, 4, 6}, {7, 4, 2, 9} }; int search = 4; foundNumber: for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { if (nums[i][j] == search) { System.out.println( "Found " + search + " at position " + i + "," + j); break foundNumber; } } } } }

Object Oriented Programming in Java


Introduction to Object Oriented Programming in Java

6.1. What is Object Oriented Programming (OOP)?


A software design method that models the characteristics of real or abstract objects using software classes and objects. Characteristics of objects:
o o o

State (what the objects have) Behavior (what the objects do) Identity (what makes them unique)

Definition: an object is a software bundle of related fields (variables) and methods. In OOP, a program is a collection of objects that act on one another (vs. procedures).

For example, a car is an object. Its state includes current:


Speed RPM Gear Direction Fuel level

Engine temperature

Its behaviors include:


Change Gear Go faster/slower Go in reverse Stop Shut-off

Its identity is:


VIN License Plate

6.2. Why OOP?


ModularitySeparating entities into separate logical units makes them easier to code, understand, analyze, test, and maintain. Data hiding (encapsulation)The implementation of an objects private data and actions can change without affecting other objects that depend on it. Code reuse through:
o o

CompositionObjects can contain other objects InheritanceObjects can inherit state and behavior of other objects

Easier design due to natural modeling

Even though OOP takes some getting used to, its main benefit is to make it easier to solve realworld problems by modeling natural objects in software objects. The OO thought process is more intuitive than procedural, especially for tackling complex problems. Although a lot of great software is implemented in procedural languages like C, OO languages typically scale better for taking on medium to large software projects.

6.3. Class vs. Object

A class is a template or blueprint for how to build an object. o A class is a prototype that defines state placeholders and behavior common to all objects of its kind.
o

Each object is a member of a single classthere is no multiple inheritance in Java.

An object is an instance of a particular class.


o o

There are typically many object instances for any one given class (or type). Each object of a given class has the same built-in behavior but possibly a different state (data). Objects are instantiated (created).

For example, each car starts of with a design that defines its features and properties. It is the design that is used to build a car of a particular type or class. When the physical cars roll off the assembly line, those cars are instances (concrete objects) of that class. Many people can have a 2007 BMW 335i, but there is typically only one design for that particular class of cars. As we will see later, classification of objects is a powerful idea, especially when it comes to inheritanceor classification hierarchy.

6.4. Classes in Java


Everything in Java is defined in a class. In its simplest form, a class just defines a collection of data (like a record or a C struct). For example:

class Employee { String name; String ssn; String emailAddress; int yearOfBirth; } The order of data fields

and methods in a class is not significant.

If you recall, each class must be saved in a file that matches its name, for example:
Employee.java

There are a few exceptions to this rule (for non-public classes), but the accepted convention is to have one class defined per source file. Note that in Java, Strings are also classes rather than being implemented as primitive types. Unlike local variables, the state variables (known as fields) of objects do not have to be explicitly initialized. Primitive fields (such as yearOfBirth) are automatically set to primitive defaults (0 in this case), whereas objects (name, ssn, emailAddress) are automatically set to null meaning that they do not point to any object.

6.5. Objects in Java

To create an object (instance) of a particular class, use the new operator, followed by an invocation of a constructor for that class, such as:
new MyClass() o The constructor method initializes the state of the new object. o The new operator returns a reference to the newly created object.

As with primitives, the variable type must be compatible with the value type when using object references, as in:
Employee e = new Employee();

To access member data or methods of an object, use the dot (.) notation: variable.field or variable.method()

Well explore all of these concepts in more depth in this module. Consider this simple example of creating and using instances of the Employee class:
public class EmployeeDemo { public static void main(String[] args) { Employee e1 = new Employee(); e1.name = "John"; e1.ssn = "555-12-345"; e1.emailAddress = "john@company.com"; Employee e2 = new Employee(); e2.name = "Tom"; e2.ssn = "456-78-901"; e2.yearOfBirth = 1974; System.out.println("Name: " + e1.name); System.out.println("SSN: " + e1.ssn); System.out.println("Email Address: " + e1.emailAddress); System.out.println("Year Of Birth: " + e1.yearOfBirth); System.out.println("Name: " + e2.name); System.out.println("SSN: " + e2.ssn); System.out.println("Email Address: " + e2.emailAddress); System.out.println("Year Of Birth: " + e2.yearOfBirth); } }

Running this code produces:


Name: John SSN: 555-12-345 Email Address: john@company.com Year Of Birth: 0 Name: Tom

SSN: 456-78-901 Email Address: null Year Of Birth: 1974

6.6. Java Memory Model

Java variables do not contain the actual objects, they contain references to the objects. o The actual objects are stored in an area of memory known as the heap.
o o

Local variables referencing those objects are stored on the stack. More than one variable can hold a reference to the same object.

Figure 4. Java Memory Model

As previously mentioned, the stack is the area of memory where local variables (including method parameters) are stored. When it comes to object variables, these are merely references (pointers) to the actual objects on the heap. Every time an object is instantiated, a chunk of heap memory is set aside to hold the data (state) of that object. Since objects can contain other objects, some of this data can in fact hold references to those nested objects. In Java:

Object references can either point to an actual object of a compatible type, or be set to null (0 is not the same as null). It is not possible to instantiate objects on the stack. Only local variables (primitives and object references) can live on the stack, and everything else is stored on the heap, including classes and static data.

6.7. Accessing Objects through References


Employee e1 = new Employee(); Employee e2 = new Employee(); // e1 and e2 refer to two independent Employee objects on the heap Employee e3 = e1; // e1 and e3 refer to the *same* Employee object e3 = e2; // Now e2 and e3 refer to the same Employee object e1 = null; // e1 no longer refers to any object. Additionally, there are no references // left to the Employee object previously referred to by e1. That "orphaned" // object is now eligible for garbage collection.

Note The statement Employee e3 = e2; sets e3 to point to the same physical object as e2. It does not duplicate the object. Changes to e3 are reflected in e2 and vice-versa.

6.8. Garbage Collection

Unlike some OO languages, Java does not support an explicit destructor method to delete an object from memory. o Instead, unused objects are deleted by a process known as garbage collection. The JVM automatically runs garbage collection periodically. Garbage collection:
o o o o

Identifies objects no longer in use (no references) Finalizes those objects (deconstructs them) Frees up memory used by destroyed objects Defragments memory

Garbage collection introduces overhead, and can have a major affect on Java application performance.
o o o

The goal is to avoid how often and how long GC runs. Programmatically, try to avoid unnecessary object creation and deletion. Most JVMs have tuning parameters that affect GC performance.

Benefits of garbage collection:

Frees up programmers from having to manage memory. Manually identifying unused objects (as in a language such as C++) is not a trivial task, especially when programs get so complex that the responsibility of object destruction and memory deallocation becomes vague. Ensures integrity of programs:
o

Prevents memory leakseach object is tracked down and disposed off as soon as it is no longer used. Prevents deallocation of objects that are still in use or have already been released. In Java it is impossible to explicitly deallocate an object or use one that has already been deallocated. In a language such as C++ dereferencing null pointers or double-freeing objects typically crashes the program.

Through Java command-line switches (java -X), you can:


Set minimum amount of memory (e.g. -Xmn) Set maximum amount of memory (e.g. -Xmx, -Xss) Tune GC and memory integrity (e.g. -XX:+UseParallelGC)

For more information, see: http://java.sun.com/docs/hotspot/VMOptions.html and http://www.petefreitag.com/articles/gctuning/

6.9. Methods in Java

A method is a set of instructions that defines a particular behavior. o A method is also known as a function or a procedure in procedural languages. Java allows procedural programming through its static methods (e.g. the main() method).
o

A static method belongs to a class independent of any of the classs instances.

It would be possible to implement an entire program through static methods, which call each other procedurally, but that is not OOP.

public class EmployeeDemo { public static void main(String[] args) { Employee e1 = new Employee(); e1.name = "John"; e1.ssn = "555-12-345"; e1.emailAddress = "john@company.com"; Employee e2 = new Employee(); e2.name = "Tom"; e2.ssn = "456-78-901"; e2.yearOfBirth = 1974; printEmployee(e1);

printEmployee(e2);

static void printEmployee(Employee e) { System.out.println("Name: " + e.name); System.out.println("SSN: " + e.ssn); System.out.println("Email Address: " + e.emailAddress); System.out.println("Year Of Birth: " + e.yearOfBirth); }

Running this code produces the same output as before:


Name: John SSN: 555-12-345 Email Address: john@company.com Year Of Birth: 0 Name: Tom SSN: 456-78-901 Email Address: null Year Of Birth: 1974

6.10. Methods in Java (cont.)

In true OOP, we combine an objects state and behavior together. o For example, rather than having external code access the individual fields of an Employee object and print the values, an Employee object could know how to print itself:

class Employee { String name; String ssn; String emailAddress; int yearOfBirth; void print() { System.out.println("Name: " + name); System.out.println("SSN: " + ssn); System.out.println("Email Address: " + emailAddress); System.out.println("Year Of Birth: " + yearOfBirth); } } public class EmployeeDemo { public static void main(String[] args) { Employee e1 = new Employee(); e1.name = "John"; e1.ssn = "555-12-345"; e1.emailAddress = "john@company.com"; Employee e2 = new Employee(); e2.name = "Tom"; e2.ssn = "456-78-901"; e2.yearOfBirth = 1974;

e1.print(); e2.print(); } }

Running this code produces the same output as before:


Name: John SSN: 555-12-345 Email Address: john@company.com Year Of Birth: 0 Name: Tom SSN: 456-78-901 Email Address: null Year Of Birth: 1974

6.11. Method Declarations


Each method has a declaration of the following format:
modifiers returnType name(params) throws-clause { body }

modifiers
public, private, protected, static, final, abstract, native, synchronized returnType A primitive type, object type, or void (no return value) name The name of the method params paramType paramName, throws-clause throws ExceptionType, body The methods code, including the declaration of local variables, enclosed in braces

Note that abstract methods do not have a body (more on this later). Here are some examples of method declarations:
public static void print(Employee e) { ... } public void print() { ... } public double sqrt(double n) { ... } public int max(int x, int y) { ... } public synchronized add(Employee e) throws DuplicateEntryException { ... } public int read() throws IOException { ... } public void println(Object o) { ... } protected void finalize() throws Throwable { ... } public native void write(byte[] buffer, int offset, int length) throws IOException { ... } public boolean equals(Object o) { ... } private void process(MyObject o) { ... } void run() { ... }

6.12. Method Signatures

The signature of a method consists of: o The method name


o

The parameter list (that is, the parameter types and their order)

The signature does not include:


o o

The parameter names The return type

Each method defined in a class must have a unique signature. Methods with the same name but different signatures are said to be overloaded.

Well discuss examples of overloading constructors and other methods later in this module.

6.13. Invoking Methods


Use the dot (.) notation to invoke a method on an object: objectRef.method(params) Parameters passed into methods are always copied (pass-by-value).
o o

Changes made to parameter variables within the methods do no affect the caller. Object references are also copied, but they still point to the same object.

For example, we add the following method to our Employee class:


void setYearOfBirth(int year) { yearOfBirth = year; year = -1; // modify local variable copy }

We invoke it from EmployeeDemo.main() as:


int y = 1974; e2.setYearOfBirth(y); System.out.println(e2.yearOfBirth); // prints 1974 System.out.println(y); // prints 1974

On the other hand, we add this method to our EmployeeDemo class:


static void printYearOfBirth(Employee e) { System.out.println(e.yearOfBirth); e.yearOfBirth = -1; // modify object's copy }

We invoke it from EmployeeDemo.main() as:

printYearOfBirth(e2); // prints 1974 System.out.println(e2.yearOfBirth); // prints

Java Programming Tutorial


Object-oriented Programming (OOP) Basics
1. Why OOP?
Suppose that you want to assemble your own PC, you go to a hardware store and pick up a motherboard, a processor, some RAMs, a hard disk, a casing, a power supply, and put them together. You turn on the power, and the PC runs. You need not worry whether the motherboard is a 4-layer or 6-layer board, whether the hard disk has 4 or 6 plates; 3 inches or 5 inches in diameter, whether the RAM is made in Japan or Korea, and so on. You simply put the hardware components together and expect the machine to run. Of course, you have to make sure that you have the correct interfaces, i.e., you pick an IDE hard disk rather than a SCSI hard disk, if your motherboard supports only IDE; you have to select RAMs with the correct speed rating, and so on. Nevertheless, it is not difficult to set up a machine from hardware components. Similarly, a car is assembled from parts and components, such as chassis, doors, engine, wheels, break, transmission, etc. The components are reusable, e.g., a wheel can be used in many cars (of the same specifications). Hardware, such as computers and cars, are assembled from parts, which are reusable components. How about software? Can you "assemble" a software application by picking a routine here, a routine there, and expect the program to run? The answer is obviously no! Unlike hardware, it is very difficult to "assemble" an application from software components. Since the advent of computer 60 years ago, we have written tons and tons of programs. However, for each new application, we have to re-invent the wheels and write the program from scratch. Why re-invent the wheels?
Traditional Procedural-Oriented languages

Can we do this in traditional procedural-oriented programming language such as C, Fortran, Cobol, or Pascal? Traditional procedural-oriented languages (such as C and Pascal) suffer some notable drawbacks in creating reusable software components: 1. The programs are made up of functions. Functions are often not reusable. It is very difficult to copy a function from one program and reuse in another program because the the function is likely to reference the headers, global variables and other functions. In other words, functions are not well-encapsulated as a self-contained reusable unit. 2. The procedural languages are not suitable of high-level abstraction for solving real life problems. For example, C programs uses constructs such as if-else, for-loop, array, method, pointer, which are low-level and hard to abstract real problems such as a Customer Relationship Management (CRM) system or a computer soccer game. (Imagine using assembly codes, which is a very low level code, to write a computer soccer game. C is better but no much better.) In brief, the traditional procedural-languages separate the data structures and algorithms of the software entities. In the early 1970s, the US Department of Defense (DoD) commissioned a task force to investigate why its IT budget always went out of control; but without much to show for. The findings are: 1. 80% of the budget went to the software (while the remaining 20% to the hardware). 2. More than 80% of the software budget went to maintenance (only the remaining 20% for new software development).

3. Hardware components could be applied to various products, and their integrity normally did not affect other products. (Hardware can share and reuse! Hardware faults are isolated!) 4. Software procedures were often non-sharable and not reusable. Software faults could affect other programs running in computers. The task force proposed to make software behave like hardware OBJECT. Subsequently, DoD replaces over 450 computer languages, which were then used to build DoD systems, with an object-oriented language called Ada.
Object-Oriented Programming Languages

Object-oriented programming (OOP) languages are designed to overcome these problems. 1. The basic unit of OOP is a class, which encapsulates both the static attributes and dynamic behaviors within a "box", and specifies the public interface for using these boxes. Since the class is well-encapsulated (compared with the function), it is easier to reuse these classes. In other words, OOP combines the data structures and algorithms of a software entity inside the same box. 2. OOP languages permit higher level of abstraction for solving real-life problems. The traditional procedural language (such as C and Pascal) forces you to think in terms of the structure of the computer (e.g. memory bits and bytes, array, decision, loop) rather than thinking in terms of the problem you are trying to solve. The OOP languages (such as Java, C++, C#) let you think in the problem space, and use software objects to represent and abstract entities of the problem space to solve the problem.

As an example, suppose you wish to write a computer soccer games (which I consider as a complex application). It is quite difficult to model the game in procedural-oriented languages. But using OOP languages, you can easily model the program accordingly to the "real things" appear in the soccer games.

Player: attributes include name, number, location in the field, and etc; operations include run, jump, kick-the-ball, and etc. Ball: Reference: Field: Audience: Weather:

Most importantly, some of these classes (such as Ball and Audience) can be reused in another application, e.g., computer basketball game, with little or no modification.
Benefits of OOP

The procedural-oriented languages focus on procedures, with function as the basic unit. You need to first figure out all the functions and then think about how to represent data. The object-oriented languages focus on components that the user perceives, with objects as the basic unit. You figure out all the objects by putting all the data and operations that describe the user's interaction with the data. Object-Oriented technology has many benefits:

Ease in software design as you could think in the problem space rather than the machine's bits and bytes. You are dealing with high-level concepts and abstractions. Ease in design leads to more productive software development. Ease in software maintenance: object-oriented software are easier to understand, therefore easier to test, debug, and maintain. Reusable software: you don't need to keep re-inventing the wheels and re-write the same functions for different situations. The fastest and safest way of developing a new application is to reuse existing codes - fully tested and proven codes.

2. OOP in Java
2.1 Class & Instances In Java, a class is a definition of objects of the same kind. In other words, a class is a blueprint, template, or prototype that defines and describes the static attributes and dynamic behaviors common to all objects of the same kind. An instance is a realization of a particular item of a class. In other words, an instance is an instantiation of a class. All the instances of a class have similar properties, as described in the class definition. For example, you can define a class called "Student" and create three instances of the class "Student" for "Peter", "Paul" and "Pauline". The term "object" usually refers to instance. But it is often used loosely, which may refer to a class or an instance. 2.2 A Class is a 3-Compartment Box encapsulating Data and Operations

A class can be visualized as a three-compartment box, as illustrated: 1. Name (or identity): identifies the class. 2. Variables (or attribute, state, field): contains the static attributes of the class. 3. Methods (or behaviors, function, operation): contains the dynamic behaviors of the class. In other words, a class encapsulates the static attributes (data) and dynamic behaviors (operations that operate on the data) in a box.

The followings figure shows a few examples of classes:

The following figure shows two instances of the class Student, identified as "paul" and "perter".

Unified Modeling Language (UML) Class and Instance Diagrams: The above class diagrams are drawn according to the UML notations. A class is represented as a 3-compartment box, containing name, variables, and methods, respectively. Class name is shown in bold and centralized. An instance (object) is also represented as a 3-compartment box, with instance name shown as instanceName:Classname and underlined.
Brief Summary

1. A class is a programmer-defined, abstract, self-contained, reusable software entity that mimics a real-world thing. 2. A class is a 3-compartment box containing the name, variables and the methods. 3. A class encapsulates the data structures (in variables) and algorithms (methods). The values of the variables constitute its state. The methods constitute its behaviors. 4. An instance is an instantiation (or realization) of a particular item of a class. 2.3 Class Definition In Java, we use the keyword class to define a class. For examples:
public class Circle { double radius; String color; double getRadius() {...} double getArea() {...} } public class SoccerPlayer { int number; String name; int x, y; void run() {...} void kickBall() {...} } // class name // variables // methods // class name // variables

// methods

The syntax for class definition in Java is:


[AccessControlModifier] class ClassName { // class body contains definition of variables and methods ... }

We shall explain the access control modifier, such as public and private, later. Class Naming Convention: A class name shall be a noun or a noun phrase made up of several words. All the words shall be initial-capitalized (camel-case). Use a singular noun for class name. Choose a meaningful and self-descriptive classname. For examples, SoccerPlayer, HttpProxyServer, FileInputStream, PrintStream and SocketFactory. 2.4 Creating Instances of a Class To create an instance of a class, you have to: 1. Declare an instance identifier (instance name) of a particular class. 2. Construct the instance (i.e., allocate storage for the instance and initialize the instance) using the "new" operator.

For examples, suppose that we have a class called Circle, we can create instances of Circle as follows:
// Declare 3 instances of the class Circle, c1, c2, and c3 Circle c1, c2, c3; // Allocate and construct the instances via new operator c1 = new Circle(); c2 = new Circle(2.0); c3 = new Circle(3.0, "red"); // You can declare and construct in the same statement Circle c4 = new Circle();

2.5 Dot Operator The variables and methods belonging to a class are formally called member variables and member methods. To reference a member variable or method, you must: 1. first identify the instance you are interested in, and then 2. Use the dot operator (.) to reference the member (variable or method). For example, suppose that we have a class called Circle, with two variables (radius and color) and two methods (getRadius() and getArea()). We have created three instances of the class Circle, namely, c1, c2 and c3. To invoke the method getArea(), you must first identity the instance of interest, says c2, then use the dot operator, in the form of c2.getArea(), to invoke the getArea() method of instance c2. For example,
// Declare and construct instances c1 and c2 of the class Circle Circle c1 = new Circle (); Circle c2 = new Circle (); // Invoke member methods for the instance c1 via dot operator System.out.println(c1.getArea()); System.out.println(c1.getRadius()); // Reference member variables for instance c2 via dot operator c2.radius = 5.0; c2.color = "blue";

Calling getArea() without identifying the instance is meaningless, as the radius is unknown (there could be many instances of Circle - each maintaining its own radius). In general, suppose there is a class called AClass with a member variable called aVariable and a member method called aMethod(). An instance called anInstance is constructed for AClass. You use anInstance.aVariable and anInstance.aMethod(). 2.6 Member Variables

A member variable has a name (or identifier) and a type; and holds a value of that particular type (as descried in the earlier chapter). A member variable can also be an instance of a certain class (to be discussed later). Variable Naming Convention: A variable name shall be a noun or a noun phrase made up of several words. The first word is in lowercase and the rest of the words are initial-capitalized (camel-case), e.g., fontSize, roomNumber, xMax, yMin and xTopLeft. Take note that variable name begins with an lowercase, while class name begins with an uppercase. The formal syntax for variable definition in Java is:
[AccessControlModifier] type variableName [= initialValue]; [AccessControlModifier] type variableName-1 [= initialValue-1] [, type variableName-2 [= initialValue-2]] ... ;

For example,
private double radius; public int length = 1, width = 1;

2.7 Member Methods A method (as described in the earlier chapter): 1. receives parameters from the caller, 2. performs the operations defined in the method body, and 3. returns a piece of result (or void) to the caller. The syntax for method declaration in Java is as follows:
[AccessControlModifier] returnType methodName ([argumentList]) { // method body or implementation ...... }

For examples:
public double getArea() { return radius*radius*Math.PI; }

Method Naming Convention: A method name shall be a verb, or a verb phrase made up of several words. The first word is in lowercase and the rest of the words are initial-capitalized (camel-case). For example, getRadius(), getParameterValues(). Take note that variable name is a noun (denoting a static attribute), while method name is a verb (denoting an action). They have the same naming convention. Nevertheless, you can easily distinguish them from the context. Methods take arguments in parentheses (possibly zero

argument with empty parentheses), but variables do not. In this writing, methods are denoted with a pair of parentheses, e.g., println(), getArea() for clarity. 2.8 Putting them together: An OOP Example

A class called Circle is to be defined as illustrated in the class diagram. It contains two variables: radius (of type double) and color (of type String); and three methods: getRadius(), getColor(), and getArea(). Three instances of Circles called c1, c2, and c3 shall then be constructed with their respective data members, as shown in the instance diagrams. The source codes for Circle.java is as follows:
Circle.java 1 // Define the Circle class 2 public class Circle { // Save as "Circle.java" 3 // Private variables 4 private double radius; 5 private String color; 6 7 // Constructors (overloaded) 8 public Circle() { // 1st Constructor 9 radius = 1.0; 10 color = "red"; 11 }

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 }

public Circle(double r) { // 2nd Constructor radius = r; color = "red"; } public Circle(double r, String c) { // 3rd Constructor radius = r; color = c; } // Public methods public double getRadius() { return radius; } public String getColor() { return color; } public double getArea() { return radius*radius*Math.PI; }

Compile "Circle.java" into "Circle.class". Notice that the Circle class does not have a main() method. Hence, it is NOT a standalone program and you cannot run the Circle class by itself. The Circle class is meant to be a building block and used in other programs.
TestCircle.java

We shall now write another class called TestCircle, which uses the Circle class. The TestCircle class has a main() method and can be executed.
1 // Test driver program for the Circle class 2 public class TestCircle { // Save as "TestCircle.java" 3 public static void main(String[] args) { // Execution entry point 4 // Construct an instance of the Circle class called c1 5 Circle c1 = new Circle(2.0, "blue"); // Use 3rd constructor 6 System.out.println("Radius is " + c1.getRadius() // use dot operator 7 to invoke member methods 8 + " Color is " + c1.getColor() 9 + " Area is " + c1.getArea()); 10 11 // Construct another instance of the Circle class called c2 12 Circle c2 = new Circle(2.0); // Use 2nd constructor 13 System.out.println("Radius is " + c2.getRadius() 14 + " Color is " + c2.getColor() 15 + " Area is " + c2.getArea()); 16 17 // Construct yet another instance of the Circle class called c3 18 Circle c3 = new Circle(); // Use 1st constructor 19 System.out.println("Radius is " + c3.getRadius() 20 + " Color is " + c3.getColor() 21 + " Area is " + c3.getArea()); 22 }

Compile TestCircle.java into TestCircle.class. Run the TestCircle and study the output:
Radius is 2.0 Color is blue Area is 12.566370614359172 Radius is 2.0 Color is red Area is 12.566370614359172 Radius is 1.0 Color is red Area is 3.141592653589793

2.9 Constructors A constructor is a special method that has the same method name as the class name. In the above Circle class, we define three overloaded versions of constructor "Circle(....)". A constructor is used to construct and initialize all the member variables. To create a new instance of a class, you need to use a special "new" operator followed by a call to one of the constructors. For example,
Circle c1 = new Circle(); Circle c2 = new Circle(2.0); Circle c3 = new Circle(3.0, "red");

A constructor is different from an ordinary method in the following aspects:


The name of the constructor method is the same as the class name, and by classname convention, begins with an uppercase. Constructor has no return type (or implicitly returns void). Hence, no return statement is allowed inside the constructor's body. Constructor can only be invoked via the "new" operator. It can only be used once to initialize the instance constructed. You cannot call the constructor afterwards. Constructors are not inherited (to be explained later).

A constructor with no parameter is called the default constructor, which initializes the member variables to their default value. For example, the Circle() in the above example. 2.10 Method Overloading Method overloading means that the same method name can have different implementations (versions). However, the different implementations must be distinguishable by their argument list (either the number of arguments, or the type of arguments, or their order). Example: The method average() has 3 versions, with different argument lists. The caller can invoke the chosen version by supplying the matching arguments.
1 // Example to illustrate Method Overloading 2 public class TestMethodOverloading {

public static int average(int n1, int n2) { 3 return (n1+n2)/2; 4 } 5 6 public static double average(double n1, double n2) { 7 return (n1+n2)/2; 8 } 9 10 public static int average(int n1, int n2, int n3) { 11 return (n1+n2+n3)/3; 12 } 13 14 public static void main(String[] args) { 15 System.out.println(average(1, 2)); // Use A 16 System.out.println(average(1.0, 2.0)); // Use B 17 System.out.println(average(1, 2, 3)); // Use C 18 System.out.println(average(1.0, 2)); // Use B 19 casted to double 2.0 20 // average(1, 2, 3, 4); // Compilation Error - No 21 } 22 } Overloading Circle Class' Constructor

// A

// B

// C

int 2 implicitly matching method

The above Circle class has three versions of constructors differentiated by their parameter list, as followed:
Circle() Circle(double r) Circle(double r, String c)

Depending on the actual argument list used when invoking the method, the matching constructor will be invoked. If your argument list does not match any one of the methods, you will get a compilation error. 2.11 public vs. private - Access Control Modifiers An access control modifier can be used to control the visibility of a class, or a member variable or a member method within a class. We begin with the following two access control modifiers:
1. public: The class/variable/method is accessible and available to all the other objects in

the system. 2. private: The class/variable/method is accessible and available within this class only. For example, in the above Circle definition, the member variable radius is declared private. As the result, radius is accessible inside the Circle class, but NOT inside the TestCircle class. In other words, you cannot use "c1.radius" to refer to c1's radius in TestCircle. Try inserting the statement "System.out.println(c1.radius);" in TestCircle and observe the error message. Try changing radius to public, and re-run the statement.

On the other hand, the method getRadius() is declared public in the Circle class. Hence, it can be invoked in the TestCircle class. UML Notation: In UML notation, public members are denoted with a "+", while private members with a "-" in the class diagram. More access control modifiers will be discussed later. 2.12 Information Hiding and Encapsulation A class encapsulates the name, static attributes and dynamic behaviors into a "3-compartment box". Once a class is defined, you can seal up the "box" and put the "box" on the shelve for others to use and reuse. Anyone can pick up the "box" and use it in their application. This cannot be done in the traditional procedural-oriented language like C, as the static attributes (or variables) are scattered over the entire program and header files. You cannot "cut" out a portion of C program, plug into another program and expect the program to run without extensive changes. Member variables of a class are typically hidden from the outside word (i.e., the other classes), with private access control modifier. Access to the member variables are provided via public assessor methods, e.g., getRadius() and getColor(). This follows the principle of information hiding. That is, objects communicate with each others using well-defined interfaces (public methods). Objects are not allowed to know the implementation details of others. The implementation details are hidden or encapsulated within the class. Information hiding facilitates reuse of the class. Rule of Thumb: Do not make any variable public, unless you have a good reason. 2.13 Getters and Setters To allow other classes to read the value of a private variable says xxx, you shall provide a get method (or getter or accessor method) called getXxx(). A get method need not expose the data in raw format. It can process the data and limit the view of the data others will see. get methods cannot modify the variable. To allow other classes to modify the value of a private variable says xxx, you shall provide a set method (or setter or mutator method) called setXxx(). A set method could provide data validation (such as range checking), and transform the raw data into the internal representation. For example, in our Circle class, the variables radius and color are declared private. That is to say, they are only available within the Circle class and not visible in any other classes including TestCircle class. You cannot access the private variables radius and color from the TestCircle class directly - via says c1.radius or c1.color. The Circle class provides two public accessor methods, namely, getRadius() and getColor(). These methods are

declared public. The class TestCircle can invoke these public accessor methods to retrieve the radius and color of a Circle object, via says c1.getRadius() and c1.getColor(). There is no way you can change the radius or color of a Circle object, after it is constructed in the TestCircle class. You cannot issue statements such as c1.radius = 5.0 to change the radius of instance c1, as radius is declared as private in the Circle class and is not visible to other classes including TestCircle. If the designer of the Circle class permits the change the radius and color after a Circle object is constructed, he has to provide the appropriate set methods (or setters or mutator methods), e.g.,
// Setter for color public void setColor(String c) { color = c; } // Setter for radius public void setRadius(double r) { radius = r; }

With proper implementation of information hiding, the designer of a class has full control of what the user of the class can and cannot do. 2.14 Keyword "this" You can use keyword "this" to refer to this instance inside a class definition. One of the main usage of keyword this is to resolve ambiguity.
public class Circle { double radius; // Member variable called "radius" public Circle(double radius) { // Method's argument also called "radius" this.radius = radius; // "this.radius" refers to this instance's member variable // "radius" resolved to the method's argument. } ... }

In the above codes, there are two identifiers called radius - a member variable of the class and the method's argument. This causes naming conflict. To avoid the naming conflict, you could name the method's argument r instead of radius. However, radius is more approximate and meaningful in this context. Java provides a keyword called this to resolve this naming conflict. "this.radius" refers to the member variable; while "radius" resolves to the method's argument.

Using the keyword "this", the constructor, getter and setter methods for a private variable called xxx of type T are as follows:
public class Aaa { // A private variable named xxx of type T private T xxx; // Constructor public Aaa(T xxx) { this.xxx = xxx; } // A getter for variable xxx of type T receives no argument and return a value of type T public T getXxx() { return xxx; } // A setter for variable xxx of type T receives a parameter of type T and return void public void setXxx(T xxx) { this.xxx = xxx; } }

For a boolean variable xxx, the getter shall be named isXxx(), instead of getXxx(), as follows:
// Private boolean variable private boolean xxx; // Getter public boolean isXxx() { return xxx; } // Setter public void setXxx(boolean xxx) { this.xxx = xxx; }

Notes:
this.varName refers to varName of this instance; this.methodName(...) invokes methodName(...) of this instance. In a constructor, we can use this(...) to call another constructor of this class.

Inside a method, we can use the statement "return this" to return this instance to the caller.

2.15 Method toString()

Every well-designed Java class should have a public method called toString() that returns a string description of the object. You can invoke the toString() method explicitly by calling anInstanceName.toString() or implicitly via println() or String concatenation operator '+'. Running println(anInstance) with an object argument invokes the toString() method of that instance implicitly. For example, if the following toString() method is included in our Circle class:
// Return a short String description of this instance public String toString() { return "Circle with radius = " + radius + " and color of " + color; }

In your TestCircle class, you can get a short text descriptor of a Circle object by:
Circle c1 = new Circle(); System.out.println(c1.toString()); System.out.println(c1); System.out.println("c1 is: " + c1); String before concatenation // explicitly calling toString() // implicit call to c1.toString() // '+' invokes c1.toString() to get a

The signature of toString() is:


public String toString() { ...... }

2.16 Constants (final) Constants are variables defined with the modifier final. A final variable can only be assigned once and its value cannot be modified once assigned. For example,
public final double X_REFERENCE = 1.234; private final int MAX_ID = 9999; MAX_ID = 10000; // error: cannot assign a value to final variable MAX_ID // You need to initialize a final member variable during declaration private final int SIZE; // error: variable SIZE might not have been initialized

Constant Naming Convention: A constant name is a noun, or a noun phrase made up of several words. All words are in uppercase separated by underscores '_', for examples, X_REFERENCE, MAX_INTEGER and MIN_VALUE. Advanced Notes:
1. A final primitive variable cannot be re-assigned a new value. 2. A final instance cannot be re-assigned a new address. 3. A final class cannot be sub-classed (or extended).

4. A final method cannot be overridden.

2.17 Putting Them Together in the Revised Circle Class

Circle.java 1 // The Circle class definition 2 public class Circle { // Save as "Circle.java" 3 // Public constants 4 public static final double DEFAULT_RADIUS = 8.8; 5 public static final String DEFAULT_COLOR = "red"; 6 7 // Private variables 8 private double radius; 9 private String color; 10 11 // Constructors (overloaded) 12 public Circle() { // 1st Constructor 13 radius = DEFAULT_RADIUS; 14 color = DEFAULT_COLOR; 15 } 16 public Circle(double radius) { // 2nd Constructor 17 this.radius = radius; 18 color = DEFAULT_COLOR; 19 } 20 public Circle(double radius, String color) { // 3rd Constructor 21 this.radius = radius; 22 this.color = color; 23 } 24 25 // Public getter and setter for private variables 26 public double getRadius() { 27 return radius; 28 } 29 public void setRadius(double radius) {

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 } TRY

this.radius = radius; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } // toString() to provide a short description of this instance public String toString() { return "Circle with radius = " + radius + " and color of " + color; } // Public methods public double getArea() { return radius*radius*Math.PI; }

Write a test driver to test ALL the public methods in the Circle class.

3. More Examples on Classes


3.1 Example: The Account Class

A class called Account, which models a bank account, is designed as shown in the class diagram. It contains:

Two private variables: accountNumber (int) and balance (double), which maintains the current account balance. Public methods credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() method shall print "amount withdrawn exceeds the current balance!" if amount is more than balance. A toString(), which returns "A/C no: xxx Balance=xxx" (e.g., A/C no: 991234 Balance=$88.88), with balance rounded to two decimal places.

Account.java 1 /* The Account class Definition */ 2 public class Account { 3 // Private variables 4 private int accountNumber; 5 private double balance; 6 7 // Constructors 8 public Account(int accountNumber, double balance) { 9 this.accountNumber = accountNumber; 10 this.balance = balance; 11 } 12 13 public Account(int accountNumber) { 14 this.accountNumber = accountNumber; 15 balance = 0; 16 } 17 18 // Public getters and setters for private variables 19 public int getAccountNumber() { 20 return accountNumber; 21 } 22 23 public double getBalance() { 24 return balance; 25 } 26 27 public void setBalance(double balance) { 28 this.balance = balance; 29 } 30 31 public void credit(double amount) { 32 balance += amount; 33 } 34 35 public void debit(double amount) { 36 if (balance < amount) { 37 System.out.println("amount withdrawn exceeds the current 38 balance!"); 39 } else { 40 balance -= amount;

} 41 } 42 43 public String toString() { 44 return String.format("A/C no: %d Balance=%.2f", accountNumber, 45 balance); 46 } } TestAccount.java 1 public class TestAccount { 2 public static void main(String[] args) { 3 Account a1 = new Account(1234, 99.99); 4 System.out.println(a1); 5 a1.credit(10.001); 6 a1.debit(5); 7 System.out.println(a1); 8 System.out.println(a1.getBalance()); 9 a1.setBalance(0); 10 System.out.println(a1); 11 12 Account a2 = new Account(8888); 13 System.out.println(a2); 14 } 15 }

3.2 Example: The Ball class

A Ball class models a moving ball, designed as shown in the class diagram, contains the following members:

Two private variables x, y, which maintain the position of the ball. Constructors, public getters and setters for the private variables. A method setXY(), which sets the position of the ball and setXYSpeed() to set the speed of the ball. A method move(), which increases x and y by the given xDisp and yDisp, respectively. A toString(), which returns "Ball @ (x,y)".

Ball.java 1 // Define the class Ball 2 public class Ball { 3 // Private variables 4 private double x, y; // x and y location 5 6 // Constructors 7 public Ball(double x, double y) { 8 this.x = x; 9 this.y = y; 10 } 11 public Ball() { 12 x = 0.0; 13 y = 0.0; 14 } 15 16 // Public getters and setters for private variables x and y 17 public double getX() { 18 return x; 19 } 20 public void setX(double x) { 21 this.x = x; 22 } 23 public double getY() { 24 return y; 25 } 26 public void setY(double y) { 27 this.y = y; 28 } 29 30 public void setXY(double x, double y) { 31 this.x = x; 32 this.y = y; 33 } 34 35 public void move(double xDisp, double yDisp) { 36 x += xDisp; 37 y += yDisp; 38 } 39 40 public String toString() { 41 return "Ball @ (" + x + "," + y + ")"; 42 } 43 }

3.3 The Author and Book Classes


Let's start with the Author class

A class called Author is defined as shown in the class diagram. It contains:

Three private member variables: name (String), email (String), and gender (char of either 'm', 'f', or 'u' for unknown - you might also use a boolean variable called male having value of true or false). One constructor to initialize the name, email and gender with the given values. (There is no default constructor for Author, as there are no defaults for name, email and gender.) Public getters/setters: getName(), getEmail(), setEmail(), and getGender(). (There are no setters for name and gender, as these attributes cannot be changed.) A toString() method that returns "author-name (gender) at email", e.g., "Tan Ah Teck (m) at ahTeck@somewhere.com".

Author.java 1 // The Author class definition 2 public class Author { 3 // Private variables 4 private String name; 5 private String email; 6 private char gender; 7 8 // Constructor 9 public Author(String name, String email, char gender) { 10 this.name = name; 11 this.email = email; 12 this.gender = gender; 13 }

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 }

// Public getters and setters for private variables public String getName() { return name; } public String getEmail() { return email; } public char getGender() { return gender; } public void setEmail(String email) { this.email = email; } // toString() to describe itself public String toString() { return name + " (" + gender + ") at " + email; }

A Test Driver Program: TestAuthor.java 1 // A test driver for the Author class 2 public class TestAuthor { 3 public static void main(String[] args) { 4 Author teck = new Author("Tan Ah Teck", "teck@somewhere.com", 'm'); 5 System.out.println(teck); // toString() 6 teck.setEmail("teck@nowhere.com"); 7 System.out.println(teck); // toString() 8 } 9} A Book is written by an Author - Using an "Object" Member Variable

Let's design a Book class. Assume that a book is written by one and only one author. The Book class (as shown in the class diagram) contains the following members:

Four private member variables: name (String), author (an instance of the class Author you have just created, assume that each book has one and only one author), price (double), and qtyInStock (int). Two overloaded constructors. Getters/Setters: getName(), getAuthor(), getPrice(), setPrice(), getQtyInStock(), setQtyInStock(). A toString() that returns "'book-name' by author-name (gender) at email". You should reuse the Author's toString() method, which returns "author-name (gender) at email".

Book.java 1 // The Book class definition 2 public class Book { 3 // Private variables 4 private String name; 5 private Author author; 6 private double price; 7 private int qtyInStock; 8 9 // Constructors 10 public Book(String name, Author author, double price) { 11 this.name = name;

this.author = author; 12 this.price = price; 13 this.qtyInStock = 0; // Not given, set to the default value 14 } 15 16 public Book(String name, Author author, double price, int qtyInStock) { 17 this.name = name; 18 this.author = author; 19 this.price = price; 20 this.qtyInStock = qtyInStock; 21 } 22 23 // Getters and Setters 24 public String getName() { 25 return name; 26 } 27 28 public Author getAuthor() { 29 return author; // return member author, which is an instance of 30 class Author 31 } 32 33 public double getPrice() { 34 return price; 35 } 36 37 public void setPrice(double price) { 38 this.price = price; 39 } 40 41 public int getQtyInStock() { 42 return qtyInStock; 43 } 44 45 public void setQtyInStock(int qtyInStock) { 46 this.qtyInStock = qtyInStock; 47 } 48 49 // toString() t describe itself 50 public String toString() { 51 return "'" + name + "' by " + author; // author.toString() 52 } 53 } A Test Driver Program - TestBook.java 1 // A test driver program for the Book class 2 public class TestBook { 3 public static void main(String[] args) { 4 Author teck = new Author("Tan Ah Teck", "teck@somewhere.com", 'm'); 5 System.out.println(teck); // toString() 6 7 Book dummyBook = new Book("Java for dummies", teck, 9.99, 88); 8 System.out.println(dummyBook); // toString() 9 10 Book moreDummyBook = new Book("Java for more dummies",

new Author("Peter Lee", "peter@nowhere.com", 'm'), 11 anonymous instance of Author 12 19.99, 8); 13 System.out.println(moreDummyBook); // toString() 14 } 15 }

//

3.4 The Student Class

Suppose that our application requires us to model a group of students. A student has a name and address. We are required to keep track of the courses taken by each student, together with the grades (between 0 and 100) for each of the courses. A student shall not take more than 30 courses for the entire program. We are required to print all course grades, and also the overall average grade. We can design the Student class as shown in the class diagram. The class contains:

Private member variables name (String), address (String), numCourses, course and grades. The numCourses maintains the number of courses taken by the student so far. The courses and grades are two parallel arrays, storing the courses taken (e.g., {"IM101", "IM102", "IM103"}) and their respective grades (e.g. {89, 56, 98}). A constructor that constructs an instance with the given name and Address. It also constructs the courses and grades arrays and set the numCourses to 0. Getters for name and address; setter for address. No setter is defined for name as it shall not be changed.

A toString(), which prints "name(address)". A method addCourseGrade(course, grade), which appends the given course and grade into the courses and grades arrays, respectively, and increments numCourses. A method printGrades(), which prints "name(address) course1:grade1, course2:grade2,...". You should reuse the toString() here. A method getAverageGrade(), which returns the average grade of all the courses taken.

UML Notations: In UML notations, a variable is written as [+|-]varName:type; a method is written as [+|-]methodName(arg1:type1, arg2:type2,...):returnType, where '+' denotes public and '-' denotes private.
Student.java

The source code for Student.java is as follows:


1 // The student class definition 2 public class Student { 3 // Private member variables 4 private String name; 5 private String address; 6 // Courses taken and grades for the courses are kept in 2 arrays of the 7 same length 8 private String[] courses; 9 private int[] grades; // A grade is between 0 to 100 10 private int numCourses; // Number of courses taken so far 11 private static final int MAX_COURSES = 30; // maximum courses 12 13 // Constructor 14 public Student(String name, String address) { 15 this.name = name; 16 this.address = address; 17 courses = new String[MAX_COURSES]; // allocate arrays 18 grades = new int[MAX_COURSES]; 19 numCourses = 0; // no courses so far 20 } 21 22 // Public getter for private variable name 23 public String getName() { 24 return name; 25 } 26 27 // Public getter for private variable address 28 public String getAddress() { 29 return address; 30 } 31 32 // Public setter for private variable address 33 public void setAddress(String address) { 34 this.address = address; 35 }

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

// Describe itself public String toString() { return name + "(" + address + ")"; } // Add a course and grade public void addCourseGrade(String course, int grade) { courses[numCourses] = course; grades[numCourses] = grade; ++numCourses; } // Print all courses taken and their grades public void printGrades() { System.out.print(this); // toString() for (int i = 0; i < numCourses; ++i) { System.out.print(" " + courses[i] + ":" + grades[i]); } System.out.println(); } // Compute the average grade public double getAverageGrade() { int sum = 0; for (int i = 0; i < numCourses; ++i) { sum += grades[i]; } return (double)sum/numCourses; }

TestStudent.java

Let us write a test program to create a student named "Tan Ah Teck", who has taken 3 courses, "IM101", "IM102" and "IM103" with grades of 89, 57, and 96 respectively. We shall print all the course grades, and the average grade.
// A test driver program for the Student class 1 public class TestStudent { 2 public static void main(String[] args) { 3 Student ahTeck = new Student("Tan Ah Teck", "1 Happy Ave"); 4 ahTeck.addCourseGrade("IM101", 89); 5 ahTeck.addCourseGrade("IM102", 57); 6 ahTeck.addCourseGrade("IM103", 96); 7 ahTeck.printGrades(); 8 System.out.printf("The average grade is %.2f", 9 ahTeck.getAverageGrade()); 10 } 11 } Tan Ah Teck(1 Happy Ave) IM101:89 IM102:57 IM103:96 The average grade is 80.67

3.5 The MyPoint and MyCircle class

A class called MyPoint, which models a 2D point with x and y coordinates, is designed as shown in the class diagram. It contains:

Two private member variables x (int) and y (int). A default (no-arg) constructor that construct a point at (0,0). A constructor that constructs a point with the given x and y coordinates. Getter and setter for the private variables x and y. A method setXY() to set both x and y. A toString() method that returns a string description of the instance in the format "(x,y)". A method distance(int x, int y), which returns the distance from this point to another point at the given (x,y) coordinates in double. An overloaded distance(MyPoint another) method, which returns the distance from this point to the given MyPoint instance called another.

MyPoint.java 1 // The MyPoint class definition 2 public class MyPoint { 3 // Private member variables 4 private int x; 5 private int y; 6 7 // Constructors 8 public MyPoint() {

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 }

x = 0; y = 0;

public MyPoint(int x, int y) { this.x = x; this.y = y; } // Getters and Setters public int getX() { return x; } public int getY() { return y; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public void setXY(int x, int y) { this.x = x; this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } public double distance(int x, int y) { int xDiff = this.x - x; int yDiff = this.y - y; return Math.sqrt(xDiff*xDiff + yDiff*yDiff); } public double distance(MyPoint another) { int xDiff = this.x - another.x; int yDiff = this.y - another.y; return Math.sqrt(xDiff*xDiff + yDiff*yDiff); }

Test Driver Program: TestMyPoint.java 1 // A test driver for MyPoint class 2 public class TestMyPoint { 3 public static void main(String[] args) { 4 MyPoint p1 = new MyPoint(); 5 System.out.println(p1); 6 p1.setXY(0, 3);

7 8 9 10 11 12 13 14 15 16 }

System.out.println(p1); MyPoint p2 = new MyPoint(4, 0); System.out.println(p2); // Test distance() methods System.out.println(p1.distance(4, 0)); System.out.println(p1.distance(p2));

A class called MyCircle is designed as shown in the class diagram. It contains:


Two private member variables: a radius (double) and a center (an instance of MyPoint, which we created earlier). A default (no-arg) constructor that construct a Circle at (0,0) with radius of 1.0. A constructor that constructs a Circle with the given xCenter, yCenter and radius. A constructor that constructs a Circle with the given instance of MyPoint as center; and radius. Getter and setter for the private variables center and radius.

Methods getCenterX(), setCenterX(), setCenterXY(), etc. A toString() method that returns a string description of the instance in the format "center=(x,y) radius=r". A distance(MyCircle another) method that returns the distance from this Circle to the given MyCircle instance called another.

MyCircle.java 1 // The MyCircle class definition 2 public class MyCircle { 3 // Private member variables 4 private MyPoint center; // Declare instance center 5 private double radius; 6 7 // Constructors 8 public MyCircle() { 9 center = new MyPoint(); // Construct instance at (0,0) 10 radius = 1.0; 11 } 12 13 public MyCircle(int xCenter, int yCenter, double radius) { 14 center = new MyPoint(xCenter, yCenter); // Construct instance 15 this.radius = radius; 16 } 17 18 public MyCircle(MyPoint center, double radius) { 19 this.center = center; 20 this.radius = radius; 21 } 22 23 // Getters and Setters 24 public double getRadius() { 25 return radius; 26 } 27 28 public void setRadius(double radius) { 29 this.radius = radius; 30 } 31 32 public MyPoint getCenter() { 33 return center; 34 } 35 36 public void setCenter(MyPoint center) { 37 this.center = center; 38 } 39 40 public int getCenterX() { 41 return center.getX(); 42 } 43 44 public void setCenterX(int x) { 45 center.setX(x);

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 }

} public int getCenterY() { return center.getY(); } public void setCenterY(int y) { center.setY(y); } public void setCenterXY(int x, int y) { center.setX(x); center.setY(y); } public String toString() { return "center=" + center + " radius=" + radius; } public double getArea() { return Math.PI * radius * radius; } public double getPerimeter() { return 2.0 * Math.PI * radius; } public double distance(MyCircle another) { return center.distance(another.center); } // use distance() of MyPoint

TestMyCircle.java 1 // A test driver for MyCircle class 2 public class TestMyCircle { 3 public static void main(String[] args) { 4 MyCircle c1 = new MyCircle(); 5 System.out.println(c1); 6 c1.setCenterXY(0, 3); 7 System.out.println(c1); 8 9 MyPoint p1 = new MyPoint(4, 0); 10 MyCircle c2 = new MyCircle(p1, 9); 11 System.out.println(c2); 12 13 // Test distance() methods 14 System.out.println(c1.distance(c2)); 15 } 16 }

3.6 The MyTime class

A class called MyTime, which models a time instance, is designed as shown in the class diagram. It contains the following private instance variables:
hour: between 00 to 23. minute: between 00 to 59. Second:

between 00 to 59.

The constructor shall invoke the setTime() method (to be described later) to set the instance variable. It contains the following public methods:
setTime(int hour, int minute, int second): It shall check if the given hour, minute and second are valid before setting the instance variables. (Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid hour, minute, or second!".) Setters setHour(int hour), setMinute(int minute), setSecond(int second): It

shall check if the parameters are valid, similar to the above. Getters getHour(), getMinute(), getSecond().

toString():

returns "HH:MM:SS". and return this instance. Take

nextSecond(): Update this instance to the next second note that the nextSecond() of 23:59:59 is 00:00:00.

nextMinute(), nextHour(), previousSecond(), previousMinute(), previousHour(): similar to the above.

MyTime.java 1 // The MyTime class definition 2 public class MyTime { // "MyTime.java" 3 // Private member variables 4 private int hour; // 0-23 5 private int minute; // 0-59 6 private int second; // 0-59 7 8 // Constructor 9 public MyTime(int hour, int minute, int second) { 10 setTime(hour, minute, second); 11 } 12 13 void setTime(int hour, int minute, int second) { 14 setHour(hour); 15 setMinute(minute); 16 setSecond(second); 17 } 18 19 // Setters which validates input with exception handling 20 void setHour(int hour) { 21 if (hour >= 0 && hour <= 23) { 22 this.hour = hour; 23 } else { 24 throw new IllegalArgumentException("Invalid hour!"); 25 } 26 } 27 28 void setMinute(int minute) { 29 if (minute >= 0 && minute <= 59) { 30 this.minute = minute; 31 } else { 32 throw new IllegalArgumentException("Invalid minute!"); 33 } 34 } 35 36 void setSecond(int second) { 37 if (second >= 0 && second <= 59) { 38 this.second = second; 39 } else { 40 throw new IllegalArgumentException("Invalid second!"); 41 } 42 } 43 44 // Getters 45 public int getHour() {

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 }

return hour; } public int getMinute() { return minute; } public int getSecond() { return second; } // Return description in the format "hh:mm:ss" with leading zeros public String toString() { return String.format("%02d:%02d:%02d", hour, minute, second); } // Increment this instance to the next second, return this instance public MyTime nextSecond() { ++second; if (second == 60) { second = 0; ++minute; } if (minute == 60) { minute = 0; ++hour; } if (hour == 24) { hour = 0; } return this; // Return this instance, to support cascaded operation }

Exception Handling

What to do if an invalid hour, minute or second was given in the constructor or setter? Print an error message? Abruptly terminate the program? Continue operation by setting the parameter to its default? This is a hard decision. In Java, instead of printing an error message, you can throw an so-called Exception object (such as IllegalArgumentException) to the caller, and let the caller handles the exception. For example,
void setHour(int hour) { if (hour >= 0 && hour <= 23) { this.hour = hour; } else { throw new IllegalArgumentException("Invalid hour!"); } }

The caller can use the try-catch construct to handle the exception (in the test driver below). For example,
try { MyTime t3 = new MyTime(12, 69, 69); // skip remaining statements in try, goto catch System.out.println(t1); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } // after catch, continue next statement

The statements in the try-clause will be executed. If all the statements are successful, the catchclause is ignored. However, if one of the statement in the try-clause throws an IllegalArgumentException, the rest of try-clause will be skipped, and the execution transferred to the catch-clause. The program always continues to the next statement after the try-catch.
A Test Driver Class: TestMyTime.java // A test driver program for MyTime 1 public class TestMyTime { 2 public static void main(String[] args) { 3 MyTime t1 = new MyTime(23, 59, 58); 4 System.out.println(t1); 5 System.out.println(t1.nextSecond()); 6 System.out.println(t1.nextSecond().nextSecond().nextSecond()); 7 8 // MyTime t2 = new MyTime(12, 69, 69); // abrupt termination 9 // NOT continue to next 10 statement 11 12 // Handling exception gracefully 13 try { 14 MyTime t3 = new MyTime(12, 69, 69); 15 // skip remaining statements in try, goto catch 16 System.out.println(t1); 17 } catch (IllegalArgumentException ex) { 18 ex.printStackTrace(); 19 } // after try or catch, continue next statement 20 21 System.out.println("Continue after exception!"); 22 } 23 }

Without the proper try-catch, the "MyTime t2" will abruptly terminate the program (i.e., the rest of the program will not be run). With proper try-catch handling, the program can continue its operation (i.e., graceful handling of exception).

Das könnte Ihnen auch gefallen