COMP 14 Introduction to Programming Adrian Ilie July

  • Slides: 22
Download presentation
COMP 14 Introduction to Programming Adrian Ilie July 8, 2005 Adrian Ilie The UNIVERSITY

COMP 14 Introduction to Programming Adrian Ilie July 8, 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

COMP 14 So Far. . . • • 2 Problem Solving Mathematical Calculations Output

COMP 14 So Far. . . • • 2 Problem Solving Mathematical Calculations Output User Input File I/O Selection (if, if-else, switch) Loops (while, do. . . while, for) Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

COMP 14 Next. . . • Object-Oriented Design • Writing Methods ♦ pieces of

COMP 14 Next. . . • Object-Oriented Design • Writing Methods ♦ pieces of code that we give a name • Writing Classes ♦ organize related pieces of information • Arrays ♦ access multiple pieces of information with a single identifier 3 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Object-Oriented Design • What is it? Designing a solution to a problem by first

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 4 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Objects VCR Example • Use it without knowing how it's made • Internal parts

Objects VCR Example • Use it without knowing how 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 objects (like Strings) that are provided by Java 5 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Objects Consist of data and operations on the data • Data - descriptive characteristics

Objects Consist 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. 6 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Objects And Methods and Classes • We represent operations with methods ♦ group of

Objects And 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 7 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Classes • A class contains data declarations and method declarations • A class is

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 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Object-Oriented Design Simplified Methodology 1. Write down detailed description of problem 2. Identify all

Object-Oriented Design Simplified Methodology 1. 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 operations 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Object-Oriented Design Example 1 • Problem Statement ♦ Write a program to input the

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 10 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Example 1 Building a Rectangle • Identify nouns ♦ length, width, rectangle, perimeter, area

Example 1 Building a Rectangle • Identify nouns ♦ length, width, rectangle, perimeter, area • Identify each class ♦ ♦ 11 length of a rectangle width of a rectangle perimeter of a rectangle area of a rectangle Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Example 1 Building a Rectangle • Identify data members for each class ♦ nouns:

Example 1 Building 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 12 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Example 1 Building a Rectangle • Identify operations for each class ♦ ♦ ♦

Example 1 Building a Rectangle • Identify operations for each class ♦ ♦ ♦ ♦ 13 input, calculate, print set. Length directly from problem set. Width statement compute. Perimeter compute. Area print customary to include operations to get. Length get the value of the data members get. Width Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

class Rectangle Data Members and Operations class name data members operations (methods) 14 Adrian

class Rectangle Data Members and Operations class name data members operations (methods) 14 Adrian Ilie Last Step: design and implement an algorithm for each operation The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Anatomy of a Class • A class contains data declarations and method declarations int

Anatomy of a Class • A class contains data declarations and method declarations int width; int length; Data declarations Method declarations (operations) 15 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Classes and Objects A class (the concept) An object (the realization) length = 15,

Classes and Objects A class (the concept) An object (the realization) length = 15, width = 3 Rectangle length = 20, width = 6 Multiple objects from the same class 16 Adrian Ilie length = 15, width = 15 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Object-Oriented Design Example 2 A place to buy candy is from a candy machine.

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. 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Object-Oriented Design: Example 2 The program should do the following: • Show the customer

Object-Oriented Design: Example 2 The program should do the following: • Show the customer the different products sold by the candy machine. • Let the customer make the selection. • Show the customer the cost of the item selected. • Accept money from the customer. • Return change. • Release the item, that is, make the sale. 18 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Object-Oriented Design: Example 2 19 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL

Object-Oriented Design: Example 2 19 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Object-Oriented Design: Example 2 20 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL

Object-Oriented Design: Example 2 20 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Non-Concrete Objects • Objects in programs don't always have realworld analogs Example object: error

Non-Concrete Objects • Objects in programs don't always have realworld analogs Example object: error message data: text of the error message operation: print the text of the error message to the screen 21 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Next in Comp 14 • Tomorrow: writing methods • Monday: review for mid-term ♦

Next in Comp 14 • Tomorrow: writing methods • Monday: review for mid-term ♦ Bring laptops ♦ Write questions before coming to class 22 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL