Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.

Similar presentations


Presentation on theme: "Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control."— Presentation transcript:

1 Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control Statements, Arrays4, 3, 5 3Object Oriented Programming: Objects and Classes6 4Data Member, Member Method, Static and final members. Constructor 6 5Visibility Modifiers, Acessors, and Mutators6 6Inheritance, Object Class8 7Review + Array of Objects, Some handy Java Classes; Arrays, String, StringBuffer, StringTokenizer, Vector 7 8MIDTERM Object Oriented Programming: Objects and Classes menu

2 Syllabus (2) 9 Array of Objects, Some handy Java Classes; Arrays, String, StringBuffer, StringTokenizer, Vector 7 10 Concrete class, Abstract Class, Interface 9 11 Polymorphism 8 12 Error Handling, Exception Classes and Custom Java Exceptions 15 13 GUI Programming, event driven programming, components and containers, AWT (Abstract Window Toolkit), swing 11, 12, 13 14 GUI Programming, event driven programming, components and containers, AWT (Abstract Window Toolkit), swing 11, 12, 13 15 GUI Programming, Applet 14 16 Review 17 FINAL

3 Review F What is an array? F What is method overloading? F Why do we use arrays? F Why passing arrays to methods is different than passing primitive variables? F When do we use arrays? F How do we create, initialize arrays?

4 Who Wants to Answer? F Write a method named calculateAverage that will receive midterm grades of 20 students and return average of this exam. Notice that the input parameter should be an array of integers, and output should be of type double. F I WANT ALL OF YOU TO ANSWER!

5 Chapter 6 Objects and Classes F OO Programming Concepts: –Declaring Classes: data fields, methods, and constructors –Creating Objects and Object Reference Variables –Differences between primitive data type and object type –Automatic garbage collection  Modifiers ( public, private and static ) F Instance and Class Variables and Methods F Scope of Variables F Use the this Keyword  Case Studies ( Loan class and StackOfIntegers class) F Inner Classes

6 Warming up... F What is an object? F Give examples of objects? F How do we define objects?

7 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods.

8 Objects An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.

9 Classes Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.

10 Classes

11 Constructors Circle() { } Circle(double newRadius) { radius = newRadius; } Constructors are a special kind of methods that are invoked to construct objects.

12 Constructors, cont. A constructor with no parameters is referred to as a default constructor.  Constructors must have the same name as the class itself.  Constructors do not have a return type—not even void.  Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

13 Creating Objects Using Constructors new ClassName(); Example: new Circle(); new Circle(5.0);

14 NOTE If a class does not explicitly define any constructors, a default constructor is defined implicitly. If a class does explicitly define constructors, a default constructor does not exist unless it is defined explicitly. Therefore, you cannot use the default constructor to create an object using new ClassName() if its default constructor is not available. See Review Questions 6.11 and 6.12.

15 Constructing Objects, cont.

16 Declaring Object Reference Variables To reference an object, assign the object to a reference variable. To declare a reference variable, use the syntax: ClassName objectRefVar; Example: Circle myCircle;

17 Declaring/Creating Objects in a Single Step ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle();

18 Accessing Objects F Referencing the object’s data: objectRefVar.data myCircle.radius F Invoking the object’s method: objectRefVar.method myCircle.findArea()

19 Example 6.1 Using Objects  Objective: Demonstrate creating objects, accessing data, and using methods. TestSimpleCircle Run

20 Using Classes from the Java Library Example 6.1 declared the SimpleCircle class and created objects from the class. Often you will use the classes in the Java library to develop programs. You learned to obtain the current time using System.currentTimeMillis() in Example 2.5, “Displaying Current Time.” You used the division and remainder operators to extract current second, minute, and hour.

21 The Date Class Java provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string. For example, the following code java.util.Date date = new java.util.Date(); System.out.println(date.toString()); displays a string like Sun Mar 09 13:50:19 EST 2003.

22 Example 6.2 Using Classes from the Java Library F Objective: Demonstrate using classes from the Java library. Use the JFrame class in the javax.swing package to create two frames; use the methods in the JFrame class to set the title, size and location of the frames and to display the frames. TestFrame Run

23 Differences between Variables of Primitive Data Types and Object Types

24 Copying Variables of Primitive Data Types and Object Types

25 Garbage Collection As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer useful. This object is known as garbage. Garbage is automatically collected by JVM. (Who does NOT know what JVM is?)

26 Garbage Collection, cont TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The Java VM will automatically collect the space if the object is not referenced by any variable.

27 Visibility Modifiers and Accessor/Mutator Methods By default, the class, variable, or method can be accessed by any class in the same package.  public The class, data, or method is visible to any class in any package.  private The data or methods can be accessed only by the declaring class. The get and set methods are used to read and modify private properties.

28

29 When do we like privacy?

30 Why Data Fields Should Be private? To make class easy to maintain.

31 Example 6.3 Using the private Modifier and Accessor Methods Circle Run In this example, private data are used for the radius and the accessor methods getRadius and setRadius are provided for the clients to retrieve and modify the radius. TestCircle

32 What to pass to methods? F Primitive types F Arrays F Objects

33 Passing Objects to Methods F Passing by value (the value is the reference to the object) Example 6.4 Passing Objects as Arguments TestPassingObject Run

34 Passing Objects to Methods, cont.

35 Instance Variables, and Methods Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.

36 Class Variables, Constants, and Methods Class variables are shared by all the instances of the class. Class methods are not tied to a specific object. Class constants are final variables shared by all the instances of the class.

37 Class Variables, Constants, and Methods, cont. To declare class variables, constants, and methods, use the static modifier.

38 Class Variables, Constants, and Methods, cont.

39 Example 6.5 Using Instance and Class Variables and Method Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numOfObjects to track the number of Circle objects created. TestCircleWithStaticVariableRun

40 Scope of Variables F The scope of instance and class variables is the entire class. They can be declared anywhere inside a class. F The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used.

41 The Keyword this F Use this to refer to the current object. F Use this to invoke other constructors of the object.

42 Array of Objects Circle[] circleArray = new Circle[10]; An array of objects is actually an array of reference variables. So invoking circleArray[1].findArea() involves two levels of referencing as shown in the next figure. circleArray references to the entire array. circleArray[1] references to a Circle object.

43 Array of Objects, cont. Circle[] circleArray = new Circle[10];

44 Array of Objects, cont. Example 6.6: Summarizing the areas of the circles TotalAreaRun

45 Class Abstraction Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user.

46 Example 6.7 The Loan Class TestLoanClassRunLoan

47 Example 6.8 The StackOfIntegers Class Run TestStackOfIntegers

48 Inner Classes Inner class: A class is a member of another class. Advantages: In some applications, you can use an inner class to make programs simple. F An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class. ShowInnerClass

49 Inner Classes (cont.) F Inner classes can make programs simple and concise. F An inner class supports the work of its containing outer class and is compiled into a class named OutClassName$InnerClassName.class. For example, the inner class InnerClass in ShowInnerClass is compiled into ShowInnerClass$InnerClass.class.

50 Inner Classes (cont.) F An inner class can be declared public, protected, or private subject to the same visibility rules applied to a member of the class. F An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class

51 Give Example F Give an example to show that a static inner class can be accessed using the outer classname.

52 Questions to Solve (1) F Write a complete Java program that contains: –a class defition for 15 customers (a customer is stored with his/her name, age, and salary) –a method that calculates average age of the customers –a method that finds the customer with the highest salary

53 Questions to Solve (2) F Write a complete Java program that contains: –a class definition for cars (defined by their brand, model, color and price) –Define/initialize an array of cars of size 5 –Write a method that receives model of a car and returns age of the car –A method called paint that changes the color of white cars to red, and black cars to blue

54 Questions to Solve (3) F Write a complete Java program that contains: –An array definition for a chessboard (a chessboard is a square of 8x8) –Half of the squares should be assigned to “white” and the rest should be assigned to “black”


Download ppt "Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control."

Similar presentations


Ads by Google