internet software development

21
Internet Software Internet Software Development Development Object-Orientation and Object-Orientation and Java Java Paul J Krause Paul J Krause

Upload: sheila

Post on 06-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Internet Software Development. Object-Orientation and Java Paul J Krause. Object-Orientation & Java. Contents Getting Started A Little Bit of Syntax Differences between C and Java Object-Oriented Programming in Java. Getting Started. Goto: http://java.sun.com - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Internet Software Development

Internet Software Internet Software DevelopmentDevelopment

Object-Orientation and JavaObject-Orientation and Java

Paul J KrausePaul J Krause

Page 2: Internet Software Development

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 Object-Oriented Programming in JavaObject-Oriented Programming in Java

Page 3: Internet Software Development

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: Internet Software Development

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: Internet Software Development

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: Internet Software Development

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 Object-Oriented Programming in JavaObject-Oriented Programming in Java

Page 7: Internet Software Development

Defining a ClassDefining a Class

Account

numberbalance

credit_accountdebit_account

{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 8: Internet Software Development

““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 9: Internet Software Development

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 10: Internet Software Development

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 11: Internet Software Development

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 12: Internet Software Development

An AssociationAn Association

MakeCircle

main

Circle

radius

circumferencearea

creates instances of

Page 13: Internet Software Development

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 14: Internet Software Development

The Circle exampleThe Circle exampleC:\>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 15: Internet Software Development

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 Object-Oriented Programming in JavaObject-Oriented Programming in Java

Page 16: Internet Software Development

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 17: Internet Software Development

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 18: Internet Software Development

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 19: Internet Software Development

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 20: Internet Software Development

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

Page 21: Internet Software Development

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 Object-Oriented Programming in JavaObject-Oriented Programming in Java