Docsity

JAVA Programming, Lecture Slides - Computer Science, Slides of Introduction to Computers

Partial preview of the text

Download JAVA Programming, Lecture Slides - Computer Science and more Slides Introduction to Computers in PDF only on Docsity!

Java

Ian Holyer

Ian Holyer

ASIDE on Java, slide 2

Java

ā—

Java was born in about 1995, which is getting old, see

http://java.sun.com/features/1998/05/birthday.html

ā—

Java became incredibly popular very quickly; it is

probably the most meteorically popular language ever

ā—

Java is now the example of an object-oriented

language

ā—

In this chapter, we will look at what OO programming

means, and how to program in a more OO way

ā—

In later chapters, we will look at design patterns

ASIDE on Java, slide 4

Mini Methods

ā—

In Java's library sorting algorithms, you can find this

kind of method, for readability and non-repetition

private void swap(X[] a, int i, int j)

{

X temp = a[i];

a[i] = a[j];

a[j] = temp;

}

private void swap(X[] a, int i, int j)

{

X temp = a[i];

a[i] = a[j];

a[j] = temp;

}

ā—

Wouldn't a lot of calls to this be horribly inefficient?

ā—

No, because any private method which is short or

called only once should be inlined by the compiler

ā—

So don't be afraid of doing this sort of thing yourself

ASIDE on Java, slide 5

Testing Programs

ā—

What if you want to use main to unit-test a program?

ā—

You also want to use main to run the program

ā—

One choice is to use java's -ea flag to indicate testing

ā—

You can then use assertions during testing

ā—

Here is the classic trick for testing the -ea flag

public static void main(String[] args)

{

boolean testing = false;

assert (testing = true); // Not ==

if (testing) doTests();

else runProgram();

}

public static void main(String[] args)

{

boolean testing = false;

assert (testing = true); // Not ==

if (testing) doTests();

else runProgram();

}

Object Oriented Programming

So what

So what is

is OO programming, really

OO programming, really

One good description is that it a combination of 4 ideas

One good description is that it a combination of 4 ideas

in a single system of classes and objects

in a single system of classes and objects

  • abstraction
  • modularity
  • reusability
  • delegation

The first two ideas are very old, pre-OO, and the third

The first two ideas are very old, pre-OO, and the third

one has been a "holy grail" for a long time, so it is

one has been a "holy grail" for a long time, so it is

really only the fourth that is new

really only the fourth that is new

Abstraction

This is a very old idea, about putting data and code

This is a very old idea, about putting data and code

together in lumps, with thin interfaces

together in lumps, with thin interfaces

The lumps have been called

The lumps have been called abstractions, abstract data

abstractions, abstract data

types, encapsulated types,

types, encapsulated types, opaque types, protected

opaque types, protected

types, modules, classes, ...

types, modules, classes, ...

It is the thin interfaces (APIs) which are important, so

It is the thin interfaces (APIs) which are important, so

that lumps appear simple from the outside

that lumps appear simple from the outside

In Java, a class

In Java, a class

is

is

an abstract data type

an abstract data type

If you make the fields private, a programmer

If you make the fields private, a programmer cannot

cannot

use a class in any other way than via its API

use a class in any other way than via its API

That means a class, even a library class, can be re-

That means a class, even a library class, can be re-

implemented without breaking any existing code

implemented without breaking any existing code

Reusability

This is about not reinventing the wheel all the time

This is about not reinventing the wheel all the time

In Java, there are several techniques which improve this

In Java, there are several techniques which improve this

In practice, the extensive standard platform independent

In practice, the extensive standard platform independent

libraries have probably had the most impact

libraries have probably had the most impact

Also, classes and objects encapsulate

Also, classes and objects encapsulate behaviour

behaviour in a

in a

way that allows it to be shared and adapted more easily

way that allows it to be shared and adapted more easily

Inheritance (extends) and interfaces (implements) are

Inheritance (extends) and interfaces (implements) are

the most obvious ways of reusing code

the most obvious ways of reusing code

Other ways, e.g. wrapping, composition, etc., we will

Other ways, e.g. wrapping, composition, etc., we will

look at under design patterns

look at under design patterns

Delegation

This is the most uniquely OO concept, so it is worth

This is the most uniquely OO concept, so it is worth

spending some time on

spending some time on

Roughly speaking, it is about putting the code next to

Roughly speaking, it is about putting the code next to

the data it operates on, and hiding the data

the data it operates on, and hiding the data

Even data hiding is not new, but OO takes it to

Even data hiding is not new, but OO takes it to

extremes with a different way of thinking

extremes with a different way of thinking

Objects know how to manage their own data, leading

Objects know how to manage their own data, leading

to an avoidance of interpretive-style monolithic code

to an avoidance of interpretive-style monolithic code

"Don't ask for the data you need to do the work; ask

"Don't ask for the data you need to do the work; ask

the object that has the data to do the work for you"

the object that has the data to do the work for you"

Roughly: make fields private, don't provide getters,

Roughly: make fields private, don't provide getters,

setters, write programs where data transfer is minimal

setters, write programs where data transfer is minimal

Example: Money

This is adapted from a JavaWorld article by Holub

This is adapted from a JavaWorld article by Holub

Suppose you define a

Suppose you define a

Money

Money

class holding an amount

class holding an amount

public class Money

{

public double amount;

}

public class Money

{

public double amount;

}

Your code becomes littered with lines like this:

Your code becomes littered with lines like this:

double total = 0;

total = total + m.amount;

double total = 0;

total = total + m.amount;

Now what happens if you decide to switch from double

Now what happens if you decide to switch from double

to int? All this scattered code breaks

to int? All this scattered code breaks

More Money

There is a strong body of opinion now that all fields

There is a strong body of opinion now that all fields

should be private, but what about getters, setters?

should be private, but what about getters, setters?

public class Money

{

private double amount;

public double getAmount()

{

return amount;

}

}

public class Money

{

private double amount;

public double getAmount()

{

return amount;

}

}

Now you can maintain a method that returns a double,

Now you can maintain a method that returns a double,

even when you decide internally to switch to int

even when you decide internally to switch to int

What happens when you go international? Maybe you

What happens when you go international? Maybe you

add a currency field and

add a currency field and getCurrency()

getCurrency() method.

method.

Anthropomorphisation

There is a technique for doing OO design called CRC

There is a technique for doing OO design called CRC

cards (Classes, Responsibilities, Collaborators)

cards (Classes, Responsibilities, Collaborators)

It is done with real cards, not on the computer, because

It is done with real cards, not on the computer, because

it is a human social activity

it is a human social activity

Role playing is used to think through situations, and

Role playing is used to think through situations, and

about who should be responsible for doing what

about who should be responsible for doing what

One of the key points is to think of objects like people

One of the key points is to think of objects like people

(see title!), so in the Money example:

(see title!), so in the Money example:

Who should be responsible for amounts? Money objects!

Who should be responsible for currencies? Money objects!

Who should be responsible for conversion? Money objects!

Who should be responsible for adding? Money objects!

Money Laundering

So what should the code in your program look like?

So what should the code in your program look like?

Money total; Money class has no

Money m; getAmount/getCurrency

... or they are used very

total.add(m); very sparingly

Money total; Money class has no

Money m; getAmount/getCurrency

... or they are used very

total.add(m); very sparingly

With code like this, you can hide currency and

With code like this, you can hide currency and

conversion inside the Money class where it belongs

conversion inside the Money class where it belongs

If the code was like this

If the code was like this from the start

from the start , changes to

, changes to

make it international would have been mostly

make it international would have been mostly

inside

inside

the Money class

the Money class

Don't ask objects for data, ask them to do things

Don't ask objects for data, ask them to do things "

Don't Copy Library Code

Don't copy blindly what you see in the Java libraries; it

Don't copy blindly what you see in the Java libraries; it

is not that the code is bad, but:

is not that the code is bad, but:

The code contains a lot of historical accident and

The code contains a lot of historical accident and

backward compatibility, e.g. the Date class in java.util

backward compatibility, e.g. the Date class in java.util

couldn't be internationalised, so it now contains just a

couldn't be internationalised, so it now contains just a

timestamp, and the day, week, month, national stuff

timestamp, and the day, week, month, national stuff

has moved to the Calendar class, and the parse/print

has moved to the Calendar class, and the parse/print

stuff has moved to the DateFormat class, but this isn't

stuff has moved to the DateFormat class, but this isn't

the way the classes should be designed if starting again

the way the classes should be designed if starting again

Library classes have a lot of get/set methods, to make

Library classes have a lot of get/set methods, to make

them exceptionally customisable, so users don't have to

them exceptionally customisable, so users don't have to

extend/replace them, but you wouldn't want to expose

extend/replace them, but you wouldn't want to expose

that much of the inner workings in your own classes

that much of the inner workings in your own classes

Example: GUIs

The Money example is tiny, but in bigger cases, it is

The Money example is tiny, but in bigger cases, it is

really easy to drift back into monolithic style, and

really easy to drift back into monolithic style, and

really difficult to think consistently in an OO way

really difficult to think consistently in an OO way

Let's look at designing GUIs (again, from Holub)

Let's look at designing GUIs (again, from Holub)

Let's suppose the code and data can be divided in two:

Let's suppose the code and data can be divided in two:

  • GUI stuff, to do with graphics, interaction etc
  • Logic stuff (often called 'business logic')

What are the main design choices for the overall

What are the main design choices for the overall

structure of the program? There are three obvious ones

structure of the program? There are three obvious ones