{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS2_16_ExaminingAndChangingState

CS2_16_ExaminingAndChangingState - CS2 Module 16 Category...

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

View Full Document Right Arrow Icon
CS2 Module 16 Category: OO Concepts Topic: Examining and Changing State Objectives: Accessors Modifiers References Parameters
Image of page 1

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

View Full Document Right Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 16 OO Concepts Examining and Changing State
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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.
Image of page 6
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(
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern