COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips 367 1.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 COMP 110 Inheritance Tabitha Peck M.S. April 16, 2008 MWF 3-3:50 pm Philips 367.
1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
COMP 110 More Loops and Strings Tabitha Peck M.S. February 18, 2008 MWF 3-3:50 pm Philips
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips
Information Hiding and Encapsulation
COMP 110 Classes Tabitha Peck M.S. March 3, 2008 MWF 3-3:50 pm Philips
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
CSC 212 Object-Oriented Programming and Java Part 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
CMSC 202 Java Classes and Object 2nd Lecture. Aug 6, Stack and Heap When your program is running, some memory is used to store local variables.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Defining Classes and Methods
Objects as a programming concept
COMPUTER 2430 Object Oriented Programming and Data Structures I
COS 260 DAY 10 Tony Gauvin.
Classes and Objects 2nd Lecture
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CLASS DEFINITION (> 1 CONSTRUCTOR)
Computer Science II Exam 1 Review.
Chapter 3 Introduction to Classes, Objects Methods and Strings
COMP 110 Information hiding and encapsulation
Defining Classes and Methods
Introduction to Java Programming
Slides by Steve Armstrong LeTourneau University Longview, TX
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Defining Classes and Methods
Defining Classes and Methods
Defining Classes and Methods
Information Hiding and Encapsulation Section 4.2
Presentation transcript:

COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips 367 1

Announcements Late days Do you have any left? 2

Questions? What is the difference = ==.equals() 3

Class and Primitive Types In memory Primitive types store value Class types store address to data Why? 4

How big is a String? int 4 bytes float 4 bytes double 8 bytes String ? bytes “hello” “How are you doing today” 5

Types in memory “hello” “A string” int num float num2 String s1 String s2 s1 = s2; s1 = “how are you?” System.out.print(s2); “How are you?” 6

Today in COMP 110 Writing a class with good programming practices Monopoly 7

Why classes? boardwalk.buyHouse(); if(!hotelOwned || !mortgaged) { if (numHouses == 4) { hotelOwned = true; numHouses = 0; } if (numHouses < 4) numHouses++; } 8

Information Hiding Method can be used without understanding detail of code A.K.A. - abstraction Programmer’s job becomes simpler Code is easy to read 9

Before You Start Define the problem Write a class for a property in Monopoly UML diagram 10

- name: String - cost: int - morgageRate: int - houseCost: int - rent: int - house1: int - house2: int - house3: int - house4: int - hotel: int - owner: String - currentRent: int - numHouses: int - hotelOwned: boolean - mortgaged: boolean Property Class Name Instance Variables Instance variables should be private 11

- calculateCurrentRent(): void + setOwner(String s): void + setNumHouse(int i): void + buyHotel(): void + buyHouse(): void + mortgageProperty(): void + buyProperty(): void + getRent(): int + inquire(): void Property Class Name Methods Most methods are public but some are private 12

Comments Precondition - everything that needs to be true before method Postcondition - describes effect of method call 13

/** Precondition: The instance variables of the calling objects have values. Postcondition: The number of houses the user owns have been updated by one if the owner had fewer than 4 houses and the property has not been mortgaged. If the owner had 4 houses, they bought a hotel and now have zero houses. */ 14

When to use Pre and Post You can omit for obvious methods get (accessor), set (mutator) … All other methods need pre and post conditions If you are unsure, write pre and post! 15

Accessor Method Read data from instance variable getRent(); 16

Mutator Methods Change data in private instance variable setOwner(String s); 17

Write methods mortgageProperty(); calculateCurrentRent(); 18

Constructors Method designed to initialize instance variables public Property No return value Same name as class Monopoly property: Name, cost, mortgage rate, cost for each house, rent, rent for each house, rent for hotel 19

Encapsulation Hiding details of a class that are not necessary to understand how objects of the class are used User Interface Implementation 20

User Interface What programmer needs to know to use the class Headings for public methods public type_or_void Method_Name(Parameter_List) Defined constants public static final Type Variable = Constant; Comments for using public methods /* Precondition: Postcondition: */ 21

Implementation All private elements of class definition Private instance variables private Type Variable; Definitions of private and public methods body of the methods 22

Implementation: Private instance variables Private constants Private Methods Bodies of all methods Method definitions Well Encapsulated Interface: Comments Headings of public methods Public defined constants Programmer 23

Guidelines Comments before class definition (this is your header) Instance variables are private Provide public accessor and mutator methods Pre and post comments before methods Make helping methods private /**/ for user-interface comments and // for implementation comments 24

Global vs. Local Variables are local to methods Instance variables are Global for all methods in a class 25

Wednesday Finish 4.3 Start