CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

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.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
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.
Objects and Classes II Chapter 6, continued. Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects.
Unit 6: Object-oriented Programming: 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,
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Introduction to Objects A way to create our own types.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
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 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.
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
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.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
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 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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
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 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 8 Objects.
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.
1 Chapter 9 Objects and Classes. 2 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
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.
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 8 Objects and Classes
Chapter 9 Objects and Classes Part 01
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 9 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010

Classes and Objects Credits: Adapted from CIS-3023 lecture slides (Spring 2010) by Dr Seema Bandyopadhyay, University of Florida, Gainesville. 2

Object Instantiation Declaring and Creating object in two steps Declaring and Creating object in single step 3 // ; Circle myCircle; myCircle = new Circle(); // = new (); Circle myCircle = new Circle();

Trace Code 4 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; Declare myCircle no value myCircle animation

Trace Code, cont. 5 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; no value myCircle Create a circle animation

Trace Code, cont. 6 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle Assign object reference to myCircle animation

Trace Code, cont. 7 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle no value yourCircle Declare yourCircle animation

Trace Code, cont. 8 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle no value yourCircle Create a new Circle object animation

Trace Code, cont. 9 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle( ); yourCircle.radius = 100; reference value myCircle reference value yourCircle Assign object reference to yourCircle animation

Accessing Objects Referencing the object’s data: Invoking the object’s method: 10 // objectRefVar.data myCircle.radius //objectRefVar.methodName(arguments) myCircle.getArea()

Trace Code, cont. 11 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle( ); yourCircle.radius = 100; reference value myCircle reference value yourCircle Assign object reference to yourCircle animation

Trace Code, cont. 12 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle reference value yourCircle Change radius in yourCircle animation

Value Type vs. Reference Type 13

Copying Variables 14

Information Hiding In a well designed OO application, a class publicizes what it can do i.e. its method signatures but hides the internal details both of how it performs these services (method bodies) and the data (attributes) that it maintains in order to support these services 15

Access Modifiers 16 class Circle { private double radius; public Circle() { this(1.0); } public Circle(double newRadius) { radius = newRadius; } public double getArea() { return radius * radius * ; } Typically: Attributes are declared private Methods are declared public

Visibility Modifiers 17 F public –The class, data, or method is visible to any class F private –The data or methods can be accessed only by the declaring class.

Accessing private attributes public class Driver { public static void main(String[] args) { Circle s1, s2; s1 = new Circle(14); s2 = new Circle(7); System.out.println(“Radius = “, s1.radius); outcome = s2.getArea();//ok! } 18 Illegal: because attribute radius is hidden or private!

Accessing private attributes How can code in any other class access them? Programmer can provide methods to get and set them. 19

Get/Set Methods 20

Example Class Code 21 public class Circle { private double radius; public double getRadius() { return radius; } public void setRadius(double radius){ this.radius = radius; } public Circle() { this(1.0); } public Circle(double r) { setRadius(r); } public getArea() { return 3.14*radius*radius; } }

Example Driver Code 22 public class Driver { Circle c1 = new Circle(14); Circle c2 = new Circle(7); System.out.println(c1.getRadius()); //ok! c1.setRadius(5); //ok! boolean outcome = s2.getArea();//ok! }

23 public class Circle { private double radius; public double getRadius() { return radius; } public void setRadius(double r){ if (r>0) radius = r; } public Circle() { this(1.0); } public Circle(double r) { setRadius(r); } public getArea() { return 3.14*radius*radius; } } Some more code

public class Student { private String name; private String ssn; private float gpa; public Student(int i, String n) { setSsn(i); setName(n); setGpa(0.0f); } // other constructors and methods, not shown here public String getName () { return name; } public void setName(String newName) { name = newName;} public String getSsn () { return ssn; } public void setSsn(String s) { ssn = s;} public float getGpa () { return gpa; } public void setGpa(float newGpa) { gpa = newGpa;} } 24 Still more code

public class Student { private String name; private String ssn; private float gpa; private int numDsFs; public Student(int i, String n) { setSsn(i); setName(n); setGpa(0.0f); setNumDsFs(0); } // other methods, not shown here public boolean isOnProbation() { if(numDsFs > 3) return true; else return false; } public String getName () { return name; } public void setName(String newName) { name = newName;} public String getSsn () { return ssn; } public float getGpa () { return gpa; } public void setGpa(float newGpa) { gpa = newGpa;} public void setNumDsFs(int n) { numDsFs = n; } } 25 Lots more code

Benefits of Information Hiding Allows Data Validation Allows control over the level of access given for an attribute Simplifies Code Maintenance 26

Class attributes Each instance of a class (called an object) has a copy of the attributes Changing an attribute in one object doesn’t affect the attribute of another object 27

Class Attributes, cont. Sometimes you may want some data to be shared among all instances. Example: we want all Student objects to have a shared access to the total student enrollment count at the university. 28

Static Attributes public class Student { private String name; private String ssn; private float gpa; private static int totalNumStudents = 0; public Student(int i, String n) { setSsn(i); setName(n); setGpa(0.0f); totalNumStudents++; } // other constructors, accessors/mutators, and methods, not shown here public int getTotalNumStudents() { return totalNumStudents; } 29

Static Attributes, cont. A static attribute is one whose value is shared by all instances of that class. It in essence belongs to the class as a whole. 30

Static Attributes, cont. // Client code: Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); System.out.println(s1.getTotalNumStudents()); System.out.println(s2.getTotalNumStudents()); System.out.println(s3.getTotalNumStudents()); All of these println statements will print the value 3. 31

Static Methods 32 public class Student { private String name; private static int totalNumStudents = 0; // other attribute details omitted... public static int getTotalNumStudents() { return totalNumStudents; } // Client code Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); System.out.println(Student.getTotalNumStudents()); System.out.println(s1.getTotalNumStudents());

Static Methods may only access static attributes 33 class Student { private String name; // NOT static private static int totalStudents; public static void print() { System.out.println(name + " is one of " + totalStudents + " students."); // ILLEGAL! }

Get more info! 34