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

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Road Map Introduction to object oriented programming. Classes
1 Chapter 2 Objects and Classes. 2 Objectives F To understand objects and classes and use classes to model objects. F To learn how to declare a class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
Chapter 9 Objects and Classes
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 6 Objects and Classes She calls out to the man on the street Sir can you help me.
Objects and Classes Mostafa Abdallah
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 7 Objects and Classes
Lecture 3: Introduction to Object and Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 8 Objects and Classes Part 1
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 8 Objects and Classes
Chapter 6 Objects and Classes
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes Part 01
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

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

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 GUI Programming, event driven programming, components and containers, AWT (Abstract Window Toolkit), swing 11, 12, GUI Programming, event driven programming, components and containers, AWT (Abstract Window Toolkit), swing 11, 12, GUI Programming, Applet Review 17 FINAL

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?

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!

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

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

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.

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

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.

Classes

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

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.

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

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.

Constructing Objects, cont.

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;

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

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

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

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.

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.

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

Differences between Variables of Primitive Data Types and Object Types

Copying Variables of Primitive Data Types and Object Types

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?)

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.

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.

When do we like privacy?

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

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

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

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

Passing Objects to Methods, cont.

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

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.

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

Class Variables, Constants, and Methods, cont.

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

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.

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

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.

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

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

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.

Example 6.7 The Loan Class TestLoanClassRunLoan

Example 6.8 The StackOfIntegers Class Run TestStackOfIntegers

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

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.

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

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

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

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

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”