CUSTOM OOP. SYNTAX OF A CLASS DEFINITION Class definitions (descriptions) look like this: class ClassName { Descriptions of the instance variables and.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Chapter 4: Writing Classes
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
C++ fundamentals.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Guide to Programming with Python
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
4.1 Instance Variables, Constructors, and Methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CPS120: Introduction to Computer Science Lecture 14 Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Classes, Interfaces and Packages
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
OOP Basics Classes & Methods (c) IDMS/SQL News
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
DESIGNING CLASSES. SPECIFICATIONS FOR THE CAR CLASS Professional programmers carefully design the classes they need before any coding is done. With well-
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Programs and Models Almost all computer programs model some artifact – Artifact: product of civilization, either concrete or abstract, simple or complex.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
More About Objects and Methods
Review What is an object? What is a class?
Java Course Review.
Loops BIS1523 – Lecture 10.
Chapter 4: Writing Classes
Java Programming: Guided Learning with Early Objects
Chapter 4: Writing Classes
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Functions I Creating a programming with small logical units of code.
CONSTRUCTORS AND DESRUCTORS
Review of Previous Lesson
Java Programming Language
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Review of Previous Lesson
Functions I Creating a programming with small logical units of code.
Presentation transcript:

CUSTOM OOP

SYNTAX OF A CLASS DEFINITION Class definitions (descriptions) look like this: class ClassName { Descriptions of the instance variables and methods each object will have and the constructors that initialize a new object. }

Often programmers separate the definition into three sections: class ClassName { // Description of the variables. // Description of the constructors. // Description of the methods. }

A TINY EXAMPLE package { import mx.controls.*; public class HelloObject { // method definition public function speak():void { Alert.show( "Hello from an object!"); } var anObject:HelloObject = new HelloObject(); anObject.speak();

If the class definition does not include a constructor a default constructor is automatically supplied by the ACTIONSCRIPT compiler. It’s good programming habit to include a default on yourself package { import mx.controls.*; public class HelloObject { //constructors in AS are like functions but without return values and have the same name as the class public function HelloObject() { } // method definition function speak():void { Alert.show( "Hello from an object!"); } var anObject:HelloObject = new HelloObject(); anObject.speak();

METHODS CAN USE VARIABLES If different instances (different objects) of the HelloObject class print different strings, then each instance must have its own data, which must be initialized. Initializing the instance variables of an object is done through its constructor

IMPROVED CLASS Here is what we have so far for HelloObject(I’ve removed the package statements as well as some other parts to make things simpler to see): class HelloObject { function speak():void { Alert.show("Hello from an object!"); } Here is a start on improving HelloObject: class HelloObject { __________ _____________ ; // keep the object's message here function speak():void { Alert.show( _________________ ); // print the object's message }

USING A CONSTRUCTOR Here is the class with a String reference variable included. Of course, you may have used a different name for it. class HelloObject { var greeting:String; function speak():void{ Alert.show( greeting ); } The class is not complete because there is no way to initialize the greeting

COMPLETED CONSTRUCTOR class HelloObject{ var greeting:String; function HelloObject(st:String) { greeting = st; } function speak():void { Alert.show(greeting); } var anObject:HelloObject = new HelloObject("A Greeting!"); anObject.speak();

FILL IN THE BLANKS 1. Fill in the blanks so that the following program writes "Hello Vesta" six times to the monitor. class VestaHello{ public function main ( ):void { var j:Number = 0 ; while ( j < 6 ) { Alert.show( "Hello Vesta" ); j = j + 1; } This program shows little of the object oriented style of programming. For the task at hand it is probably the best of all ten programs since it accomplishes the task in a simple and clear manner. But let us look at some other programs.

2. Now write the program so that it makes use of a Vesta object that writes "Hello Vesta" to the monitor. class Vesta{ function speak():void { Alert.show( “Hello Vesta” ); } class VestaHello2{ public function main ( ):void { int j = 0 ; var vest:Vesta = new Vesta(); while ( j < 6 ) { vest.speak(); j = j + 1; } In this program one Vesta object is created, whose speak() method is invoked size times. If all that you wanted to do was to write the phrase six times, the first version of the program is probably better than this version.

3. Here is a modification of the program. Now six Vesta objects are created, and each one uses its speak() method once. class Vesta{ function speak():void { Alert.Show( "Hello Vesta" ); } class VestaHello3{ public function main ( ):void { int j = 0 ; while ( j < 6 ) { var vest:Vesta = new Vesta() ; vest.speak(); j = j + 1; } This version of the program is less efficient than the previous program because this version creates six objects all of which do the same thing, and each of which immediately becomes garbage after it is used. This is a poor programming style.

4. Here is a further modification of the program. Again six Vesta objects are created, but now each one is a temporary, unnamed object. class Vesta{ function speak():void { Alert.show( "Hello Vesta" ); } class VestaHello4{ public function main ( ):void { int j = 0 ; while ( j < 6 ) { new Vesta().speak() ; j = j + 1; } The new operator is used to create each object and each newly created object's speak() method is invoked. In spite of its sophistication, this version of the program is about as impractical as the previous version.

5. Now the Vesta class is modified so a Vesta object writes out its message six times: class Vesta { function speak():void { int j = 0 ; while ( j < 6 ) { Alert.show(“Hello Vesta"); j = j + 1; } class VestaHello5{ public function main ( ):void { var vest:Vesta = new Vesta() ; vest.speak(); } This version has the advantage that only one object is created, and has the further advantage that main() is now a very simple program. If main() is expected to be become longer as more and more things are added to it, the Vesta object might be sensible. But for the simple task as stated, the first program is still the best.

6. In this version the class definition includes a constructor that initializes the message to be printed: class Vesta{ var message:String ; // The string to be printed // function Vesta(mess:String ) { message = mess ; // Initialize the message } function speak():void { int j = 0 ; while ( j < 6 ) { Alert.show(message); j = j + 1; } class VestaHello6{ public function main ( ):void { var vest:Vesta = new Vesta ( "Hello Vesta" ) ; vest. speak() ; } Again, whether the Vesta class is sensible depends on the task. If the task is complicated, and the above program is just a small step toward a complete solution, then maybe the Vesta class is a good design.

7. Further sophistication is to include the iteration limit in the constructor. class Vesta{ var message:String ; // The string to be printed var limit:int ; // Number of times to print // Constructor Function Vesta(mess:String, lim:Number ) { message = mess ; // Initialize the message limit = lim ; // Initialize the limit } function speak():void { int j = 0 ; while ( j < limit ) { Alert.show( message ); j = j + 1; } class VestaHello7{ public function main ( ):void { var vest:Vesta = new Vesta ( "Hello Vesta", 6) ; vest. speak() ; } This version of the Vesta class is more flexible than the previous version. It is probably better since it can be used for more situations than the previous.

8. On further thought (which should have been done before any coding was done) it seems more sensible that the number of times to write the message is better specified as a parameter to the speak() method. This further increases the flexibility of the class. class Vesta{ var message:String ; // The string to be printed // Constructor function Vesta( mess:String ) { message = mess ; // Initialize the message } function speak( i:int ):void { int j = 0 ; while ( j < i ) { Alert.show( message ); j = j + 1; } class VestaHello8{ public function main ( ):void { var vest:Vesta = new Vesta ( "Hello Vesta" ) ; vest. speak( 6 ) ; } This version seem more logical, as well as being more versatile. The number of times a message should be repeated should be a characteristic of the request, not an inherent characteristic of the object.

9. Mostly a Vesta object says "Hello Vesta" each time it is used. If that is what we want, the main() method should not have to say so. Classes can have several constructors that each take different parameters (remember the Point class.) In the following version of the program there are two constructors, one automatically initializes the message to "Hello Vesta" and the other initializes the message to the requested message. class Vesta{ var message:String ; // The string to be printed // Parameter-less Constructor function Vesta( ) { message = “Hello Vesta" ; // Initialize the message } // One Parameter Constructor function Vesta(mess:String) { message = mess; // Initialize the message } function speak( int limit ):void { int j = 0 ; while ( j < limit ) { Alert.show( message ); j = j + 1; } class VestaHello9{ public function main ( ):void { var vest:Vesta = new Vesta () ; // Use the default value "Hello Vesta“ vest. speak(6) ; }

10. A further improvement to the Vesta class is to include a method that changes the message string. This is NOT a constructor---it is a method that is part of an existing object and changes that object's message. class Vesta{ String message ; // The string to be printed // Parameter-less Constructor Function Vesta( ) { message = "Hello Vesta" ; // Initialize the message } // One Parameter Constructor fuction Vesta(mess:String ) { message = mess ; // Initialize the message } // Method that changes this object's message function changeMessage(mess:String):void { message = mess ; // Initialize the message } function speak( int limit ):void { int j = 0 ; while ( j < limit ) { Alert.show(message ); j = j + 1; } class VestaHello10{ public function main ( ):void { var vest:Vesta = new Vesta () ; // Use the default value "Hello Vesta" vest. speak( 6 ) ; // Change the message to "Hello Ceres" vest.changeMessage( “ Hello Ceres " ); vest. speak( 6 ) ; }

PROGRAMMING EXERCISES Exercise 1 Modify the program in this ppt so that a HelloObject object writes out the greeting as many times as there are characters in the greeting. The HelloObject class will have a constructor that allows it to initialize objects to different greetings. HelloHelloHelloHelloHello class HelloObject{ var greeting:String; function HelloObject(st:String ) { for (var i:int = 0; i < st.length; i++) { greeting += st; } function speak():void { Alert.show( greeting ); }

Exercise 2 Modify the program so that class HelloObject has two greeting messages: a morning greeting and an evening greeting. There will two output methods, one for each greeting. Good morning World!Good evening World! class HelloObject{ var greeting:String; function morning() { greeting = “Good morning World!”; } function evening() { greeting = “Goog evening World!”; } function speak():void { Alert.show( greeting ); }

Exercise 3 Write a version of the HelloObject program where the greeting that is printed by the object is given by the user: Enter Greeting:Hello Mars! Hello Mars! class HelloObject{ function speak(input:String):void { Alert.show(input); }

Exercise 4 Add another constructor to the HelloObject class that takes a HelloObject object as a parameter: function HelloObject( init:HelloObject){ // initialize the new object's greeting to the same as that of the init parameter this.init = init; } The additional constructor will not alter its parameter (of course), merely use its data. Use "dot notation" to refer to the String inside the parameter.