Presentation is loading. Please wait.

Presentation is loading. Please wait.

EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University

Similar presentations


Presentation on theme: "EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University"— Presentation transcript:

1 EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University wenbing@ieee.org

2 2 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Outline Dissect a simple Java program Object-oriented programming concepts Exceptions handling Packages Materials taken from Sun’s Java Tutorial: –http://java.sun.com/docs/books/tutorial/java/index.htmlhttp://java.sun.com/docs/books/tutorial/java/index.html Another good Java tutorial: –http://www.cafeaulait.org/course/http://www.cafeaulait.org/course/

3 3 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao

4 4 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao

5 5 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao

6 6 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Object Concept Real-world objects: state and behavior –State of a bicycle: current gear, current pedal cadence, current speed –Behavior of a bicycle: changing gear, changing pedal cadence, applying brakes

7 7 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Software Object A software object mimics a real-world object –Member variables (or fields): store the state of the object –Member functions (or Methods): expose the object behavior –Methods operate on an object's internal state Fields (state) Methods (behavior)

8 8 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Object Oriented Concept Object-oriented communication: Different objects communicate (interact) with each other by calling methods defined in the objects Data encapsulation: Hiding internal state and requiring all interaction to be performed through an object's methods

9 9 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Class A class is the blueprint from which individual objects are created

10 10 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Access Level Modifiers At the top level—public, or package-private (no explicit modifier) At the member level—public, private, protected, or package-private (no explicit modifier) Access Levels ModifierClassPackageSubclassWorld public YYYY protected YYYN no modifierYYNN private YNNN

11 11 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Creating Objects Bicycle b = new Bicycle() Declaration: associate a variable name with an object type Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object

12 12 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Using Objects Calling an object’s methods: Referencing an object’s fields: Bicycle b = new Bicycle() b.speedup(10); objectReference.fieldName

13 13 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Returning a Value from a Method A method returns to the code that invoked it when it –completes all the statements in the method, –reaches a return statement, or –throws an exception (covered later), whichever occurs first return returnValue; return; If no return value is needed

14 14 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Inheritance Different kinds of objects often have a certain amount in common with each other –Mountain bikes, road bikes, all share the characteristics of bicycles (current speed, current pedal cadence, current gear) Yet each also defines additional features that make them different –Road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio

15 15 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Inheritance Classes can inherit commonly used state and behavior from other classes. Bicycle is the superclass of MountainBike, RoadBike, and TandemBike –In the Java, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses

16 16 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Inheritance Super class Subclasses

17 17 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Inheritance A class that is derived from another class is called a subclass (also a derived class, extended class, or child class) The class from which the subclass is derived is called a superclass (also a base class or a parent class) A subclass inherits all the members (fields, methods, and nested classes) from its superclass Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass

18 18 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Inheritance MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it

19 19 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Inheritance You can assign a subclass to a superclass variable –Bicycle b = new MountainBike(10); But not the other way around. You have to cast a superclass object to a sublass object –MountainBike mb = (MountainBike)b;

20 20 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Abstract Class An abstract class is a class that is declared abstract: it may or may not include abstract methods

21 21 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Abstract Class An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon If a class includes abstract methods, the class itself must be declared abstract Abstract classes cannot be instantiated, but they can be subclassed

22 22 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Abstract Class: Example

23 23 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Interface Objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with outside world An interface is a group of related methods with empty bodies

24 24 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Interface To implement this interface, use the implements keyword in the class declaration:

25 25 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Interface Interfaces form a contract between the class and the outside world –This contract is enforced at build time by the compiler If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile

26 26 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Exception An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions

27 27 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Exception Code that might throw certain exceptions must be enclosed by either of the following: –A try statement that catches the exception. The try must provide a handler for the exception –A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception public void writeList() throws IOException { … }

28 28 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Exception Try-Catch-Finally

29 29 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao How to Throw an Exception All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object

30 30 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Throwable Class and Its Subclasses

31 31 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Package A package is a namespace that organizes a set of related classes and interfaces –Think of packages as being similar to different folders The Java platform provides an enormous class library (a set of packages) –This library is known as the "Application Programming Interface", or "API" for short –Java platform API specification: http://java.sun.com/j2se/1.5.0/docs/api/ http://java.sun.com/j2se/1.5.0/docs/api/ –Example packages: java.lang (Fundamental classes), java.io (Classes for reading and writing)

32 32 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Benefits of Creating a Package You and other programmers can easily determine that these types are related You and other programmers know where to find types that can provide graphics-related functions The names of your types won't conflict with the type names in other packages because the package creates a new namespace You can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package

33 33 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Creating a Package To create a package, you choose a name for the package and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package The package statement (for example, package graphics;) must be the first line in the source file. There can be only one package statement in each source file

34 34 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao

35 35 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Managing Source and Class Files Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is.java. For example:

36 36 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Managing Source and Class Files Then, put the source file in a directory whose name reflects the name of the package:.....\graphics\Rectangle.java The qualified name of the package member and the path name to the file are parallel class namegraphics.Rectangle pathname to filegraphics/Rectangle.java Produce a jar file for your classes –Under the parent directory of graphics –jar –cf graphics.jar graphics

37 37 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Using a Package To use a public package member from outside its package, you must do one of the following: –Refer to the member by its fully qualified name: System.out.println(“Hello World”); –Import the package member import graphics.Rectange; Rectangle r = new Rectangle(); –Import the member's entire package import graphics.*; Circle c = new Circle();

38 38 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Accessing a Package (Compilation and Execution) For compilation: –javac –classpath graphics.jar YourClass.java For execution: –java –classpath graphics.jar YourClass

39 39 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Questions Real-world objects contain ___ and ___. A software object's state is stored in ___. A software object's behavior is exposed through ___. Hiding internal data from the outside world, and accessing it only through publicly-exposed methods is known as data ___. A blueprint for a software object is called a ___.

40 40 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Questions Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword. A collection of methods with no implementation is called an ___. A namespace that organizes classes and interfaces by functionality is called a ___. The term API stands for ___?

41 41 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Questions What is wrong with the following interface? Is the following interface valid?

42 42 Fall Semester 2008EEC-681: Distributed Computing SystemsWenbing Zhao Questions Is the following code legal?


Download ppt "EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University"

Similar presentations


Ads by Google