Java and OO Programming Workshop - Introduction

farrightΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 10 μήνες)

276 εμφανίσεις

TEACHING WORKSHOP

INTRODUCTION TO JAVA

AND

OBJECT ORIENTED
PROGRAMMING

Gillian Miller

Department Of Computing

Macquarie University

Copyright 2003 Macquarie University

These slides may be used for any non
-
commercial purpose.

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

2

Course Content

Learning Programming in JAVA

On the way understand OO programming


Enough techniques and ideas for you to get started
programming in Java

From simple programs to objects to applets and GUIs
-

the
building blocks for applications such as a simple game or
animation

Useful tools


Ambitious for a weekend !

Format

Lecture + Hands
-
On + Exercises for later self
-
study

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

3

What we will cover

Session 1
-

Java Language Basics


Introduction


Getting Started


Basic Functions


BlueJ

Session 2
-

Thinking in Objects


Introduction to OO thinking


Java Objects and Classes


Exceptions and console IO

Session 3
-

Advanced OO and GUIS


Inheritance, Polymorphism


Swing and Applets


Java API’s

Session 4
-

More Advanced Java


Graphics Programming


Threads


Introduction to NetBeans



Check
backgrounds

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

4

What is Object Oriented (OO)

The
World

is made of “
OBJECTS



We are looking to solve real
World

problems


OO Software Engineering Paradigm

Set of techniques and processes

How to analyse and model a real world problem and
design and develop a solution



OO Technology

Tools and support for carrying out OO Software
Engineering

OO Programming Language

Programming model that supports the OO Model


Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

5

Structured Programming

Top Down


Step wise refinement

Solves all of problem in one program

Break down into hierarchy of modules

1.
INITIALISE

2.
MAIN PROCESS A

2.1 PROCEDURE
-
A

2.3


PROCEDURE
-
B

2.3.1 Function A

2.3.2 Procedure B



Function B1



Subroutine B2

65. FINALISE

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

6

Window

Icon

Button

Network Socket

DB Connection

Authorised Clerk

Student

Enrolment

Student Application

TextArea

Objects



real world things

Arrows



messages for tasks and activities

OOP

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

7

Paradigm Shift

Real
World

DATA

first


Not processing first

In real world, algorithms and processes change

Underlying data objects remain stable


Intended benefits

Solve the “software” crisis

Break complexity into small manageable chunks

Make software maintenance easier

Allow reuse of components


plug and play

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

8

So what is an object ?

AN OBJECT IS A


Any “thing” can be an object

Concrete things


person, car, student

Abstract things


meeting, enrolment, registration

System artefacts


window, textbox, db connector


Rule of thumb


look for the nouns


Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

9

OO Languages

Started with Simula
-
67 around 1970


Then in no particular order

SmallTalk

Eiffel

Objective C

Object Pascal

C++

Java


OO also having impact on software engineering design
methodologies (UML)
-

databases (OO and Object
Relational)

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

10

What is JAVA ?

Object
-
Oriented Language

C++ like Syntax but improves C++ weaknesses


Logical and easy to program

Designed for any device including toasters
(Oak, Gosling)

Formally announced by SUN in 1995

Big industry push by Sun, Oracle, Netscape,
IBM

Huge API for industrial strength programming

Took off with WEB


now 2.5 million
developers worldwide


Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

11

Overwhelming Alphabet soup

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

12

Undaunted
-

Lets do Java

Tools

Computer, internet, monitor (Windows)

YES YOU CAN DO THIS FROM HOME

Text editor (eg Notepad)

A Java Development Kit ()


Java 2 SDK

Java compiler


javac.exe

Java VM


java.exe


More advanced environments

BlueJ
-

Later

Commercial IDE’s


Netbeans IDE

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

13

Let Us Get Started

filename
.java

Comments
/* */

classname

main

(will explain later)


Print to console

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

14

Running from the DOS window


filename.java

javac
filename
.java





filename.class

java
filename



path

java
-
version



Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

15

Your Turn

Practical 1


Task 1

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

16

Java Errors

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

17

IDE

It is inevitable that we will make errors !


Integrated Development Environment

Environment to help coding and compiling

IDE will help pin point errors

IDE will have multiple windows


one for editing,
one for errors

TypeAheads, online help, output windows, library
configurations …

Simple IDE

BlueJ

Professional IDE

NetBeans, JBuilder ….


Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

18

BLUEJ

Start
-
> Programs
-
> BlueJ

Menu
-
> Project
-
> NonBlueJ Project

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

19

BlueJ 2

Double Click HelloWorld

Press Compile

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

20

BlueJ
-

Running

1 From editting window, press Close

2 RightMouseClick HelloWorld


3. Click OK on MethodCall Dialog Box

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

21

BlueJ


Output Window

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

22

Your Turn

Getting Used to BlueJ

Practical 1


Task 2

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

23

Java Programming

Usual Programming Constructs


Variables and types


Statements


If then Else constructs


While …..


Methods

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

24

Java Variable Data Types

String

char

int

float

boolean

double

variabletype varname;

variabletype varname = initvalue;

varname = newvalue
;


Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

25

Variables1.Java

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

26

Results

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

27

Java Basic Constructs

if (cond) {


}

i1 = i2 + i3 ;

i6 = ( (i1 + i2) / i3 )


i4 ;

i1 = i2 * i3 ;

if (cond) {


….

}

else {




}

Arithmetic expressions


If statements


While constructs


while (cond) {


}

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

28

Simple Program

public class SimpleProg1 (


public static void main ( String args[ ] ) {




int j = 1;


boolean more = true;


int jsquare;


while ( more ) {



jsquare = j * j;



System.out.println ( “Square is “ + jsquare);



j = j + 1;



if ( j > 10 ) {




more = false;



}


}


}


Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

29

Simple Program 2

public class SimpleProg2 (


public static void main ( String args[ ] ) {

//

// typical Java programming in professional style

//



for (int j = 1; j <= 10; j++)



System.out.println ( “Square is “ + j *j );




}

}


Implied string concatenation

for

(
start; cond; expr
) {

}

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

30

Constructs

if (
booleancond

) { …

}

else

{ ….

}


while (
cond
) { ..


}

for (
init
;
cond
;
expr
) {


}

switch (
intname
) {

case 1: … ; break;

case 2: …. ; break;


default: ..

}

if (i < j)

if (! done)

if (x == y)

if (a && b)

if (a || b)



Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

31

Methods
-

Calling

public class Method1 (


public static void main ( String args[ ] ) {

//


System.out.println ( “Hello From Main Method “);


myFirstMethod ();


myFirstMethod ();

}


public static void myFirstMethod () {


System.out.println (“Hello from the method call “);

}

}


Call to method

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

32

Methods


Calling 2

public class Method2 (


public static void main ( String args[ ] ) {

//


System.out.println ( “Hello From Main Method “);


helloMethod ( “Gillian “);


helloMethod (“Victoria”);


helloMethod (“Teachers “);

}


public static void helloMethod (String arg) {


System.out.println (“Hello “ + arg);

}

}


Call to method with argument

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

33

Methods

public class CircArea (


public static void main ( String args[ ] ) {

//


for (int j = 1; j <= 10; j++)



System.out.println ( “Circle Area for radius “ + j +




“ is “ +
circleArea

(j) );

}


public static double
circleArea

(int radius) {


return radius * radius * Math.PI ;


}

}


Call to method

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

34

Your Turn

Practical 1


Task 3

Gillian Miller

Java Teacher Workshop :

M慣qu慲楥i啮楶敲獩瑹

35

Summary


Session 1


Introduction to OO as paradigm shift

Java background

Hello World

BlueJ

Basic Types

Basic Constructs

Methods