Top Banner
13X11 Java Lecture 3 CS 1311 Structure 13X11
62

13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

Mar 31, 2015

Download

Documents

Robyn Buckridge
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Java Lecture 3

CS 1311

Structure

13X11

Page 2: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Types of Java Programs

• Applications– Stand alone– Requires JVM (java.exe, etc.) on your machine

• Applets– JVM built in to web browser runs program– Can be downloaded from network or files can be on

your machine– Susceptible to browser problems!

• Servlets– Like an applet but code runs on server when a

particular web page is browsed

Page 3: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

A View of Memory

Class Pool(What you wrote)

Stack (Static Area)(Store stuff here)

Heap (Dynamic Area)(Store stuff here)

Page 4: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

class CokeMachine {

int numCans = 3;

void vend() {if(numCans > 0) {

System.out.println("Have a Coke!");numCans = numCans - 1;

}else {

System.out.println("Sorry!");}

}

void load(int n) {numCans = numCans + 1;System.out.println("Ready to vend!");

}

public static void main(String args[]) {CokeMachine cc;cc = new CokeMachine();cc.vend();cc.vend();cc.vend();cc.vend();cc.load();cc.vend();

}}

Page 5: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Two Views• External

CokeMachine cc;

cc = new CokeMachine();

cc.vend();

cc.load(5);

class CokeMachine {

int numCans = 3;

void vend() { if(numCans > 0) { System.out.println ("Have a Coke!"); numCans = numCans - 1; } else { System.out.println ("Sorry!"); } }

void load(int n) { numCans = numCans + 1; System.out.println ("Ready to vend!"); }}

Page 6: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

javac *.java

java Driver

Could write...class Driver {

public static void main(String args[]) {

CokeMachine cc;

cc = new CokeMachine();

cc.vend();

cc.vend();

cc.vend();

cc.vend();

cc.load(5);

} // main

} // Simulation

Page 7: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

java Driver

DrivermainC

lass

Pool

Stac

kH

eap

Page 8: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

Java searches for(and finds) staticmain method

Cla

ssPo

olSt

ack

Hea

p

Drivermain

Page 9: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

Java searches for(and finds) staticmain method

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main

Page 10: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

CokeMachine cc;

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main

CokeMachinenumCans vend() load()

Page 11: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

CokeMachine cc;

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main cc (CokeMachine)

CokeMachinenumCans vend() load()

Page 12: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

cc = new CokeMachine();

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main cc (CokeMachine)

CokeMachinenumCans vend() load()

Obj: CokeMachinenumCans vend() load()

3

Page 13: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

cc.vend();

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main cc (CokeMachine)

CokeMachinenumCans vend() load()

Obj: CokeMachinenumCans vend() load()

2

Have a Coke!

Page 14: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

cc.vend();

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main cc (CokeMachine)

CokeMachinenumCans vend() load()

Obj: CokeMachinenumCans vend() load()

1

Have a Coke!Have a Coke!

Page 15: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

cc.vend();

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main cc (CokeMachine)

CokeMachinenumCans vend() load()

Obj: CokeMachinenumCans vend() load()

0

Have a Coke!Have a Coke!Have a Coke!

Page 16: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

What happened?

cc.vend();

Cla

ssPo

olSt

ack

Hea

p

Drivermain

main cc (CokeMachine)

CokeMachinenumCans vend() load()

Obj: CokeMachinenumCans vend() load()

0

Have a Coke!Have a Coke!Sorry!

Page 17: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

As a convenience...

• We don't really need the driver class• We can put a static main in the CokeMachine

• But the operation is essentially the same!

• In fact, you should always put a static main in every class you write

• Use it to test the class for correct operation

• Will Java get confused?

Page 18: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

No Way, Jose!

class Amain()

class Bmain()

class Cmain()

class Dmain()

class Emain()

class Fmain()

Why?

Page 19: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Because!

class Amain()

class Bmain()

class Cmain()

class Dmain()

class Emain()

class Fmain()

javac *.javajava F

Page 20: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Questions?

Page 21: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Fields

• Sometimes called attributes• In our CokeMachine: numCans• Can be

– Primitives– References

• Accesscc.numCans = 42; // Direct access

cc.vend(); // Via modifier

• Is direct access a "GoodThing"® or a "BadThing"®

Page 22: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Controlling Access

• Access to fields (and methods) can be controlledpublic -- Anyone can access

private -- Access is only from within class

Recall

public static void main(String args[])

Page 23: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Exampleclass CokeMachine

{

int numCans = 3;

double cash = 0.0;

static final double PRICE = 0.65;

void vend() {

if(numCans > 0) {

numCans--;

cash += PRICE;

System.out.println("Have a coke!");

} else {

System.out.println("Sorry, no Cokes");

}

}

Page 24: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Let's Test It!class Tester

{

public static void main(String args[])

{

CokeMachine cm; // Make reference

cm = new CokeMachine(); // Make a machine

cm.numCans--; // Steal a Coke

System.out.println("Cash = " + cm.cash);

}

} Where's my money?!?!?!

Page 25: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Let's add some protectionclass CokeMachine

{

private int numCans = 3;

private double cash = 0.0;

private static final double PRICE = 0.65;

public void vend() {

if(numCans > 0) {

numCans--;

cash += PRICE;

System.out.println("Have a coke!");

} else {

System.out.println("Sorry, no Cokes");

}

}

Page 26: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Warning about Testingclass CokeMachine

{

private int numCans = 3;

private double cash = 0.0;

private static final double PRICE = 0.65;

public void vend() {

if(numCans > 0) {

numCans--;

cash += PRICE;

System.out.println("Have a coke!");

} else {

System.out.println("Sorry, no Cokes");

}

}

}

class Tester {

public static void main(String a[]) {

CokeMachine cm = new CokeMachine();

cm.numCans--;

}

}

class CokeMachine

{

private int numCans = 3;

private double cash = 0.0;

private static final double PRICE = 0.65;

public void vend() {

if(numCans > 0) {

numCans--;

cash += PRICE;

System.out.println("Have a coke!");

} else {

System.out.println("Sorry, no Cokes");

}

}

public static void main(String a[]) {

CokeMachine cm = new CokeMachine();

cm.numCans--;

}

}

Page 27: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Demo

Page 28: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Details

• Methods– Module defined in a class which effectively becomes a

message that an object of that class can understand

cc.vend();

– Send a "vend()" message to the object referenced by cc

– Almost all code executed in Java will be executed in methods

Page 29: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Anatomy of a method• <access> <static> <return type>

<name>(<parameter list>) { <method body> }• Examples:

public static void main(String args[]){

System.out.println("Hello World!");}

void vend(){ /* code goes here */ }

private int addEmUp(int a, int b, int c) {return a + b + c;

}

Page 30: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Parts is parts

• <access>

public

private– There are others we won't discuss

• <static>

staticnothing which implies dynamic or instance

Page 31: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Parts is parts

• <return type>– Methods may optionally return a value– May be any valid type

• Class name• Primitive

void indicates no return value– If you promise to return something Java will expect you

to keep your promise– If you promise to not return anything Java will break

your kneecaps with a Louisville Slugger® if you don't keep your promise

Page 32: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

A Promise is a Promisepublic int max(int a, int b)

{

if(a >= b)

{

return a;

}

if(a < b)

{

return b;

}

}

Page 33: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

A Promise is a Promisepublic int max(int a, int b)

{

if(a >= b)

{

return a;

}

if(b < a)

{

return b;

}

}

Classname.java:<lineno>: Return required at end of int max(int, int). public int max(int a, int b)

Page 34: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Method Name...

• <name>– Method name follows normal identifier rules– Style note: Start method names with a lower case letter

but capitalize multiword names:enqueue

vend

vendACoke

load

loadCokes

Page 35: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Parameter List

• <parameter list>– Declare each parameter individually– i.e. int a, float f, CokeMachine cm– Java only supports pass by value (in) parameters.

public void swap(int a, int b) {

/* This method is worthless! */

int t = a;

a = b;

b = t;

}

Page 36: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Parameter Listpublic static void testMachine(int n, CokeMachine cm)

{

for(int i=0; i < n; i++)

{

cm.vend();

}

n = 0; // Just to be mean

cm = null; // and nasty!

}

• What is the effect on the outside world?

• Could this be considered (just maybe) a side effect?

Page 37: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pass by Valueclass Demo {

/* Code for testMachine here*/public static void Main(String args[]) {

int i = 3;CokeMachine cm;cm = new CokeMachine();testMachine(i, cm);

}}

i: cm:3

Object: CokeMachinenumCans = 3

Object: CokeMachinenumCans = 3

main:

Page 38: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pass by Valueclass Demo {

/* Code for testMachine here*/public static void Main(String args[]) {

int i = 3;CokeMachine cm;cm = new CokeMachine();testMachine(i, cm);

}}

i: cm:3

Object: CokeMachinenumCans = 3

Object: CokeMachinenumCans = 3

main:

testMachine: n: cm:3

Page 39: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pass by Valueclass Demo {

/* Code for testMachine here*/public static void Main(String args[]) {

int i = 3;CokeMachine cm;cm = new CokeMachine();exerciseMachine(i, cm);

}}

i: cm:3

Object: CokeMachinenumCans = 3

Object: CokeMachinenumCans = 3

main:

testMachine: n: cm:3

public static void testMachine

(int n, CokeMachine cm) {

for(int i=0; i < n; i++) {

cm.vend();

}

n = 0; // Just to be mean

cm = null; // and nasty!

}

Page 40: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pass by Valueclass Demo {

/* Code for testMachine here*/public static void Main(String args[]) {

int i = 3;CokeMachine cm;cm = new CokeMachine();exerciseMachine(i, cm);

}}

i: cm:3

Object: CokeMachinenumCans = 0

Object: CokeMachinenumCans = 0

main:

testMachine: n: cm:3

public static void testMachine

(int n, CokeMachine cm) {

for(int i=0; i < n; i++) {

cm.vend();

}

n = 0; // Just to be mean

cm = null; // and nasty!

}

Page 41: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pass by Valueclass Demo {

/* Code for testMachine here*/public static void Main(String args[]) {

int i = 3;CokeMachine cm;cm = new CokeMachine();exerciseMachine(i, cm);

}}

i: cm:3

Object: CokeMachinenumCans = 0

Object: CokeMachinenumCans = 0

main:

testMachine: n: cm:0

public static void testMachine

(int n, CokeMachine cm) {

for(int i=0; i < n; i++) {

cm.vend();

}

n = 0; // Just to be mean

cm = null; // and nasty!

}

Page 42: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pass by Valueclass Demo {

/* Code for testMachine here*/public static void Main(String args[]) {

int i = 3;CokeMachine cm;cm = new CokeMachine();exerciseMachine(i, cm);

}}

i: cm:3

Object: CokeMachinenumCans = 0

Object: CokeMachinenumCans = 0

main:

testMachine: n: cm:0

public static void testMachine

(int n, CokeMachine cm) {

for(int i=0; i < n; i++) {

cm.vend();

}

n = 0; // Just to be mean

cm = null; // and nasty!

}

Page 43: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pass by Valueclass Demo {

/* Code for testMachine here*/public static void Main(String args[]) {

int i = 3;CokeMachine cm;cm = new CokeMachine();exerciseMachine(i, cm);

}}

i: cm:3

Object: CokeMachinenumCans = 0

Object: CokeMachinenumCans = 0

main:

Page 44: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Questions?

Page 45: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Factorial (Yes, again)

class FactDemo {

public static void main(String args[]) {

int n = 10;

int i;

int answer = 1;

for(i = 1; i <= n; i++) {

answer *= i;

} // for

System.out.println(n + "! = " + answer);

} // main

} // factDemo

Page 46: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Make it a method

public static void fact(int n) {

int i;

int answer = 1;

for(i = 1; i <= n; i++) {

answer *= i;

} // for

System.out.println(n + "! = " + answer);

} // fact

Page 47: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Make it smaller and more efficient?

public static void fact(int n) {

int i, ans;

for(i = 1, ans = 1; i <= n; ans *= i++);

System.out.println(n + "! = " + ans);

} // fact

Page 48: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Make it more readable?

public static void fact(int n) {

int i;

int ans = 1;

for(i = 1; i <= n; i++)

ans *= i;

System.out.println(n + "! = " + ans);

} // fact

Page 49: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Make it more useful?

public static int fact(int n) {

int i;

int ans = 1;

for(i = 1; i <= n; i++)

ans *= i;

return ans;

} // fact

Page 50: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Make it more useful?

class FactDemo {

public static int fact(int n) {

int i;

int ans = 1;

for(i = 1; i <= n; i++)

ans *= i;

return ans;

} // fact

public static void main(String args[]) {

System.out.println("10! = " + fact(10));

}

}

Page 51: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Do it recursively!

class FactDemo {

public static int fact(int n) {

if(n == 0)

return 1;

else

return n * fact(n - 1);

} // fact

public static void main(String args[]) {

System.out.println("10! = " + fact(10));

}

}

Page 52: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Do it recursively!

class FactDemo {

public static int fact(int n) {

if(n == 0)

return 1;

else

return n * fact(n - 1);

} // fact

public static void main(String args[]) {

for(int i = 1; i < 18; i++)

System.out.println(i + "! = " + fact(i));

}

}

Page 53: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

ShakyResults

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

7! = 5040

8! = 40320

9! = 362880

10! = 3628800

11! = 39916800

12! = 479001600

13! = 1932053504

14! = 1278945280

15! = 2004310016

16! = 2004189184

17! = -288522240

Correct value: n! = 6,227,020,800 > ?

Page 54: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Primitive Data Type Ranges

Type Size Min Default

boolean false

Max

1 false* true*

char '\u0000' (null)16

byte (byte) 08 -128 127

short (short) 016 -32,768 32,767

int 032 -2,147,483,648 2,147,483,647

long 0L64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

float 0.0F32 Approx ±3.4E+38 with 7 significant digits

double 0.0D64 Approx ±1.7E+308 with 15 significant digits

void

* Not truly min and max.

Page 55: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Notice!

• Notice how Java gave you a helpful warning message to help keep you spacecraft from plunging into the Sun

NOT.

Page 56: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Sidebar on Methods

• Fix the problem (forever?)!

class FactDemo {

public static long fact(long n) {

if(n == 0)

return 1;

else

return n * fact(n - 1);

} // fact

public static void main(String args[]) {

for(int i = 1; i < 18; i++)

System.out.println(i + "! = " + fact(i));

}

}

Page 57: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pop Quizclass Widget

{

int a, b;

public int sum() {

return a + b;

}

public static void main(String a[]) {

a = 5;

b = 10;

System.out.println(sum());

}

}

Page 58: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Pop Quizclass Widget{int a, b;public int sum() {

return a + b;}public static void main(String a[]) {

Widget w;w = new Widget();w.a = 5;w.b = 10;System.out.println(w.sum());

}}

Page 59: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Questions?

Page 60: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Final Thought

• Just what is a class?

class

declarations

initializationmodules*

methods

Page 61: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.

13X11

Questions?

Page 62: 13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.