COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.

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,
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
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.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
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.
Objects and Classes Mostafa Abdallah
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
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.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
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)
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.
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.
Introduction to Java ProgrammingChapter 7- 1 Chapter 8 Objects and Classes.
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 8 Objects and Classes
Chapter 9 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 8 Objects and Classes Part 2
Chapter 7 Objects and Classes
Presentation transcript:

COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

 Exam returned in lab sessions  Average: 43/65  HW6 due March 16  PA4 due March 16 Announcement

 Chapter 8  Class  Constructors  Data fields  UML notation Objectives

Class

 Suppose you want to develop a graphical user interface as shown below. How do you program it? Motivation

 object  An entity in the real world that can be distinctly identified.  A student, a desk, a circle, a button, and even a loan can all be viewed as objects.  has a unique identity, state, and behaviors.  State = a set of data fields (also known as properties) with their current values.  Behavior = a set of methods Object

 Classes are constructs that define objects of the same type Class vs. Object

Class Example

UML

 UML class diagram UML Notation

Constructor

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

 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. Constructor

 Creating objects using constructors Constructor new ClassName(); new Circle(); new Circle(5.0);

 A class may be declared without constructors.  In this case, a no-arg constructor with an empty body is implicitly declared in the class.  This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class. Constructor

 Declaring & creating objects in a single step  Two steps Creating Objects Circle myCircle = new Circle(); declare create //step 1: declare Circle myCircle; //step 2: create myCircle = new Circle();

Accessing Data Fields & Methods

 Referencing the object’s data fields:  Invoking the object’s method: Access myCircle.radius myCircle.getArea()

 The data fields can be of reference types  Example: name data field is of type String Data Fields public class Student { String name; // name has default value null int age; // age has default value 0 boolean isScienceMajor; // has default value false char gender; // c has default value '\u0000‘ //methods … }

 If a data field of a reference type does not reference any object, the data field holds a special literal value: null. null

 The default value of a data field is  null for a reference type  0 for a numeric type  false for a boolean type  '\u0000' for a char type  However, Java assigns no default value to a local variable inside a method. Default Values

 Java assigns no default value to a local variable inside a method. Default Values public class Test { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } Compilation error: variables not initialized

 Accessing methods and data fields Program TestCircle1 Run TestTV Run TV

 Primitive type copy Copying

 Reference type copy Copying

 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 referenced.  This object is known as garbage.  Garbage is automatically collected by JVM. Garbage Collector

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

 Chapter 8  OOP  Constructors Previously

 Instance fields belong to a specific instance.  Instance methods are invoked by an instance of the class.  i.e. non-static methods Instance Fields

 Static methods are not tied to a specific object.  Static variables are shared by all the instances of the class.  Static constants are final variables shared by all the instances of the class.  All declared using static keyword Static

 Example Static vs. Instance

 Static fields or methods can be used from instance or static methods.  Instance fields or methods can be only used from instance methods.  So: a variable or method that does not depend on a specific instance of the class, should be specified as static. Static vs. Instance

public class Foo { int i = 5; static int k = 2; public static void main(String[] args) { int j= i; // Wrong because i is an instance variable m1(); // Wrong because m1() is an instance method } public void m1() { i = i + k + m2(i, k); // Correct: since instance and static variables and //methods can be used in an instance method } public static int m2( int i, int j) { return ( int )(Math.pow(i, j)); }

Static vs. Instance public class Foo { int i = 5; static int k = 2; public static void main(String[] args) { Foo foo = new Foo(); int j= foo.i; // OK foo.m1(); // OK } public void m1() { i = i + k + m2(i, k); } public static int m2( int i, int j) { return ( int )(Math.pow(i, j)); }

 Which one is a better design? Static vs. Instance

 This example adds a class (i.e. static) variable numberOfObjects to track the number of Circle objects created. Program TestCircle2Run Circle2

1. Package access (default in Java)  The class, variable, or method can be accessed by any class in the same package. 2. public  The class, data, or method is visible to any class in any package. 3. private  The data or methods can be accessed only by the declaring class. Accessibility

 Access C1 methods & fields from C2/C3? Accessibility Example public class C1 { public int x; int y; private int z; public void m1() {..} void m2() {..} private void m3() {..} } Package 1 public class C2 { void aMethod() { C1 o = new C1(); } public class C3 { void aMethod() { C1 o = new C1(); } Package 2

Accessibility Example public class C1 { public int x; int y; private int z; public void m1() {..} void m2() {..} private void m3() {..} } Package 1 public class C2 { void aMethod() { C1 o = new C1(); //can access o.x; //can access o.y; //cannot access //o.z; //can invoke //o.m1(); //can invoke o.m2() //cannot invoke //o.m3(); } public class C3 { void aMethod() { C1 o = new C1(); //can access o.x; //cannot access //o.y; //cannot access //o.z; //can invoke //o.m1(); //cannot invoke //o.m2(); //cannot invoke //o.m3(); } Package 2

 Which classes can access C1? Accessibility Example class C1 { … } Package 1 public class C2 { //can access C1? } public class C3 { //can access C1? //can access C2? } Package 2

 Which classes can access C1? Accessibility Example class C1 { … } Package 1 public class C2 { //can access C1 } public class C3 { //cannot access C1 //can access C2 } Package 2

 The private modifier restricts access to within a class  The default modifier restricts access to within a package  The public modifier enables unrestricted access. Accessibility Summary

 Example: Accessibility public class Foo { private boolean x; public void test() { System.out.println(x); System.out.println(convert()); } private int convert( boolean b) { return x ? 1 : -1; } OK

 Example:  Because x and convert are private members Accessibility public class Test { public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); System.out.println(foo.convert(foo.x)); } Error!

 To protect data.  To make class easy to maintain. Why Private?

 The get and set methods are used to read and modify private properties.  Data encapsulation get/set The - sign indicates private modifier

 Data encapsulation Program Circle3 Run TestCircle3

 Passing by value for primitive type value (the value is passed to the parameter)  Passing by value for reference type value (the value is the reference to the object) Program TestPassObject Run

GUI

 PA4 and HW6 extended to Sunday 4:00 pm  Test data available for PA4 (on Sakai) Announcements

 When you develop programs to create graphical user interfaces, you will use Java classes such as JFrame, JButton, and JList to create frames, buttons, lists, and so on.  Here is an example that creates two windows using the JFrame class. GUI Program TestFrame Run

 You can add graphical user interface components, such as buttons, labels, text fields, combo boxes, lists, and menus, to the window. The components are defined using classes.  Here is an example to create buttons, labels, text fields, check boxes, radio buttons, and combo boxes. GUI Program GUIComponents Run

Array of Objects

 An array of objects is actually an array of reference variables.  So invoking circleArray[1].getArea() involves two levels of referencing.  circleArray references to the entire array.  circleArray[1] references to a Circle object. Array of Objects Circle[] circleArray = new Circle[10];

Array of Objects Circle[] circleArray = new Circle[10];

 Summarizing the areas of the circles Program 56 TotalAreaRun