introduction to java and object-oriented programming ajss computer camp department of information...

23
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Upload: noah-morgan

Post on 13-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Introduction to Java and Object-Oriented Programming

AJSS Computer CampDepartment of Information Systems

and Computer ScienceAteneo de Manila University

Page 2: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Outline Morning Session

Introduction to Java, OOP, and the BlueJ environment

Creating classes for the Robots project Afternoon Session

Graphics and Graphical User Interfaces in Java Demo: BombsAway application Creating your own game using the a game engine Demo: developing mobile apps within the

Android platformCopyright 2008, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L1: Intro Java

Slide 2

Page 3: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 3

The JavaProgramming Language

Java: an object-oriented programming language that is simple safe platform independent designed for the internet

Many universities use Java as the introductory programming language for beginning programmers Ateneo adopted Java for CS 21a in 1997

Page 4: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 4

Two types of Java programs:

Applications general-purpose programs standalone executed through the operating system

Applets programs meant for the WWW embedded in a Web page normally executed through a browser

Page 5: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 5

Simple Java Application

File: Hello.java

// Hello World application

public class Hello

{

public static void main( String args[] )

{

System.out.println( “Hello world” );

}

}

Page 6: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 6

The Programming Process

Create/EditProgram

CompileProgram

ExecuteProgram

Compile Errors? Run-Time Errors?

SourceProgram

ObjectProgram

Page 7: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 7

Creation, Compilation, and Execution

Create Java programC:\> edit Hello.java Hello.java file is created

Compile using javac (compiler)C:\> javac Hello.java Hello.class file is produced

Execute using java (interpreter)C:\>java Hello requires a Hello.class file

Page 8: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 8

Simple Java AppletFile: HelloAgain.java

import javax.swing.*;import java.awt.*;public class HelloAgain extends JApplet{ public void paint( Graphics g ) { g.drawString( “Hello”, 50, 50 ); }}

Page 9: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 9

Executing Applets

After compiling the java program:

Embed an “applet tag” in an .html document that references the .class file

Open the .html document using a browser or the appletviewer

Page 10: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 10

Sample .html Document

File: HA.html

<h1> My Sample Applet </h1><applet code="HelloAgain.class" height=200

width=100></applet>

Page 11: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 11

Java Program Structure

Java Program (optional) import declarations class declaration

Class class name should match its file name may extend an existing class

(such as JApplet) contains method/function declarations

Page 12: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 12

Object-Oriented Programming

The traditional definition of a program is:a sequence of instructions to be executedon a computer

In the Object-Oriented Programming (OOP) paradigm, a program that executes is a collection of interacting objects

In this paradigm, the programs we specify what are in these objects and how these objects behave

Page 13: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 13

So … What is an Object?

A “thing” that has type, identity, state, and behavior type: belongs to a “class” of similar objects identity: is a distinct “instance” of a class of

objects state / attributes: has a set of properties (aka

fields) each field can have different values

behavior: has “methods” (things that the object knows how to do)

we say we “call” a method on the object

Page 14: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 14

Examples of Objects

state/attributes on (true or false)

behavior switch on switch off check if on

state/attributes # of liters of gas in tank total # of km run so far efficiency (km/liter)

behavior drive load gas change efficiency check gas check odometer reading

LightBulb Car

BankAccount

state/attributes balance

behavior deposit withdraw check balance

Note each object is an

“instance” of that “type” of object

each instance has its own values for its attributes

e.g., different accounts can have different balances

Page 15: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 15

BankAccount example(A Preview)

BankAccount

state/attributes balance

behavior get balance deposit withdraw

BankAccount

double balance

double getBalance()void deposit( double amount )… and more

public class BankAccount{ private double balance = 0;

public double getBalance() { return balance; }

public void deposit( double amount ) { balance = balance + amount; } …}

BankAccount.java

Page 16: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 16

Class Definition in JavaBankAccount

type (or class)

state/attributes (fields)

behavior (methods) may have input parameters

in parenthesis may have output

(or “return”) type has “body” with code

public class BankAccount{ private double balance = 0;

public double getBalance() { return balance; }

public void deposit( double amount ) { balance = balance + amount; } …}

BankAccount.java

“double” means a floatingPoint number like1234.25

Page 17: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 17

A Class with a Constructor

public class BankAccount{ private double balance;

public BankAccount() { balance = 0; }

public double getBalance() { return balance; }

public void deposit( double amount ) { balance = balance + amount; } …}

BankAccount.java

Constructor: special method that handles initialization

For now, view constructors as an alternative to initializing fields as they are declared

Later: more advanced uses for constructors

Page 18: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 18

A Class and Its Instances

A single class can have multiple instances Each instance is a separate object Each instance can have different values for

its fields The definition of methods is the same for

all instances of the same type Thus, there is only one class definition

Written as the .java file for that class

Page 19: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 19

Lab Exercise: Try it in BlueJ

Open the project (ObjectExamples) that contains a BankAccount class (BankAccount.java)

Create BankAccount objects(instances of the class) Right-click on the class

Carry out operations on the BankAccount objects (invoke the deposit and getBalance methods) Right-click on the instances

Page 20: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 20

Lab Exercise, continued Instantiate and use a BankAccount

object within in a Java application How? Create a UseObjects class

(UseObjects.java) “New Class” on left pane of BlueJ

environment Inside the UseObjects class type the

following code…

Page 21: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 21

Lab Exercise, continuedpublic static void main( String args[] ){ BankAccount b = new BankAccount(); b.deposit( 1000.00 ); b.withdraw( 100.00 ); System.out.println( b.getBalance() ); b.deposit( 2000.00 ); System.out.println( b.getBalance() );}

Compile and execute UseObjects.java The values 900.0 and 2900.0 should be printed

out

Page 22: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Lab Exercise, continued Close the current BlueJ project Create a Robots project Create a Robot class moving along a two

dimensional maze Attributes: x and y coordinates (int type) Methods: moveLeft, moveRight, moveUp, moveDown

Test the class by creating instances within BlueJ Test the class by creating another class that uses

the Robot class—call it RobotTest

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 22

Page 23: Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved

L3: Intro to OOPSlide 23

Summary In Java, we write programs for objects These programs are called classes A class consists of fields and methods to

specify the state and behavior for its objects Once a class has been defined, objects of

that class can be created (instantiated) Methods are invoked on an object, and may

cause the state of the object to change