object-oriented software engineering

28
Object-Oriented Object-Oriented Software Engineering Software Engineering Object-Orientation and Java Object-Orientation and Java

Upload: buzz

Post on 07-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Object-Oriented Software Engineering. Object-Orientation and Java. Object-Orientation & Java. Contents Getting Started A Little Bit of Syntax Differences between C and Java. Getting Started. Goto: http://java.sun.com Download the Java Software Development Kit (SDK) Its free! - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object-Oriented Software Engineering

Object-Oriented Software Object-Oriented Software EngineeringEngineering

Object-Orientation and JavaObject-Orientation and Java

Page 2: Object-Oriented Software Engineering

22UniS

Object-Orientation & JavaObject-Orientation & Java

ContentsContents Getting StartedGetting Started A Little Bit of SyntaxA Little Bit of Syntax Differences between C and JavaDifferences between C and Java

Page 3: Object-Oriented Software Engineering

33UniS

Getting StartedGetting Started

Goto:Goto:http://java.sun.comhttp://java.sun.com

Download the Java Software Development Kit Download the Java Software Development Kit (SDK)(SDK) Its free!Its free!

Download the DocumentationDownload the Documentation Also free! (Also free! (http://java.sun.com/docs/books/tutorial)http://java.sun.com/docs/books/tutorial)

Buy Buy JAVA In a NutshellJAVA In a Nutshell, by David Flanagan, , by David Flanagan, Publ. O’RiellyPubl. O’Rielly It’ll cost you, sorry!It’ll cost you, sorry!

Page 4: Object-Oriented Software Engineering

44UniS

File extensions in JavaFile extensions in Java

.java Source

Byte code.class

javac (compiler)

JVM Java Virtual Machine

Any Hardware (that supports the JVM)

Page 5: Object-Oriented Software Engineering

55UniS

What you get in the JDKWhat you get in the JDK

appletviewerappletviewer For running AppletsFor running Applets

javacjavac Compiles .java Compiles .java .class .class

javajava Interprets a Java ClassInterprets a Java Class

classes.zipclasses.zip The system provided classesThe system provided classes

src.zipsrc.zip Complete source for standard Complete source for standard classesclasses

javadocjavadoc Generates Java HTML documentsGenerates Java HTML documents

… …

……

Page 6: Object-Oriented Software Engineering

66UniS

JDK Swing BasicsJDK Swing Basics

Java JDK provides a collection of Foundation classes to allow easy implementation of GUI-s (Graphical User Interface)

For this lecture borrowed heavily from:http://java.sun.com/docs/books/tutorial/information/

 Top-Level Containers

               

Applet

                                                

Dialog

                                 

Frame

Page 7: Object-Oriented Software Engineering

77UniS

JDK Swing BasicsJDK Swing Basics

General-Purpose Containers

                                    

Panel

                                       

Scroll pane

                                                   

Split pane

                                         

Tabbed pane

                        Tool bar

Page 8: Object-Oriented Software Engineering

88UniS

JDK Swing BasicsJDK Swing Basics

Special-Purpose Containers

                                              

Internal frame

                                                       

Layered pane

                                                               

Root pane

Page 9: Object-Oriented Software Engineering

99UniS

JDK Swing BasicsJDK Swing Basics

Basic Controls  

                       

Buttons

                               

Combo box

                   

List

                                     

Menu

                                            

Slider

       Spinner

                   Text field or Formatted text field

Page 10: Object-Oriented Software Engineering

1010UniS

JDK Swing BasicsJDK Swing Basics

Uneditable Information Displays

                              

Label

                           

Progress bar

                               

Tool tip

Page 11: Object-Oriented Software Engineering

1111UniS

JDK Swing BasicsJDK Swing Basics

Interactive Displays of Highly Formatted Information  

                                

Color chooser

                                                      

File chooser

                                         

Table

                   

Text

                      

Tree

Page 12: Object-Oriented Software Engineering

1212UniS

Object-Orientation & JavaObject-Orientation & Java

ContentsContents Getting StartedGetting Started Some SyntaxSome Syntax Differences between C and JavaDifferences between C and Java

Page 13: Object-Oriented Software Engineering

1313UniS

Defining a ClassDefining a Class

Account

numberbalance

creditdebit

{membersfields

methods

public class Account {

public int number; public double balance;

public void credit(double x) { // do some sums } public void debit(double y) { // do checking then sums }}

Page 14: Object-Oriented Software Engineering

1414UniS

““Circle” ExampleCircle” Example

Circle

radius

circumferencearea

public class Circle {

}

public double radius;

public double circumference() { return 2 * PI * radius;}public double area() { return PI * radius * radius;}

public static final double PI = 3.14159;

Page 15: Object-Oriented Software Engineering

1515UniS

The “Circle” classThe “Circle” class

public class Circle {

// A class field public static final double PI= 3.14159; // A useful constant // A class method: just compute a value based on the arguments public static double radiansToDegrees(double rads) { return rads * 180 / PI; } // An instance field public double r; // The radius of the circle

// Two instance methods: they operate on the instance fields // of an object public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; }}

Page 16: Object-Oriented Software Engineering

1616UniS

The “main” methodThe “main” method

public class Circle { public double r; // The radius of the circle public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; }

}

public static void main(String[] args) { int input = Integer.parseInt(args[0]); Circle c = new Circle(); c.r = input; double result = c.circumference(); System.out.println(result); }

Page 17: Object-Oriented Software Engineering

1717UniS

The “main” methodThe “main” method

int input = Integer.parseInt(args[0]);

Call to Integer Class

Integer Class Static Method

parameters to method call

Page 18: Object-Oriented Software Engineering

1818UniS

Put “main” in a new class?Put “main” in a new class?

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

int input = Integer.parseInt(args[0]);

Circle c = new Circle(); c.r = input; double circum = c.circumference(); System.out.println(circum); double a = c.area(); System.out.println(a);

}}

Page 19: Object-Oriented Software Engineering

1919UniS

An AssociationAn Association

MakeCircle

main

Circle

radius

circumferencearea

creates instances of

Page 20: Object-Oriented Software Engineering

2020UniS

Make Circle ExecutionMake Circle Execution

Java Interpreter

:MakeCircle<<creates>>

Circle c = new Circle();

main([“34”])

circumference()

area()

c.r = input;

c.circumference();

c.area();

r

<<creates>> c :Circle

java MakeCircle 34

Page 21: Object-Oriented Software Engineering

2121UniS

File extensions in JavaFile extensions in Java

.java Source

Byte code.class

javac (compiler)

JVM Java Virtual Machine

Any Hardware (that supports the JVM)

Page 22: Object-Oriented Software Engineering

2222UniS

The Circle exampleThe Circle example

C:\>cd Java

C:\Java>javac Circle.java

C:\Java>javac MakeCircle.java

C:\Java>java MakeCircle 4

25.1327250.26544

C:\Java>java MakeCircle 5

31.415978.53975

C:\Java contains Circle.java and MakeCircle.java

C:\Java now also contains Circle.class and MakeCircle.class

Page 23: Object-Oriented Software Engineering

2323UniS

Object-Orientation & JavaObject-Orientation & Java

ContentsContents Getting StartedGetting Started A Little Bit of SyntaxA Little Bit of Syntax Differences between C and JavaDifferences between C and Java

Page 24: Object-Oriented Software Engineering

2424UniS

DifferencesDifferences

No PreprocessorNo Preprocessor No analogues of No analogues of #define#define, , #include#include, , #ifdef#ifdef

Constants are replaced by Constants are replaced by static finalstatic final fieldsfields

No Global VariablesNo Global Variables Avoids possibility of namespace collisionsAvoids possibility of namespace collisions We will see later how you can make a We will see later how you can make a

constant or variable globally accessibleconstant or variable globally accessible

Page 25: Object-Oriented Software Engineering

2525UniS

Java vs. CJava vs. C

Well-defined primitive type sizesWell-defined primitive type sizes Removes this as a platform dependencyRemoves this as a platform dependency

No pointersNo pointers Although Java Classes and Arrays are Although Java Classes and Arrays are

reference types, these references are reference types, these references are “opaque”. No “address of” or “dereference” “opaque”. No “address of” or “dereference” operatorsoperators

This is This is notnot a handicap and eliminates an a handicap and eliminates an important source of bugsimportant source of bugs

Page 26: Object-Oriented Software Engineering

2626UniS

Java vs. CJava vs. C

Garbage CollectionGarbage Collection Objects are “tidied away” as soon as there are Objects are “tidied away” as soon as there are

no further references to themno further references to them So, no need to explicitly manage memorySo, no need to explicitly manage memory Eliminates memory leaksEliminates memory leaks

No goto statementNo goto statement Adds exception handling and labelled Adds exception handling and labelled breakbreak

and and continuecontinue statements statements

Page 27: Object-Oriented Software Engineering

2727UniS

Java vs. CJava vs. C

Variable declarations anywhereVariable declarations anywhere Java allows local variable definitions to be Java allows local variable definitions to be

made anywhere in a method or blockmade anywhere in a method or block Good practice to group them, thoughGood practice to group them, though

Forward referencesForward references Methods can be invoked before they are Methods can be invoked before they are

defined (we’ll see why it is important to be defined (we’ll see why it is important to be able to do this)able to do this)

Page 28: Object-Oriented Software Engineering

2828UniS

Java vs. CJava vs. C

Method overloadingMethod overloading Multiple methods can be defined with the same name, Multiple methods can be defined with the same name,

so long as they have different parameter listsso long as they have different parameter lists

No struct and union typesNo struct and union types No enumerated typesNo enumerated types No bitfieldsNo bitfields No typedefNo typedef No method pointersNo method pointers No variable-length argument listsNo variable-length argument lists