Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Similar documents
Chapter 4: Writing Classes

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

CS1004: Intro to CS in Java, Spring 2005

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Designing Classes part 2

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Where do objects come from? Good question!

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

ECE 122. Engineering Problem Solving with Java

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

Assignment 1 due Monday at 11:59pm

ECE 122. Engineering Problem Solving with Java

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Packages & Random and Math Classes

Chapter 5: Writing Classes and Enums

Java Basic Introduction, Classes and Objects SEEM

Objects and Classes -- Introduction

Java Basic Introduction, Classes and Objects SEEM

Formatting Output & Enumerated Types & Wrapper Classes

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

Chapter 4 Writing Classes

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

writing classes objectives chapter

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

Encapsulation. Mason Vail Boise State University Computer Science

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Programming II (CS300)

Chapter 4 Defining Classes I

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter

CS1004: Intro to CS in Java, Spring 2005

Midterms Save the Dates!

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

ECE 122. Engineering Problem Solving with Java

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

CS1083 Week 2: Arrays, ArrayList

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

ECOM 2324 COMPUTER PROGRAMMING II

Creating an object Instance variables

CS108 Lecture 16: User Defined Classes. Overview/Questions

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS 302 Week 9. Jim Williams

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Classes Classes 2 / 35

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

ITI Introduction to Computing II

Methods and Data (Savitch, Chapter 5)

CSCE 156 Computer Science II

Inheritance and Polymorphism

Cloning Enums. Cloning and Enums BIU OOP

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Midterms Save the Dates!

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

EECS168 Exam 3 Review

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Topic 3 Encapsulation - Implementing Classes

Java Review. Fundamentals of Computer Science

ITI Introduction to Computing II

Classes Classes 2 / 36

Example: Fibonacci Numbers

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

CS-202 Introduction to Object Oriented Programming

Robots. Byron Weber Becker. chapter 6

Principles of Object Oriented Programming. Lecture 4

CS 251 Intermediate Programming Methods and Classes

Final Exam Practice. Partial credit will be awarded.

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CS112 Lecture: Defining Instantiable Classes

Handout 7. Defining Classes part 1. Instance variables and instance methods.

CMSC 132: Object-Oriented Programming II

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

Slide 1 CS 170 Java Programming 1

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CprE 281: Digital Logic

Recommended Group Brainstorm (NO computers during this time)

CS 116 Week 8 Page 1

More on variables and methods

Transcription:

Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday we will have a review session ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev No class on Friday (Sep 21, 2007) Writing Classes The programs we ve written in previous examples have used classes defined in the Java standard class library Quick review of last lecture Now we will begin to design programs that rely on classes that we write ourselves The class that contains the main method is just the starting point of a program True object-oriented programming is based on defining classes that represent objects with welldefined characteristics and functionality Classes and Objects Recall from our overview of objects in Chapter 1 that an object has state and behavior Consider a six-sided die (singular of dice) It s state can be defined as which face is showing It s primary behavior is that it can be rolled We can represent a die in software by designing a class called Die that models this state and behavior The class serves as the blueprint for a die object We can then instantiate as many die objects as we need for any particular program Classes A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations 1

Classes The values of the data define the state of an object created from the class The functionality of the methods define the behaviors of the object For our Die class, we might declare an integer that represents the current value showing on the face One of the methods would roll the die by setting that value to a random number between one and six Classes We ll want to design the Die class with other data and methods to make it a versatile and reusable resource Any given program will not necessarily use all aspects of a given class See RollingDice.java (page 157) See Die.java (page 158) The Die Class The Die class contains two data values a constant MAX that represents the maximum face value an integer facevalue that represents the current face value The roll method uses the random method of the Math class to determine a new face value There are also methods to explicitly set and retrieve the current face value at any time The tostring Method All classes that represent objects should define a tostring method The tostring method returns a character string that represents the object in some way It is called automatically when an object is concatenated to a string or when it is passed to the println method System.out.println ("Die One: " + die1 + ", Die Two: " + die2); Constructors As mentioned previously, a constructor is a special method that is used to set up an object when it is initially created A constructor has the same name as the class The Die constructor is used to set the initial face value of each new die object to one We examine constructors in more detail later in this chapter Data Scope The scope of data is the area in a program in which that data can be referenced (used) Data declared at the class level can be referenced by all methods in that class Data declared within a method can be used only in that method Data declared within a method is called local data In the Die class, the variable result is declared inside the tostring method -- it is local to that method and cannot be referenced anywhere else 2

Instance Data The facevalue variable in the Die class is called instance data because each instance (object) that is created has its own version of it A class declares the type of the data, but it does not reserve any memory space for it Every time a Die object is created, a new facevalue variable is created as well The objects of a class share the method definitions, but each object has its own data space That's the only way two objects can have different states Instance Data We can depict the two Die objects from the RollingDice program as follows: die1 facevalue 5 die2 facevalue 2 Each object maintains its own facevalue variable, and thus its own state Chapter 4 Section 4.3 We can take one of two views of an object: internal - the details of the variables and methods of the class that defines it external - the services that an object provides and how the object interacts with the rest of the system From the external view, an object is an encapsulated entity, providing a set of specific services These services define the interface to the object One object (called the client) may use another object for the services it provides The client of an object may request its services (call its methods), but it should not have to be aware of how those services are accomplished An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client The client invokes the interface methods of the object, which manages the instance data Any changes to the object's state (its variables) should be made by that object's methods We should make it difficult, if not impossible, for a client to access an object s variables directly That is, an object should be self-governing Client Methods Data 3

Method Control Flow If the called method is in the same class, only the method name is needed Method Control Flow The called method is often part of another class or object compute mymethod main doit helpme mymethod(); obj.doit(); helpme(); Why we don t have to use new with the NumberFormat class? The new is performed for you inside that class main NumberFormat fmt= NumberFormat. getcurrencyinstance(); getcurrencyinstance() getinstance() return getinstance(..); NumberFormat.java NumberFormat format = new NumberFormat(..); return format; UML Diagrams UML stands for the Unified Modeling Language UML diagrams show relationships among classes and objects A UML class diagram consists of one or more classes, each with sections for the class name, attributes (data), and operations (methods) Lines between classes represent associations A dotted arrow shows that one class uses the other (calls its methods) UML Class Diagrams A UML class diagram for the RollingDice program: In Java, we accomplish through the appropriate use of visibility modifiers RollingDice main (args : String[]) : void facevalue : int Die roll() : int setfacevalue (int value) : void getfacevalue() : int tostring() : String A modifier is a Java reserved word that specifies particular characteristics of a method or data We've used the final modifier to define constants Java has three visibility modifiers: public, protected, and private The protected modifier involves inheritance, which we will discuss later 4

public private Members of a class that are declared with public visibility can be referenced anywhere Variables Methods Violate Provide services to clients Enforce Support other methods in the class Members of a class that are declared with private visibility can be referenced only within that class Members declared without a visibility modifier have default visibility and can be referenced by any class in the same package An overview of all Java modifiers is presented in Appendix E Public variables violate because they allow the client to reach in and modify the values directly Therefore instance variables should not be declared with public visibility It is acceptable to give a constant public visibility, which allows it to be used outside of the class Public constants do not violate because, although the client can access it, its value cannot be changed Methods that provide the object's services are declared with public visibility so that they can be invoked by clients Public methods are also called service methods A method created simply to assist a service method is called a support method Since a support method is not intended to be called by a client, it should not be declared with public visibility Accessors and Mutators Variables public Violate private Enforce Because instance data is private, a class usually provides services to access and modify data values An accessor method returns the current value of a variable Methods Provide services to clients Support other methods in the class A mutator method changes the value of a variable The names of accessor and mutator methods take the form getx and setx, respectively, where X is the name of the value They are sometimes called getters and setters 5

Mutator Restrictions The use of mutators gives the class designer the ability to restrict a client s options to modify an object s state A mutator is often designed so that the values of variables can be set only within particular limits For example, the setfacevalue mutator of the Die class should have restricted the value to the valid range (1 to MAX) Run examples from the book We ll see in Chapter 5 how such restrictions can be implemented THE END 6