Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.

Similar presentations


Presentation on theme: "Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause."— Presentation transcript:

1 Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause

2 Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

3 Getting Started  Goto: http://java.sun.com  Download the Java Development Kit (JDK) Its free! Its free!  Download the Documentation Also free! Also free!  Buy JAVA In a Nutshell, by David Flanagan, Publ. O’Rielly It’ll cost you £19.95, sorry! It’ll cost you £19.95, sorry!

4 File extensions in Java.java Source Byte code.class javac (compiler) JVM Java Virtual Machine Any Hardware (that supports the JVM)

5 What you get in the JDK appletviewer For running Applets javac Compiles.java .class java Interprets a Java Class classes.zip The system provided classes src.zip Complete source for standard classes javadoc Generates Java HTML documents javapDisassembler …

6 Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

7 Defining a Class Account number balance credit_account debit_account { members fields 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 }

8 “Circle” Example Circle radius circumference area 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;

9 The “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; }

10 The “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); }

11 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); }

12 An Association MakeCircle main Circle radius circumference area creates instances of

13 File extensions in Java.java Source Byte code.class javac (compiler) JVM Java Virtual Machine Any Hardware (that supports the JVM)

14 The Circle example C:\>cd Java C:\Java>javac Circle.java C:\Java>javac MakeCircle.java C:\Java>java MakeCircle 4 25.13272 50.26544 C:\Java>java MakeCircle 5 31.4159 78.53975 C:\Java contains Circle.java and MakeCircle.java C:\Java now also contains Circle.class and MakeCircle.class

15 Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

16 Differences  No Preprocessor No analogues of #define, #include, #ifdef No analogues of #define, #include, #ifdef Constants are replaced by static final fields Constants are replaced by static final fields  No Global Variables Avoids possibility of namespace collisions Avoids possibility of namespace collisions We will see later how you can make a constant or variable globally accessible We will see later how you can make a constant or variable globally accessible

17 Java vs. C  Well-defined primitive type sizes Removes this as a platform dependency Removes this as a platform dependency  No pointers Although Java Classes and Arrays are reference types, these references are “opaque”. No “address of” or “dereference” operators Although Java Classes and Arrays are reference types, these references are “opaque”. No “address of” or “dereference” operators This is not a handicap and eliminates and important source of bugs This is not a handicap and eliminates and important source of bugs

18 Java vs. C  Garbage Collection Objects are “tidied away” as soon as there are no further references to them Objects are “tidied away” as soon as there are no further references to them So, no need to explicitly manage memory So, no need to explicitly manage memory Eliminates memory leaks Eliminates memory leaks  No goto statement Adds exception handling and labelled break and continue statements Adds exception handling and labelled break and continue statements

19 Java vs. C  Variable declarations anywhere Java allows local variable definitions to be made anywhere in a method or block Java allows local variable definitions to be made anywhere in a method or block Good practice to group them, though Good practice to group them, though  Forward references Methods can be invoked before they are defined (we’ll see why it is important to be able to do this) Methods can be invoked before they are defined (we’ll see why it is important to be able to do this)

20 Java vs. C  Method overloading Multiple methods can be defined with the same name, so long as they have different parameter lists Multiple methods can be defined with the same name, so long as they have different parameter lists  No struct and union types  No enumerated types  No bitfields  No typedef  No method pointers  No variable-length argument lists

21 Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

22 The Members of a Class  Class fields public static final double PI = 3.1416; public static final double PI = 3.1416;  Class methods public static double radiansToDegrees(double rads) { public static double radiansToDegrees(double rads) { … }  Instance fields public double radius; public double radius;  Instance methods public double circumference() {…} public double circumference() {…}

23 Class Fields public static final double PI = 3.14159  A field of type double  Named PI (capitalise constants)  Assigned a value of 3.14159  The static modifier tags this as a Class Field Associated with the class in which it is defined Associated with the class in which it is defined  The final modifier means it cannot be changed

24 Class Fields…  There is only one copy of PI  Any instance of Class can refer to this field as PI  PI is essentially a Global Variable BUT  Methods that are not part of Circle access this as Circle.PI No name collisions No name collisions

25 Class Methods public static double radiansToDegrees(double rads) { return rads * 180 / PI; return rads * 180 / PI; }  Single parameter of type double and returns a value of type double  Is essentially a “global method” // how many degrees is 2.0 radians? double d = Circle.radiansToDegrees(2.0);

26 Instance Fields public double radius;  Each Circle object can have a have a radius independent of other Circle objects  Outside a class, a reference to an instance field must be prepended by a reference to the object that contains it Circle c = new Circle(); c.radius = 2.0; Circle d = new Circle(); d.radius = c.radius; Are they the same object?

27 Instance Methods  Instance methods operate on instances of a Class, and not on the Class itself  E.g. area() area() circumference() circumference()  If an instance method is used from outside the Class itself, it must be prepended by a reference to the instance to be operated on: Circle c = new Circle(); Circle c = new Circle(); c.radius = 2.0; c.radius = 2.0; double a = c.area(); double a = c.area();

28 Creating an Instance  Every Class has at least one constructor  This is used as a default constructor - a method with the same name as the Class Circle c = new Circle();  The new operator creates a new uninitialised instance of the Class  The constructor method is then called, with the new object passed implicitly

29 Initialising an Instance  A Constructor can use arguments placed between the parentheses to perform initialisation  Define a new Constructor for Circle public Circle(double r) {this.r = r;}  Now two ways: Circle c = new Circle(); c.r = 0.25;  Or Circle c = new Circle(0.25);

30 Multiple Constructors Public Circle() { r = 1.0; } Public Circle(double r) {this.r = r;}  This is perfectly legal  Each constructor must have a different parameter list  This is a simple example of method overloading

31 Destroying Objects  Java automatically reclaims the memory occupied by an object when it is no longer needed Garbage Collection Garbage Collection  The Java interpreter can determine when an object is no longer referred to by any other object or variable Also works for cycles Also works for cycles


Download ppt "Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause."

Similar presentations


Ads by Google