chapter 11-12 more on classes intro to computer science cs1510, section 2 dr. sarah diesburg

14
Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Upload: thomasine-atkins

Post on 17-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

The Three OOP Factors Remember, we said there were 3 factors that distinguished an Object Oriented Programming language:  Encapsulation  Inheritance  Polymorphism

TRANSCRIPT

Page 1: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Chapter 11-12More on Classes

Intro to Computer ScienceCS1510, Section 2Dr. Sarah Diesburg

Page 2: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

New Programming Assignment PA10

Page 3: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

The Three OOP Factors

Remember, we said there were 3 factors that distinguished an Object Oriented Programming language: Encapsulation Inheritance Polymorphism

Page 4: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

We are Still at Encapsulation Feature of encapsulation:

Hide details of the implementation so that the program is easier to read and write

Modularity, make an object so that it can be reused in other contexts

Provide an interface (the methods) that is the approved way to deal with the class In a similar way to other classes Ex: the in operator

Page 5: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Instance Variables

An instance variable is a variable inside of a class that stores information for a particular object

In which class method do these variables first appear with some sort of initial value?

5

Page 6: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Let’s look at an updated version of the Student Class

Page 7: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

How do we compute GPA?

7

Page 8: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

How do we compute GPA?

First, convert letter grade into the number equivalent A- is 3.67, C+ is 2.33, etc.

Next, multiply the number of credits by the letter grade’s number equivalent to get a specific course’s grade point

Finally, add up the number of grade points and divide by the total number of credits

8

Page 9: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Method Types

Which class methods in class Student are accessor methods?

Which class methods in class Student are mutator methods?

9

Page 10: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Two types of programmers now Programmer that builds the class Programmer that uses the class

10

Page 11: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Private Values

We briefly talked about why class users may not want to access instance variables directly

print(s1.firstNameStr)

By only accessing data through methods, the class programmers are free to change how the class works E.g. changing a variable name or adding new

variables

Page 12: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Private Variables in an Instance Many OOP approaches allow you to make a

variable or function in an instance private. Private means not accessible by the class

user, only the class developer. There are advantages to controlling who can

access the instance values.

Page 13: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

‘Privacy’ in Python

Python takes the approach “We are all adults here.” No hard restrictions.

Python implements this notion of privacy with the underscore: _ Which functions have we already seen with

double underscores?

Page 14: Chapter 11-12 More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

‘Privacy’ in Python

Provides naming to avoid accidents. Use _ in front of any variable

Let’s modify our Student class to do that

14