CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Similar documents
Chapter 8 Objects and Classes

Chapter 9. Objects and Classes

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Chapter 8 Objects and Classes Part 1

UFCE3T-15-M Object-oriented Design and Programming

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

OO Programming Concepts

ECOM 2324 COMPUTER PROGRAMMING II

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Chapter 3 Objects and Classes

CS 170, Section /3/2009 CS170, Section 000, Fall

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Object-Oriented Programming Concepts

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Programming II (CS300)

Comments are almost like C++

Chapter 4 Defining Classes I

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF -

Chapter 8 Objects and Classes

CS1004: Intro to CS in Java, Spring 2005

Objects as a programming concept

Programming II (CS300)

Object Oriented Programming in C#

Making New instances of Classes

CS151 Principles of Computer Science I Fall 2018 Homework 6 S

Programming in the Large II: Objects and Classes (Part 1)

2 What are the differences between constructors and methods?

Classes and Objects. CGS 3416 Spring 2018

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Last lecture. Lecture 9. in a nutshell. in a nutshell 2. Example of encapsulation. Example of encapsulation. Class test. Procedural Programming

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Documenting, Using, and Testing Utility Classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Chapter 6 Introduction to Defining Classes

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS 251 Intermediate Programming Methods and More

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Chapter 4. Defining Classes I

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

CS1150 Principles of Computer Science Objects and Classes

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Ticket Machine Project(s)

Lecture 5: Inheritance

Handout 7. Defining Classes part 1. Instance variables and instance methods.

What is Inheritance?

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Inheritance and Polymorphism

Creating and Using Objects

CS 251 Intermediate Programming Methods and Classes

Lecture Notes Chapter #9_b Inheritance & Polymorphism

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Questions Answer Key Questions Answer Key Questions Answer Key

Distributed Systems Recitation 1. Tamim Jabban

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Principles of Object Oriented Programming. Lecture 4

Lecture 7: Classes and Objects CS2301

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Classes. Classes as Code Libraries. Classes as Data Structures

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer?

BM214E Object Oriented Programming Lecture 7

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

TaxiBot New attributes Variables Math! TaxiBot

COMP 250. inheritance (cont.) interfaces abstract classes

Object Oriented Relationships

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Computer Science II (20082) Week 1: Review and Inheritance

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

Object Oriented Programming COP3330 / CGS5409

Simple Java Reference

Computer Science II (20073) Week 1: Review and Inheritance

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Distributed Systems Recitation 1. Tamim Jabban

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

ITI Introduction to Computing II

And Even More and More C++ Fundamentals of Computer Science

Lecture 6 Introduction to Objects and Classes

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Objects and Classes Lecture 2

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Assignment 1 due Monday at 11:59pm

Transcription:

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

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

Object Instantiation Declaring and Creating object in two steps // <ClassName> <objectrefvar>; Circle mycircle; mycircle = new Circle(); Declaring and Creating object in single step // <ClassName> <objectrefvar> = new <ClassName>(); Circle mycircle = new Circle(); 3

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

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

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

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

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle no value : Circle Create a new Circle object radius: 1.0 8

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle( ); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 Assign object reference to yourcircle yourcircle reference value : Circle radius: 1.0 9

Accessing Objects Referencing the object s data: // objectrefvar.data mycircle.radius Invoking the object s method: //objectrefvar.methodname(arguments) mycircle.getarea() 10

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle( ); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 Assign object reference to yourcircle yourcircle reference value : Circle radius: 1.0 11

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle reference value Change radius in yourcircle : Circle radius: 100.0 12

Value Type vs. Reference Type Value type int i = 1 i 1 Created using new Circle() Reference type Circle c c reference c: Circle radius = 1 13

Copying Variables Primitive type assignment i = j Before: After: i 1 i 2 j 2 j 2 Object type assignment c1 = c2 Before: After: c1 c1 c2 c2 c1: Circle radius = 5 C2: Circle radius = 9 c1: Circle radius = 5 C2: Circle radius = 9 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 class Circle { private double radius; public Circle() { this(1.0); public Circle(double newradius) { radius = newradius; Typically: Attributes are declared private Methods are declared public public double getarea() { return radius * radius * 3.14159; 16

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

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

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 Get method or Accessor A method that returns the value of an attribute e.g., getufid( ) { return studentufid; Set method or Mutator A method that changes the value of an attribute e.g., getufid( ) { return studentufid; 20

Example Class Code 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; 21

Example Driver Code 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! 22

public class Circle { private double radius; Some more code 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; 23

public class Student { private String name; private String ssn; private float gpa; Still more code 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

public class Student { private String name; private String ssn; private float gpa; private int numdsfs; Lots more code 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

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 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()); 32

Static Methods may only access static attributes 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! 33

Get more info! Value Types and Reference Types: http://java.sun.com/docs/books/jls/second_edition/html/typesvalues.doc.html Creating and initializing objects: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_02.htm (Online) Search keywords: JAVA classes and objects JAVA pass by reference or value 34