COMP 401 INTERFACES. Instructor: Prasun Dewan

Size: px
Start display at page:

Download "COMP 401 INTERFACES. Instructor: Prasun Dewan"

Transcription

1 COMP 401 INTERFACES Instructor: Prasun Dewan

2 PREREQUISITE State Properties 2

3 INTERFACES Define contracts between our users and implementers Optional they may not be used Good style to use them 3

4 MOTIVATION: TWO WAYS OF DOING THE BMI SPREADSHEET ABMISpreadsheet is one way to implement the spreadsheet user-interface Let us create AnotherBMISpreadsheet to illustrate another way Difference is in number of variables used 4

5 ALTERNATIVE IMPLEMENTATION public class ABMISpreadsheet { double height; public double getheight() { return height; public void setheight(double newheight) { height = newheight; double weight; public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; public double getbmi() { BMI return weight/(height*height); Height Weight Editable Independent Stored Editable Independent Stored Read-only Dependent Computed 5

6 ABMISPREADSHEET ABMISpreadsheet Instance weight height reads writes reads writes reads getweight() setweight() getheight() setheight() getbmi() calls calls calls calls weight new weight height new height ObjectEditor 6

7 ANOTHERBMISPREADSHEET AnotherBMISpreadsheet Instance weight height bmi reads writes reads writes reads getweight() setweight() getheight() setheight() getbmi() calls calls calls calls weight new weight height new height ObjectEditor 7

8 SETWEIGHT() ABMISpreadsheet Instance weight bmi writes setweight() calls new weight public void setweight(double newweight) { weight = newweight; bmi = weight / (height*height); ObjectEditor 8

9 SETHEIGHT() ABMISpreadsheet Instance height bmi writes setheight() public void setheight(double newheight) { height = newheight; bmi = weight / (height*height); calls new height ObjectEditor 9

10 METHODS THAT CHANGE ABMISpreadsheet Instance weight height bmi writes writes reads setweight() setheight() getbmi() calls calls new weight new height ObjectEditor 10

11 GETBMI() ABMISpreadsheet Instance bmi reads getbmi() public double getbmi() { return bmi; ObjectEditor 11

12 ANOTHERBMISPPREADSHEET public class AnotherBMISpreadsheet { double height, weight, bmi; public double getheight() { return height; public void setheight(double newheight) { height = newheight; bmi = weight/(height*height); public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; bmi = weight/(height*height); public double getbmi() { return bmi; 12

13 GRAPHICAL ALGORITHM ABMISpreadsheet Instance weight height bmi reads writes reads writes reads getweight() setweight() getheight() setheight() getbmi() calls calls calls calls weight new weight height new height ObjectEditor 13

14 OBJECTEDITOR USER INTERFACE? public class AnotherBMISpreadsheet { double height, weight, bmi; public double getheight() { return height; public void setheight(double newheight) { height = newheight; bmi = weight/(height*height); public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; bmi = weight/(height*height); public double getbmi() { return bmi; 14

15 OBJECTEDITOR USER INTERFACES 15

16 SIMILARITIES IN THE TWO CLASSES public class AnotherBMISpreadsheet { double height, weight, bmi; public double getheight() { return height; public void setheight(double newheight) { height = newheight; bmi = weight/(height*height); public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; bmi = weight/(height*height); public double getbmi() { return bmi; public class ABMISpreadsheet { double height; public double getheight() { return height; public void setheight(double newheight) { height = newheight; double weight; public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; public double getbmi() { return weight/(height*height); Same headers, signatures Signature = Header parameter names public void setheight(double) OE Beautified Signature 16

17 REAL-WORLD ANALOGY Corvette Specification 17

18 INTERFACE 18

19 IMPLEMENTING AN INTERFACE Contract public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getheight() { return height; public void setheight (double newheight) { height = newheight; bmi = weight/(height*height); Parameter names never matter to Java 19

20 INTERFACE implements ABMISpreadsheet instance of BMISpreadsheet implements ABMISpreadsheet instance ABMISpreadsheet instance AnotherBMISpreadsheet instance of AnotherBMISpreadsheet instance AnotherBMISpreadsheet instance 20

21 USING INTERFACES TO CLASSIFY implements ABMISpreadsheet instance of BMISpreadsheet implements BMISpreadsheet instance BMISpreadsheet instance AnotherBMISpreadsheet instance of BMISpreadsheet instance BMISpreadsheet instance 21

22 USING CAR SPECIFICATIONS TO CLASSIFY Corvette Corvette Specification Corvette Corvette Corvette 22

23 USING INTERFACES TO TYPE public class BMISpreadsheetUser { public static void main(string[] args) { BMISpreadsheet bmispreadsheet = new ABMISpreadsheet(1.77, 75); System.out.println(bmiSpreadsheet.getBMI()); bmispreadsheet = new AnotherBMISpreadsheet(); bmispreadsheet.setheight(1.77); bmispreadsheet.setweight(75); System.out.println(bmiSpreadsheet.getBMI()); Interface as type Same variable assigned instances of two different classes 23

24 TYPE CHECKING public class BMISpreadsheetUser { public static void main(string[] args) { BMISpreadsheet bmispreadsheet = new ABMISpreadsheet(1.77, 75); System.out.println(bmiSpreadsheet.obtainBMI()); bmispreadsheet = new AnotherBMISpreadsheet(); bmispreadsheet.setheight(1.77); bmispreadsheet.setweight(75); System.out.println(bmiSpreadsheet.getBMI()); Not defined in interface (or class) 24

25 INTERFACE METHODS CONSIDERED IN TYPE CHECKING public class BMISpreadsheetUser { public static void main(string[] args) { BMISpreadsheet bmispreadsheet = new ABMISpreadsheet(1.77, 75); System.out.println(bmiSpreadsheet.getBMI()); bmispreadsheet = new AnotherBMISpreadsheet(); bmispreadsheet.setheight(1.77); bmispreadsheet.setweight(75); System.out.println(bmiSpreadsheet.getBMI()); Not defined in interface so illegal, even though defined in class Not defined in interface 25

26 YET ANOTHER SPRADSHEET CLASS public class ABMISpreadsheetAndCalculator implements??? { double height, weight, bmi; public double getheight() { return height; public void setheight(double newheight) { height = newheight; bmi = calculatebmi(height, weight); public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; bmi = calculatebmi(height, weight); public double getbmi() { return bmi; public double calculatebmi(double height, double weight) { return weight/(height*height); 26

27 IMPLEMENTING MULTIPLE INTERFACES public class ABMISpreadsheetAndCalculator implements BMISpreadsheet, BMICalculator{ double height, weight, bmi; public double getheight() { return height; public void setheight(double newheight) { height = newheight; bmi = calculatebmi(height, weight); public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; bmi = calculatebmi(height, weight); public double getbmi() { return bmi; public double calculatebmi(double height, double weight) { return weight/(height*height); 27

28 BMICALCULATOR INTERFACE public interface BMICalculator { public double calculatebmi(double height, double weight); 28

29 ABMICALCULATORWITHINTERFACE public class ABMICalculatorWithInterface implements BMICalculator { public double calculatebmi(double height, double weight) { return weight/(height*height); 29

30 TYPING public static void main (String[] args) { BMICalculator bmicalculator = new ABMISpreadsheetAndCalculator(); BMISpreadsheet bmispreadsheet = new ABMISpreadsheetAndCalculator(); double bmi = bmicalculator.calculatebmi(1.77, 75); bmi = bmispreadsheet.getbmi(); // bmi = bmicalculator.getbmi(); // bmi = bmispreadsheet.calculatebmi(1.77, 75); public static void main (String[] args) { BMICalculator[] bmicalculators = {new ABMISpreadsheetAndCalculator(), new ABMICalculatorWithInterface(); 30

31 CAR ANALOGY A car is characterized by Its make License plate Registration Licensing authority groups car by the registration or license plate 31

32 CANNOT INSTANTIATE SPECIFICATION Cannot order a car from a specification Must order from factory A car defined by Corvette specification ordered from factory implementing the specification Cannot instantiate interface Must instantiate class BMISpreadsheet instance created by instantiating class implementing interface 32

33 INTERFACE AS A SYNTACTIC SPECIFICATION public class ABMISpreadsheet implements BMISpreadsheet{ double height; public double getheight() { return height; public void setheight(double newheight) { height = newheight; double weight; public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; public double getbmi() { return weight/(height*height); 33

34 INTERFACE AS A SYNTACTIC SPECIFICATION public class ABMISpreadsheet implements BMISpreadsheet{ double height; public double getheight() { return height; public void setheight(double newheight) { height = newheight; double weight; public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; public double getbmi() { return 13450; Syntactic Contract Bombay Market Index 34

35 INTERFACE REQUIRED Define interfaces for All classes (that are instantiated) Some are not Each public method of a class should be in some interface it implements 35

36 IMPACT OF DIFFERENCES IN THE TWO CLASSES public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getheight() { return height; public void setheight(double newheight) { height = newheight; bmi = weight/(height*height); public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; bmi = weight/(height*height); public double getbmi() { return bmi; public class ABMISpreadsheet implements BMISpreadsheet { double height; public double getheight() { return height; public void setheight(double newheight) { height = newheight; double weight; public double getweight() { return weight; public void setweight(double newweight) { weight = newweight; public double getbmi() { return weight/(height*height); 36

37 ABMISPREADSHEET VS. ANOTHERBMISPREADSHEET AnotherBMISpreadsheet harder to program Computation of bmi done in multiple setters. These setters have side effects AnotherBMISpreadsheet does eager evaluation Value computed may never be used. ABMISpreadsheet uses less space (variables) Getter methods of AnotherBMISpreadhseet are faster Setter methods of ABMISpreadsheet are faster Usually getter methods are called more often that setter methods e.g. when ObjectEditor refresh command is executed Typically AnotherBMISpreadsheet will be faster, overall 37

38 TIME-SPACE TRADEOFF Time Space Space Miser Time Miser 38

39 TIME-SPACE TRADEOFF Space Time Space Miser Time Miser 39

40 Stack VARIABLE POINTING TO ANOTHERBMISPREADHSEET INSTANCE BMISpreadsheet bmispreadsheet = new ABMISpreadsheet(1.77, 75); System.out.println(bmiSpreadsheet.getBMI()); bmispreadsheet = new AnotherBMISpreadsheet(); addresses variables memory Because object variable stores a pointer, it can point to variable size chunks of memory height 1.77 weight 75 height weight 0 0 bmi NaN Heap bmispreadsheet 64 40

41 RELATING INTERFACE AND CLASS NAMES Class Name: <Qualifier><Interface> - ABMISpreadsheet - ASpaceEfficientBMISpreadsheet - SpaceEfficientBMISpreadsheet <Interface><Qualifier> Impl - BMISpreadsheetImpl - BMISpreadsheetSpaceEfficientImpl Interface Name: <ClassName>Interface - ABMISpreadsheetInterface Assumes only one implementation of interface will be created 41

COMP 110/401 LEAST PRIVILEGE. Instructor: Prasun Dewan

COMP 110/401 LEAST PRIVILEGE. Instructor: Prasun Dewan COMP 110/401 LEAST PRIVILEGE Instructor: Prasun Dewan PREREQUISITE Interfaces 2 NON-PUBLIC INSTANCE VARIABLES public class ABMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; 3 MAKING

More information

COMP 401 USER INTERFACE AND ANNOTATIONS. Instructor: Prasun Dewan

COMP 401 USER INTERFACE AND ANNOTATIONS. Instructor: Prasun Dewan COMP 401 USER INTERFACE AND ANNOTATIONS Instructor: Prasun Dewan PREREQUISITES Interfaces 2 INTERACTIVE APP. VS. USER/PROG. INTERFACE public class ABMISpreadsheet implements BMISpreadsheet { double height;

More information

COMP 401 COMMAND OBJECTS AND UNDO. Instructor: Prasun Dewan

COMP 401 COMMAND OBJECTS AND UNDO. Instructor: Prasun Dewan COMP 401 COMMAND OBJECTS AND UNDO Instructor: Prasun Dewan PREREQUISITES Animation Threads Commands 2 TOPICS Command Object Object representing an action invocation such as Do your homework. Threads Support

More information

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

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan COMP 401 INHERITANCE: IS-A Instructor: Prasun Dewan PREREQUISITE Interfaces Inheritance and Arrays 2 IS-A IS-A Relationship Human IS-A Mammal Salmon IS-A Fish ACartesianPoint IS-A Point T1 IS-A T2 if T1

More information

COMP 110/401 Prasun Dewan 1

COMP 110/401 Prasun Dewan 1 4. State COMP 110/401 Prasun Dewan 1 The computations performed by the code we wrote in the previous chapter were essentially the kind we would do with a calculator. The difference was that our code was

More information

Chapter 2 Dual Roles of a Class

Chapter 2 Dual Roles of a Class Chapter 2 Dual Roles of a Class In the previous chapter, we looked very cursorily at some basic constructs in Java showing the equivalents of constructs you might have seen in other languages. With this

More information

COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan

COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan PREREQUISITES Interfaces Main Console Input Inheritance 2 INTERACTIVE APP. VS. USER/PROG. INTERFACE public class ABMISpreadsheet implements

More information

20. Assertions and Visitors

20. Assertions and Visitors COMP 401 Prasun Dewan 1 20. Assertions and Visitors Assertions are useful to convince ourselves that our programs work correctly. They can be used in formal correctness proofs. A more practical application

More information

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

COMP 110/401 DOCUMENTATION: ASSERTIONS. Instructor: Prasun Dewan COMP 110/401 DOCUMENTATION: ASSERTIONS Instructor: Prasun Dewan PREREQUISITE Documentation Annotations 2 INVALID BMI public double getbmi() { return weight/(height*height); getbmi() should really not have

More information

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

COMP 401 PATTERNS, INTERFACES AND OBJECTEDITOR. Instructor: Prasun Dewan COMP 401 PATTERNS, INTERFACES AND OBJECTEDITOR Instructor: Prasun Dewan A TALE OF TWO PIAZZA THREADS In part 1 of Assignment 5, we are required to create a line class that implements the line pattern for

More information

COMP 401 INHERITANCE. Instructor: Prasun Dewan

COMP 401 INHERITANCE. Instructor: Prasun Dewan COMP 401 INHERITANCE Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 INHERITANCE Inheritance Inheriting ancestor s traits Inheriting benefactor s assets Inheriting instance members

More information

COMP 110 Prasun Dewan 1

COMP 110 Prasun Dewan 1 7. Representation COMP 110 Prasun Dewan 1 Here we take object-based programming to the next level by gaining more experience in defining objects and learning some new concepts. Using the BMI spreadsheet

More information

COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan

COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan COMP 401 MODEL-VIEW-CONTROLLER (MVC) Instructor: Prasun Dewan PREREQUISITES Interfaces Main Console Input Inheritance 2 GENERAL PROBLEM How to break up our program into multiple classes? 3 SEPARATION OF

More information

Encapsulation in Java

Encapsulation in Java Encapsulation in Java EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Encapsulation (1.1) Consider the following problem: A person has a name, a weight, and a height. A person s

More information

COMP 110 Prasun Dewan 1

COMP 110 Prasun Dewan 1 9. Loan Case Study COMP 110 Prasun Dewan 1 In the representation section, we have studied a number of concepts, including testing, class members, structured objects, and polymorphism. These concepts were

More information

COMP 401 THREAD COORDINATION. Instructor: Prasun Dewan

COMP 401 THREAD COORDINATION. Instructor: Prasun Dewan COMP 401 THREAD COORDINATION Instructor: Prasun Dewan INTERACTIVE TRAFFIC CONTROL How does a thread wait for an event? 2 WAIT FOR ANOTHER THREAD TO RELEASE METHOD Shuttle Animation Thread 1 Waiting on

More information

COMP 110/401* Prasun Dewan 1

COMP 110/401* Prasun Dewan 1 7. Graphics COMP 110/401* Prasun Dewan 1 Here we take object-based programming to the next level by gaining more experience in defining objects and learning some new concepts. Using the BMI spreadsheet

More information

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

COMP 110 AND 401 CLASS (STATIC) STATE. Instructor: Prasun Dewan COMP 110 AND 401 CLASS (STATIC) STATE Instructor: Prasun Dewan PREREQUISITES State and Properties Interfaces 2 COUNTING INSTANCES OF ACARTESIANPOINT 3 INCREMENTED NUMBER OF INSTANCES 4 CREATING MIDPOINT

More information

COMP 401 INHERITANCE: TYPE CHECKING. Instructor: Prasun Dewan

COMP 401 INHERITANCE: TYPE CHECKING. Instructor: Prasun Dewan COMP 401 INHERITANCE: TYPE CHECKING Instructor: Prasun Dewan PREREQUISITE Inheritance 2 TYPE-CHECKING EXAMPLES StringHistory stringhistory = new AStringDatabase(); StringDatabase stringdatabase = new AStringHistory();

More information

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

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150, 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)

More information

COMP 401 FACTORIES. Instructor: Prasun Dewan

COMP 401 FACTORIES. Instructor: Prasun Dewan COMP 401 FACTORIES Instructor: Prasun Dewan NEW CONCEPTS Factory Classes Static Factory Methods Indirection Binding Time Reading Files Static Blocks Reflection Multi-Exception Catch Block Abstract Factories

More information

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

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable? Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable

More information

COMP 110 Prasun Dewan 1

COMP 110 Prasun Dewan 1 2. Objects COMP 110 Prasun Dewan 1 Now that we have a model of how the computer works, we can address the business-at-hand: how do we program the computer. Using two simple, though realistic, examples,

More information

20. Command Objects COMP 401. Reflection. Prasun Dewan 1

20. Command Objects COMP 401. Reflection. Prasun Dewan 1 COMP 401 Prasun Dewan 1 20. Command Objects We will see here that some of the concepts we use to define objects such as types (classes/interfaces) and actions (methods) can themselves be considered as

More information

20. Command Objects COMP 401. Reflection. Prasun Dewan 1

20. Command Objects COMP 401. Reflection. Prasun Dewan 1 COMP 401 Prasun Dewan 1 20. Command Objects We will see here that some of the concepts we use to define objects such as types (classes/interfaces) and actions (methods) can themselves be considered as

More information

Programming by Delegation

Programming by Delegation Chapter 2 a Programming by Delegation I. Scott MacKenzie a These slides are mostly based on the course text: Java by abstraction: A client-view approach (4 th edition), H. Roumani (2015). 1 Topics What

More information

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius;

More information

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

How a Language-based GUI Generator Can Influence the Teaching of Object-Oriented Programming How a Language-based GUI Generator Can Influence the Teaching of Object-Oriented Programming Prasun Dewan University of North Carolina Department of Computer Science Chapel Hill, NC 27599 001-919-962-1823

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects Islamic University of Gaza Faculty of Engineering Computer Engineering Dept Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn how to describe objects and classes and how to define

More information

COMP 401 GRAPHICS. Instructor: Prasun Dewan

COMP 401 GRAPHICS. Instructor: Prasun Dewan COMP 401 GRAPHICS Instructor: Prasun Dewan PREREQUISITE Interfaces 2 MORE ON OBJECTS Graphics types Test-first approach Stubs Physical vs. logical representation Representations with errors 3 MATHEMATICAL

More information

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan COMP 401 EXCEPTIONS Instructor: Prasun Dewan PREREQUISITE Inheritance Interfaces Input Iterator 2 EXCEPTIONS: WHAT? Exceptions have to do with error handling Run time Custom Error kinds Internal errors

More information

Chapter 10. Object-Oriented Thinking

Chapter 10. Object-Oriented Thinking Chapter 10 Object-Oriented Thinking 1 Class Abstraction and Encapsulation Class abstraction is the separation of class implementation details from the use of the class. The class creator provides a description

More information

COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE. Instructor: Prasun Dewan (FB 150,

COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE. Instructor: Prasun Dewan (FB 150, COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE Instructor: Prasun Dewan (FB 150, dewan@unc.edu) SCOPE: BASICS AND BEYOND Basic use: CS 1 Beyond basic use: CS2 2 DOWNLOAD FROM WWW.ECLIPSE.ORG Get the

More information

Inheritance (an intuitive description)

Inheritance (an intuitive description) Inheritance (an intuitive description) Recall the Orange class properties found in Orange are also shared with other Fruits (e.g. Apple, Banana, Pineapple) We associate behavior as well as state with with

More information

COMP 401 Fall Recitation 6: Inheritance

COMP 401 Fall Recitation 6: Inheritance COMP 401 Fall 2017 Recitation 6: Inheritance Agenda Brief Review of Inheritance Examples of extending existing classes Exercises and Quiz 2 High-level Classes are Abstract Data Types We can define a set

More information

COMP 401 COPY: SHALLOW AND DEEP. Instructor: Prasun Dewan

COMP 401 COPY: SHALLOW AND DEEP. Instructor: Prasun Dewan COMP 401 COPY: SHALLOW AND DEEP Instructor: Prasun Dewan PREREQUISITE Composite Object Shapes Inheritance 2 CLONE SEMANTICS? tostring() Object equals() clone() Need to understand memory representation

More information

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is

More information

Lec 3. Compilers, Debugging, Hello World, and Variables

Lec 3. Compilers, Debugging, Hello World, and Variables Lec 3 Compilers, Debugging, Hello World, and Variables Announcements First book reading due tonight at midnight Complete 80% of all activities to get 100% HW1 due Saturday at midnight Lab hours posted

More information

Chapter 6: Inheritance

Chapter 6: Inheritance Chapter 6: Inheritance EECS 1030 moodle.yorku.ca State of an object final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT);

More information

Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I

Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I Attempt Four questions Time: 120 mins 1. Examine the following

More information

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010 CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010 Personal Information: Name: Section: Student ID #: TA: You have 50 minutes to complete this exam. You may receive a deduction if you keep working

More information

CP122 Computer Science I. Chapter 2: Using Objects

CP122 Computer Science I. Chapter 2: Using Objects CP122 Computer Science I Chapter 2: Using Objects Tech News! Cyber Monday: $3.3B https://youtu.be/r4rfcay9fiq Tech News! Cyber Monday: $3.3B https://youtu.be/r4rfcay9fiq Small drone warfare https://cdn1.tnwcdn.com/wpcontent/blogs.dir/1/files/2016/11/ezgif.comoptimize-1-1.mp4

More information

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015 COMP 110-001 Information Hiding and Encapsulation Yi Hong June 03, 2015 Review of Pass-By-Value What is the output? public void swap(student s1, Student s2) Student s3 = s1; s1 = s2; s2 = s3; Student berkeley

More information

Comp 401 Assignment 6: Collections

Comp 401 Assignment 6: Collections Comp 401 Assignment 6: Collections Date Assigned: Wed Sep 28, 2011 Completion Date: Fri Oct 7, 2011 Early Submission Date: Wed Oct 5, 2011 In this assignment, you will use arrays to create different variable-sized

More information

COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan

COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan COMP 401 ABSTRACT CLASSES Instructor: Prasun Dewan PREREQUISITE Inheritance 2 TOPICS Top-Down vs. Bottom-Up Inheritance Abstract Classes 3 COURSE DISPLAYER User inputs course title Program displays course

More information

Introduction to Programming (Java) 4/12

Introduction to Programming (Java) 4/12 Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

TRACEABLE ALGORITHMS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill

TRACEABLE ALGORITHMS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill TRACEABLE ALGORITHMS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill dewan@cs.unc.edu Code available at: https://github.com/pdewan/colabteaching PRE-REQUISITES Model-Interactor

More information

COMP-202: Foundations of Programming. Lecture 11: ArrayList, and Linked List Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 11: ArrayList, and Linked List Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 11: ArrayList, and Linked List Sandeep Manjanna, Summer 2015 Announcements Assignment 4 will be posted by the end of day today. Course Evaluations are now open.

More information

COMP 401 ANIMATION, THREADS, COMMAND OBJECTS. Instructor: Prasun Dewan

COMP 401 ANIMATION, THREADS, COMMAND OBJECTS. Instructor: Prasun Dewan COMP 401 ANIMATION, THREADS, COMMAND OBJECTS Instructor: Prasun Dewan PREREQUISITE Animation MVC 2 TOPICS Animation Command Object Object representing an action invocation such as Do your homework. Threads

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming - Encapsulation Data & code bound together - > objects - Polymorphism Overloading of constructors & methods - Inheritance A class incorporates another class into its declaration

More information

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan COMP 110/401 COLLECTION KINDS Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 COLLECTION TYPES StringHistory, StringDatabase, StringSet Array ArrayList, List Map Stack Queue 3

More information

Chapter 2: Java OOP I

Chapter 2: Java OOP I Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control OO Concepts Object Oriented Methods Object An

More information

Object Oriented Programming

Object Oriented Programming Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 11 Object Oriented Programming Eng. Mohammed Alokshiya December 16, 2014 Object-oriented

More information

Interfaces & Generics

Interfaces & Generics Interfaces & Generics CSC207 Winter 2018 The Programming Interface The "user" for almost all code is a programmer. That user wants to know:... what kinds of object your class represents... what actions

More information

COMP 102: Test August, 2017

COMP 102: Test August, 2017 Family Name:.......................... Other Names:.......................... Student ID:............................ Signature.............................. COMP 102: Test 1 14 August, 2017 Instructions

More information

1. Word Analysis: (Which nouns suggest a need for memory (a variable) and which verbs suggest a need for action (a function maybe).

1. Word Analysis: (Which nouns suggest a need for memory (a variable) and which verbs suggest a need for action (a function maybe). Program 3 Seven Step Problem Solving Methodology Skeleton Problem Statement for Body Mass Index Program Enhancement: Design a program that calculates a person s body mass index (BMI). The BMI is often

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Ray John Pamillo 1/27/2016 1 Nokia Solutions and Networks 2014 Outline: Brief History of OOP Why use OOP? OOP vs Procedural Programming What is OOP? Objects and Classes 4 Pillars

More information

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

IT101. Inheritance, Encapsulation, Polymorphism and Constructors IT101 Inheritance, Encapsulation, Polymorphism and Constructors OOP Advantages and Concepts What are OOP s claims to fame? Better suited for team development Facilitates utilizing and creating reusable

More information

Week 3 Classes and Objects

Week 3 Classes and Objects Week 3 Classes and Objects written by Alexandros Evangelidis, adapted from J. Gardiner et al. 13 October 2015 1 Last Week Last week, we looked at some of the different types available in Java, and the

More information

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

Rectangle. Lecture 2 week 2 - Objects & Classes Exercise 1. A. Write a driver class code in java depending on the following UML:

Rectangle. Lecture 2 week 2 - Objects & Classes Exercise 1. A. Write a driver class code in java depending on the following UML: Lecture 2 week 2 - Objects & Classes Exercise 1 A. Write a driver class code in java depending on the following UML: Rectangle - height : double - width : double Rectangle()

More information

Checklist (for Today) What we are reinforcing with the exercises this class. recognizing the various different arithmetic operators (in situ)

Checklist (for Today) What we are reinforcing with the exercises this class. recognizing the various different arithmetic operators (in situ) CSE1710 Click to edit Master Week text 03, styles Lecture 06 Second level Third level Fourth level Fifth level Fall 2013 Thursday, Sept 26, 2013 1 Checklist (for Today) What we are reinforcing with the

More information

Comments are almost like C++

Comments are almost like C++ UMBC CMSC 331 Java Comments are almost like C++ The javadoc program generates HTML API documentation from the javadoc style comments in your code. /* This kind of comment can span multiple lines */ //

More information

Comp Assignment 4: Commands and Graphics

Comp Assignment 4: Commands and Graphics Comp 401 - Assignment 4: Commands and Graphics Date Assigned: Thu Sep 12, 2013 Completion Date: Fri Sep 20, 2013 Early Submission Date: Wed Sep 18, 2013 This assignment has two parts having to do with

More information

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8 Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

More information

CPSC 233 Final Exam, Winter 2003 DEPARTMENT OF COMPUTER SCIENCE THE UNIVERSITY OF CALGARY Time: 120 minutes 100 marks total L02, L03, L04

CPSC 233 Final Exam, Winter 2003 DEPARTMENT OF COMPUTER SCIENCE THE UNIVERSITY OF CALGARY Time: 120 minutes 100 marks total L02, L03, L04 CPSC 233 Final Exam, Winter 2003 DEPARTMENT OF COMPUTER SCIENCE THE UNIVERSITY OF CALGARY Time: 120 minutes 100 marks total L02, L03, L04 First Name Last Name Check the box to indicate the lecture that

More information

Review: Array Initializer Lists

Review: Array Initializer Lists More on Arrays Review of Arrays of ints, doubles, chars Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 7.2 Reading for this lecture: L&L 7.3 7.7,

More information

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved. Java Foundations 7-2 Copyright 2015, Oracle and/or its affiliates. All rights reserved. Objectives This lesson covers the following objectives: Understand the memory consequences of instantiating objects

More information

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line A SIMPLE JAVA PROGRAM Class Declaration The Main Line INDEX The Line Contains Three Keywords The Output Line COMMENTS Single Line Comment Multiline Comment Documentation Comment TYPE CASTING Implicit Type

More information

BM214E Object Oriented Programming Lecture 6. Classes and Objects

BM214E Object Oriented Programming Lecture 6. Classes and Objects BM214E Object Oriented Programming Lecture 6 Classes and Objects Classes and Objects Class: describes the form of an object, a template or blueprint or mold specifies data representation, behavior, and

More information

CS 302 Week 9. Jim Williams

CS 302 Week 9. Jim Williams CS 302 Week 9 Jim Williams This Week P2 Milestone 3 Lab: Instantiating Classes Lecture: Wrapper Classes More Objects (Instances) and Classes Next Week: Spring Break Will this work? Double d = new Double(10);

More information

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

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

Recitation 3 Class and Objects

Recitation 3 Class and Objects 1.00/1.001 Introduction to Computers and Engineering Problem Solving Recitation 3 Class and Objects Spring 2012 1 Scope One method cannot see variables in another; Variables created inside a block: { exist

More information

CS106A, Stanford Handout #18. Writing a Class

CS106A, Stanford Handout #18. Writing a Class CS106A, Stanford Handout #18 Fall, 2004-05 Nick Parlante Writing a Class Up to now we have used classes like Binky and DWindow defined by others, but now it is time to see how to write our own classes.

More information

Objects and Classes (1)

Objects and Classes (1) Objects and Classes (1) Reading: Classes (interface, implementation, garbage collection) http://moodle.cs.man.ac.uk/course/view.php?id=81 Interface Examples Creating and using objects of existing classes

More information

COMP 102: Test August, 2017

COMP 102: Test August, 2017 Family Name:.......................... Other Names:.......................... Student ID:............................ Signature.............................. COMP 102: Test 1 14 August, 2017 Instructions

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Sunday, Tuesday: 9:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming A programming

More information

Test Code Patterns. How to design your test code

Test Code Patterns. How to design your test code Test Code Patterns How to design your test code Testing and Inheritance n Should you retest inherited methods? n Can you reuse superclass tests for inherited and overridden methods? n To what extent should

More information

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders CMSC131 Creating a Datatype Class Continued Exploration of Memory Model Reminders The name of the source code file needs to match the name of the class. The name of the constructor(s) need(s) to match

More information

Testing and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs

Testing and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs Test Code Patterns How to design your test code Testing and Inheritance n Should you retest inherited methods? n Can you reuse superclass tests for inherited and overridden methods? n To what extent should

More information

Separate Compilation Model

Separate Compilation Model Separate Compilation Model Recall: For a function call to compile, either the function s definition or declaration must appear previously in the same file. Goal: Compile only modules affected by recent

More information

Collections and Inheritance

Collections and Inheritance COMP 401 Prasun Dewan 1 Collections and Inheritance In the previous chapter, we defined logical structures with fixed number of nodes. In this chapter, we will see how we can use arrays to define types

More information

Warmup : Name that tune!

Warmup : Name that tune! Warmup : Name that tune! Write, using a loop, Java code to print the lyrics to the song 99 Bottles of Beer on the Wall 99 bottles of beer on the wall. 99 bottles of beer. Take one down, pass it around,

More information

Processes, Threads and Processors

Processes, Threads and Processors 1 Processes, Threads and Processors Processes and Threads From Processes to Threads Don Porter Portions courtesy Emmett Witchel Hardware can execute N instruction streams at once Ø Uniprocessor, N==1 Ø

More information

Variables and Java vs C++

Variables and Java vs C++ Variables and Java vs C++ 1 What can be improved? (variables) public void godirection(string directionname) { boolean wenttoroom = false; for (Direction direction : currentroom.getdirections()) { if (direction.getdirectionname().equalsignorecase(directionname))

More information

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score. CS 1331 Exam 1 Fall 2016 Name (print clearly): GT account (gpburdell1, msmith3, etc): Section (e.g., B1): Signature: Failure to properly fill in the information on this page will result in a deduction

More information

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan COMP 110/401 COLLECTION KINDS Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 STATIC VS. DYNAMIC STRUCTURES Static Beans have fixed number of properties Arrays have fixed number

More information

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

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad

More information

INTERFACES. 24-Dec-10 INTERFACES VS. INHERITANCE. Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman ) Interfaces: Inheritance:

INTERFACES. 24-Dec-10 INTERFACES VS. INHERITANCE. Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman ) Interfaces: Inheritance: My name is Ryan; I inherited the ship from the previous Dread Pirate Roberts, just as you will inherit it from me. The man I inherited it from is not the real Dread Pirate Roberts either. His name was

More information

Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman )

Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman ) My name is Ryan; I inherited the ship from the previous Dread Pirate Roberts, just as you will inherit it from me. The man I inherited it from is not the real Dread Pirate Roberts either. His name was

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Tuesday: 9:30-11:59 AM Thursday: 10:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Computer Science II Lecture 1 Introduction and Background

Computer Science II Lecture 1 Introduction and Background Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,

More information

The Singleton Pattern. Design Patterns In Java Bob Tarr

The Singleton Pattern. Design Patterns In Java Bob Tarr The Singleton Pattern Intent Ensure a class only has one instance, and provide a global point of access to it Motivation Sometimes we want just a single instance of a class to exist in the system For example,

More information

CS Programming I: Classes

CS Programming I: Classes CS 200 - Programming I: Classes Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Classes 1/23

More information

INF 212 ANALYSIS OF PROG. LANGS PLUGINS. Instructors: Crista Lopes Copyright Instructors.

INF 212 ANALYSIS OF PROG. LANGS PLUGINS. Instructors: Crista Lopes Copyright Instructors. INF 212 ANALYSIS OF PROG. LANGS PLUGINS Instructors: Crista Lopes Copyright Instructors. Modules as conceptual units Modules as physical components Software modules as physical components Source components

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information