to understand the essentials of object- oriented programming in java to review the primitive data...

63
Review Java

Upload: doris-arnold

Post on 21-Dec-2015

228 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Review Java

Page 2: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 2

To understand the essentials of object-oriented programming in Java

To review the primitive data types of Java, how to use the control structures of Java

To learn how to use predefined classes such as Math, JOptionPane, String, StringBuffer, and StringTokenizer

To review how to write and document your own Java classes

Objectives

Page 3: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 3

To understand how to use arrays in Java To learn how to perform I/O in Java using

simple dialog windows To learn how to perform I/O in Java using

streams

Chapter Objectives (continued)

Page 4: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 4

Compiling and Executing a Java Program

Page 5: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 5

The class is the fundamental programming unit Every program is written as a collection of

classes Class definitions are stored in separate files

with the extension .java and the file name must be the same as the class name

A class is a named description for a group of entities

A class is a general description of a group of entities that all have the same characteristics; each entity is an object

Classes and Objects

Page 6: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 6

Java consists of small core language augmented by an extensive collection of packages

Each package contains a collection of related Java classes, such as:◦ Swing◦ AWT◦ util

The Java API

Page 7: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Review of Java fundamentalsTemplate for Class Definition

class {

}

Import StatementsImport Statements

Class CommentClass Comment

Class NameClass Name

Data MembersData Members

Methods(incl. Constructor)

Methods(incl. Constructor)

Page 8: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 8

Java distinguishes two kinds of entities

◦ Primitive types: data is stored in primitive type variables

◦ Objects: are associated with reference variables which store an object’s address

Primitive Data Types and Reference Variables

Page 9: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 9

Represent numbers, characters, and Boolean values

Integers: byte, short, int, and long Real numbers: float and double Characters: char

Primitive Data Types

Page 10: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

ASCII Encoding

For example, character 'O' is 79 (row value 70 + col value 9 = 79).

For example, character 'O' is 79 (row value 70 + col value 9 = 79).

O

9

70

Page 11: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Format:<data type> <variable_name>;

If more than one variable has the same data type:<data type> <name1>, <name2>..;

Variable declaration

Page 12: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

<variable name> = <expression>;Example:x =2*5+6-1;

Assignment statement

Page 13: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

It must be a legal identifier. It must not be a keyword, a boolean literal

(true or false), or the reserved word null. It must be unique within its scope.

Variable names

Page 14: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Legal identifier:be composed of letters, numbers, _ and $. Identifiers may only begin with a letter, _, or $.

Keyword:http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

Java styles:◦ Variable names begin with a lowercase letter◦ Class names begin with an uppercase letter

Variable name (cont.)

Page 15: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 15

Which of the following is not a valid Java identifier?

a. my Valueb. $_AAA1c. widthd. m_x

Review question

Page 16: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 16

Which of the following is not a valid Java identifier?

a. my Valueb. $_AAA1c. widthd. m_x

Review question

Page 17: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 17

Which of the following is a correct variable declaration statement?

a. int x - float y;b. int x: float y;c. int x,y;d. Long int x;

Review question

Page 18: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 18

Which of the following is a correct variable declaration statement?

a. int x - float y;b. int x: float y;c. int x,y;d. Long int x;

Review question

Page 19: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Constant:◦ Value it contains doesn’t change

final int MONTHS_IN_YEAR = 12;

Variables:◦ Value it contains may vary

double loanAmount; loanAmount =0; loanAmount = 1000.99;

Constant and variables

Page 20: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Integer division:◦ Integer/integer = integer, 7/2 = 3◦ Integer/double = double, 7/2.0 = 3.5◦ Double/integer = double, 7.0/2 = 3.5

Type casting: a process that converts a value of one data type to another data type.

Implicit castingExplicit casting

Integer division and type casting

Page 21: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Implicit casting:◦ Operand is converted from a lower to a higher

precision◦ Higher precision: a data type with a larger range

of values Double has a higher precision than float Int has a higher precision than short

◦ Operand: can be a constant, variable, method call or another arithmetic expression

Type casting (cont.)

Page 22: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Explicit casting◦ (<data type>) <expression>◦ Example:

float result;result = (float) ((3+5)/6);

and result = ((float) (5+3))/6;

Type casting (cont.)

Page 23: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 23

Widening conversion: operations involving mixed-type operands, the numeric type of the smaller range is converted to the numeric type of the larger range

In an assignment operation, a numeric type of smaller range can be assigned to a numeric type of larger range

Type Compatibility and Conversion

Page 24: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

if (<boolean expression>) <block>;else <block>;

Control Statements: Simple Choice Statement

if (<boolean expression>) single statement;else single statement;

Page 25: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Boolean expression: is a conditional expression that is evaluated to either true or false.

Conditional expression: is a three part expression:<exp.> <relational operators> <exp.>

Boolean expressions can be combined by boolean operators

Boolean expression

Page 26: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Relational Operators

Expression Meaninga == b Is a equal to b?a != b Is a not equal to b?a > b Is a greater than b?a < b Is a less than b?a >= b Is a greater than or equal to b?a <= b Is a less than or equal to b?

Page 27: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

&& means AND||means OR

! means NOT

Boolean operators

Page 28: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 28

How many times the method readData() will be called in the following code segnment?

int i;i = 0;

 while ( i <= 4 ) {

readData(); i = i + 1;

} // end while

Pre-review question

5

Page 29: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 29

.          can be used to traverse a two-dimensional array.

a. A do while statement.b. A for statement.c. Two nested for statements.d. Three nested for statements.

Pre-review question

Page 30: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 30

.          can be used to traverse a two-dimensional array.

a. A do while statement.b. A for statement.c. Two nested for statements.d. Three nested for statements.

Pre-review question

Page 31: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

The While Loop

while(<boolean expression>){

// Repeat multiple statements.

statement 1

statement 2

statement 3

...

}

Page 32: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

do { // Repeat multiple statements. statement 1 statement 2 statement 3 ...} while(<boolean expression);

• Note that the statements in the body of the loop are always executed at least one.

• Note the final semicolon, which is required.

The Do-While Loop

Page 33: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

The For-Loop Outline

// Repeat multiple statements.for(initialization; condition; post-body update){ // Statements to be repeated. statement 1 statement 2 statement 3 ...}

• Commonly used with increment and decrement operators.

Page 34: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Used as a shorthand for add-one-to and subtract-one-from:

value = value+1; value += 1; value++;

Prefix and postfix forms: ++value; --value; value--;

Increment and Decrement

Page 35: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Attributes (Data Member) Declaration

<modifiers> <data type> <name> ;

private String ownerName ;

ModifiersModifiers Data TypeData Type NameName

Note: There’s only one modifier in this example.

Note: There’s only one modifier in this example.

Page 36: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Method Declaration<modifier> <return type> <method name> ( <parameters> ){

<statements>

}

public void setOwnerName ( String name ) {

ownerName = name;

}

StatementsStatements

ModifierModifier Return TypeReturn Type Method NameMethod Name ParameterParameter

Page 37: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

A constructor is a special method that is executed when a new instance of the class is created.

Constructor

public <class name> ( <parameters> ){ <statements> }

public Bicycle ( ) {

ownerName = “Unassigned”;

}StatementsStatements

ModifierModifier Class NameClass Name ParameterParameter

Page 38: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

An argument is a value we pass to a method. A parameter is a placeholder in the called

method to hold the value of the passed argument.

Arguments and Parameters

class Account {

. . .

public void add(double amt) {

balance = balance + amt; }

. . . }

class Sample {

public static void main(String[] arg) { Account acct = new Account(); . . . acct.add(400); . . . }

. . . } argument

Page 39: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

An argument is a value we pass to a method. A parameter is a placeholder in the called

method to hold the value of the passed argument.

Arguments and Parameters

class Account {

. . .

public void add(double amt) {

balance = balance + amt; }

. . . }

class Sample {

public static void main(String[] arg) { Account acct = new Account(); . . . acct.add(400); . . . }

. . . }

parameter

Page 40: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 40

You can declare reference variables that reference objects of specified types

Two reference variables can reference the same object

The new operator creates an instance of a class

A constructor executes when a new object is created

Referencing and Creating Objects

Page 41: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 41

Programmers use methods to define a group of statements that perform a particular operation

The modifier static indicates a static or class method

A method that is not static is an instance method

All method arguments are call-by-value If the argument is a primitive type, its value is

passed to the method◦ The method can’t modify the argument value and have

the modification remain after return from the method

Methods

Page 42: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 42

If the argument is of a class type, the value of the reference variable is passed, not the value of the object itself

Reference variables point to the object and any modification to the object will remain after return from the method

Methods (continued)

Page 43: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 43

Consider the following Java statements:

int x = 9;double y = 5.3;result = calculateValue( x, y ); Which of the following statements is false?a. A method is called with its name and arguments inside

parentheses.b. x and y are parameters.c. Copies of x and y are passed to the method

calculateValue().d. x and y are arguments

Review question

Page 44: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 44

Consider the following Java statements:

int x = 9;double y = 5.3;result = calculateValue( x, y ); Which of the following statements is false?a. A method is called with its name and arguments inside

parentheses.b. x and y are parameters.c. Copies of x and y are passed to the method

calculateValue().d. x and y are arguments

Review question

Page 45: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 45

Provides a collection of methods that are useful for performing common mathematical operations

The Class Math

Page 46: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 46

An escape sequence is a sequence of two characters beginning with the character \

Represents characters or symbols that have a special meaning in Java

Escape Sequences

Page 47: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 47

String class defines a data type that is used to store a sequence of characters

You cannot modify a String object◦ If you attempt to do so, Java will create a new

object that contains the modified character sequence

The String Class

Page 48: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 48

You can’t use the relational operators or equality operators to compare the values stored in strings or other objects

Comparing Objects

Page 49: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Examples

We can do thisbecause Stringobjects areimmutable.

Page 50: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

50

Stores character sequences Unlike a String object, the contents of a

StringBuffer object can be changed

The StringBuffer Class

Page 51: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 51

Consider the Java segment:String line1 = new String( "c = 1 + 2 + 3" ) ;StringTokenizer tok = new

StringTokenizer( line1 );int count = tok.countTokens(); What is the value of count ?  

Pre-review question

7

Page 52: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

52

Consider the Java segment:String line1 = new String( "c = 1 + 2 + 3" ) ;StringTokenizer tok = new StringTokenizer( line1, delimArg );

For the String line1 to have 4 tokens, delimArg should be:

a. String delimArg = "+=";b. String delimArg = "123"c. String delimArg = "c+";d. String delimArg = " ";

Pre-review question

Page 53: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

53

Consider the Java segment:String line1 = new String( "c = 1 + 2 + 3" ) ;StringTokenizer tok = new StringTokenizer( line1, delimArg );

For the String line1 to have 4 tokens, delimArg should be:

a. String delimArg = "+=";b. String delimArg = "123"c. String delimArg = "c+";d. String delimArg = " ";

Pre-review question

Page 54: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 54

We often need to process individual pieces, or tokens, in a string

StringTokenizer Class

Page 55: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 55

Sometimes we need to process primitive-type data as objects

Java provides a set of classes called wrapper classes whose objects contain primitive-type values: Float, Double, Integer, Boolean, Character, etc.

Wrapper Classes for Primitive Types

Page 56: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 56

Unified Modeling Language is often used to represent a class◦ Standard means of documenting class

relationships widely used in industry

Defining Your Own Classes

Page 57: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 57

The modifier private sets the visibility of each variable or constant to private visibility◦ These data fields can be accessed only within the

class definition Only class members with public visibility

can be accessed outside of the class Constructors initialize the data fields within

a class

Defining Your Own Classes (continued)

Page 58: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 58

In Java, an array is also an object The elements are indexes and are

referenced using a subscripted variable of the form arrayname[subscript]

Arrays

Page 59: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 59

A programmer must do the following before using an array:

a. declare then reference the array.b. create then declare the array.c. create then reference the array.d. declare then create the array.

Review question

Page 60: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 60

A programmer must do the following before using an array:

a. declare then reference the array.b. create then declare the array.c. create then reference the array.d. declare then create the array.

Review question

Page 61: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 61

Input/Output using Class JOptionPane (continued)

Page 62: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 62

A dialog window always returns a reference to a string

Therefore, a conversion is required

Converting Numeric Strings to Numbers

Page 63: To understand the essentials of object- oriented programming in Java  To review the primitive data types of Java, how to use the control structures

Appendix A: Introduction to Java 63

An input stream is a sequence of characters representing program data

An output stream is a sequence of characters representing program output

The console keyboard stream is System.in or Scanner

The console window is associated with System.out.print(ln) or Formatter

Input/Output using Streams