26
COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00- 12:15 Peabody Hall 218

COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Embed Size (px)

Citation preview

Page 1: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

COMP 14Introduction to Programming

Mr. Joshua StoughFebruary 23, 2005

Monday/Wednesday 11:00-12:15

Peabody Hall 218

Page 2: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Today in COMP 14

• Object-Oriented Design

• Review of String methods

• Review of booleans

• Review of increment operator

Page 3: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Object-Oriented Design

• What is it?

Designing a solution to a problem by first identifying components called objects, and determining how the objects interact with each other

Page 4: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

ObjectsVCR Example

• Use it without knowing know it's made

• Internal parts are hidden -- only interact with the provided buttons

• Can't modify the functions of a VCR -- record button always records, play button always plays

Same is true for with objects (like strings) that are provided by Java

Page 5: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Objects

Consists of data and operations on the data

• Data - descriptive characteristics• Operations - what it can do (or what can be

done to it)

Example A coin that can be flipped so that its face shows

either "heads" or "tails"– data: its current face (heads or tails)– operations: it can be flipped

Operations can change data.

Page 6: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

ObjectsAnd Methods and Classes• We represent operations with methods

– group of statements that are given a name

• We can use objects and their methods without knowing exactly how the methods work

• An object is an instance of a class. A class is the blueprint of an object.– the class provides the methods that can

operate on an object of that class

Page 7: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Classes

• A class contains data declarations and method declarations

• A class is a description of an object– just a model, not an actual object– you can think of the concept of a book

without thinking of a particular book

• A class is no more an object than a blueprint is an actual house

Page 8: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Object-Oriented DesignSimplified Methodology1. Write down detailed description of

problem

2. Identify all (relevant) nouns and verbs

3. From list of nouns, select objects

4. Identify data components of each object

5. From list of verbs, select operation

Page 9: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Object-Oriented Design

Example 1• Problem Statement

– Write a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle

Page 10: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Example 1Building a Rectangle

• Identify nouns– length, width, rectangle, perimeter,

area

• Identify each class– length of a rectangle– width of a rectangle– perimeter of a rectangle– area of a rectangle

Page 11: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Example 1Building a Rectangle

• Identify data members for each class– nouns: length, width,

area, perimeter– what are the essential

nouns for describing the rectangle?

– area and perimeter can be computed if we know the length and width

Page 12: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Example 1Building a Rectangle

• Identify operations for each class– input, calculate, print– setLength– setWidth– computePerimeter– computeArea– print– getLength– getWidth

directly from problemstatement

customary to include operations to get the value of the data members

Page 13: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

class Rectangle Data Members and Operations

Last Step:design and implement an algorithm for each operation

class name

data members

operations(methods)

Page 14: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Anatomy of a Class

• A class contains data declarations and method declarations

int width;int length;

Data declarations

Method declarations(operations)

Page 15: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Classes and Objects

Rectangle

A class(the concept)

length = 15, width = 3

An object(the realization)

length = 20, width = 6

length = 15, width = 15

Multiple objectsfrom the same class

Page 16: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Object-Oriented Design Example 2

A place to buy candy is from a candy machine. A new candy machine is bought for the gym, but it is not working properly. The candy machine has four dispensers to hold and release items sold by the candy machine and a cash register. The machine sells four products —candies, chips, gum, and cookies—each stored in a separate dispenser. You have been asked to write a program for this candy machine so that it can be put into operation.

multiple objects/classes (pgs. 302-305)

Page 17: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Non-Concrete Objects

• Objects in programs don't always have real-world analogs

Exampleobject: error messagedata: text of the error messageoperation: print the text of the error message to the screen

Page 18: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

The String Class

• The String class is used to define String objects

• Each String object contains specific characters (its data members)

• Each String object can perform services (operations), such as toUpperCase, equals

Page 19: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

String MethodsMethod Description

boolean equals(String str)

int length()

String toLowerCase()

char charAt(int index)

the result can be stored in a boolean variable or used in a boolean expression (return type)

the method needs to be given another String (parameter)

method name

Page 20: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Using String Methods

• list of String methods: Chapter 3 (pgs 104-112) and Appendix E (pgs 914-916)

• Steps:– create a String object

– put data into the object

– use the dot operator to "call" the method

String line;

line = keyboard.readLine();String greeting = "Hello";

int len = line.length();greeting = greeting.toUpperCase();char ch = greeting.charAt(3);

Page 21: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

The class IntClass

• Provided by the textbook authors (not a part of standard Java library)– also DoubleClass

• Described pgs 306-307 and Appendix D

• Mentioned in some examples, but we probably won't use it

Page 22: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Booleans

• boolean variables can take one of two possible values: true or false

• Mostly used in decision (conditional) statements– if, if-else– loops

Page 23: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Questions

Given: count = 4, max = 8, factor = 2

1. count < max

2. (count * factor) >= max

3. (max / factor) == (count + 1)

true

true

false

Page 24: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Write Boolean Expressions1. factor times count is less than

total and sum

2. total is less than or equal to sum or factor

3. the expression evaluates to true when isFound is false

(factor * count < total) && (factor * count < sum)

(total <= sum) || (total <= factor)

!isFound OR (isFound == false)

Page 25: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Increment Operator

Example:int count = 0;count++; // same as count = count + 1;

Not:int count = 0;count = count++;

using post-increment, so this stores the original value of count in count and then increments (but doesn't actually change count)

Page 26: COMP 14 Introduction to Programming Mr. Joshua Stough February 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Next Time in COMP 14

• We'll go over answers to the previous mid-term that's posted on Blackboard– ignore questions 7-12, 18-20

• Anything else?– send me email by Saturday