java coding 5 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Java Coding 5 PowerPoint Presentation
Download Presentation
Java Coding 5

Loading in 2 Seconds...

play fullscreen
1 / 29

Java Coding 5 - PowerPoint PPT Presentation


  • 110 Views
  • Uploaded on

Java Coding 5. To object or not…. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Java Coding 5' - phyliss


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
java coding 5

Java Coding 5

To object or not…

David Davenport

Computer Eng. Dept.,

Bilkent UniversityAnkara - Turkey.

email: david@bilkent.edu.tr

important
IMPORTANT…
  • Students…

This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn!

  • Instructors…

You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it.

thank you,David.

from the beginning
From the beginning…
  • History of programming paradigms
    • GoTo Programming (spaghetti code!)
    • Structured Programming
    • Object-Oriented Programming
    • ??? Aspect-Oriented…
  • Paradigm changes response to
    • Need to build ever larger programs
    • Correctly
    • On time
    • On budget
key attributes of oop
Key Attributes of OOP
  • Abstraction, Encapsulation, Inheritance, Polymorphism

What?

  • Ease of reuse
      • Speeds implementation & facilitates maintenance.
      • Component-based approach
        • Easy to use existing code modules
        • Easy to modify code to fit circumstances!
  • A natural way to view/model world
      • Makes design quicker, easier & less error-prone.
the world as we see it
The world as we see it…
  • Look around & what do you see?
    • Things (books, chairs, tables, people!)
  • Actually, see individual things!
    • Ayse, David, my textbook, that chair, Mehmet’s pencil, etc.
  • The world is
    • a set of things
    • interacting with each other.
describing the world 1

Individual

Category

Describing the world (1)
  • Describe a particular person
    • Ayse has long blond hair, green eyes, is 1.63m tall, weighs 56Kg and studies computer engineering. Now lying down asleep.
    • Mehmet studies electronics, has short black hair and brown eyes. He is 180cm and 75 kilos. Now running to class!
  • Notice how all have specific values of
    • name, height, weight, eye colour, state, …
describing the world 2

Category

Individual

Describing the world (2)
  • Type/category determine an object’s properties & functionality
    • Person
      • has name, height, weight, can run, sleep, …
    • Category gives default properties
      • “Ayse is a person with green eyes.” We infer/assume she has two of them, as well as two legs, arms, nose, mouth, hair, can speak, run, sleep, etc!
      • Can concentrate on “relevant” properties
java oop terminology
Java OOP terminology
  • Class - Category
    • Properties/states
    • Functionality/Services(examines/alters state)

data

methods

  • object - Individual/unique thing(an instance of a class)
    • Particular value for each property/state
    • & functionality of all members of class.
java oop software

Ayse

David

Java OOP Software
  • Software System
    • Set of objects
    • Which interact with each other

Created (instantiated) from class definitions

One object will send a message to another object asking it to do a particular task. The first object does not need to know how the task is done (only how to request that it be done.)

This corresponds to calling one of the second object’s methods!

Person

David: Say your name

“David”

in more detail

Person

name,

age,

salary,

comments

sayName,

getNetSalary

getComments

setComments

increaseAge

Name: “David”

Age: 22

Salary: 2000

Comments:“Teaches CS101”

Name: “Ayse”

Age: 18

Salary: 500

Comments:“Good student”

In more detail
  • Create & manipulate person objects
coding java classes

String name;

int age;

double salary;

String comments;

public Person( String theName,

int theAge ) {

name = theName;

age = theAge;

comments = “”;

}

public void sayName() {

System.out.println( name);

}

Coding Java Classes

// header

public class Person {

// properties

// constructors

// methods

}

coding java classes1
Coding Java Classes

public String getName() {

return name;

}

public String getComments() {

return comments;

}

public void setComments( String someText) {

comments = someText;

}

“get” & “set” methods for some properties(no setName!)

public void increaseAge() {

age = age + 1;

}

public double getNetSalary() {

double netSalary;

netSalary = salary - TAX;

return netSalary;

}

Variables which are not parameters or properties must be defined locally.

creating using objects

salary:

name:

age:

“Ayse”

18

0.0

aStudent{Person}

comments:

“”

Creating & Using Objects
  • Always
    • Declare variable to “hold” object
    • Create object using “new” statement
    • Call object’s methods

Person aStudent;

aStudent = new Person( “Ayse”, 18);

aStudent.sayName();

Put this in method of another class, (e.g main method)

creating using objects1

salary:

salary:

name:

age:

name:

age:

“Ayse”

18

“David”

22

0.0

0.0

friend{Person}

aStudent{Person}

comments:

comments:

“”

“”

Creating & Using Objects

Person aStudent;

aStudent = new Person( “Ayse”, 18);

Person friend;

friend = new Person( “David”, 22);

23

“Good student”

friend.increaseAge();aStudent.setComments( “Good student”);

examples existing classes
Examples: existing classes
  • Random class

Random die;

die = new Random();

int face = die.nextInt(6) + 1;

System.out.println( face);

  • StringTokenizer class

StringTokenizer tokens;

tokens = new StringTokenizer( “to be or not to be”);

while ( tokens.hasMoreTokens() ) {

aWord = tokens.nextToken();

System.out.println( aWord);

}

System.out.println( “done”);

writing your own classes
Writing Your Own Classes
  • Coins
  • Dice
  • Traffic lights
  • TV
  • Video
  • Wallet
  • Robo
  • Music CD
  • Time/Date (in various formats!)
object vs reference

david{Person}

derya{Person}

CS101 instructor{Person}

Derya’s dad{Person}

david2{Person}

Object vs. Reference
  • In the “real” world

So too in Java!

object vs reference1

david{Person}

david2{Person}

derya{Person}

CS101 instructor{Person}

Derya’s dad{Person}

David’sclone

Object vs. Reference
  • In the Java world

David

Derya

same or different 1

myQCd{CD}

Title B.R.

Artist Queen

Date 1976

Length 3:50

Title Best of

Artist Genesis

Date 1983

Length 2:40

myCd{CD}

yourCd{CD}

Title B.R.

Artist Queen

Date 1976

Length 3:50

yourQCd{CD}

Same or Different? (1)
  • Comparing objects

if ( myCd == yourCd) System.out.println( “Same”);

else

System.out.println( “Different”);

if ( myCd == yourQCd) System.out.println( “Same”);

else

System.out.println( “Different”);

same or different 2

myQCd{CD}

Title B.R.

Artist Queen

Date 1976

Length 3:50

Title Best of

Artist Genesis

Date 1983

Length 2:40

myCd{CD}

yourCd{CD}

Title B.R.

Artist Queen

Date 1976

Length 3:50

yourQCd{CD}

Same or Different? (2)
  • Define an “equals” method

if ( myCd.equals( yourCd) ) System.out.println( “Same”);

else

System.out.println( “Different”);

if ( myCd.equals( yourQCd) ) System.out.println( “Same”);

else

System.out.println( “Different”);

copying
Copying
  • in primitive vs. Object types…

int i, j;

i = 5;

j = i;

i++;

Sys… ( i, j);

Person me, x;

me = new Person( …);

x = me;

me.setComments( “nice!”);

Sys… ( me.getComments()

+ x.getComments(), );

Different

Same!

copy vs clone

myCd{CD}

yourQCd{CD}

favouriteCd{CD}

Copy vs. Clone

Title B.R.

Artist Queen

Date 1976

Length 3:50

Title B.R.

Artist Queen

Date 1976

Length 3:50

favouriteCd = myCd;

yourQCd = myCd.clone();

lost objects

aCd{CD}

aCd = null;

Lost objects
  • Java collects its garbage!

Title B.R.

Artist Queen

Date 1976

Length 3:50

Title Best of

Artist Genesis

Date 1983

Length 2:40

yourCd{CD}

myCd{CD}

myCd = yourCd;

parameter passing 1

main

xyz

a

i

b

Parameter Passing (1)
  • Primitive types…

public int xyz( int i) {

i++;

return i;

}

5

5

6

6

main

int a, b;

a = 5;

b = xyz( a);

Sys… ( a, b);

parameter passing 2

main

xyz

a

x

b

Parameter Passing (2)
  • Object types…

David221000“”

public Person xyz( Person x) {

x.setComments(“Nice);

return x;

}

Nice

main

Person a, b;

a = new Person( …);

b = xyz( a);

Sys… ( a.getComments()

+ b.getComments() );

parameter passing 3

main

xyz

a

x

b

Parameter Passing (3)
  • Object types…

David221000“”

Derya18500“”

public Person xyz( Person x) {

x = new Person( …);

x.setComments(“Nice);

return x;

}

Nice

main

Person a, b;

a = new Person( …);

b = xyz( a);

Sys… ( a.getComments()

+ b.getComments() );

static vs instance

Person

static

nameagesalarycomments

instance

David222000“Quiet”

Derya18500“Nice”

Gunes211500“Sunny”

Ayse251000“Happy”

a

b

c

d

Static vs. instance
  • Keep count of number of Person objects

count

0

4

3

1

2

all objects
All Objects…
  • automatically have
    • boolean equals( Object)
    • Object clone()
    • String toString()
  • BUT
    • they may not do what you would like/expect, so implement yourself!