COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Similar documents
Chapter 2 Dual Roles of a Class

COMP 401 PATTERNS, INTERFACES AND OBJECTEDITOR. Instructor: Prasun Dewan

COMP 401 USER INTERFACE AND ANNOTATIONS. Instructor: Prasun Dewan

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

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

Principles of Object Oriented Programming. Lecture 4

How to be a Good Bean

CS1004: Intro to CS in Java, Spring 2005

Programming II (CS300)

COMP 110/401 Prasun Dewan 1

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

ECE 122. Engineering Problem Solving with Java

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

Chapter 4: Writing Classes

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Recommended Group Brainstorm (NO computers during this time)

COMP 401 INITIALIZATION AND INHERITANCE. Instructor: Prasun Dewan

What will this print?

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

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

by Pearson Education, Inc. All Rights Reserved. 2

Software Development. Modular Design and Algorithm Analysis

COMP 110/401 DOCUMENTATION: ASSERTIONS. Instructor: Prasun Dewan

Java Review. Fundamentals of Computer Science

COMP 110 Prasun Dewan 1

Collections and Inheritance

Chapter 4 Defining Classes I

Assignment 1 due Monday at 11:59pm

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

CSE 341, Autumn 2015, Ruby Introduction Summary

COMP 401 FACTORIES. Instructor: Prasun Dewan

AP COMPUTER SCIENCE A

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

THE MYSTERY OF THE 20GB LOG FILE. Instructor: Prasun Dewan (FB 150,

5. Defining Classes and Methods

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

COMP 401 INTERFACES. Instructor: Prasun Dewan

COMP 110 Prasun Dewan 1

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan

ITI Introduction to Computing II

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Programming II (CS300)

COMP 110/401 LEAST PRIVILEGE. Instructor: Prasun Dewan

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

Programming II (CS300)

COMP 401 COURSE OVERVIEW

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Challenges in component based programming. Lena Buffoni

EECS168 Exam 3 Review

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Programming II (CS300)

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

CMSC 132: Object-Oriented Programming II

ITI Introduction to Computing II

COMP 110 AND 401 CLASS (STATIC) STATE. Instructor: Prasun Dewan

Chapter 6 Introduction to Defining Classes

Methods and Data (Savitch, Chapter 5)

Encapsulation. Mason Vail Boise State University Computer Science

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Recitation 3 Class and Objects

CSE 214 Computer Science II Java Classes and Information Hiding

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Comp Assignment 4: Commands and Graphics

COMP 401 Spring 2013 Midterm 1

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Defining Classes and Methods

Classes and Objects. CGS 3416 Spring 2018

Data Structures (list, dictionary, tuples, sets, strings)

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Arrays & Classes. Problem Statement and Specifications

Informatik II Tutorial 6. Subho Shankar Basu

Anatomy of a Class Encapsulation Anatomy of a Method

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS. Instructor: Prasun Dewan

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

JAVA BYTE IPC: PART 4-NON- BLOCKING-NIO-MANAGER. Instructor: Prasun Dewan (FB 150,

Comments are almost like C++

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

CS-202 Introduction to Object Oriented Programming

CS 302 Week 9. Jim Williams

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Mobile Application Programming. Swift Classes

DISTRIBUTED SYSTEMS. Instructor: Prasun Dewan (FB 150,

COMP 401 Spring 2014 Midterm 1

CS112 Lecture: Defining Instantiable Classes

Distributed Collaboration Prasun Dewan 1

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

COMP 110/401 WINDOWS COMMAND INTERPRETER. Instructor: Prasun Dewan (FB 150,

How a Language-based GUI Generator Can Influence the Teaching of Object-Oriented Programming

Transcription:

COMP 401: THE DUAL ROLE OF A CLASS Instructor: Prasun Dewan (FB 150, dewan@unc.edu)

SCRIPTS ANALOGY Script Program Follows Follows Theater Performer 2

STRUCTURING IN SCRIPTS Script (Folder) Act (File) Act (File) Scene Scene Sentence Sentence 3

SHAKESPEARE PLAYS VS. TWEETS Script (Folder) Tweet Sentence Act (File) Act (File) Scene Scene Sentence Sentence 4

STRUCTURING IN JAVA PROGRAMS Package (Folder) Class (File) Class (File) Method Method Local Declaration Statement Global (Static) Declaration 5

JAVA VS. SCRIPTING LANGUAGES Package (Folder) Perl Declaration Statement Class (File) Class (File) Method Method Java is designed for Shakespearean programs Local Declaration Statement Global (Static) Declaration 6

LOOP AND VARIABLE long product = 1; while (n > 0) { product *= n; n -= 1; return product; 7

METHOD ENCAPSULATION public static long loopingfactorial(int n) { long product = 1; while (n > 0) { product *= n; n -= 1; return product; 8

CLASS AND PACKAGE ENCAPSULATION package lectures.java_basics_overview; public class Factorials { public static long loopingfactorial(int n) { long product = 1; while (n > 0) { product *= n; n -= 1; return product; Static? 9

FACTORIAL USER package lectures.java_basics_overview; public class Permutations{ public static long permutations(int n, int r) { return Factorials.loopingfactorial(n) / Factorials.loopingFactorial(n-r); 10

REMEMBERING FACTORIALS Cannot be accessed from classes in other packages Can be accessed from classes in other packages package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; Computes the factorial of a stored number ~ grade spreadsheet computing the grade of stored scores 11

CLASSES AS MODULES Classes are modules that put walls around the methods and global variables in a program These walls make only certain methods and variables visible outside Class (File) Principles about what should be visible outside? Method Method Local Declaration Statement Global (Static) Declaration 12

LEAST PRIVILEGE AND ENCAPSULATION Do not make non final variable public Do not give user of some code more rights than it needs Export state through properties package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; 13

CLASSES AS MODULES (REVIEW) Classes are modules that put walls around the methods and global variables in a program These walls make only certain methods and variables visible outside Class (File) Principles about what should be visible outside? Method Method Local Declaration Statement Global (Static) Declaration 14

LEAST PRIVILEGE AND ENCAPSULATION (REVIEW) Do not make non final variable public Do not give user of some code more rights than it needs Export state through getters (accessors), setters (mutators) and properties package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; 15

PROPERTIES Number Factorial package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; 16

PROPERTIES? Number Factorial package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return Factorials.loopingFactorial(number); 17

READ-ONLY AND EDITABLE STATIC PROPERTIES Typed, Named Unit of Exported Class State public class C { Bean Name P Type T public static T getp() {... public static void setp(t newvalue) {... Read-only Editable Getter method Setter method obtainp Violates Bean convention newp Bean convention: For humans and tools 18

READONLY AND EDITABLE PROPERTIES Number editable Factorial readonly package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; 19

DEPENDENT AND INDEPENDENT Number Independent Factorial Dependent package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; 20

STORED AND COMPUTED Number Stored Factorial Computed package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; public static long getfactorial() { return Factorials.loopingFactorial(number); 21

MY FACTORIAL package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; A second spreadsheet? 22

REUSE THROUGH COPY AND PASTE package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet2 { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; StaticLoopingFactorialSpreadsheet.setNumber(2); StaticLoopingFactorialSpreadsheet2.setNumber(4); Code duplication Initial work Maintenance 23

CLASSES AS MODULES Classes are modules that put walls around the methods and global variables in a program These walls make only certain methods and variables visible outside Class (File) Method Method Local Declaration Statement Global (Static) Declaration 24

CLASSES AS TYPES Classes are ways for programmers to define their own types Values of these types are created by instantiating the classes and share the behavior defined by the type. These values have copies of the dynamic variables defined by the class Class (File) Method Method Local Declaration Statement Global (Static) Declaration 25

CLASSES AS TYPES public class ALoopingFactorialSpreadsheet { int number; long factorial; public int getnumber() { return number; public void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public long getfactorial() { return factorial; ALoopingFactorialSpreadsheet factorial1 = new ALoopingFactorialSpreadsheet (); ALoopingFactorialSpreadsheet factorial = new ALoopingFactorialSpreadsheet (); ALoopingFactorialSpreadsheet factorial = new ALoopingFactorialSpreadsheet (); factorial1.setnumber(2); factorial2.setnumber(2); No static: instance variables and instance methods A new set of instance variables dynamically created each time a new instance is created Instance variables can be accessed only by instance methods Instance is essentially an extra parameter to instance method Class as type Dynamically creating a Class instance 26

Factory FACTORY ANALOGY Operations accelerate start assembly Factory Operations class looping Factorial() Class (static) Methods instance instance Class (instance) method: method invoked on class (class instance) Instance Methods getfactorial() Since class can be active, more like a factory than a blue print. Factory-metaphor!= factory pattern. 27

JAVA INSTANCEOF BOOLEAN OPERATOR (new ALoopingFactorialSpreasheet()) instanceof ALoopingFactorialSpreasheet true (new ALoopingFactorialSpreasheet()) instanceof Factorials false (new ALoopingFactorialSpreasheet()) instanceof String false true and false are values of Java type boolean instanceof is Java keyword If class of object o is T then o instanceof T returns true 28

PROPERTIES OF STATIC SPREADSHEET Number Factorial package lectures.java_basics_overview; public class StaticLoopingFactorialSpreadsheet { static int number; static long factorial; public static int getnumber() { return number; public static void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public static long getfactorial() { return factorial; 29

PROPERTIES OF DYNAMIC SPREADSHEET? Number Factorial public class ALoopingFactorialSpreadsheet { int number; long factorial; public int getnumber() { return number; public void setnumber(int newval) { number = newval ; factorial = Factorials.loopingFactorial(number); public long getfactorial() { return factorial; 30

READ-ONLY AND EDITABLE STATIC PROPERTIES Typed, Named Unit of Exported Class State public class C { Bean Name P Type T public static T getp() {... Read-only Editable public static void setp(t newvalue) {... 31

READ-ONLY AND EDITABLE INSTANCE PROPERTIES Typed, Named Unit of Exported Class State public class C { public getp() {... Bean Name P Type T Read-only Editable public void setp(t newvalue) {... No static 32

STATIC GLOBAL VARIABLES Global variable declared outside a method static int number = 1; Static (global) variable created in memory without creating an object Static methods can access only static global variables as they are invoked on classes and would not know which instance s variables should be accessed static void setnumber (int newval) { number = newval; 33

INSTANCE GLOBAL VARIABLES int number = 1; Instance (global) variable created in memory as part of a specific object void setnumber (int newval) { number = newval; Instance methods are invoked on a specific instance and can access that instance s variables and also static variables of the class Static variables are more global than instance variables as they can accessed by both instance and static methods 34

MORE ON INSTANCE VS. STATIC 110 and 401 Object-first Introduction to Programming (8/27, 8/29) PowerPoint PDF Objects Chapter lectures.objects Package 110 and 401 State and Properties (9/5, 9/10 ) PowerPoint PDF State Chapter Video lectures.state.properties Package 110 (401 Prerequisite ) Static State PowerPoint PDF Static Chapter lectures.static_state P 35

DOUBLE ROLE OF CLASS Classes are modules that puts walls around the methods and variables in a program These walls make only certain methods and variables visible outside Classes are ways for programmers to define their own types Values of these types are created by instantiating the classes In either case they are used to create reusable code Reusable code is often packaged as libraries to be used by other projects 36