
Object Oriented Programming Designing and Object Oriented Programming is not new. – Simula 1 (1962 - 1965) and Simula 67 (1967) Norwegian Implementing Classes Computing Center, Oslo, Norway by Ole-Johan Dahl and Kristen Nygaard. Topic 6 – Smalltalk (1970s), Alan Kay's group at Xerox PARC – C++ (early 1980s), Bjarne Stroustrup, Bell Labs Don't know much geography – Eiffel, Bertrand Meyer Don't know much trigonometry – Java and others Don't know much about algebra Becoming the dominant style for implementing Don't know what a slide rule is for complex programs with large numbers of -Sam Cooke interacting components and in education AP Computer Science Designing and Implementing Classes 1 AP Computer Science Designing and Implementing Classes 2 Tenants of Object Oriented Main Tenets of OO Programming Programming - Encapsulation Encapsulation information hiding, using something without – abstraction, information hiding, responsibility knowing (or caring or having to worry) about driven programming how it works. abstraction Inheritance achieved via classes and objects. Useful in – code reuse, specialization breaking up large problems based on a study Polymorphism of the data involved first – allows old code to use new code, do X, where X Don Knuth, Turing award winner is different depending on the type of object algorithms + data structures = programming AP Computer Science Designing and Implementing Classes 3 AP Computer Science Designing and Implementing Classes 4 Class and Objects Using Objects What are classes? An object is a model (abstraction of) a real- – classes are user defined data types. (as opposed to the primitive data types) world object. – classes specify the operations that may be carried out on – it is a collection of variables (fields) and methods variables of this type. Behaviors A method is an operation on an object. – They also specify what sub data the variable consists of and have the implementation of the behaviors listed Objects interact with messages. A message above. is sent to an object and specifies the method – objects are simply variables of a given class that object should perform. It is easier to describe how to use an object than it – The sender (normally another object) doesn't is to create a class know anything about how the method is – easier to teach some one to drive car or teach someone performed how to design and build a car? AP Computer Science Designing and Implementing Classes 5 AP Computer Science Designing and Implementing Classes 6 Using Objects The dot Operator Objects are variables, they have a declared type Once an object is created the dot operator and a set of behaviors or operations ( . ) is used to perform operations on the object or access sub data in the object String name1 = new String("Olivia A. Scott"); String name2 = new String("Isabelle M. Scott"); String name1 = new String("Olivia A. Scott"); char first = name1.charAt(0); All object variables are reference variables (i.e. char second = name2.charAt(1); pointers) Any public part of the object may be accessed. Objects must be dynamically allocated using the normally only methods are public. new operator and calling a constructor – Must call a method that actually exists and match one of – special cases for Strings and arrays the parameter signatures or syntax error To determine the various behaviors of an object the dot operator calls or invokes the method must look at the class documentation flow of control shifts to that code AP Computer Science Designing and Implementing Classes 7 AP Computer Science Designing and Implementing Classes 8 Object Data Object Methods occasionally fields of an object are public, but this Object methods either is generally considered bad style – tell you something about the object array length vs. String length • accessors, have a return value int[] list = new int[x]; • the charAt and length methods in String for(int i = 0; i < list.length; i++) – change the object in some way Only a good idea if the data is constant. • mutators, no return value. Changes the state of the Good object oriented programming style calls for object any changes to an object to be done via a method • the setSize method in Rectangle call – do both, some people consider this bad style Normally a data's object is hidden (encapsulated) • mutators that return a value, return value and a and if I am using an object I don't know or care change to the object's state about the details of that implementation AP Computer Science Designing and Implementing Classes 9 AP Computer Science Designing and Implementing Classes 10 Finding Object Methods Classes fundamental unit of OO programming JavaTM 2 Platform, Standard Edition, v 1.4.x API Specification Java programs of any significance are a – Your best friend in the world collection of classes http://java.sun.com/j2se/1.4.1/docs/api/ Has specification for all classes in the Java standard library This is a rather smallish Only documents the public portion, what we program and it contains can use! 6 classes not counting another 6 classes from A product of javadoc the Java Standard Library java source code in -> easy to read html out AP Computer Science Designing and Implementing Classes 11 AP Computer Science Designing and Implementing Classes 12 What are Classes? More on Classes classes are blueprints Each class in Java is contained in its own file with the name – they specify what new objects will contain and what matching the class name and with the .java extension can be done to the objects The basics elements of a class are – classes are analogous to data types and objects are – constructors • used in the creation of objects. Similar to methods (procedures and the variables of that type functions). classes consist of an interface (what is it?) and • Normally several overloaded for the convenience of the class users an implementation (how it is done?) – methods • instance methods and class (static) methods – interface describes the ways an object of this type may – variables (or fields) be created via constructors and how a client may use • instance variables and class variables (static) or manipulate an object of this type via methods – constants – implementation is hidden and of no concern to the • instance (rare) and class (static) client. implementation can be changed without affecting any client code AP Computer Science Designing and Implementing Classes 13 AP Computer Science Designing and Implementing Classes 14 The Die Class The Die Class Interface Consider a class used Constructors (used in creation of objects) to model a die – default, single int parameter to specify the What is the interface? What number of sides, int and boolean to determine if actions should a die be able should roll to perform? Mutators (change state of objects) –roll Accessors (do not change state of objects) – getTopSide, getNumSides, toString The methods or behaviors can be broken up Public constants into constructors, mutators, accessors – DEFAULT_SIDES AP Computer Science Designing and Implementing Classes 15 AP Computer Science Designing and Implementing Classes 16 Visibility Modifiers The Die Class Implementation All parts of a class have visibility modifiers Implementation is made up of constructor code, method code, and private data members of the – Java keywords class. – public, protected, private, (no modifier means package access) scope of data members / instance variables – private data members may be used in any of the – do not use these modifiers on local variables (syntax error) constructors or methods of a class public means that constructor, method, or field may Implementation is hidden from users of a class and be accessed outside of the class. can be changed without changing the interface or – part of the interface affecting clients (other classes that use this class) – constructors and methods are generally public – Example: Previous version of Die class, private means that part of the class is hidden and DieVersion1.java Once Die class completed can be used in anything inaccessible by code outside of the class requiring a Die or situation requiring random – part of the implementation numbers between 1 and N – data fields are generally private – DieTester class. What does it do? AP Computer Science Designing and Implementing Classes 17 AP Computer Science Designing and Implementing Classes 18 DieTester method DieTester continued int total = 0; int numRolls = 0; public static void main(String[] args) { do final int NUM_ROLLS = 50; { d1.roll(); final int TEN_SIDED = 10; d2.roll(); Die d1 = new Die(); d3.roll(); Die d2 = new Die(); total = d1.getTopSide() + d2.getTopSide () Die d3 = new Die(TEN_SIDED); + d3.getTopSide (); final int MAX_ROLL = d1.getNumSides() + numRolls++; d2.getNumSides() + d3.getNumSides(); } while(total != MAX_ROLL); for(int i = 0; i < NUM_ROLLS; i++) System.out.println("\n\nNumber of rolls to get " { d1.roll(); + MAX_ROLL + " was " + numRolls); d2.roll(); System.out.println("d1: " + d1.getResult() + " d2: " + d2.getTopSide() + " Total: " + (d1.getTopSide() + d2.getTopSide() ) ); } AP Computer Science Designing and Implementing Classes 19 AP Computer Science Designing and Implementing Classes 20 The Steps of Class Design Correctness Sidetrack Requirements When creating the public interface of a class give – what is the problem to be solved careful thought and consideration to the contract – detailed requirements lead to specifications you are creating between yourself and users (other Nouns may be classes programmers) of your class Use preconditions to
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages11 Page
-
File Size-