Mo#va#ng the OO Way. COMP 401, Fall 2017 Lecture 05

Similar documents
Strings, Arrays, A1. COMP 401, Spring 2015 Lecture 4 1/20/2015

Array Basics: Outline

Array Basics: Outline

Strings and Arrays. COMP 401, Fall 2017 Lecture 4

Array Basics: Outline

Encapsula)on, cont d. Polymorphism, Inheritance part 1. COMP 401, Spring 2015 Lecture 7 1/29/2015

CH3: C# Programming Basics BUILD YOUR OWN ASP.NET 4 WEB SITE USING C# & VB

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

Object Oriented Programming. Feb 2015

Encapsula)on CMSC 202

Classes & Objects CMSC 202

Objec&ves. Packages Collec&ons Generics. Sept 28, 2016 Sprenkle - CSCI209 1

Ch. 6. User-Defined Methods

CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

Data Base Management System LAB LECTURES

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Overview. A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Merge Sort

Declaring and ini,alizing 2D arrays

Learning objec<ves. Classes, Objects, and Methods. Problem Decomposi<on. Problem Decomposi<on (cont d)

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

Related Course Objec6ves

11/12/11. Objec&ves Overview. Databases, Data, and Informa&on. Objec&ves Overview. Databases, Data, and Informa&on. Databases, Data, and Informa&on

Methods. Methods. Mysteries Revealed

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

A Founda4on for Programming

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Algorithms Lecture 11. UC Davis, ECS20, Winter Discrete Mathematics for Computer Science

CSE Lecture 10: Modules and separate compila5on 18 Feb Nate Nystrom University of Texas at Arlington

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

Design Principles & Prac4ces

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

Subclassing, con.nued Method overriding, virtual methods, abstract classes/methods. COMP 401, Spring 2015 Lecture 9 2/19/2015

Thinking Induc,vely. COS 326 David Walker Princeton University

Func+on applica+ons (calls, invoca+ons)

method method public class Temperature { public static void main(string[] args) { // your code here } new

L6: System design: behavior models

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Component diagrams. Components Components are model elements that represent independent, interchangeable parts of a system.

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Module 01 Processing Recap

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Principles of Programming Languages

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Classes and objects. Chapter 2: Head First Java: 2 nd Edi4on, K. Sierra, B. Bates

Object Oriented Design (OOD): The Concept

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

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

Java's Memory Management

Programming Languages and Techniques (CIS120)

Mul$media Techniques in Android. Some of the informa$on in this sec$on is adapted from WiseAndroid.com

Classify Triangles. by the Angle Measure &The Side Lengths. Properties a SCALENE Triangle angles 1.Sum of the interior

Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

Objec0ves. Gain understanding of what IDA Pro is and what it can do. Expose students to the tool GUI

Local defini1ons. Func1on mul1ples- of

BBM 102 Introduc0on to Programming II Spring 2014

Programming Languages and Techniques (CIS120)

Introduc)on to Classes and Objects. Dr. Bowen Hui Computer Science University of Bri)sh Columbia Okanagan

C Pointers. ENGG1002 Computer Programming and Applica;ons Dr. Hayden Kwok Hay So Week 2. GeGng Hold of Memory. Sta;c: When we define a variable

CS 251 Intermediate Programming Methods and Classes

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CS 10: Problem solving via Object Oriented Programming. Lists Part 1

3 Vectors and the Geometry of Space

Writing a Fraction Class

The return Statement

Preliminary ACTL-SLOW Design in the ACS and OPC-UA context. G. Tos? (19/04/2016)

VARIABLES AND TYPES CITS1001

CSE Compilers. Reminders/ Announcements. Lecture 15: Seman9c Analysis, Part III Michael Ringenburg Winter 2013

COMP 110/L Lecture 4. Kyle Dewey

Java Review. Fundamentals of Computer Science

CS110D: PROGRAMMING LANGUAGE I

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

Object-Oriented Programming

CS177 Python Programming. Recita4on 2 - Compu4ng with Numbers

CISC327 - So*ware Quality Assurance

Itera0ve version. Have a array or vector called A. Want to find the maximum element:

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Introduc3on to Data Management

Programming for Engineers in Python

Proofs about Programs

Module 01 Processing Recap. CS 106 Winter 2018

Ways to implement a language

Write a program that draws this image. The ligle squares are 5x5. The screen is the default 100x100. It doesn t change.

COMPUTER APPLICATIONS

CS 251 Intermediate Programming Methods and More

Common Loop Algorithms 9/21/16 42

Ø Interface methods are public by default

Evalua&ng Secure Programming Knowledge

Lab Midterms Not Graded Yet

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Lecture 02, Fall 2018 Friday September 7

Programming Languages and Techniques (CIS120)

CS 101: Computer Programming and Utilization

Lecture 13: Abstract Data Types / Stacks

Arrays. Loops + arrays: challenge problem. Arrays (II) An array is a set of variables of the same type accessed by their index.

Through Python. "Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura..." Dante Alighieri

A formal design process, part 2

Transcription:

Mo#va#ng the OO Way COMP 401, Fall 2017 Lecture 05

Arrays Finishing up from last #me

Mul#dimensional Arrays Mul#dimensional array is simply an array of arrays Fill out dimensions lef to right. int[][] marray = new int[5][]; for(int i=0; i<5; i++) { marray[i] = new int[10]; } Each subarray can have an independent size. Some#mes known as as a ragged or uneven array int[][] marray = new int[5][]; for (int i=0; i<5; i++) { marray[i] = new int[i+1]; } If each sub-dimension is same size, we can create it with a single new statement int[][] marray = new int[5][10];

Arrays u#lity class Arrays is a library of useful func#ons for manipula#ng arrays Note s in Arrays Like Math class, all methods are sta#c binarysearch sort filling and copying subranges htp://docs.oracle.com/javase/8/docs/api/java/u#l/arrays.html

Lecture 4, Example 6 Uses scanner to read input. Expects input to be a number indica#ng a size and then one of the following words: integer, real, string Creates an array of that size of the corresponding type (i.e., int, double, or String) Uses a loop to read in that many of the appropriate type into the array. Prints the array. Does it all over again indefinitely.

Mo#va#ng the OO Way

TriangleAreaApp example Write a program that reads input as sequences of triangle defini#ons. Each triangle defined by an iden#fying name as a single word followed by 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle: Categorize triangle as one of equilateral, isosceles, scalene Report triangle category for each triangle AFer end of all input Report average size of triangles by category Report area of smallest triangle

TriangleAreaApp ta.v01 Write a program that reads input as sequences of triangle defini#ons. Each triangle defined by an iden#fying name as a single word followed by 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle: Categorize triangle as one of equilateral, isosceles, scalene Report triangle category for each triangle AFer end of all input Report average size of triangles by category Report area of smallest triangle

TriangleAreaApp ta.v02 Write a program that reads input as sequences of triangle defini#ons. Each triangle defined by an iden#fying name as a single word followed by 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle: Categorize triangle as one of equilateral, isosceles, scalene Report triangle category for each triangle AFer end of all input Report average size of triangles by category Report area of smallest triangle

TriangleAreaApp ta.v03 Write a program that reads input as sequences of triangle defini#ons. Each triangle defined by an iden#fying name as a single word followed by 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle: Categorize triangle as one of equilateral, isosceles, scalene Report triangle category for each triangle AFer end of all input Report average size of triangles by category Report area of smallest triangle

TriangleAreaApp ta.v04 Write a program that reads input as sequences of triangle defini#ons. Each triangle defined by an iden#fying name as a single word followed by 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle: Categorize triangle as one of equilateral, isosceles, scalene Report triangle category for each triangle AFer end of all input Report average size of triangles by category Report area of smallest triangle

Review of non-oo approach All func#ons are sta#c Variables are all either declared locally or passed in as parameters Sta#c class func#ons simply act as a library of triangle-related func#ons used by our applica#on.

Thinking with an object mindset Consider the role of ax, ay, bx, by, cx, cy As a collec#ve, the represent a specific triangle. Consider the func#ons for finding area and classifying Onus on us to provide this informa#on as parameters As well as ensuring that they actually represent a triangle. Object-oriented programming flips this rela#onship. Formalizes the collec#ve meaning of these pieces of informa#on as an abstrac#on. Abstrac#on provides means to query proper#es and invoke behavior

Step 1: Name the abstrac#on In Java this means create a class corresponding to the abstrac#on s name. ta.v05

Step 2: Declare its fields The fields of your abstrac#on are pieces of informa#on that collec#vely define it. Declared like variables Must specify type and adhere to variable naming rules. Declared in class defini#on NOT within a method, but floa#ng off by themselves. Good idea to keep them together at the top of the class. Here you start to make design decisions In our example, triangles defined by 3 coordinates. How else could a triangle be defined? Note that part of this is deciding on types as well. What would be impact of choosing something other than double? ta.v06

Step 3: Define a constructor Constructor is a special type of method Job is to create and ini#alize a new instance. Declara#on differs from a normal method Name must match class name. Does not have a any sort of return value in its signature. Within the constructor, the keyword this refers to the new object to be ini#alized. Any informa#on needed should be passed in as parameters. Code in the constructor is responsible for making sure that the fields of this are appropriately set. To call a constructor, use the new keyword Result will be a new instance (i.e., object) of the class ta.v07

Step 4: Define instance methods Func#ons/procedures that depend on the specific instance What func#ons in our example calculate values specific to a par#cular triangle? triangle_category triangle_area Declare instance methods without sta#c keyword That is what makes it an instance method. Instance methods only make sense in the context of a specific instance. Must be called with the. operator using a reference to an object. reference.method() Within an instance method, the keyword this provides a reference to the object itself. To get to a specific instance field: this.field If unambiguous, then this can be lef off. Must use this keyword if another local variable or parameter name conflicts with field name. ta.v08

One more improvement No#ce that within area() and category() we end up calcula#ng side lengths. Would be beter if we simply provided methods for retrieving each of the side lengths this.side_ab() this.side_bc() this.side_ca() Implied this also works for method names Don t need to use this keyword if the method is being called for the current object ta.v09

Repea#ng with Point Consider role of ax, ay within Triangle class Collec#vely they represent a point Same with bx, by and cx, cy Opportunity for abstrac#on again. ta.v10 No#ce name conflict in constructor between parameters passed in and field names. Forces use of this keyword when assigning fields. This is a common idiom for constructors. But otherwise, you generally want to avoid having method parameter names or local variable names that shadow field names.

Classes and Objects Fundamental units of abstrac6on Physical Analogy Classes are like factories Contain a blueprint for an object Defines the inner workings (i.e., fields aka members) Defines what it can do, its behavior (i.e., instance methods) Factory itself may have some capabili#es Class members and class methods Useful for defining named constants and helper methods that are related to the abstrac#on as a whole but not specific to an instance. Objects are what the factory builds Each object is an instance of a class Name of the class is the type of the object. Which means the class name is the type we use for a variable that can reference the object.

Objects as state An object is defined by its state Collec#on of named fields that represent informa#on about the object The current values assigned to those fields reflect the state of the object Object design reflects purpose What fields to include in an object will depend on how that object is to be used and the kinds of opera#ons that object will be involved in.

Sta#c class fields Instance fields are data associated with each instance Every object has it s own set of values Class fields are data associated with the class as a whole. Declared like an instance field, but with sta#c keyword. Like class methods, you can access them via the class name or directly by code within the class. Most common use: Named Constants Best prac#ce: all caps, ini#alized when declared, declared with final keyword to indicate that it won t ever change. ta.v11