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