You are on page 1of 7

Some areas Smalltalk Language has been applied to are: Business Information System o Chosen because of Technical merit

t and flexibility o Well suited for large projects Embedded in an oscilloscope Manages the telephone system of an entire country Batch programs for large mainframes The demand for Smalltalk programmers is growing in areas where the telecommunications industry is strong.

1|Page

Description This program demonstrates how to count letters in a sentence and compares Smalltalk with Pascal Source Code Pascal program frequency const size = 80; var s: string[size]; i: integer; c: char; f: array[1..26] of integer; k: integer; begin writeln('enter line'); readln(s); for i := 1 to 26 do f[i] := 0; for i := 1 to size do begin c:= asLowerCase(s[i}); if isLetter(c) then begin K := ord(c) - ord('a') + 1; f[k] := f[k] + 1 end end for i := 1 to 26 do write(f[i], ' ') end.

Smalltalk

|scfk|

f := Array new: 26.

s := Prompter prompt: 'Enter line' default: ''. 1 to: 26 do [:i | f at: i put: 0]. 1 to: s size do: [:i | c := (s at: i) asLowerCase. c isLetter ifTrue: [ k := c asciiValue - &a asciiValue + 1 ] ]. ^f

Using Smalltalks powerful built-in buiding blocks the same code could have been written as this: |sf| s := Prompter prompt: 'Enter line' default: ''. f := Bag new. s do [ :c | c isLetter ifTrue: [f add: c asLowerCase}}. ^f Program Notes This program was created using Smalltalk Express for Windows.

2|Page

OBJECT-ORIENTED PROGRAMMING
Methods Declaration Method declaration: two parts 1. method header consists of modifiers (optional), return type, method name, parameter list and a throws clause (optional) types of modifiers access control modifiers abstract * the method body is empty. E.g. abstract void sampleMethod( ); static * * final * 2. method body Methods Invocations * invoked as operations on objects/classes using the dot ( . ) operator reference.method(arguments) * static method: Outside of the class: reference can either be the class name or an object reference belonging to the class Inside the class: reference can be ommitted * non-static method: reference must be an object reference cannot be overridden in subclasses

represent the whole class, no a specific object can only access static fields and other static methods of the same class

Method - Overloading A class can have more than one method with the same name as long as they have different parameter list. public class Pencil { ... public void setPrice (float newPrice) { price = newPrice; } public void setPrice (Pencil p) { price = p.getPrice(); } } How does the compiler know which method youre invoking? compares the number and type of the parameters and uses the matched one Methods Parameter Values Parameters are always passed by value. public void method1 (int a) { a = 6; } public void method2 ( ) { int b = 3; method1(b); // now b = ? // b = 3 } 3|Page

When the parameter is an object reference, it is the object reference, not the object itself, getting passed.

The Main Method - Concept main method the system locates and runs the main method for a class when you run a program other methods get execution when called by the main method explicitly or implicitly must be public, static and void

4|Page

The Main Method - Getting Input from the Command Line When running a program through the java command, you can provide a list of strings as the real arguments for the main method. In the main method, you can use args[index] to fetch the corresponding argument class Greetings { public static void main (String args[]){ String name1 = args[0]; String name2 = args[1]; System.out.println("Hello " + name1 + & +name2); } } java Greetings Jacky Mary Hello Jacky & Mary Note: What you get are strings! You have to convert them into other types when needed. Modifiers of the classes A class can also has modifiers public publicly accessible without this modifier, a class is only accessible within its own package abstract no objects of abstract classes can be created all of its abstract methods must be implemented by its subclass; otherwise that subclass must be declared abstract also final can not be subclassed Normally, a file can contain multiple classes, but only one public one. The file name and the public class name should be the same

Types of Methods

There are 4 basic types of methods: Modifier (sometimes called a mutator)


Accessor

Changes the value associated with an attribute of the object E.g. A method like Change_Car_Color Returns the value associated with an attribute of the object E.g. A method like Price_of_Car Called once when the object is created (before any other method will be invoked) E.g. Car(Mustang) Called when the object is destroyed E.g.~Car( )

Constructor Destructor

Identity Identity is the property of an object that distinguishes it from all other objects.

The failure to recognize the difference between the name of the object and the object itself is the source of many errors in object-oriented (OO) programming.

Relationships There are two kinds of relationships that are of particular interest. Using relationship E.g. Owner uses the Car. Containing relationship E.g. Car contains an Engine 5|Page

Definition of a class According to Webster A group, set, or kind marked by common attributes or a common attribute A class is a set of objects that share a common structure and a common behavior An object is a concrete entity that exists in space and time, an instance of a class A class represents only an abstraction, the essence of an object from the class A class can be thought of as a cookie cutter, form which objects can be instantiated A class is to an object, as a blueprint is to a building The class mammal represents the characteristics common to all mammals Live birth, nurse young, have hair, Paul, PJ, Lisa, Heidi, and James are specific instances from the class mammal Inheritance The term, inheritance, is used in many object oriented (OO) programming languages to describe the generalization relationship Inheritance is a relationship where one class shares the structure or behavior defined in one class (single inheritance) or more (multiple inheritance)

Benefits of Inheritance One view of inheritance is that it provides a way to specify some properties/behaviors that all subclasses must exhibit Inheritance can be used to re-use code Inheritance also provides the ability to generalize A method can be written to work with the super-class but subclasses can be passed as arguments

6|Page

Views of a class A class can be viewed as a sort of contract that specifies what instances of the class can, and cannot do It is possible to distinguish between the outside and inside view of a class The interface of a class provides its outside view and emphasizes the abstraction The implementation of a class is its inside view

7|Page

You might also like