Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon)...

163
11/20/2005 M. Q. Azhar (Sumon) TECS Workshop Module 7 Introduction to Objects and Object- Oriented Programming Presented by M. Q. Azhar (Sumon) Comments? Questions? [email protected]

Transcript of Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon)...

Page 1: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

TECS Workshop Module 7

Introduction to Objects and Object-Oriented Programming

Presented by M. Q. Azhar (Sumon)

Comments? Questions? [email protected]

Page 2: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 211/20/2005M. Q. Azhar (Sumon)

Motivation!• People

– TECS– Brooklyn College

• CIS Department• College Now• Dean of Undergraduate Studies.

• Agendas– Safe Surfing– Effective use of the Internet – Integration of technology in the classroom environment – Engaging women and minorities in Computing– Community Building

Page 3: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 311/20/2005M. Q. Azhar (Sumon)

TECS Module 7Introduction to Objects and

Object-Oriented Programming Topic 1: Introduction to ObjectTopic 2: Introduction to Programming Using JavaTopic 3: Introduction to Object Oriented ProgrammingLunch DiscussionTopic 4: Object Oriented Design Topic 5: Graphics: AppletDiscussion: Object Oriented Programming using

RoboticsDiscussion: AP Exam in Computer Science

Page 4: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

Topic 1:Introduction to Objects

Page 5: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 511/20/2005M. Q. Azhar (Sumon)

• An object is a "thing,” a “gizmo," a "gadget,” … an object.• For example, a car, a soda machine, a dog, a person, a house, a

bank account, a pair of dice, a deck of cards, a point in the plane, a TV, a VCR, an ATM machine, an elevator, a square, a circle, a flea, an elephant, a camera, a movie star, a computer mouse, a live mouse, a phone, an airplane, a song, ... just about anything is an object.

• In computing, a window is an object, so is a mouse, a menu, a textbox, and a button.

• Objects come in all shapes, sizes, and colors. An object may be physical, like a radio, or intangible, like a song.

• For our purposes, however, objects are entities that have1. attributes, (characteristics or properties), and

2. methods, (actions or behaviors of an object ).

Objects

Page 6: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 611/20/2005M. Q. Azhar (Sumon)

Elevator Object• Notice that the three elevator

objects have different attribute values.

The attribute values determine the state of an object.

• Thus the state of elevator 1 is that the floor is 3 and the door is open.

• The state of elevator 3 is that the current floor is 2 and the door is closed.

• Each elevator object has a unique state.

• On the other hand, all elevator objects have the samebehavior.

• However, all three objects can do the same things (open the door, close the door etc.).

• All three have the same methods.

Page 7: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 711/20/2005M. Q. Azhar (Sumon)

Rectangle Object

• A rectangle is an object• Some possible properties or

attributes of a rectangle are:1. length and2. width

• Some possible methods are:1. get the length

2. get the width 3. change the length 4. change the width 5. get the area 6. get the perimeter

Here are two rectangle objects:

•The state of the first rectangle object is {length = 7, width = 5} ; •The state of the second rectangle is {length = 2, width = 8}.

Page 8: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 811/20/2005M. Q. Azhar (Sumon)

Computer Widow Object!• A computer window is an object

Some of the (many) attributes include:1. length2. width3. background color4. font style5. font color6. state – maximized, minimized or downscaled

Some of the (many) methods include:1. resize the window ( change length and width)2. maximize3. minimize4. change background color5. change font etc.

Page 9: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 911/20/2005M. Q. Azhar (Sumon)

Defining Object• In the context of a computer program, you might

think of an object as a representation, model or abstraction of some entity consisting of1. data (attributes) and2. functions (methods) which use or manipulate the data

• An object’s data determines its state. For example, the data for the first elevator (above) specifies that the “floor” attribute has value 3 and the door is open. The current state of the first rectangle indicates that the length of the rectangle is 7 and the width is 5.

• The methods/functions specify what an object does, i.e., the behavior of an object.

Page 10: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1011/20/2005M. Q. Azhar (Sumon)

How do you define Object?• The attributes and methods of an object depend on our

specific use and view of an object. • For example, in one application, a rectangle object might

be a simple geometrical figure with just two attributes, length and width.

• Another, perhaps graphical, view of a rectangle might include color and location (x and y coordinates) among the attributes.

• Similarly, an elevator has many potential attributes (carpet color, number of passengers, maximum weight, , date of last inspection) but only a few attributes are of interest in any application.

Page 11: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1111/20/2005M. Q. Azhar (Sumon)

Object has Object!• Some attributes themselves might be other

objects.• For example, a light object may have:

– attributes:• number of watts• current state (on or off)

– methods:• turn light on• turn light off

• Now a light object may be part of (an attribute of) an elevator object.

Page 12: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1211/20/2005M. Q. Azhar (Sumon)

Your Turn!

•The methods (functions) might be

a. give (return) its areab. give (return) its circumference.

•attribute (data) of a circle:

–radius, a real number.

A circle is an object.

In each of the examples, the attributes and methods have been chosen arbitrarily. Indeed, choosing the “right” attributes and methods for an object is a skill and an art that comes with practice and patience.

Page 13: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1311/20/2005M. Q. Azhar (Sumon)

Lets Try another one!

The methods/functions/operations might be

a. give (return) the balanceb. give personal information about the account ownerc. make a depositd. make a withdrawal

data or attributes of a bank account might be

a. an ID numberb. customer's name, c. address, and d. balance.

A Bank account is an object.

Page 14: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1411/20/2005M. Q. Azhar (Sumon)

What is Classes?• A class is a template, blueprint, or description of a

group of objects. • Every object is described by some class. For example,• An elevator class specifies the characteristics and

behaviors of all elevator objects. The elevator class is a general description of an elevator. An elevator class is not an elevator.

• A rectangle class describes the attributes and methods of all rectangle objects. A rectangle class may specify that every rectangle object has both a length and a width. However, a rectangle class is not a rectangle.

Page 15: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1511/20/2005M. Q. Azhar (Sumon)

Analogy

• An architect’s blueprint is analogous to a class.

• A blueprint is not a house but a description or specification of a potential house.

• When a builder constructs two real houses from a blueprint, well, now we have two “house objects.”

• The skill of the programmer in defining classes is akin to the skill of the architect, and the labor of the compiler in building objects is like the work of the construction company.

Architect

blueprint

Programmer

class

House Object

Construction Company

Compiler

Page 16: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1611/20/2005M. Q. Azhar (Sumon)

Object AnalogyAn object is an instance of a class!

• Just as a builder creates houses from a blueprint,

• From one blueprint, a builder can build many houses.

• Every house builds from the blue print

• a program creates objects from a class.

• From one class, a program can create many objects

• Every object is “manufactured” according to some class specification.

• Every object belongs to some class.

Page 17: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1711/20/2005M. Q. Azhar (Sumon)

Class Example

• Let us look at an example of a Rectangle class in Java.

• The class is a description (in Java) of the attributes and behaviors of a rectangle object.

• For now, don’t be concerned with the syntax or any of the Java particulars.

Page 18: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1811/20/2005M. Q. Azhar (Sumon)

Rectangle Classpublic class Rectangle{//Every Rectangle object has both length and width

attributes (int)private int length;private int width;//default values for a rectangle object are length =

1 and width = 1public Rectangle() // default constructor{length = 1;width = 1;}//can create a Rectangle object with any

dimensionspublic Rectangle(int x,int y) //constructor{length = x;width = y;}

//can change the dimensions of any rectangle object

public void changeDimensions(int x,int y) // mutator

{ length = x; width = y;}//gives the area of a rectangle object // accessorpublic int getArea(){ return length*width;}//gives the perimeter of a rectangle objectpublic int getPerimeter(){ return 2*(length+width);}}

Page 19: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 1911/20/2005M. Q. Azhar (Sumon)

Analogy of the Code• The preceding code (a Rectangle class) is a template for a rectangle.

According to the specifications, any potential rectangle object has both a length and a width of type int. Moreover any rectangle object can

· change its dimensions,· give its area, and· give its perimeter.

• OK, we know what a rectangle object has and what it can do. So, we can now manufacture, create, instantiate as many rectangles as we like. We have the blueprint, so let’s start production:

// makes a 5 X 7 rectangle named r1 Rectangle r1 = new Rectangle(5,7);// makes a 7 X 5 rectangle named r2 Rectangle r2 = new Rectangle(7,5); //makes a default 1 X 1 rectangle named r3Rectangle r3 = new Rectangle();

Page 20: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2011/20/2005M. Q. Azhar (Sumon)

Object

• With three magic statements, we have created three rectangle objects --- built according to the specifications of our class/blueprint.

• Each rectangle object has a length property and a width property with appropriate values:

Page 21: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2111/20/2005M. Q. Azhar (Sumon)

Messages• In a Java program, objects interact with other objects by

sending messages. • Messages are similar to function calls in procedural style

programming. • The following three statements send messages to r1, r2 and

r3, respectively:System.out.println(r1.getPerimeter() );

area = r2.getArea(); r3.changeDimensions(7,3);• The purpose of the messages should be pretty obvious:

– “r1, get your perimeter!”– “r2, get your area!”– “r3, change your dimensions!”

Page 22: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2211/20/2005M. Q. Azhar (Sumon)

Example of Message Passing

• From our perspective a Dog object is a very simple creature:

• Our simple Dog class which has only a single attribute:

– bark.

• A Dog object can do only two things:– set its bark and– speak, i.e., bark

public class Dog{/*A Dog object has but a single

attribute—its bark*/ private String bark; /*set the sound: “woof-woof,” “bow-

wow” etc.*/public void setBark(String s) { bark = s;}/* Every dog can bark*/public void speak() { System.out.print(bark);}}

Page 23: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2311/20/2005M. Q. Azhar (Sumon)

Creation of “Fido” and “Brutus”• We now create (instantiate) a few Dog

objects:/* create a Dog named fido*/

Dog fido = new Dog(); /*create a Dog named brutus*/

Dog brutus = new Dog();• send a few messages to the critters:

/*a message to fido*/fido.setBark(“Bow-Wow”);/* a message to brutus*/brutus.setBark(“Woof-Woof”); /* fido, speak, boy!!*/ fido.speak();/* you too, brutus, speak*/ brutus.speak();

BoW-WOW

Woof-Woof

Page 24: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2411/20/2005M. Q. Azhar (Sumon)

Who or what sends these mysterious messages?

• In a Java program, objects send other objects messages.

• For example:• a customer object might send an ATM object

a message: “deposit this money.”• a gambler object might send a slot machine

object a message: “take my money, please.”• the slot machine object might send back a

message to the gambler object (“You lose.”).

Page 25: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

Introduction to Programming

Using Java

Page 26: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2611/20/2005M. Q. Azhar (Sumon)

What is a Program?• Program is a set of instructions written in a

programming language (i.e high level Language such as Java) that is supposed to perform some specific tasks.

• Very Basic Program Construction include:• Sequential• Selection• Iteration• I/O (input and output)• Arrays (Data Structure)

Page 27: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2711/20/2005M. Q. Azhar (Sumon)

The Java Programming Language

•Simple •Safe •Platform-independent ("write once,

run anywhere") •Rich library •Designed for the internet

Page 28: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2811/20/2005M. Q. Azhar (Sumon)

The Java Programming Language• Java

– Based on C and C++– Originally developed in early 1991 for intelligent consumer

electronic devices• Market did not develop, project in danger of being cancelled

– Internet exploded in 1993, saved project• Used Java to create web pages with dynamic content

– Java formally announced in 1995– Now used to create web pages with interactive content,

enhance web servers, applications for consumer devices (pagers, cell phones)...

Page 29: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 2911/20/2005M. Q. Azhar (Sumon)

The Java Programming Language• Java programs

– Consist of pieces called classes– Classes contain methods, which perform tasks

• Class libraries– Also known as Java API (Applications Programming

Interface)– Rich collection of predefined classes, which you can use

• Two parts to learning Java– Learning the language itself, so you can create your own

classes– Learning how to use the existing classes in the libraries

Page 30: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3011/20/2005M. Q. Azhar (Sumon)

Basics of a Typical Java Environment• Java Systems

– Consist of environment, language, Java Applications Programming Interface (API), Class libraries

• Java programs have five phases– Edit

• Use an editor to type Java program• vi or emacs, notepad, Jbuilder, Visual J++• .java extension

– Compile• Translates program into bytecodes, understood by Java interpreter• javac command: javac myProgram.java• Creates .class file, containing bytecodes (myProgram.class)

Page 31: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3111/20/2005M. Q. Azhar (Sumon)

Basics of a Typical Java Environment

• Java programs have five phases– Loading

• Class loader transfers .class file into memory– Applications - run on user's machine– Applets - loaded into Web browser, temporary

• Classes loaded and executed by interpreter with java commandjava Welcome

• HTML documents can refer to Java Applets, which are loaded into web browsers. To load, appletviewer Welcome.html– appletviewer is a minimal browser, can only interpret applets

Page 32: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3211/20/2005M. Q. Azhar (Sumon)

Basics of a Typical Java Environment

• Java programs have five phases Verify• Bytecode verifier makes sure bytecodes are valid and

do not violate security• Java must be secure - Java programs transferred over

networks, possible to damage files (viruses)– Execute

• Computer (controlled by CPU) interprets program one bytecode at a time

• Performs actions specified in program– Program may not work on first try

• Make changes in edit phase and repeat

Page 33: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3311/20/2005M. Q. Azhar (Sumon)

Program is created in the editor and stored on disk.Compiler creates bytecodes and stores them on disk.

Class loader puts bytecodes in memory.

Bytecode verifier confirms that all bytecodes are valid and do not violate Java’s security restrictions.

Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes.

Phase 1

Phase 2

Phase 3

Phase 4

Phase 5

DiskEditor

Compiler

Class Loader

Disk

Disk

PrimaryMemory

PrimaryMemory

Primary Memory

Bytecode Verifier

Interpreter

Page 34: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3411/20/2005M. Q. Azhar (Sumon)

General Notes about Java

• Just-in-time compiler– Midway between compiling and interpreting

• As interpreter runs, compiles code and executes it• Not as efficient as full compilers

– Being developed for Java

– Integrated Development Environment (IDE)• Tools to support software development• Several Java IDE's are as powerful as C / C++ IDE's

Page 35: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3511/20/2005M. Q. Azhar (Sumon)

An Integrated Development Environment

Page 36: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3611/20/2005M. Q. Azhar (Sumon)

Compiling and Running

•Type program into text editor •Save •Open command shell •Compile into byte codesjavac Hello.java

•Execute byte codesjava Hello

Page 37: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3711/20/2005M. Q. Azhar (Sumon)

From Source Code to Running Program

Page 38: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3811/20/2005M. Q. Azhar (Sumon)

Errors

•Syntax errorsSystem.ouch.print("...");System.out.print("Hello);

•Detected by the compiler •Logic errorsSystem.out.print("Helo");

•Detected (hopefully) through testing

Page 39: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 3911/20/2005M. Q. Azhar (Sumon)

Simple Basic Java• Plain Vanilla HelloWorld

Style Program:– Example 1:1. /* our first program!!!!*/2. public class Prog13. {4. public static void main (String

args[])5. {6. System.out.println(“Dopey”);7. System.out.println(“Grumpy”);8. }9. }

• Let’s look at the program line by line:– Line 1 is a comment. As in C++, comments

begin with the symbol // and continue to the end of the line. Multiple line comments are enclosed by the symbols /* and */.

– (Line 2) • The word class indicates that we are

defining a new class. The name of the class is Prog1. (A class is a blueprint for objects, so, in theory, we could create a Prog1object from class Prog1).

• The keyword public is an access modifier. A public class is accessible anywhere in a program. We will discuss this in greater detail later. C++ does not have access modifiers for classes.

Page 40: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4011/20/2005M. Q. Azhar (Sumon)

Explanation of The Program:

• Plain Vanilla HelloWorld Style Program:

– Example 1:1. /* our first program!!!!*/2. public class Prog13. {4. public static void main (String

args[])5. {6. System.out.println(“Dopey”);7. System.out.println(“Grumpy”);8. }9. }

• Line 4:– is the heading of the method

called main. Every Java application must have one method called main which is defined exactly as it is on line 4.

– The main method is always the first method that is executed. When the program executes, main goes first.

– This particular class is very simple and has but a single method, main().

– Of course most classes will have many methods.

Page 41: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4111/20/2005M. Q. Azhar (Sumon)

Explanation

• Plain Vanilla HelloWorld Style Program:

– Example 1:1. /* our first program!!!!*/2. public class Prog13. {4. public static void main (String

args[])5. {6. System.out.println(“Dopey”);7. System.out.println(“Grumpy”);8. }9. }

• Line 4: – The word public on line 4 is an

access modifier for the main method.

– If a method is declared public then that method is accessible anywhere within the program i.e. anywhere outside the class where it is defined.

– The private access modifier specifies that the method is accessible only within the class.

– If main were not public, then main could not be called from “outside” the class (i.e. by the system) and the program could not be executed.

Page 42: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4211/20/2005M. Q. Azhar (Sumon)

Explanation

• Plain Vanilla HelloWorld Style Program:

– Example 1:1. /* our first program!!!!*/2. public class Prog13. {4. public static void main (String

args[])5. {6. System.out.println(“Dopey”);7. System.out.println(“Grumpy”);8. }9. }

• Line 4: – The keyword static signifies that a method

is a class method. – A static method is one that can be invoked

whether or not an instance of the class is created.

– Outside of the defining class, non-static methods are invoked via objects ( r1.getArea();) and static methods are invoked using the class name, (Math.sqrt(64.0);, where Math is a class).

– Static methods are available whether or not any objects are created.

– A static method belongs to the class, not to any particular instance of the class (object).

– In a way, static methods really go against the grain of object-oriented programming, and they are very much like the functions of C, and procedures of Pascal.

Page 43: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4311/20/2005M. Q. Azhar (Sumon)

Explanation

• Plain Vanilla HelloWorld Style Program:

– Example 1:1. /* our first program!!!!*/2. public class Prog13. {4. public static void main (String

args[])5. {6. System.out.println(“Dopey”);7. System.out.println(“Grumpy”);8. }9. }

• Line 4– The keyword void is used

as in C++, to specify that method main returns no value.

– String args[] is a parameter to main.

• Line 1 and 9 | Line 5 and 8– The curly braces denote

the start and end of a block and are used as in C++.

Page 44: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4411/20/2005M. Q. Azhar (Sumon)

Explanation

• Plain Vanilla HelloWorld Style Program:

– Example 1:1. /* our first program!!!!*/2. public class Prog13. {4. public static void main (String

args[])5. {6. System.out.println(“Dopey”);7. System.out.println(“Grumpy”);8. }9. }

• Line 6 and 7– System.out is a pre-defined system

object associated with the standard output stream (usually the screen).

– So, in each of these statements, the System.out object invokes its println method.

– The println method accepts a string, s, as a parameter (strings are enclosed in double quotes) and sends s, followed by a carriage return, to the standard output device (the screen).

– “Dopey” and “Grumpy” are string literals.– Notice the class Prog1 is sending a

message to System.out. (I.e The message is “println(“Dopey”). )

Page 45: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4511/20/2005M. Q. Azhar (Sumon)

Compiling and Running

•Type program into text editor •Save •Open command shell •Compile into byte codesjavac Prog1.java

•Execute byte codesjava Prog1

Page 46: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4611/20/2005M. Q. Azhar (Sumon)

From Source Code to Running Program

Page 47: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4711/20/2005M. Q. Azhar (Sumon)

Syntax Errors vs. Logic Errors

•Syntax errorsSystem.ouch.print("...");System.out.print("Hello);

•Detected by the compiler •Logic errorsSystem.out.print("Helo");

•Detected (hopefully) through testing

Page 48: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4811/20/2005M. Q. Azhar (Sumon)

Java Vs. C++

• Here are a few other points to keep in mind:∀ • Java, like C++, is case sensitive∀ • The class Prog1 must be saved in a file

called Prog1.java. ∀ • Java programs often contain many classes.

Each class is usually saved in a separate file (although this is not necessary). The file name must be the same as the name of the class and have a .java extension. You may save only one public class per file.

∀ • Java convention dictates that class names begin with an uppercase letter. All other names begin with a lowercase letter. Uppercase letters are used to separate “words” within a name: getArea(), getCircleRadius() etc.

• C++, is case sensitive• This is not the case with C++. C++ is

not so picky about file names.• Classes does not have access-

specifiers in C++.• C++ supports Multiple inheritance

while Java does not.• Java has built in Garbage collector

which deallocate memory that has been allocated by the object when no longer needed. C++ does not provide that.

• In C++, range of type (i.e int) is machine dependent, its not the case in Java.

JAVA C++

Page 49: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 4911/20/2005M. Q. Azhar (Sumon)

Let us try an example

• Write a program to display:– “Welcome to tecs at Brooklyn College”

Page 50: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5011/20/2005M. Q. Azhar (Sumon)

String Concatenationpublic class Prog3 //saved as prog3.java{ public static void main (String args[]) {

System.out.println(“Dopey” + 2+3+4);System.out.println(2+3+4+”Dopey” );System.out.println(“Dopey” + ( 2+3+4) )

;System.out.println(2+3+4);System.out.println( ( 2+3+4) );

}}Output:Dopey2349DopeyDopey999

Here, you should notice that:• Addition, as usual, is performed left to

right.• The argument to println is always a

String.• If the argument to println is x+y and

either x or y is a string, then + effects string concatenation.

• For example, consider the method call println(“Dopey” + 2+3+4). The integer 2,3,4 is converted to a string and the concatenated string “Dopey234” is passed to println.

• However, in the method call println(2+3+4+”Dopey”), the first/Second plus represents addition, the third effects concatenation. So, the string “9Dopey” is passed to the println method.

Page 51: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5111/20/2005M. Q. Azhar (Sumon)

Selection and Iteration

public class Grades1{public static void main( String args[]){ char answer; int grade, sum = 0; double average;/*Iteration Structure*/for (int i = 0; i < 4; i++) sum = sum + Integer.parseInt(args[i]) ; /* converts from string to int*/ average = sum/4.0; /*note that an explicit cast is not necessary*/System.out.print("Average is "+ average + " ");

/*Selection Structure*/if (average >= 90) System.out.println('A'); else if (average >= 80) System.out.println('B'); else if (average >= 70) System.out.println('C'); else if (average > 60) System.out.println('D'); else System.out.println('F');} //end main()} //end class Grades1

/*Reads 4 grades from the command line. Thus, the grades are stored as strings and must be converted to a numeric type before any processing can occur.*/

Page 52: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5211/20/2005M. Q. Azhar (Sumon)

Grades Program• To run the Grades1 program: > java Grades1 80 90 70 60 Notice that

– args[0] is “80”– args[1] is “90”– args[2] is “70”– args[3] is “60”

• In order to calculate an average, the strings stored in args must be converted to integers. Java provides a class Integer with a static method, parseInt(String s), that does exactly that -- accepts a string of digits and converts that string to an integer.

• If the string contains any nonnumeric characters, the program will crash.

• Again, notice that parseInt is a static method so it can be invoked without instantiating an Integer object.

• Finally, while loops, do-while loops and the switch statement work exactly as in C++.

Page 53: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5311/20/2005M. Q. Azhar (Sumon)

Java Data Types

• Reference Types• Primitive Data Types

Page 54: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5411/20/2005M. Q. Azhar (Sumon)

Primitive Data Types

• The usual suspects:– byte, short, int, long– char– boolean– float, double

• The usual operators and rules apply mostly

Page 55: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5511/20/2005M. Q. Azhar (Sumon)

Reference Types (1)• Can't write about objects without referring

to them• Reference value: the only way to refer to an

object• Java has:

– reference values (or just references)– reference expressions– reference variables– assignment of references

Page 56: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5611/20/2005M. Q. Azhar (Sumon)

Declarations, Variables, Assignments

• C-like rules apply for the most part

• Type specifier followed by identifier list

Page 57: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5711/20/2005M. Q. Azhar (Sumon)

Objects

• Only accessed via reference values– Rectangle r1= new Rectangle( );

• Not seen— we (the programmers) only get references to them and send them messages.– r1.getArea();

Page 58: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5811/20/2005M. Q. Azhar (Sumon)

Objects Can NOT Be• assigned to a variable

– int x=Rectangle new Rectangle();• the value of an expression

– new Rectangle()= x+y; • passed as an argument in a message to an object

– R1.setArea( new Rectangle());• returned by an object responding to a message• declared

Page 59: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 5911/20/2005M. Q. Azhar (Sumon)

But References Can Be

• assigned to a variable• the value of an expression• passed as an argument in a message to an

object• returned by an object responding to a

message• declared

Page 60: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6011/20/2005M. Q. Azhar (Sumon)

Different Reference Types• References to objects of different classes

are different types• Every class implicitly defines a distinct

reference type

Page 61: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6111/20/2005M. Q. Azhar (Sumon)

References Are Not Pointers

• can't take "address" of object— can not refer to object without reference

• can’t do arithmetic with references• Java has no pointers

Page 62: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6211/20/2005M. Q. Azhar (Sumon)

What's The Use Of References?

• only way to access an object• only way to send a message to an object

Page 63: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6311/20/2005M. Q. Azhar (Sumon)

Messages: Sending• Message Form:

methodName(argument1, argument2, …, argumentN)

• Sending a Message (Form):

reference . message

Page 64: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6411/20/2005M. Q. Azhar (Sumon)

Messages: Response From Receiver

• value– send message form can be used in expression

• often the right side of an assignment statement

– can be primitive data OR a reference to an object

• void

Page 65: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6511/20/2005M. Q. Azhar (Sumon)

Java Pre-defined Classes

• Huge number of predefined classes– utlity, I/O, GUI, network, time/day, database, math, etc.

• PrintStream class:– println(string), print(string)

• String class:– toUpperCase, trim, substring, indexOf, etc.

• File class– delete, renameTo

Page 66: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6611/20/2005M. Q. Azhar (Sumon)

Java Pre-defined Objects

• System.out, System.err, System.in• String constants

Page 67: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6711/20/2005M. Q. Azhar (Sumon)

Sample Code 1String s;

int i=0, k;

s = "hello, world";

k = s.length();

while (i<k-1) {

System.out.println(s.substring(0,i+1));

i++;

}

Page 68: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6811/20/2005M. Q. Azhar (Sumon)

Cascading MessagesString s;

s = "Hello".concat(" World").toUpperCase();

System.out.println(s);

Page 69: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 6911/20/2005M. Q. Azhar (Sumon)

Creating Objects

• new keyword• constructor + arguments• expression's value is reference to create

object

Page 70: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7011/20/2005M. Q. Azhar (Sumon)

Creating And Using An Object: Example

File junk;junk = new File("garbage");junk.delete();

Page 71: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7111/20/2005M. Q. Azhar (Sumon)

Class Definitions

class NameOfClass {

method definitions (including constructors)

instance variable declarations

}

Page 72: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7211/20/2005M. Q. Azhar (Sumon)

Method Definitions

• similar to function definitions• additional keywords: public or private• optional keyword: static• optional phrase: throws

SomeKindOfException

Page 73: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7311/20/2005M. Q. Azhar (Sumon)

Class Definition Example 1 class Laugher4 {

public Laugher4(String defaultSyl) {default = defaultSyl;

}

public void laugh() {System.out.println(default);

}

private String default;}

Page 74: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7411/20/2005M. Q. Azhar (Sumon)

Class Definition Example 2 class Laugher2 {

public Laugher2() { default = "ha"; }

public Laugher2(String defaultSyl) {default = defaultSyl;

}

public void laugh() {System.out.println(default);

}

public void laugh(String syl) {System.out.println(syl);

}private String default;

}

Page 75: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7511/20/2005M. Q. Azhar (Sumon)

Signatures and Overloading

• Signature: method name + argument types• Overloading: methods of the same name but

different signaturespublic Laugher2() { public Laugher2(String defaultSyl) {public void laugh() {public void laugh(String syl) {

Page 76: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7611/20/2005M. Q. Azhar (Sumon)

Input and Output

• Input and output classes model different i/o services

• Setting up i/o involves a composition of constructors

• Example (input):BufferedReader br = new BufferedReader(

new InputStreamReader(new FileInputStream(new File("USData"))));

// file input

Page 77: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7711/20/2005M. Q. Azhar (Sumon)

BufferedReader

• Provides a readLine method• Returns reference to String modeling line read

in OR returns null if end of file is reached• Setting up i/o involves a composition of

constructors• Example (input):

String s = br.readLine();while (s!=null) {

System.out.println(s);s = br.readLine();

}

Page 78: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7811/20/2005M. Q. Azhar (Sumon)

Command Line Input

/*saved as Prog4.java*/public class Prog4 {public static void main

(String args[]){System.out.println(args[0]);System.out.println(args[1]);}}

• If you run this program with the command> java Prog4 Dopey Grumpy

• the two strings entered at the command line, “Dopey” and “Grumpy,” are stored in the array args.

• Consequently, args[0] holds the string “Dopey” and args[1] holds “Grumpy.”

• Notice that arrays are indexed from 0, as they are in C++.

• Output:DopeyGrumpy

Page 79: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 7911/20/2005M. Q. Azhar (Sumon)

Input/Output Trial!• Write a program that will take one input,

teacher’s last name. Then display it back for the user with a welcome message. So, if we run the program with the command:

>java tecsProg2 maria The program will display back:Output:

Hello Maria, Welcome to Tecs at Brooklyn College!

inputProg Name

Page 80: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8011/20/2005M. Q. Azhar (Sumon)

Keyboard Input!• System.in is an

InputStream• Make sure to include:

– import java.io.*;• By default Java includes

Java.lang package. • Example:• // keyboard input

BufferedReader keyboard = new BufferedReader(

new InputStreamReader(System.in));

/*saved as Prog5.java*/import java.io.*;public class Prog5 {public static void main (String args[]){// keyboard inputSystem.out.println(“Type your name”);BufferedReader keyboard = new

BufferedReader( new InputStreamReader(System.in));

String name= keyboard.readLine();System.out.println( “your name is” +name );}}

Page 81: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8111/20/2005M. Q. Azhar (Sumon)

A Name Classclass Name {

private String first, last, title;

public Name(String first, String last) {this.first = first;this.last = last;

}public String getInitials() {

String s; // M Azhars = first.substring(0,1); // Ms = s.concat("."); //M. s = s.concat(last.substring(0,1));//M.As = s.concat(".");//M.Areturn s;

}

Page 82: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8211/20/2005M. Q. Azhar (Sumon)

Name Class (2) public String getLastFirst() {

return lastName.concat(", ").concat(firstName);}

public String getFirstLast() {return first.concat(" ").concat(last);

}

public void setTitle(String newTitle) {title = newTitle;

}

Page 83: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8311/20/2005M. Q. Azhar (Sumon)

Name Class (3)

public static Name read(BufferedReader br) throws Exception {

String first, last;first = br.readLine();last = br.readLine();return new Name(first,last);

}

}

Page 84: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8411/20/2005M. Q. Azhar (Sumon)

Using The Name Classpublic class TestName{ public static void main(String [] args){BufferedReader br = new BufferedReader( new

InputStreamReader(System.in));

Name n; n = Name.read(br); System.out.println(n.getInitials());}

}

Page 85: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8511/20/2005M. Q. Azhar (Sumon)

Class Variables: Static• Although a class is not an object, a

class can have both class variables and class methods.

• Class variables and class methods can exist whether or not any object is created.

• Class variables and class methods are indicated with the keyword static.

Page 86: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8611/20/2005M. Q. Azhar (Sumon)

Static (Class) MethodsA class/static method, as we have already seen, is a method

that: • exists as a member of a class, • may be invoked using either the class name or the

name of an object, • may not access instance variables, (Since class

methods may be invoked regardless of whether or not any object have been created, object/instance variables cannot be accessed by static methods.)

• is often used when it is important to know how many class instances exist or to restrict the number of instances of a class.

Page 87: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8711/20/2005M. Q. Azhar (Sumon)

Static (Class) VariablesIf a class contains a static variable then:∀ • All objects/instances of the class share that variable.∀ • There is only one version of the variable defined for the whole

class.∀ • The variable belongs to the class.∀ • The variable exists regardless of whether or not any objects

have been created.∀ • The variable may be accessed using either the class name or an

object name, if an object has been created.Example:• Static or class variables are often used for constants. The Math

class contains two class constants: Math.PI and Math.E ( approximate value: 2.71828).

Page 88: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8811/20/2005M. Q. Azhar (Sumon)

Static

• The Java System library contains many class/static methods. The methods of Math are all static as are the (final) variables.

• Of course, allowing static methods and variables is contrary to the principles of object-oriented programming since Java is providing a mechanism for what amounts to global variables and methods!

Page 89: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 8911/20/2005M. Q. Azhar (Sumon)

Static

• The following simple class contains two class/static variables:

• The variable count keeps track of the number of Circle objects that have been created. One version of count exists for the entire class. All objects access this same variable.

• The static variable pi is a constant. Notice the keyword final. Also note that pi is declared public so that any other class may access it as Circle.pi.

Page 90: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9011/20/2005M. Q. Azhar (Sumon)

Example

Page 91: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9111/20/2005M. Q. Azhar (Sumon)

Test Driver

Page 92: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9211/20/2005M. Q. Azhar (Sumon)

Explanation

• The class method, getCount(), returns the number of Circle objects that have been created.

• Notice that getCount() accesses count – a class/static variable. Remember: a static method cannot access an instance variable, since instance variables may not even exist.

• The example also illustrates use of the keyword this. Sometimes the parameter has the same name as one of the instance variables.

• To differentiate between the parameter and the private variable, use the keyword this. As in C++, this is a reference to the invoking object.

Page 93: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9311/20/2005M. Q. Azhar (Sumon)

Scope and Lifetimes

• objects• local (method) variables and parameters• instance variables• class variables

Page 94: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9411/20/2005M. Q. Azhar (Sumon)

Arrays

• Superficially Similar To C:– [ ] syntax– start from 0

• Actually they are classless objects• An array variable holds a reference to an

array object• Has length field— read-only

Page 95: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

Topic 3:Introduction to Object Oriented

Programming

Page 96: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9611/20/2005M. Q. Azhar (Sumon)

OOP Concept: Encapsulation• Webster defines encapsulation as being

“enclosed by a capsule.”• Real world examples of encapsulation

surround us:• A computer is an example of real world

encapsulation. The chips, boards, and wires of a computer are never exposed to a user. Like the TV viewer, a computer user operates a computer via an interface -- a keyboard, screen, and pointing device.

Page 97: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9711/20/2005M. Q. Azhar (Sumon)

Example: Encapsulation• A cabinet hides (encapsulates) the “guts” of a television,

concealing from TV viewers the internal apparatus of the TV.

• Moreover, the television manufacturer provides users with an interface --the buttons on the TV or perhaps a remote control unit.

• To operate the TV and watch The Simpsons or Masterpiece Theatre, viewers utilize this interface. The inner circuitry of a TV is of no concern to most TV viewers.

• Cameras, vending machines, slot machines, DVD players, lamps, cars, video games, clocks, and even hourglasses are all physical examples of encapsulation.

Page 98: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9811/20/2005M. Q. Azhar (Sumon)

Encapsulation: Explained

• Each of the devices enumerated above supplies the user with an interface — switches, buttons,remote controls, whatever -- for operation.

• Technical details are tucked away and hidden from users.

• Each encapsulated item functions perfectly well as a “black box.”

• Certainly, Joe User need not understand how his Radio Shack gadget is constructed in order to operate it correctly.

• A user-friendly interface and perhaps an instruction manual will suffice.

Page 99: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 9911/20/2005M. Q. Azhar (Sumon)

Encapsulation: Explained

• Encapsulation has a similar (though somewhat expanded) meaning when applied to software development and object oriented programming:

The ability to provide users with a well-defined interface to a set of functions in a way which hides their internal workings. In object-oriented programming, the technique of keeping together data structures and the methods (procedures) which act on them. – The Online Dictionary of Computing

• Java provides encapsulation, as defined above, via classes and objects.

• As we have already seen, classes bundle data and methods into a single unit. Classes encapsulate.

Page 100: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10011/20/2005M. Q. Azhar (Sumon)

Rectangle Class: Revisitedpublic class Rectangle{//Every Rectangle object has both length and width

attributes (int)private int length;private int width;//default values for a rectangle object are length = 1

and width = 1public Rectangle() // default constructor{length = 1;width = 1;}//can create a Rectangle object with any dimensionspublic Rectangle(int x,int y) //constructor{length = x;width = y;}

//can change the dimensions of any rectangle object

public void changeDimensions(int x,int y) // mutator

{ length = x; width = y;}//gives the area of a rectangle object // accessorpublic int getArea(){ return length*width;}//gives the perimeter of a rectangle objectpublic int getPerimeter(){ return 2*(length+width);}

}

Page 101: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10111/20/2005M. Q. Azhar (Sumon)

Rectangle: Test Driver!

public TestRectangle{public static void main(String args[]) {// makes a 5 X 7 rectangle named r1 Rectangle r1 = new Rectangle(5,7); // makes a 7 X 5 rectangle named r2 Rectangle r2 = new Rectangle(7,5); //makes a default 1 X 1 rectangle named r3 Rectangle r3 = new Rectangle(); System.out.println(r1.getPerimeter() ); double area = r2.getArea(); r3.changeDimensions(7,3);}}

Page 102: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10211/20/2005M. Q. Azhar (Sumon)

OO Programming Concepts

data field 1

method n

data field n

method 1

An object

...

...

State

Behavior

Data Field length = 1 width=1

Method getArea

A Rectangle object

Page 103: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10311/20/2005M. Q. Azhar (Sumon)

Encapsulation

• The Rectangle class provides a simple example of encapsulation – data and methods, attributes and functionality, are combined into a single unit, a single class.

• Furthermore, all data are accessed not directly but through the class methods, i.e. via an interface /TestRectangle.

• The user of Rectangle – the client -- need not know how the class is implemented – only how to use the class.

• Variable names are of no concern to the client. If the client wishes to know the perimeter of a Rectangle object, the interface provides an accessor method.

Page 104: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10411/20/2005M. Q. Azhar (Sumon)

Encapsulation• If the client wants to dimension the size of a Rectangle

object, the client simply uses the mutator method available through the interface.

• That the length of the sides of a square is held in a variable called dimension is irrelevant to the client.

• Further, if the implementation of the class is modified, programs utilizing the Rectangle class will not be affected provided the interface remains unchanged.

• Like a TV or a camera, the inner workings of the Rectangle class are encapsulated and hidden from the client.

• Public methods provide the interface just as the remote control unit provides the interface for a TV viewer.

Page 105: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10511/20/2005M. Q. Azhar (Sumon)

Encapsulation: Information Hiding• Another term that is often associated with encapsulation is

information hiding. • Many authors regard encapsulation and information hiding as

synonyms. • However OO purists might define encapsulation as the

language feature allowing the bundling of data and methods into one unit

• and information hiding as the design principle that restricts clients from the inner workings of a class.

• With this distinction, programs can have encapsulation without information hiding.

• Regardless of how you define your terms, classes should be designed with a well-defined interface in which implementation decisions and details are hidden from the client.

Page 106: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10611/20/2005M. Q. Azhar (Sumon)

Inheritance and Polymorphism– Inheritance

• Software reusability• Classes are created from existing ones

– Absorbing attributes and behaviors– Adding new capabilities– Convertible inherits from Automobile

– Polymorphism• Enables developers to write programs in general

fashion– Handle variety of existing and yet-to-be-specified classes

• Helps add new capabilities to system

Page 107: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10711/20/2005M. Q. Azhar (Sumon)

Inheritance• Inheritance makes it possible to build new classes

from existing classes thus facilitating the reuse of methods and data from one class in another.

• Moreover, inheritance allows data of one type to be treated as data of a more general type.

Subclass inherits from superclass– Subclass usually adds instance variables and methods

• Single vs. multiple inheritance– Java does not support multiple inheritance

» Interfaces (discussed later) achieve the same effect• “Is a” relationship

• Composition• “Has a” relationship

Page 108: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10811/20/2005M. Q. Azhar (Sumon)

Superclasses and Subclasses

• “Is a” Relationship– Object “is an” object of another class

• Rectangle “is a” quadrilateral– Class Rectangle inherits from class Quadrilateral

– Form tree-like hierarchical structures

Page 109: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 10911/20/2005M. Q. Azhar (Sumon)

  

Superclass Subclasses Student GraduateStudent

UndergraduateStudent Shape Circle

Triangle Rectangle

Loan CarLoan HomeImprovementLoan MortgageLoan

Employee FacultyMember StaffMember

Account CheckingAccount SavingsAccount

Fig. Some simple inheritance examples in which the subclass “is a” superclass.

Page 110: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11011/20/2005M. Q. Azhar (Sumon)

An inheritance hierarchy for university CommunityMembers.

CommunityMember

Employee Student

Faculty Staff

Administrator Teacher

Alumni

CommunityMember is a direct superclass of Employee

CommunityMember is an indirect superclass of

Faculty

Page 111: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11111/20/2005M. Q. Azhar (Sumon)

A portion of a Shape class hierarchy.

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron

Page 112: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11211/20/2005M. Q. Azhar (Sumon)

protected Members

• protected access members– Between public and private in protection– Accessed only by

• Superclass methods• Subclass methods• Methods of classes in same package

– package access

Page 113: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11311/20/2005M. Q. Azhar (Sumon)

Relationship between Superclass Objects and Subclass Objects

• Subclass object– Can be treated as superclass object

• Reverse is not true– Shape is not always a Circle

– Every class implicitly extends java.lang.Object

• Unless specified otherwise in class definition’s first line

Page 114: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11411/20/2005M. Q. Azhar (Sumon)

Constructors and Finalizers in Subclasses

• Superclass constructor– Initializes superclass instance variables of subclass– Not inherited by subclass– Called by subclass

• Implicitly or explicitly with super reference

• finalize method– Garbage collection– Subclass finalize method

• should invoke superclass finalize method

Page 115: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11511/20/2005M. Q. Azhar (Sumon)

Implicit Subclass-Object-to-Superclass-Object Conversion

• Superclass reference and subclass reference– Implicit conversion

• Subclass reference to superclass reference– Subclass object “is a” superclass object

– Four ways to mix and match references• Refer to superclass object with superclass reference• Refer to subclass object with subclass reference• Refer to subclass object with superclass reference

– Can refer only to superclass members• Refer to superclass object with subclass reference

– Syntax error

Page 116: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11611/20/2005M. Q. Azhar (Sumon)

Software Engineering with Inheritance

• Inheritance– Create class (subclass) from existing one

(superclass)• Subclass creation does not affect superclass

– New class inherits attributes and behaviors– Software reuse

Page 117: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11711/20/2005M. Q. Azhar (Sumon)

Composition vs. Inheritance

• Inheritance– “Is a” relationship– Teacher is an Employee

• Composition– “Has a” relationship– Employee has a TelephoneNumber

Page 118: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11811/20/2005M. Q. Azhar (Sumon)

Class: Cat

public class Cat{protected int weight; // notice the keyword "protected"

public Cat(){weight = 10;}public Cat(int weight){this.weight = weight;}

public void setWeight(int w){ weight = w;}public int getWeight(){ return weight;}public void eat(){ System.out.println("Slurp, slurp");}public int mealsPerDay(){ return 2 + weight/50;}}

Page 119: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 11911/20/2005M. Q. Azhar (Sumon)

Extended Class: Leopard

// here is the inheritance part!public class Leopard extends Cat /* "extends" indicates

inheritance*/{protected int numSpots;public Leopard(){weight = 100;numSpots =0; // a poor excuse for

a leopard!!}

public Leopard(int weight, int numSpots)

{super(weight); // a call to the

one argument constructor of Animal

this.numSpots = numSpots;}public void setNumSpots(int n){numSpots = n;}

Page 120: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12011/20/2005M. Q. Azhar (Sumon)

Extended Class: Leopard

public int getNumSpots(){return numSpots;}public void eat() /*overriding the eat method of Animal*/{

System.out.println("CRUNCH...CHOMP...CRUNCH...SLURP");

}

public int mealsPerDay() /*overriding the method of Animal*/

{return super.mealsPerDay() * 2; /* note call to parent method*/}public void roar() /*a non-inherited method*/{System.out.println("GRRRRRRRRR

RRRRRRRRRRR");}}

Page 121: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12111/20/2005M. Q. Azhar (Sumon)

A Leopard is-a Cat

• Inheritance allows the creation of a more specialized class from a parent class.

• The derived class extends the attributes and/or functionality of the base class.

• A derived class has everything that the base class has, and more.

• The relationship between the base class and a derived class is often called an is-a relationship because every derived class is a (kind of) superclass.

Page 122: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12211/20/2005M. Q. Azhar (Sumon)

A Leopard is-a Cat

• For example, A Leopard is a Cat in the sense that a Leopard can do everything that a Cat can do.

• A Leopard has all the attributes and functionality of a Cat (and more).

• When deciding whether or not to extend a class, you should determine whether or not an is-a relationship exists. If not, inheritance is probably not appropriate.

Page 123: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12311/20/2005M. Q. Azhar (Sumon)

Inheritance• The two classes, Cat and Leopard are related through inheritance.∀ • Use of the keyword extends signifies an inheritance

relationship: Leopard extends Cat, Leopard inherits from Cat.∀ • Cat is called the base class, super class, or parent class.∀ • Leopard is a derived class, sub class or child class.∀ • The Leopard class inherits all data and methods of the parent

base class. • However, the Leopard class can also override any inherited

methods and provide its own implementation. • Further, the Leopard class may include new methods and

variables that are not part of the base class.∀ • Constructors are not inherited.

Page 124: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12411/20/2005M. Q. Azhar (Sumon)

Inheritance: Using Super

• The derived class can call the constructor of the parent class with the keyword super (super() or super(x) ).

• If a derived class calls a super class constructor then this call must be made before any other code is executed in the

constructor of the derived class.• If an explicit super() call is not made, the default constructor of

the parent is automatically invoked. • If a superclass defines constructors but not a default

constructor, the subclass cannot use the default constructor of the super class because none exists.

• It is a good practice to define a default constructor whenever you define any constructor.

Page 125: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12511/20/2005M. Q. Azhar (Sumon)

No more Extension! –Use Final

• To prevent a class from being extended, use the modifier final. A final class cannot be a parent class:

• Example: public final class MyClass The class MyClass cannot be extended

and is the parent of no other classes.

Page 126: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12611/20/2005M. Q. Azhar (Sumon)

Inheritance: Access

• The access modifier protected is used in the parent. A protected variable or method in a public class MyClass can be accessed by any subclass of MyClass.

Page 127: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

Discussion

Students views on Technology and learningEngaging Women and Minorities

Community building

Page 128: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12811/20/2005M. Q. Azhar (Sumon)

Student Views on Technology and Learning

(source: www.netday.org)

• According to NetDay's (www.netday.org) Speak Up Day for Students 2004 where – 167,000 students from 1,782 schools

participated in the online surveys – from October 11 through November 12, 2004. – Over the past two years, over 377,000 K-12

students – shared their ideas on education and technology

with NetDay through Speak Up Day.

Page 129: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 12911/20/2005M. Q. Azhar (Sumon)

Commonly used Words in Student Responses

(source: www.netday.org)

• IM 1,934 • Internet 9,314 • Money 1,789 • Music 746 • Filter/Firewall/Block 1,887 • Phone 2,237 • Future 1,213 • Career/Job 1,373

Page 130: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13011/20/2005M. Q. Azhar (Sumon)

Recurring Theme(source: www.netday.org)

• Technology is important in my life. • Technology is a big part of my learning. • We use technology for learning in many ways. • Technology improves grades and test scores. • We use technology to communicate. • We also learn outside of the classroom. • We want to learn OUR way. • Technology is important for our careers and our

futures. • Please listen to our recommendations.

Page 131: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13111/20/2005M. Q. Azhar (Sumon)

Communication is #1 with today’s students!

(source: www.netday.org)

• Access:– 81% of students in grades 6-12 have at least one email

account. 14% have 4+ email accounts. – Three-quarters of these students have at least one IM

screen name and one-quarter of those students say that they have 4 or more IM screen names. 33% of students in grades 3-5 have an IM screen name.

– 58% have their own cell phone and 68% of those gr 6-12 students bring it to school, whether the school allows cell phones or not. 1 in 5 of those school-bound cel phones has text and photo/video capabilities.

Page 132: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13211/20/2005M. Q. Azhar (Sumon)

Students in the NET! (source: www.netday.org)

• Usage – Email and instant messaging is the #1 technology

activity for students in grades 6-12 outside of school.

– 40% of students say that they use email and instant messaging on a weekly basis.

– Two-thirds of students email or IM with others besides friends on a weekly basis

– 69% email parents & family members, 25% email with adults involved in their education and 4% email with a religious leader.

Page 133: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13311/20/2005M. Q. Azhar (Sumon)

Students in the NET!(source: www.netday.org)

– While girls are more likely to use email than boys, there is little gender split on instant messaging both boys and girls IM with the same frequency.

– Students cite instant messaging as one of the top 3 ways they use technology for schoolwork to IM a classmate about a school project.

– 1 out 3 students say that they know someone who has been bullied or harassed online by another student using email or IM.

Page 134: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13411/20/2005M. Q. Azhar (Sumon)

Students in the NET! (source: www.netday.org)

• If students could change one thing about tech use at middle or high school their #1 change would be to allow email and IM access from school.

• For elementary students, it would be use their cell phone!!!

• Technology is more than just fun & games for today’s students but that’s important too!

• 83% of students say losing Internet would negatively impact schoolwork!!!

• 94% of students said that technology access is important to their education and future jobs and careers.

Page 135: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13511/20/2005M. Q. Azhar (Sumon)

Sampling of Emerging Themes from National Findings

(source: www.netday.org)• Today’s students are creating a new norm for technology use that seamlessly combines learning (both in school and out of school), entertainment and communications into their daily activities.

• Students. use of new technologies such as instant messaging, music downloads and cell phones have little or not gender differentiation. The greatest gender split continues to be in online and video gaming.

• Younger students. access and usage of technology, particularly communications technology, is expanding dramatically and these students are now pushing the expectations for greater tech use in elementary school.

– (Disclaimer: The Speak Up Day surveys are a convenience sampling and we have not done any statistical significance testing on the data.)

Page 136: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13611/20/2005M. Q. Azhar (Sumon)

Engaging Women and Minorities• Equity in IT classroom

– http://www.josanders.com/portal/index.htm• Lesson plans designed to engaging girls and minorities:

Submitted by Amanda Stent of Stony Brook University– MCSA Workshop 2003 http://teachertech.rice.edu/Lessons/– CRA-W careers brochure http://www.sdsc.edu/CRAW/careers/– Think Again -- Girls Can! http://www.girlscan.org

• TECS Initiative:• http://tecs.acm.org/resources/equity.cfm

• Educational Robotics @ Brooklyn College• http://www.sci.brooklyn.cuny.edu/~sklar/er

Page 137: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 13711/20/2005M. Q. Azhar (Sumon)

Integrating technology into Education

• What is Educational Robotics?– use of robotics as a hands-on learning environment for

teaching topics other than specifically robotics. • Our Approach:

– We are developing curricular materials which are on the use of educational robotics currently in

• High School classes (mark twain)• undergraduate classes (Brooklyn College).

• Contests:• RobocupJunior  (http://www.robocupjunior.org)

– (Next Competition: Friday, March 31st, 2005)• Robocup (http://www.robocup.org)

Page 138: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

Topic 4:Object Oriented Analysis and

Design

Page 139: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

Specification

Analysis

Design

Implementation

Testing

Page 140: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14011/20/2005M. Q. Azhar (Sumon)

Basic Concepts

• A Class: – A class is a description used to instantiate

objects• An Object:

– Is an instance of a class, it has a name, attributes and their values, and methods

– An object models an idea found in reality, (tangible or abstract)

Page 141: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14111/20/2005M. Q. Azhar (Sumon)

Basic Concepts (cont’d)• Attributes• Methods (Services, Messages)• Information hiding and Encapsulation: A

technique in which an object reveals as little as possible about its inner workings.

• Inheritance• Polymorphism, Overloading, Templates

Page 142: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14211/20/2005M. Q. Azhar (Sumon)

Identifying the Object• Identifying Objects (and Classes)• Identify the nouns (objects) in the problem

description. • Identify more objects in your developing

problem solution. (Design is incremental and iterative!   --   Grady Booch)

• From these objects, determine what new types (classes) you need.

Page 143: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14311/20/2005M. Q. Azhar (Sumon)

Example• Example: BlackJack Program

– cards – a deck – a dealer – player(s)

• Sample Design Questions: – Do different cards represent different types? Or are suit

and rank attributes of a single card type? – Are the dealer and player instances of the same class?

I.e., objects of the same type? – Should there be a separate Hand class?

Page 144: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14411/20/2005M. Q. Azhar (Sumon)

Identifying Operations

• Identify what can be done to objects.     or • Identify what objects can do for themselves.

    or • Identify who is responsible for doing what. • Terminology: operations, responsibilities,

contracts, message passing, encapsulation, data hiding

Page 145: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14511/20/2005M. Q. Azhar (Sumon)

Example• Example: BlackJack Program

– Card: display, report value (report suit? maybe internally) (modify value/suit? or set these in constructor only?)

– Deck: shuffle, deal next card, report how many cards left in deck

– Dealer: deal cards, add card to hand, show (sometimes partial) hand, calculate (partial) hand value, number of cards in hand, hit?, determine winner, reset hand

– Player: add card to hand, show hand, calculate hand value, number of cards in hand, hit?, reset hand

Page 146: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14611/20/2005M. Q. Azhar (Sumon)

Identify Relationships

• Identify object relationships, e.g. what objects need to know about what other objects, what objects are part of other objects.

• Terminology: collaboration; aggregation/has-a relationships; knows-about relationships (Note: is-a relationships are class relationships, not object relationships. They should be handled separately, usually by inheritance.)

Page 147: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14711/20/2005M. Q. Azhar (Sumon)

Example• Example: BlackJack Program • Card: • Deck: has cards • Dealer: has deck, has cards (or hand, which

has cards), knows about player(s) • Player: has cards (or has hand, which has

cards)

Page 148: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14811/20/2005M. Q. Azhar (Sumon)

Developing Scenarios

• Develop scenarios of how the objects interact to see if you have identified the right objects, the right operations, and the right relationships.

• Develop common scenarios (this is how the thing should usually work).

• Remember to also develop uncommon scenarios, "weird cases," boundary conditions.

• Scenarios are similar to, but more informal than, design-based test cases.

• Developing scenarios will often cause you to recognize new design requirements. Remember that design is incremental and iterative!  

Page 149: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 14911/20/2005M. Q. Azhar (Sumon)

Implementing an Object-Oriented Design• Design public interfaces (public member function

declarations) for the classes and operations you identified in the design.

• Design the internal data representation (private data members). Include information about relationships.

• Implement the public member functions. Identify useful internal helper functions (private member functions).

• Test individual objects and member functions before putting everything together.

Page 150: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15011/20/2005M. Q. Azhar (Sumon)

Summary

• Identify Objects and Classes                                 Operations

                                                Relationships

• Strive for Well-defined Responsibilities, Strong Cohesion, Weak Coupling

• Develop Scenarios • Remember:   Design is incremental and iterative!

Page 151: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

11/20/2005M. Q. Azhar (Sumon)

Graphics:

Applets

Page 152: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15211/20/2005M. Q. Azhar (Sumon)

Graphics: Applets

Client

Client

Server

applet

browser

browser

applet

Page 153: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15311/20/2005M. Q. Azhar (Sumon)

Exampleimport java.awt.*;import java.applet.Applet;public class HelloWorld extends Applet { public void init(){ add(new Button("Hello World! init")); } public void start(){ add(new Label("Hello World! start")); } public void paint(Graphics g){ g.drawString("Hello World! paint",50,50); }}

Overwrite the one in Panel

Page 154: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15411/20/2005M. Q. Azhar (Sumon)

html File

<html><body><applet

code=“HelloWorld.class"width=400 height=400>

</applet></body></html>

Page 155: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15511/20/2005M. Q. Azhar (Sumon)

Life Cycle of an Applet

create an instance of Applet

init()

start()

stop()

loading code

clean up

Page 156: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15611/20/2005M. Q. Azhar (Sumon)

Test The Life Cycle of an Appletimport java.applet.Applet;import java.awt.Graphics;

public class Simple extends Applet { StringBuffer buffer; public void init() {

buffer = new StringBuffer(); addItem("initializing... "); } public void start() { addItem("starting... "); } public void stop() { addItem("stopping... "); } public void destroy() { addItem("preparing for unloading..."); }

void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord); repaint(); }

public void paint(Graphics g) { g.drawRect(0, 0, getSize().width - 1,

getSize().height - 1); g.drawString(buffer.toString(), 5, 15); }}

Page 157: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15711/20/2005M. Q. Azhar (Sumon)

Retrieving Remote Imagesimport java.awt.*;import java.applet.Applet;

public class TestImage extends Applet {Image queen;public void init(){

queen = getImage(getCodeBase(),"queen.jpg");}public void paint(Graphics g){

g.drawImage(queen,50,50,100,100,this);}

}

Page 158: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15811/20/2005M. Q. Azhar (Sumon)

Parameterizing Appletspublic class TestParam extends Applet { Image img; public void init() throws RuntimeException { String imageName = getParameter("image"); if (imageName==null){ throw new RuntimeException("Parameter image is missing"); } else { img = getImage(getCodeBase(),imageName); } } public void paint(Graphics g){ g.drawImage(img,50,50,100,100,this); }}

Page 159: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 15911/20/2005M. Q. Azhar (Sumon)

html File<html><body><applet

code="TestParam.class"width=400 height=400>

<param name="image" value="queen.jpg"></applet></body></html>

Page 160: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 16011/20/2005M. Q. Azhar (Sumon)

Restrictions on Applets

• An applet CANNOT– contain native methods– read and write files on the client– make network connections except with the

server where it came from– cannot start programs

Page 161: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 16111/20/2005M. Q. Azhar (Sumon)

References• Tecs Resources:

– http://tecs.acm.org/resources• Project NetDay

– http://www.netday.org• OOP using Java with Lego Mindstorms

– http://www.sci.brooklyn.cuny.edu/~mqazhar/java

Page 162: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 16211/20/2005M. Q. Azhar (Sumon)

More References• College Board Website with various Free-response questions:

– http://www.collegeboard.com/student/testing/ap/prep_free.html• AP computer Scinece Links:

– It has pointer to various good website– http://www.cs.cmu.edu/~mjs/apcs.html

• AP Computer Science Resources:– http://www.cs.duke.edu/csed/ap/

• Advanced Placement Unofficial Website:– http://cs.colgate.edu/APCS/

• Marine Biology– http://max.cs.kzoo.edu/AP/MBS/

Page 163: Presented by M. Q. Azhar (Sumon) - Brooklyn Collegemqazhar/tecsBCModule7.pdf · M. Q. Azhar (Sumon) • An object is a "thing,” a “gizmo," a "gadget,” … an object. • For

Tecs @ Brooklyn College 16311/20/2005M. Q. Azhar (Sumon)

References

• Appropriate Lab Materials:– http://max.cs.kzoo.edu/AP/Java/A_Materials.html

• Marine Biology Case Study in C++– http://max.cs.kzoo.edu/AP/Fish/index.html

• Marine Biology Case study in OOP– http://max.cs.kzoo.edu/AP/

• Cool Website with Marine Biology Picture Diagram– http://max.cs.kzoo.edu/AP/MBS/ObjectDiagrams/