31
DON’T PANIC!! Lots of new notions coming in these slides Don’t worry if not all of it makes perfect sense We’ll meet most of this stuff again in detail later Do worry if none of it makes any sense You should get the general picture now Now brace yourself … stop me if you get confused … ask questions … throw money …

DON’T PANIC!!

  • Upload
    lacey

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

DON’T PANIC!!. Lots of new notions coming in these slides Don’t worry if not all of it makes perfect sense We’ll meet most of this stuff again in detail later Do worry if none of it makes any sense You should get the general picture now - PowerPoint PPT Presentation

Citation preview

Page 1: DON’T PANIC!!

DON’T PANIC!!

Lots of new notions coming in these slides

Don’t worry if not all of it makes perfect sense

We’ll meet most of this stuff again in detail later

Do worry if none of it makes any senseYou should get the general picture now

Now brace yourself … stop me if you get confused … ask questions … throw money …

Page 2: DON’T PANIC!!

Programs and Classes

A program is made up from classes

Classes may be grouped into packages

A class has two parts

static parts exist independently Non-static parts define what objects in the class look like.

Every class is automatically in existence when the program runs.

Page 3: DON’T PANIC!!

Classes and Objects

An object is an instance of a class, and is created using the new operator.

The non-static part of the class defines what each object looks like.

Many instances (objects) can be created from a class … no limit except reality

An object contains information and functionality of a “thing”, e.g., Account, Vehicle, Employee, etc.

Page 4: DON’T PANIC!!

Classes’ and Objects’ Components

Classes (and thus also objects) are composed of methods and data values

Data values store information

Methods do things, and also have their own local data

Page 5: DON’T PANIC!!

Graphical Representation

AccountThe class name appears on top of the icon.

The class name appears on top of the icon.

An icon for a class is the rectangle.

An icon for a class is the rectangle.

Account

SV129

The class name is placed inside the object icon.

The class name is placed inside the object icon.

The object’s name appears on top of the icon.

The object’s name appears on top of the icon.

An icon for an object is the rounded rectangle.

An icon for an object is the rounded rectangle.

Page 6: DON’T PANIC!!

Instance-of Relationship

Employee

Employee

Bill

Employee

Steve

Employee

AndyThe class name can be omitted since it is clear which class these objects belong to .

The class name can be omitted since it is clear which class these objects belong to .

The dotted line shows the instance-of relationship.

The dotted line shows the instance-of relationship.

Before you can create instances of a class, the class must be defined.

Before you can create instances of a class, the class must be defined.

Page 7: DON’T PANIC!!

Visibility Modifiers: public and private

The modifiers public and private designate the accessibility of objects’ and class’ data values and methods

If a component is declared private, nothing outside the class can access it.

If a component is declared public, anything outside the class can access it.

Page 8: DON’T PANIC!!

In general, be private (military demotion)

Make class components private whenever you can

This supports the notion of encapsulation, which makes for more robust software development

Page 9: DON’T PANIC!!

Class and Instance Data Values

A class data value (indicated by the static modifier) is used to maintain information shared by all instances or aggregate information about the instances.

An instance data value is used to maintain information specific to individual instances.

Make instance data values private always

Page 10: DON’T PANIC!!

Sample Data Values

Account

SV129

Account

SV506

Account

SV008

current balance current balance current balance908.55 1304.98 354.00

Account

minimum balance

100.00

There is one copy of minimum balance for the whole class and shared by all instances.

There is one copy of minimum balance for the whole class and shared by all instances.

All three Account objects possess the same instance data value current balance.

All three Account objects possess the same instance data value current balance.

Page 11: DON’T PANIC!!

Primitive and Reference Data Values

Primitive variables contain values

Reference variables point at objects

byte short

intdouble

long

floatboolean

String

AppletMessageBox

HiLoInputBox

etc.

char

primitive reference

Data Type

Page 12: DON’T PANIC!!

Variable and Constant Data Values

There are two types of data values:

Account

Account

SV129

minimum balance

100.00current balance

908.55

account prefix

6427opening balance

100.00A constant whose value must remain fixed over time.

A constant whose value must remain fixed over time.

A variable whose value can change over time.

A variable whose value can change over time.

Constants are indicated by the final modifierNon-final public class data values will give you warts

Page 13: DON’T PANIC!!

Methods

Methods have code (to do stuff) and data

A method defined for a class is called a class method (indicated by the static modifier) and a method defined for an object is called an instance method.

Page 14: DON’T PANIC!!

Method Data = Local Variables

A local variable is a variable that is declared within a method.

Local variables are accessible only in the method in which they are declared.

The final and static modifiers do useful things to local variables - more about this later

Page 15: DON’T PANIC!!

Messages

To instruct a class or an object to do something, we a message to one of its methods

Values passed to a method when sending a message are called arguments or parameters of the message.

The (formal) parameters of a method are local variables that receive the message parameters

Methods can return one data value to the calling method

Page 16: DON’T PANIC!!

Sending a Message

Message deposit with the argument 250.00 is sent to chk-008.

Message deposit with the argument 250.00 is sent to chk-008.

Account

chk-008

depositdeposit 250.00

Message name is usually omitted in the diagram.

Message name is usually omitted in the diagram.

deposit250.00

Page 17: DON’T PANIC!!

Getting an Answer

This message has no argument.

This message has no argument.

Account

chk-008

getMonthlyFee

monthly fee

The method returns the value monthly fee back to the message sender.

The method returns the value monthly fee back to the message sender.

Page 18: DON’T PANIC!!

Calling a Class Method

Account

getAverageBalance

average balance

The average balance of all accounts is returned.

The average balance of all accounts is returned.

Page 19: DON’T PANIC!!

Program Components

A Java file is composed of

comments,

import statements, and

class declarations.

Page 20: DON’T PANIC!!

// Program MyFirstApplication

/*This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen.

*/

import javabook.*;

public class MyFirstApplication {public static void main(String[ ] args) {

MainWindow mainWindow;

mainWindow = new MainWindow();

mainWindow.setVisible( true );}

}

Program Component: Comment

CommentComment

Page 21: DON’T PANIC!!

Program Component: Import Statement

// Program MyFirstApplication

/*This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen.

*/

import javabook.*;

public class MyFirstApplication {public static void main(String[ ] args) {

MainWindow mainWindow;

mainWindow = new MainWindow();

mainWindow.setVisible( true );}

}

Import StatementImport Statement

Page 22: DON’T PANIC!!

Import Statement Syntax and Semantics

<package name> . <class

name> ;

e.g. javabook . InputBox;

MoreExamples

import javabook.*;import java.awt.image.ColorModel;import com.drcaffeine.galapagos.*;

Package NameName of the package that contains the classes we want to use.

Package NameName of the package that contains the classes we want to use.

Class NameThe name of the class we want to import. Use asterisks to import all classes.

Class NameThe name of the class we want to import. Use asterisks to import all classes.

Page 23: DON’T PANIC!!

Program Component: Class Declaration

// Program MyFirstApplication

/*This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen.

*/

import javabook.*;

public class MyFirstApplication {

public static void main(String[ ] args) {

MainWindow mainWindow;

mainWindow = new MainWindow();

mainWindow.setVisible( true );}

}

Class DeclarationClass Declaration

Page 24: DON’T PANIC!!

Program Component: Method Declaration

// Program MyFirstApplication

/*This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen.

*/

import javabook.*;

public class MyFirstApplication {

public static void main(String[ ] args) {

MainWindow mainWindow;

mainWindow = new MainWindow();

mainWindow.setVisible( true );}

}

Method DeclarationMethod Declaration

Page 25: DON’T PANIC!!

Method Declaration Elements

public static void main ( String[ ] args )

{

MainWindow mainWindow;

mainWindow = new MainWindow();

mainWindow.setVisible( true );

}

ModifierModifier ModifierModifier Return TypeReturn Type Method NameMethod Name ParameterParameter

Method BodyMethod Body

Page 26: DON’T PANIC!!

Statements

Method bodies contain statements

Simple statements end with a ;

Compound statements are enclosed in {}s

public static void main (String[] args) {

int someData = 0;

if (someData == 27) { System.out.println(“Cosmic rays!”); someData = 0; }}

Page 27: DON’T PANIC!!

Sample Method

public double fromDollar( double dollar )

{

double amount, fee;

fee = exchangeRate - feeRate;

amount = dollar * fee;

return amount;

}

ParameterParameter

Local Variables

Local Variables

Page 28: DON’T PANIC!!

Files and Classes

A Java program file ends with .java

There must be one public class per file

It must have the same name as the file

One public class (i.e., one file) must have the main method

Page 29: DON’T PANIC!!

Simple Java Programs

Simple Java programs can be written in just the one file, containing

One public class (with the main method)

Other class methods and final data values as required

Such programs do not create any objects, but simply run class methods (starting with the main method) and use primitive data.

Page 30: DON’T PANIC!!

Template for Simple Java Applications

center of the screen, and the size of the window

is almost as big as the screen.

*/

import javabook.*;public class MyFirstApplication{

public static void main(String[ ] args){

MainWindow mainWindow;

mainWindow = new MainWindow();

mainWindow.setVisible( true );}

}

CommentComment

Import Statements

Import Statements

Class NameClass Name

Method BodyMethod Body

Page 31: DON’T PANIC!!

DON’T PANIC!!

We’ll write some programs without creating objects, i.e., you’ll have to think about only classes (and their methods and data values)

We’ll write some programs that create objects from pre-existing classes, i.e., you’ll use objects before you have write code to define them.

Then we’ll write programs that define and create their own objects