CS2_16_ExaminingAndChangingState

CS2_16_ExaminingAndChangingState - CS2 Module 16 Category:...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2 Module 16 Category: OO Concepts Topic: Examining and Changing State Objectives: Accessors Modifiers References Parameters CS 2 Introduction to Object Oriented Programming Module 16 OO Concepts Examining and Changing State What is an object? A chunk of memory which contains some data and methods which can operate on the data. Something that when created may have had special code run to initialize it. Something which has additional behavior defined by methods which can Be passed data via parameters Perform calculations/operations which may Change some of its data Perform some desired operation Return the value of some of its data Return the results of calculations using both data passed in and data contained in the object How do we define different types of objects? For each different type of object we write a class. New classes may be based on existing classes to allow code reuse A class serves as a blueprint or template defining how all objects made from this class operate and what data they hold. One class may be used to make one or many objects Classes and Objects Classes and Objects Class: It describes the form of an object. It is a template or blueprint. It specifies data representation, behavior, and inheritance (via variables, methods and parents) Object: It is an instance of a class. It has a unique copy of every non-static variable. (i.e., the instance variables but not the class variables (those labeled with static). Naming Conventions: Classes: Identifiers begin with cap letters for each word in the identifier, e.g., class GraduateStudent Objects: Identifiers begins with lower case letter, then caps for other words in identifier, e.g., graduateStudentPresident Difference between a class and an object of that class is analogous to the difference between a type and a variable of that type. Key Idea Suppose... We are working with the Point class we already started For our application a Point really means the x and y coordinate of the Point. We want to be able to set these values and also to get these value from a Point object We might also want a Point to be able tell us its distance from the origin but we don't necessarily want to store this value in the object We need something that defines how all of these Point objects will function. Specification of a Class public class Point { int x; int y; public Point() { x = 0; y = 0; } // Constructor public void setX (int newX) { x = newX; } // setX public int getX() { return x; } // getX public void setY(int newY) { y = newY; } // setY public int getY() { return y; } // getY public double distToOrig() { return Math.sqrt((double)x*x+y*y); } // disToOrig public double dist(Point p) { return Math.sqrt( sqr(x - p.getX())+ sqr(y - p.getY()) ); } // dist public double sqr(int x) { return (double)x*x; } // sqr } // Point Use accessors and modifiers!(some books call them "getters" and "setters") Specification of a Class...
View Full Document

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 47

CS2_16_ExaminingAndChangingState - CS2 Module 16 Category:...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online