Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.

Similar presentations


Presentation on theme: "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3."— Presentation transcript:

1 Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 3

2 Rina Zviel-Girshin @ARC2 Overview OOP Class libraries and Packages Classes and Objects

3 Rina Zviel-Girshin @ARC3 Java and OOP Some languages do not support OOP – e.g. C, Pascal. Some languages support OOP but not as the only method – e.g. C++. Java not only supports but compels OOP. OOP in Java has its own special flavor.

4 Rina Zviel-Girshin @ARC4 The central idea - objects The central new idea in object-oriented programming is that of an object. Objects tend to map the real-world to the set of entities. Basic OO idea: –Look at the problem, decompose into objects.

5 Rina Zviel-Girshin @ARC5 Real-life objects The real-world consist of objects: –University, students, instructors, classroom, … –Bus, driver, passengers, bus station, tickets, … –Computer, programmer, CD ROM, monitor,… –Cinema, movie, actor, director, …

6 Rina Zviel-Girshin @ARC6 Objects Objects include both: data (fields, variables, state) - attributes processing (behavior, methods, functions) – what it can do or can be done to it Objects have two kinds of behavior: outer – I/O, messages, relations with others inner – processing and computing the messages

7 Rina Zviel-Girshin @ARC7 Communicating objects Relations between objects are implemented using communication (messages), you have to ‘send a message’ to ask an object to perform some action.

8 Rina Zviel-Girshin @ARC8 Object example Example: Student has name, id, age, address, library card, grades. - All these called data or state. Address of the student can be changed. Library card can be lost. New grade can be added to the grades. - All these called processing or behavior.

9 Rina Zviel-Girshin @ARC9 Example

10 Rina Zviel-Girshin @ARC10 Example

11 Rina Zviel-Girshin @ARC11 Class Libraries A class library is a collection of classes that we can use when developing programs. There is a Java standard class library that is part of any Java development environment. These classes are not part of the Java language per se, but we rely on them heavily. The System class and the String class are part of the Java standard class library. Other class libraries can be obtained through third party vendors, or you can create them yourself.

12 Rina Zviel-Girshin @ARC12 Packages The classes of the Java standard class library are organized into packages. Some of the packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities and components Network communication Utilities

13 Rina Zviel-Girshin @ARC13 Java API (Packages) Java comes with 4,000+ pre-designed components. The Java API is the library of classes supplied by Java. The classes in the Java API is separated into packages. Each package contains a set of classes that are related in some way.

14 Rina Zviel-Girshin @ARC14 Documentation: Packages.html List of Packages

15 Rina Zviel-Girshin @ARC15 java.lang List of Classes

16 Rina Zviel-Girshin @ARC16 String Class Class Documentation Class Hierarchy

17 Rina Zviel-Girshin @ARC17 String Methods Methods List

18 Rina Zviel-Girshin @ARC18 The import Declaration When you want to use a class from a package, you could use its fully qualified name java.util.Random Or you can import the class, then just use the class name import java.util.Random; To import all classes in a particular package, you can use the * wildcard character import java.util.*;

19 Rina Zviel-Girshin @ARC19 The import Declaration All classes of the java.lang package are automatically imported into all programs. That's why we didn't have to explicitly import the System or String classes in earlier programs. The Random class is part of the java.util package. It provides methods that generate pseudo-random numbers. We often have to scale and shift a number into an appropriate range for a particular purpose.

20 Rina Zviel-Girshin @ARC20 Example import java.util.Random; public class RandomNumbers { public static void main (String[] args) { Random generator = new Random(); int num; num = generator.nextInt(); System.out.println ("A random int: " + num); System.out.print(”Two Dice throws:”);

21 Rina Zviel-Girshin @ARC21 Example num = Math.abs(generator.nextInt()) % 6; System.out.print(num + “ and “); num = Math.abs(generator.nextInt()) % 6; System.out.println(num); }

22 Rina Zviel-Girshin @ARC22 Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods are called class methods or static methods The Math class contains many static methods, providing various mathematical functions, such as absolute value, trigonometry functions, square root, etc. temp = Math.cos(90) + Math.sqrt(delta);

23 Rina Zviel-Girshin @ARC23 Classes As New Types The class declaration is a way of defining new types for your program – extending your language “vocabulary”. Once a class is declared you can use it to declare object variables. All those objects will be of the same type, they will have the same data and the same methods.

24 Rina Zviel-Girshin @ARC24 Objects & Classes

25 Rina Zviel-Girshin @ARC25 Objects & Classes Class is a data type which describes the pattern of data and behavior. Class is the type of object. Class is the set of all objects of same type. Class is the set of all objects of same type, including also data and behavior of the class and not of an object in it.

26 Rina Zviel-Girshin @ARC26 Objects & Classes Objects of a class are called also instances of the class. Class type, in contrast to its instance type, is the features of “all” of its objects (together) as opposite to the features of “each” of its objects (individually).

27 Rina Zviel-Girshin @ARC27 OO vocabulary ClassClass – a data type which describes the model or pattern of data and behavior. ObjectObject – an instance of a class. MessagingMessaging – request to perform an action. MethodMethod – the code body that implements a message.

28 Rina Zviel-Girshin @ARC28 Creating Objects To create a new object you need: allocate a memory for the object assign the reference to the object the address of the allocated memory The new operator has to be used. Objects always initialized when they are created. The creation of some objects require additional information about their initial state.

29 Rina Zviel-Girshin @ARC29 Creating Objects Example: new Student(“Avi”,1234); new Point(3,4); The keyword new followed by class name and a pair of parenthesis creates an object in Java. If creation requires additional information it is written inside the parenthesis.

30 Rina Zviel-Girshin @ARC30 Creating Objects Point mypoint; mypoint = new Point(235,456"); This calls the Point class constructor, which is a special method that sets up the object Creating an object is called instantiation. An object is an instance of a particular class. Objects are always initialized when they are created.

31 Rina Zviel-Girshin @ARC31 Object References Object are created in the main memory but have no name and cannot be found. In order to interact and use them we have to assign a reference to the address of the allocated memory. –We refer to an object by means of an object reference. –An object reference holds the data about memory location of the object.

32 Rina Zviel-Girshin @ARC32 Object Reference Variables Usually object references are stored in variables: Student avi = new Student(“Avi”,1234); Point p = new Point(3,4); p x = 3 y = 4 This variable has a type “reference to the object of type point”

33 Rina Zviel-Girshin @ARC33 Memory Allocation Why is it important to allocate a specific place in memory for each object? –Preserve the object state –Refer to it from different places –Not destroy it by overriding it with other data in that memory cells

34 Rina Zviel-Girshin @ARC34 Garbage Collection Garbage collection takes care of de-allocating the memory storage. The basic idea is storage reuse: –When an object is created the memory space is allocated for this object. –Later if the object or data are not used the memory is returned to the system for future reuse. Java language performs the garbage collection periodically automatically.

35 Rina Zviel-Girshin @ARC35 Allocation Example new new, new… empty references …new? x …new

36 Rina Zviel-Girshin @ARC36 Class declaration public class Rectangle { // data int width,height; // methods public Rectangle() { width=0; height=0; } int perimeter() { return (width+height)*2;} int area() { return width*height;} }

37 Rina Zviel-Girshin @ARC37 Methods Object has data and processing. Data is not much use unless we can manipulate it. For this purpose classes have methods. Methods are implemented as part of class definition. We interact with objects by method invocation.

38 Rina Zviel-Girshin @ARC38 Method invocation We invocate a method by using – object name (object reference), – “.”, –method name and a pair of parenthesis. If method invocation requires additional information it is written inside the parenthesis. Example: Point p = new Point(3,4); p.setx(0); p.sety(5);

39 Rina Zviel-Girshin @ARC39 Method declaration Method name and parameter list called method signature. The return type is not part of the signature. Example (methods of Rectangle class): int perimeter() { return (width+height)*2;} public void set(int width, int height) { this.width = width; this.height = height;}

40 Rina Zviel-Girshin @ARC40 Method declaration Some methods have return value. int newX = p.getx(); Each method has an access modifier. Method modifier is one of public, protected, private, static. (We will talk about them later.) An additional information, written inside the parenthesis, called method parameters.

41 Rina Zviel-Girshin @ARC41 Constructors Objects must be initialized before they can be used (similar to primitive data types). We must specify what is the initial state of the object before we can use it. We specify the way an object is initialized using a constructor, which is a special method which is invoked every time we create a new object.

42 Rina Zviel-Girshin @ARC42 Rectangle constructor public class Rectangle { … // constructor public Rectangle() { width=0; height=0; } // … other methods }

43 Rina Zviel-Girshin @ARC43 Constructor Constructor has no declared return type and cannot return anything. Constructor must have exactly the same name as class. public Rectangle() Constructors can have parameters.

44 Rina Zviel-Girshin @ARC44 Any Questions? More about objects and constructors next time.


Download ppt "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3."

Similar presentations


Ads by Google