Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Object Oriented Software Development
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Lecture 5 What is object-oriented programming OOP techniques How Windows Forms applications rely on OOP.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
CSE 1201 Object Oriented Programming ArrayList 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
OOP Basics Classes & Methods (c) IDMS/SQL News
Unified Modeling Language (UML)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Coding 6 David Davenport Computer Eng. Dept.,
Lecture 12 Inheritance.
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Java Coding 4 David Davenport Computer Eng. Dept.,
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

OOP Object Oriented Programming

Structured Programming Assume familiar with… data representation & storage sequence, decision, repetition input, output, assignment defining & using methods use of actual & formal parameters return values Tracing, debugging, pre/post cond’s Top-Down, Structured design

Object-Oriented Programming How does OOP help? Facilitates reuse (through components & inheritance) Facilitates design (provides a natural way to model world) OOP incorporates Abstraction, Encapsulation, Inheritance & Polymorphism Paradigm Shifts Go To Programming Structured Programming Object-Oriented Programming Deliver maintainable error-free programs on-time, in-budget! ???

The world is a set of things interacting with each other. Things have State/properties/attributes Services/functionality/behaviour We group things into categories (share properties/functionalities) & sub-categories… The world as we “see” it… create objects methods main method call methods data class hierarchy class objects

OOP Example Implement this UML class diagram in Java MyClass +a : int -b : int #c : int +MyClass( r : int) +f1() : int +f2( s : int) : int +f3( t : int) : int TestMyClass +main( args) Additional info… Class/static variable c should be initialised to 3 Instance variable a to the value of parameter r Instance variable b to the value -1 f1 should compute the square of a f2 computes the sum of b and parameter s f3 computes the result of dividing parameter t by c

MyClass - implementation // MyClass // David, 29/01/2007 public class MyClass { public int a; private int b; staticint c = 3: public MyClass( int r) { a = r; b = -1; } public int f1() { return a * a; } public int f2( int s) { return b + s; } public static int f3( int t) { return t/c; } } // end of class MyClass

TestMyClass - questions What is the output of the following… MyClass x, y, z; x = new MyClass( 4); y = new MyClass( 5); System.out.println( x.f1() ); System.out.println( x.f2( 15) ); System.out.println( x.f3(6) ); System.out.println( MyClass.f3( 6) ); System.out.println( MyClass.c ); System.out.println( x.a ); System.out.println( y.a ); z = y; System.out.println( z.a ); System.out.println( z.f2( 7) ); x = null: System.out.println( x.a ); x = z.clone(); System.out.println( x.a ); System.out.println( MyClass.a );

MyClass – more questions More things to consider… - What is the function of constructors? - What differentiates them from other methods? - Add a default constructor (one with no parameters) - Add another method called f2 having two parameters, demo it. - What is the technical term for having two methods with the same name but different signatures? - Add a toString method - Add an equals method (does it have to include all property values?) - Add a clone method (or at least consider whether or not it is needed.) - Can you replace c with a in f3? Why/why not? - Can you replace b with c in f2? Explain. - Implement f1 using a loop (what is the meaning/use of local variables) - Add another method, f4, that takes a parameter of type MyClass. Use it to demo “pass-by-reference” vs “pass-by-value” and show when/how the object & its properties may change in the calling method.

Arrays Common Data Structure

Arrays Common data structure All elements of same type Are Objects in Java grades Access individual element using [] notation, e.g. grades[3] Each element identified by an index (label/subscript) Name/reference to entire structure

Arrays – usage (1) String[] seasons = { “Spring”, “Summer”, “Autumn”, “Winter” }; int i = 1; System.out.println( seasons[0] ); System.out.println( seasons[length-1] ); System.out.println( seasons.length ); System.out.println( seasons[i] ); System.out.println( seasons[i+1] ); 1230 seasons SpringSummerAutumnWinter

Arrays – usage (2) int[] x; System.out.println( x[0] ); x = new int[100]; x[0] = 0; x[1] = 1; x[2] = 4 x[3] = x[2] * 2 + x[1]; System.out.println( x[3] ); for ( int k = 0; k < x.length; k++) x[k] = k * k; for ( int k = 0; k < x.length; k++) System.out.println( x[k] ); int[] x; System.out.println( x[0] ); x = new int[100]; x[0] = 0; x[1] = 1; x[2] = 4 x[3] = x[2] * 2 + x[1]; System.out.println( x[3] ); int[] x; System.out.println( x[0] );

Using part of an array (1) Array size specified & fixed at instantiation Problem if number of elements required is unknown? Solution make big enough for worst-case & use part of it Must divide array into two sets, in-use & not in-use … but how? grades in-usenot in-use One simple & common solution

Using part of an array (2) Store elements sequentially from element zero Keep count of number of in-use elements (valid) grades ??? 567 in-usenot in-use 5 valid 8 maxEls Now process only valid elements, not maxEls array length