Unformatted text preview:

Mo va ng the OO Way COMP 401 Spring 2013 Lecture 03 1 17 2013 TriangleArea example Write a program that reads as input sequences of coordinates de ning a triangle Speci ed as 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle Categorize triangle as one of equilateral isosceles scalene Report triangle category for each triangle AUer end of all input Report average size of triangles by category Report area of smallest triangle TriangleArea example ta v1 Write a program that reads as input sequences of coordinates de ning a triangle Speci ed as 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle Categorize triangle as one of equilateral isosceles scalene Report triangle category for each triangle AUer end of all input Report average size of triangles by category Report area of smallest triangle TriangleArea example ta v2 Write a program that reads as input sequences of coordinates de ning a triangle Speci ed as 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle Categorize triangle as one of equilateral isosceles scalene Report triangle category for each triangle AUer end of all input Report average size of triangles by category Report area of smallest triangle TriangleArea example ta v3 Write a program that reads as input sequences of coordinates de ning a triangle Speci ed as 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle Categorize triangle as one of equilateral isosceles scalene Report triangle category for each triangle AUer end of all input Report average size of triangles by category Report area of smallest triangle TriangleArea example ta v4 Write a program that reads as input sequences of coordinates de ning a triangle Speci ed as 6 real numbers ax ay bx by cx cy Input will end with the word end For each triangle Categorize triangle as one of equilateral isosceles scalene Report triangle category for each triangle AUer end of all input Report average size of triangles by category Report area of smallest triangle Review of non OO approach All func ons are sta c Variables are all either declared locally or passed in as parameters Class simply acts as a library of related func ons Thinking with an object mindset Consider the role of ax ay bx by cx cy As a collec ve the represent a speci c triangle Func ons for nding area and classifying Onus on us to provide this informa on as parameters Object oriented programming ips this rela onship Formalizes the collec ve meaning of these pieces of informa on as an abstrac on Provide func ons that operate on the abstrac on Step 1 Name the abstrac on In Java this means create a class corresponding to the abstrac on s name ta v5 Step 2 Declare its elds The elds of your abstrac on are pieces of informa on that collec vely de ne it Declared like variables Must specify type and adhere to variable naming rules Directly declared in class de ni on Here you start to make design decisions In our example triangles de ned by 3 coordinates How else could a triangle be de ned Note that part of this is deciding on types as well What would be impact of choosing something other than double ta v6 Step 3 De ne a constructor Constructor is a special type of func on Job is to create and ini alize a new instance Declara on di ers from a normal func on Name must match class name Does not have a any sort of return value in its signature Within the constructor the keyword this refers to the new object Any informa on needed should be passed in as parameters Code in the constructor is responsible for making sure that the elds of this are appropriately set To call a constructor use the new keyword Result will be a new instance i e object of the class ta v07 Step 4 De ne instance methods Func ons procedures where value of func on or opera on of procedure depends on the speci c instance What func ons in our example calculate values speci c to a par cular triangle triangle category triangle area Declared within class without sta c Can only be called through a reference to a speci c instance previously created by a constructor Within an instance method the keyword this provides a reference to the speci c object in ques on Implied this Variable names referenced in constructors and instance methods that are not named parameters or locally declared are assumed to be elds of this Can leave o the this part One more improvement No ce that within area and category we end up calcula ng side lengths Would be beger if we simply provided methods for retrieving each of the side lengths this side ab this side bc this side ca Implied this also works for func on names Don t need to use this keyword if method is part of the same object ta v09 Repea ng with Point Consider role of ax ay within Triangle class Collec vely they represent a point Same with bx by and cx cy Opportunity for abstrac on again ta v10 Classes and Objects Fundamental units of abstrac0on Physical Analogy Classes are like factories Contain a blueprint for an object De nes the inner workings i e elds aka members De nes what it can do i e instance methods Factory itself may have some capabili es Class members and class methods Objects are what the factory builds Each object is an instance of a class Name of the class is the data type of the object Which means it is the data type of any variable that can reference the object Objects as state An object is de ned by its state Collec on of named elds that represent informa on about the object The current values assigned to those elds re ect the state of the object Object design re ects purpose What elds to include in an object will depend on how that object is to be used and the kinds of opera ons that object will be involved in


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lec03Motivating_OO

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view comp401sp13lec03Motivating_OO and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lec03Motivating_OO and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?