JAVA (something like C). Object Oriented Programming Process orientated – code acting on data...

54
JAVA (something like C)

Transcript of JAVA (something like C). Object Oriented Programming Process orientated – code acting on data...

Page 1: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

JAVA (something like C)

Page 2: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Object Oriented Programming• Process orientated – code acting on data• Object oriented – data controls access to

code• Encapsulation – java has a ‘class’. The data

is the state and the methods (code) is the behavior. You control the methods and access to the data

• Inheritance – where one class acquires the properties of another class

• Polymorphism – one interface multiple methods

Page 3: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

JAVA editors• did a ‘search’ for JAVA editors on the NET

and obtained...

• JCreator – www.jcreator.com– Download the free one

• editPlus - I have used this one– http://www.editplus.com/– shareware - pay after 30 days

• WingSoft– http://www.wingsoft.com/wingeditor.shtml

Page 4: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

comments

• A single line comment

• int row; // keeps track of the row

• multiple line comments

• /* this is one line of the comment

• this is the 2nd line

• this is the third line */

Page 5: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Documentation comments• acts like multiple line comments

• /** this is one line of the comment

• this is the 2nd line

• this is the third line */

• note the initial /** The javadoc program

• will collect these blocks of comments as documentation for the program

• javadoc filename.java

Page 6: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

special symbols• { } indicates a block of code• ( ) parameters to a method• [ ] indicates an array• ; terminates a statement• , a seperator• . used to separate package names

• Note table of reserved words on page 39

Page 7: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Identifier names• Start with a letter• Contains• letters (upper or lower case)• numbers• _ (the underscore)• $• Can be any length• No imbedded spaces or special characters• Do not start an identifier with a number

Page 8: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Examples/Naming Conventions • numberOfRows• num_rows• row8• Naming Conventions• classes start with a capitol letter• HelloWorld ShoppingCart• methods & identifiers with a small letter• println() getChar() • constants are all caps PIE

Page 9: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

System.out.println( )

• System.out.println (….) sends a String to the monitor

• System.out.println(“Hi Mom”);

• The + ‘concatenates’ two variables

• int aVal = 17;

• System.out.println(“a = “ + aVal);

• System.out.println(“a=“ + aV + “ b=“ + bV)

Page 10: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Primitive data types

• JAVA has 8 primitive data type• Name size |-------range-------|• • byte 8-bits -128 ~ 127• short 16-bits -32k ~ +32k• int 32-bits -2**31 ~ +2**31• long 64-bits -2**63 ~ + 2**63

Page 11: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Primitive data types (cont.)

Name size |-------range-------|• • float 32-bits +- 3.4 X 10**38• double 64-bits +- 1.8 X 10**308• char 16-bits 0 ~ 65,535• boolean 1-bit true or false• (not a 0 or a 1)

Page 12: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Examples of primatives• int x;

• int x, y=7, zz, rows=8;

• boolean isRunning = false;

• double dd;

• float f = 10.3; (error - defaults to a double)

• float fx = 22.4f; note the ‘f’

• byte b1, b2 = 44, cx;

• short ss=5;

Page 13: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

The char• char uses a 16-bit Unicode character set

• The Unicode set allows 65,536 different characters. (about half are assigned)

• char ch = ‘a’;

• char ch1, ch2 = ‘7’, ch3 = ‘X’;

• char delta = ‘\u0394’ delta symbol

• char copyrt = ‘\u00AE’ copy write symbol

• escape seq. – page 32 (EX \t is tab)

Page 14: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

the String class

• String is a class, not a primitive data type

• is a sequence of characters

• not an array of char terminated by a null

• immutable – cannot be changed once created

• String ss;

• String s1 = “hi mom”;

• String s1, su=“XXX”, s3=“SDE”;

Page 15: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Java DOS commands• Javac A:\>javac test.java

– compiler– uses a text file named ????.java– creates a ????.class file of byte code

• java A:\>java test– the interpreter (runs the class code)– runs an application

• appletviewer A:\>appletviewer test.html– runs an applet

Page 16: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Minimum JAVA program

• class Skeleton

• {

• public static void main(String args[])

• {

• } // end main

• } // end class

Page 17: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

arithmetic• Expression – series of variables (methods) that evaluate to a single variable• = assign (evaluate right, assign to right)• + add x = x + 7; • - subtract x = 8 – cx;• * multiply pay = rate * time;• / divide tax = too_much / 1.0;• % remainder (mod)• row = x % 4; • row is the remainder of x / 4• ans = 33 % 6; ans is 3

Page 18: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Consider the primitives

• byte 8 bits

• short 16 bits

• int 32 bits

• long 64 bits

• float 32 bits

• double 64 bits

• char 16 bits

Page 19: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Some examples

• Define and initialize all the primitives

• Add the following to skeleton and compile

• short = byte;

• byte = short;

• double = float

• float = double;

• int = boolean;

• int = short * byte;

• int = short * float;

• double = int * double;

• float = short * double;

• int = char; chat = int;

Page 20: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Conversion and casting• Automatic – • types are compatible • destination is larger than the source• Casting (target type) value• byte b = (byte) someValue;• byte b = intExample; error• byte b = (byte) intExample; OK• byte b = 50;• byte x = b/2; Error• Byte x = (byte)(b/2); ok

Page 21: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Shortcuts in arithmetic

• Shortcuts

• x++ x = x + 1;

• x-- x = x – 1;

• x+=7; x = x + 7;

• x*=3; x = x * 3;

• x/=2; y = y / 2;

• q-=5; q = q – 5;

Page 22: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

ternary expression

• expression01 ? expression02 : expression03

• Expression01 must evaluate to a boolean

• If expression01 is true,

• expression 02 is evaluated, else

• expression03 is evaluated

Page 23: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Example of ? :

• if (x > y)

• max = x;

• else

• max = y;

• is the same as

• max = x>y ? x : y;

Page 24: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

‘pre’ & ‘post’

• Difference between x++; and ++x;

• Example

• int x = 5;

• System.out.println(x + “ “ + x++);

• int x = 5;

• System.out.println(x + “ “ + ++x);

Page 25: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Operator precedence• Do work within ( )

• Then, left to right….

• ++ --

• * / %

• + -

• =

• What’s the answer to the following..

• n = 1 - 2 * 3 - 4 + 5;

Page 26: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

if statement

• if(expression) // expression is evaluated• block of code // execute if true• else• block of code // execute if false

• Note – a block of code can contain a nested if statement

Page 27: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

examples• if (x > 6)• System.out.println(“x is bigger);• else• System.out.println(“x is less than 6);

• if (x>8)• {}• else• System.out.println(“x is less than 8”);

Page 28: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

logical operators• & evaluate both, both must be true• | evaluate both, either must be true• && both must be true. If 1st is false, do not

evaluate the 2nd

• || one must be true. If 1st is true, do not evaluate the 2nd

• ~ tilda - not• Shift and bit-wise operators – you are on

your own (page 80)

Page 29: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

&& and &• if ((a>b) & (c<=7))

• System.out.println(“both true”);

• else

• System.out.println(“one not true”);

• if ((a>b) && (c<=7))

• System.out.println(“both true”);

• else

• System.out.println(“one not true”);

Page 30: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

|| and |• if ((a>b) | (c<=7))

• System.out.println(“at least one true”);

• else

• System.out.println(“both false”);

• if ((a>b) || (c<=7))

• System.out.println(“at least one true”);

• else

• System.out.println(“both false”);

Page 31: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

• if ((a>b) && (a++))• System.out.println(“(true)a=” + a);• else• System.out.println(“(false)a=” + a);

• if (a>b)• if (c!=7)• System.out.println(“123456”);• is the same as• If((a>b) & (c!=7))• System.out.println(“123456”);

Page 32: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Relational operators

• == equal to

• != not equal

• > greater than

• < less than

• >= greater than or equal to

• <= less than or equal to

Page 33: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

The loops

• For a loop you need 3 things

• a counter of some kind

• a comparison statement - a test

• increment the counter some time

• the loops are

Page 34: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

for loop

• for (int i = 6; i<5; i++)

• {

• System.out.println(“counter is “ + i);

• }

• the ‘for’ statement contains the counter, increment, and test all together

Page 35: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

example• For (int k=0; k<3; k++)

• System.out.println(k);

• for (int d=5; d<3; d++)

• System.out.println(k);

• for (int d=3, int e=7; d<e; d++, e--)

• System.out.println(d + “ and e=“ + e);

Page 36: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

for loop – using a comma• for (a =1, b=4; a<b; a++, b--)• {• System.out.println(“a = “ + a);• System.out.println(“b = “ + b);• }• Gives a = 1

– b = 4– a = 2– b = 3

Page 37: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

for loop – some variations• boolean done = false;

• for (a =1; !done; a++)

• { ……… done = true; }

• boolean done = false;

• for ( ; !done; )

• { ……… done = true; }

Page 38: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

while loop

• int x=0; // counter

• while (x<5) // test

• {

• System.out.println(“counter = “ + x);

• x++; // increment

• }

Page 39: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Example #1

• int x=4; // counter

• while (x<5) // test

• {

• System.out.println(“counter = “ + x);

• x++; // increment

• }

Page 40: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Example #2

• int x=4; // counter

• while (x<5) // test

• {

• System.out.println(“counter = “ + x);

• }

Page 41: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Example #3

• int x=14; // counter

• while (x<5) // test

• {

• System.out.println(“counter = “ + x);

• x++; // increment

• }

Page 42: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

do/while loop

• int c=3; // counter

• do {

• System.out.println(“counter = “ + c);

• c++; // increment

• } while (c<5); // test

• what’s ‘special’ about a do/while loop??

Page 43: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Example #1

• int c=3; // counter

• do {

• System.out.println(“counter = “ + c);

• } while (c<5); // test

Page 44: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Example #2

• int c=13; // counter

• do {

• System.out.println(“counter = “ + c);

• c++; // increment

• } while (c<5); // test

Page 45: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Switch statement• switch(expression)• {• case value01:statement;• break;• case value02:statement;• break;• default: statement;• }• The expression is evaluated. Jump to the

corresponding case statement with = value

Page 46: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

• expression must be a byte, short, int, or char

• When you jump to the proper case value, you execute code until the end of the switch statement or a break statement is executed

• No matches – jump to default

• No default – out of switch

Page 47: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

• Switch(month)• {• case 1: days=31;• break;• case 2: days=28;• break;• case 3: days=31;• break;• case 4: days=30;• break;• |• case 12: days=31;• break;• default: days=0;• }

Page 48: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

• switch(month)• {• case 9:• case 4:• case 6:• case 11: days=30;

break;• case 2: days=28;• break;• default: days=31;• }

Page 49: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

More use of “break”• hitting a ‘break’ will terminate a loop• for(int x=0; x<100; x++) {• if (x==5) break;• System.out.println(“X=“ + x);• }• X=0• X=1• X=2• X=3• X=4

Page 50: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

• Using ‘break’ in nested loops• for(int x=1; x<4; x++) • {• System.out.println(“X=“ + x);• for (int y=0; y<30; y++) • { • if (y==x) break;• System.out.println} (“Y=“ + y);• }• }

Page 51: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

“break” with the while loop• while (x<100 {• if (x==5) break;• System.out.println(“X=“ + x);• x++;• }• X=0• X=1• X=2• X=3• X=4

Page 52: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

break with a label• outer: for(i=1; i<100; i++)• { • for (j=4; j<50; j++)• { …….• if((j%i)==0)• break outer;• ……..• } // end j for loop• } // end i for loop

Page 53: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

continue statement• for (int x=0; x<8; x++) {• System.out.print(x + “ “);• if(x%2) continue;• System.out.println(“ “);• }• 0 1• 2 3• 4 5• 6 7

Page 54: JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

break / continue / go to

• break and continue may specify a label

• a label is a JAVA identifier that indicates a block of code

• aLabel:

• {

• stuff;

• }

• It is close to a GoTo. Don’t use it;