CS112 Lecture: Defining Instantiable Classes
|
|
- Theodore Fowler
- 5 years ago
- Views:
Transcription
1 CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials: 1. Handout of code for BankAccount class showing various sections 2. Transparency of Wu figure 4.2 (use 2nd ed; 3rd ed does not project well) 3. Transparency of Wu figure Transparency of Wu figure Online documentation for class java.awt.color I. Introduction A. One of the most important questions we must ask when designing an OO system is what classes do we need for this system? 1. This is a question we will keep coming back to as our understanding of OO grows. 2. It turns out that identifying the right set of classes is a key step in OO design. Finding the right set of classes for a given problem is non-trivial, and requires considerable thought. 3. In a later course (CS211), we ll talk a lot about the process of determining which classes will be needed. I did want to say just a little bit about this now, though. B. In determining what classes are needed for a given program, we can begin by thinking about what objects must exist when the program is running. Each object will, of course, have to belong to some class - so if we can identify the objects, we are well on our way to identifying the classes. C. One helpful approach we can take is to make use of the notion of responsibility driven design - every thing that the program does must be the responsibility of one or more objects belonging to some class in the program. Example: In lab 3, we were led to define a class CarpeterRobot because the problem s requirements included needing an object responsible for laying carpet. 1
2 D. It turns out that objects in a program often fall into one of three broad categories: 1. Entity objects represent the basic entities the program deals with - e.g. robots. 2. Controller objects are responsible for directing the activities of other objects - e.g. our robot programs. 3. Boundary objects represent the interface between the system and the outside world. The class World which we used in our robot programs provided access to an object that visibly displayed the world our robots operated within. In general, we will need to use/develop classes of all three kinds to solve a particular problem. II. Defining an Instantiable Class A. As we discover the responsibilities that need to be fulfilled by a given system and assign them to objects, we will need to determine what class each object must belong to. 1. When possible, we will want to make use of existing classes that already have methods for carrying out the necessary responsibilities. 2. But, in any non-trivial system, there will be a need to define one or more new classes of objects to fulfill responsibilities that are distinctive to that system - either from scratch, or by extending an existing class. B. Once we have determined that a new class needs to be defined to fulfill a given responsibility or set of responsibilities, we need to design the class. To do this, we can consider a series of basic questions: 1. What is the purpose of this class? Can we summarize the responsibilities the class and/or its objects must fulfill in a single, short sentence? EXAMPLE: We earlier discussed an example using classes that represent bank accounts. We might say something like the following: The purpose of the BankAccount class is to model individual bank accounts. 2
3 2. What do objects belonging to this class need to know (about themselves?) EXAMPLE: BankAccount objects need to know their number and their current balance. 3. Whom do objects belonging to this class need to know (what other objects are they associated with - what objects to they need to use to help them do their job?) EXAMPLE: BankAccount objects need to know their owner. 4. What do objects belonging to this class need to be able to do? EXAMPLE: BankAccount objects need to be able to deposit money, withdraw money, etc. Instance methods are of two general kinds: a) Mutators that change the state of the object (e.g. deposit, withdraw). Typically - but not always - mutators return void. b) Accessors that furnish information about the state of the object without changing it (e.g. reportbalance, getaccountnumber). Accessors always return some non-void result. 5. Also to be considered is whether there is any knowledge or behavior that belongs to the class as a whole, rather than to individual objects. EXAMPLE: We might require our BankAccount class to maintain a master list of all open accounts. We might have a behavior payinterest() that calculates interest for each open account. These are properties/behaviors of the class, not of each individual account. C. Having considered these questions, we are ready to define the class. HANDOUT Annoted BankAccount class example 1. Each file should begin with a file comment - a comment that describes the file and specifies its author and date. NOTE in handout 2. We begin each class with a class comment - a comment (in English) that describes the purpose of the class. In Java, the preferred way to do this is 3
4 with a comment that goes immediately before the class, and begins with /** - e.g. NOTE in handout 3. Give the class a name. The name should be clearly related to the purpose of the class. 4. Determine whether the class should stand alone or be an extension of an existing class. 5. Identify the instance variables of the class a) Recall that each object that is an instance of a given class has its own copy of each instance variable. These variables exist as long as the object exists. TRANSPARENCY - Wu figure 4.2 b) In general, each piece of information that an object needs to know will be represented by an an instance variable. NOTE instance variables accountnumber and currentbalance in handout. Note use of a comment to explain how currentbalance is represented c) Further, each object will also need an instance variable to refer to each kind of other object that it knows. (I.e. the instance variables hold answers to two questions: what does this object know? and who does this object know?) There are two possibilities for these instance variables (1) The instance variable may simply be a reference to some other object. NOTE in handout: Instance variable owner (2) Or, the instance variable may be a special kind of object called a collection which holds references to several other objects of the same kind. EXAMPLE: Since bank accounts can have multiple owners (joint accounts), instead of an instance variable called owner, we might have one called the owners that is a collection. (Collections are a 4
5 topic we will discuss later this semester - so far now we don t know how to do this in Java, so we ll stick with a single owner for this example.) d) Conceptually, we decide on the instance variables for a class early in the process. In this example, and in the examples in Wu, they are placed at the beginning of the class. However, a case can also be made for listing the instance variables near the end of the class declaration. Either place is acceptable (but not strung out everywhere!) 6. Identify the instance methods (mutators and accessors) a) In general, each thing an object needs to be able to do will be represented by an instance method. NOTE in handout b) Because defining methods is so important, we ll come back to a much more detailed coverage of this task later in this lecture. 7. Determine whether any class variables, constants, or methods are needed. a) A variable that holds a value that is shared by all the objects in the class can be declared as a class variable. NOTE in handout: two class variables. One is used to ensure that each account gets a unique number. The constructor uses this to assign a number to a newly-created account, and then adds one to it. Obviously, this must be shared by all instances of the class. Another is used to hold a master list of open accounts. b) A constant value that is the same for all objects in the class, and that either methods or clients of the class need to make use of can be declared as a class constant. NOTE in handout: many banks have a minimum balance allowed in account for paying interest. (An account with a balance below this amount is not eligible for interest.) Since this rule applies to all accounts, a class constant MINIMUM_BALANCE_FOR_INTEREST is used. (Note naming convention) 5
6 c) A method that represents a responsibility of the entire class, not associated with any particular object in the class, can be declared as a class method. NOTE in handout: payinterest() method. d) Note that instance methods can freely make use of class constants, variables, and methods; but that class methods cannot use instance variables or methods since they are not associated with any particular instance of the class. e) Class variables, constants, and methods are distinguished from instance variables and methods by being declared static. III. Defining Methods Within A Class A. When we are defining a class, most of our time will be spent defining its methods. B. In general, a method definition consists of a method header and a method body. In addition, a method should have a method header comment that describes what it does and how to use it. 1. Recall that a method is invoked when some object sends a message to our object. The method header specifies what form that message must take. It consists of optional modifiers, a return type, the method name, and a (possibly empty) parenthesized list of formal parameters. a) The possible modifiers are (1) The visibility modifiers (public, protected, private) - discussed below These control who may make use of this method. (2) The word static, to indicate that the method is a class method. (Otherwise, it is taken to be an instance method). If the method is an instance method, the message is sent to a particular object; if it is a class method, it is sent to the class as a whole. (3) The world final, to indicate that the method may not be overridden in any subclass. b) The return type is either the name of a primitive type, or the name of an object type, or the word void. It specifies what information, if any, is returned to the sender of the message when the method has completed its work. 6
7 c) The method name should clearly and simply state what the method does. Good method names have the property that the method does exactly what its name says it does - no more and no less. Good method names generally consist of an imperative verb, perhaps with an object - e.g. deposit() reportbalance() d) The formal parameters specify the information the sender of the message that invokes the method must supply. (1) In the simplest case, there are no parameters, and the parameter list consists of an empty pair of parentheses (). (2) In many cases, the parameter list will consist of one or more items of information. Each element in the parameter list has two components: a type and a name. (a) The type specifies what kind of information will be supplied - e.g. an integer value (int) or a reference to an object or what have you. Any type that is legal for a variable is legal for a parameter - in fact, within the method the parameters behave like variables. (b) The name indicates what the parameter is used for - i.e. what it means. Again, the name should be chosen carefully so that anyone using the method can know clearly what information is expected. EXAMPLE: The deposit method of class BankAccount needs a numeric parameter to specify the amount of the deposit (3) Note an important distinction in function between the parameters and the return type: (a) Parameters represented information flowing in to the method from the caller. (b) The return type represents information flowing out of the method back to the caller. EXAMPLE: A BankAccount method named reportbalance() would return a number 7
8 (c) In Java, it is possible to have any number of items flowing in to a method, but only one item coming back. While other programming languages provide a way for some of the parameters to represent information flowing back out of the method, this is not the case in Java. (However, if one of the parameters is a reference to an object, the method can potentially alter the object that is referred to.) 2. The method body contains the code that is executed when the method is called. It may contain a mixture of variable declarations and executable statements. a) When a variable is declared in a method, it is called a local variable. A local variable comes into existence when its declaration is encountered during the execution of the method, and ceases to exist when the method is finished. This is distinguished from the instance variables of an object, which exist the entire time that the object exists. Example: TRANSPARENCY - Wu figure 4.3 b) Actually, a formal parameter can be thought of as a special kind of local variable - one that is declared in the method header and initialized with a value when the method is called. TRANSPARENCY - Wu figure 4.4 c) We have seen a number of different kinds of executable statements that can occur within a method body - e.g. (1) Assignment statements (2) if statements (3) loop (for, while) statements (4) return statements. A return statement serves two purposes: (a) It always immediately ends the execution of the method - even if it occurs in the middle. (b) If the method is defined as returning a value, the return statement specifies what value is to be returned. 3. A method comment should go before each method. This comment should include: 8
9 a) A brief, clear description of the what the method does b) An explanation of each parameter (what its role is) c) An explanation of the return value - if any The handout shows the standard form for method comments NOTE on handout - method comments for deposit(), withdraw(), reportbalance() tag d) When a method is used, the caller must specify: 4. The object or class to which it is being applied (depending on whether it is an instance or class method) 5. The name of the method 6. A list of actual parameters. These must correspond to the formal parameters in the method header in number, order, and type. EXAMPLE: The constructors we used for our robots required four integer parameters: the starting street, the starting avenue, its initial direction, and its initial number of beepers. When we used new to construct a new robot, we had to specify these values in exactly that order. C. Instance methods fall into one of several broad categories: 1. A constructor method is used when creating an object. a) Wu calls the constructor a class method, because it is used when the new message is sent to the class. However, it partakes of some of the nature of an instance method because, at the time it is called, the new object has already been brought into existence, and hence its instance variables and methods are available to the constructor. (True class methods do not have access to any instance variables and methods of any particular object ). b) The name of a constructor is always exactly the same as the name of the class - that is how the compiler recognizes a method as a constructor. 9
10 c) The purpose of the constructor is to put the object into a consistent initial state. When a new object is created by sending a new message to a class, the class always calls the constructor of the newly created object. d) The parameters of a constructor specify things which the object must know from the very outset of its existence. EXAMPLE: When we defined a new class of robot, we always defined a constructor method whose parameters specified its initial location, orientation, and number of beepers. e) Note that while a constructor may have parameters, it never has a return value. Also, a constructor is never called directly - it is always called as a result of sending the new message to the class, requesting that a new object be created. f) It is possible to define more than one constructor for a class, taking a different number or types of parameters. The names of the constructors are always the same, but the compiler can distinguish between them based on the types of their parameters. EXAMPLE: If we were frequently in the practice of starting robots off at 1st st and 1st ave facing East, we could define a constructor for our robot subclasses that took only one parameter - the number of beepers - in addition to the constructors we defined. NOTE: In distinguishing between two or more methods having the same name, the compiler considers only the number and types of the parameters - not their names. g) If no constructor is defined for a given class, the compiler automatically creates a default constructor that takes no parameters. However, if any constructor is defined for the class, no default constructor is automatically created - if one is needed, you must write it yourself. 2. An accessor method is used to obtain information from the object, without altering the object itself. a) Accessors may or may not have parameters going in, but always have a return value. b) Accessors never alter any instance variables of the object they are applied to. (In some programming languages, there is a way to enforce this - but not in Java) 10
11 EXAMPLE: What were some of the accessor methods for our robots? ASK boolean nexttoabeeper() boolean nexttoarobot() boolean frontisclear() boolean facingnorth() etc. We could also imagine a more sophisticated robot class that might have accessors like int numberofbeepersinbeeperbag() int currentstreet() etc. c) As was the case with constructors, it is possible to have two accessors with the same name, but different parameter types. This, however, is not common. 3. A mutator method is used to tell the object to alter itself in some way. a) Mutators frequently have parameters going in, but usually do not have return values. (If a mutator does have a return value, it is usually some boolean that indicates whether or not the operation was successful.) EXAMPLE: What were some mutators of our robot classes? ASK turnleft() move() pickbeeper() etc. As was the case with constructors, it is possible to have two mutators with the same name, but different parameter types. For example, we might have had two move() methods in one of our robot classes - one with no parameters that moves the robot one block, and one with an int parameter that specifies the number of blocks to move, so that we might be able to say something like: karel.move(3); 11
12 IV. Visibility of Class Members A. Each class, and each member in a class (instance or class variable, instance or class method) has associated with it a visibility that controls whether it can be accessed by the methods of other classes. B. Java provides four levels of visibility - listed in order from greatest access allowed to least access allowed 1. public - any method of any class can access this item 2. protected - we will discuss this later in the course 3. default - any method of any other class in the same package can access this item 4. private - only methods of this class can access this item All four of these may be applied to class members. Ordinary classes themselves can only have public or default visibility - the other two would not be meaningful for ordinary classes and therefore are not allowed. (There is a case in which private or protected can be applied to certain kinds of classes, but that s an advanced feature of the language we are not likely to get to in this course.) C. The visibility of a class or member is specified by preceding its declaration with a keyword: 1. The keywords public, protected, and private specify the corresponding level of visibility. 2. The omission of a visibility keyword produces default visibility. D. In general, the following guidelines are helpful in deciding what visibility to assign to each class member: 1. Instance variables should almost always be private. (Maybe the correct word is just always!). a) If it is necessary for other classes to have direct access to an instance variable, this can be provided by defining appropriate accessor and or mutator methods. Such methods typically have names like getwhatever() or setwhatever(newvalue) - and for this reason are sometimes called getter and setter methods. 12
13 This, however, should be done with caution - often the class is better to have methods that perform the specific operations required, rather than allowing some other class to access the instance field and then perform the operation. b) This represents a key notion called encapsulation or information hiding - a class should encapsulate (or hide) the details of its implementation to help make a system modular and more easily modifiable. EXAMPLE: If we had a class representing bank accounts that had a publicly-accessible balance field, we could do deposits and withdrawals by simply modifying this field directly. By making it private - and thus forcing other classes to use mutator methods like deposit() and withdraw() - we make it possible to enforce rules like the rule that a balance cannot go below 0 - or to implement features like overdraft protection if it does. 2. Instance methods are frequently public, since they provide the behaviors that the class is responsible for providing. The exception would be instance methods that serve as helpers to other methods and are not used directly. 3. Class variables and methods follow similar rules to those given for instance variables and methods. 4. Class constants are often made public. No encapsulation issue arises, since they cannot be modified. However, if the constant is used only in the implementation of the methods of the class, it should be private. 5. The other two levels of protection for class members will be discussed later. E. The following guidelines are helpful in deciding what visibility to apply to a class. 1. The main class in an application, or the Applet subclass in an applet, must be public. (It is, after all, the entry point into the whole system.) (Actually, this is not strictly necessary in the case of main/applet classes that are not part of some package, but it is good practice to make the class explicitly public.) 2. Classes that provide service to lots of other classes should be public. 13
14 3. A class that serves to provide service only to one or a few other classes might have default protection if it resides in the same package as the class(es) it helps. V. Summary One important note: Java requires that a public class be defined in a file whose name is the same as that of the class. That, of course, implies that only one public class can be defined in any one file. Go over online documentation for class java.awt.color, noting class constants; identifying methods as constructors, accessors, and mutators and discussing parameters. Note that this documentation was generated automatically (by a program called javadoc) from the source code for the class. The special class and method comments we have discussed (beginning with /**), plus the special are recognized by javadoc - hence, this style of comment is called a javadoc comment. 14
CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class
CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount
More informationCPS122 Lecture: Defining a Class
Objectives: CPS122 Lecture: Defining a Class last revised January 14, 2016 1. To introduce structure of a Java class 2. To introduce the different kinds of Java variables (instance, class, parameter, local)
More informationCPS122 Lecture: Introduction to Java
CPS122 Lecture: Introduction to Java last revised 10/5/10 Objectives: 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3. To introduce
More informationCS112 Lecture: Working with Numbers
CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation Outline Problem: Create, read in and print out four sets of student grades Setting up the problem Breaking
More informationCPS122 Lecture: Detailed Design and Implementation
CPS122 Lecture: Detailed Design and Implementation Objectives: Last revised March 3, 2017 1. To introduce the use of a complete UML class box to document the name, attributes, and methods of a class 2.
More information1. BlueJ bank example with subclasses of BankAccount 2. Transparency of UML diagram for BankAccount class hierarchy
CS112 Lecture: Fundamental Concepts of Object-Oriented Software Development Last revised 1/13/04 Objectives: 1. To review/introduce key concepts of object-orientation: object, class, data members (class
More informationCS112 Lecture: Extending Classes and Defining Methods
Objectives: CS112 Lecture: Extending Classes and Defining Methods Last revised 1/9/04 1. To introduce the idea of extending existing classes to add new methods 2. To introduce overriding of inherited methods
More informationObjectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class
CS112 Lecture: Course Introduction Last revised 1/3/06 Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class Materials: 1. Questionnaire 2. Syllabi
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
More informationCPS122 Lecture: Course Intro; Introduction to Object-Orientation
Objectives: CPS122 Lecture: Course Intro; Introduction to Object-Orientation 1. To introduce the course requirements and procedures. 2. To introduce fundamental concepts of OO: object, class Materials:
More informationSTUDENT LESSON A5 Designing and Using Classes
STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of
More informationCS 251 Intermediate Programming Methods and Classes
CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationCPS122 Lecture: Detailed Design and Implementation
CPS122 Lecture: Detailed Design and Implementation Objectives: Last revised March 12, 2012 1. To introduce the use of a complete UML class box to document the name, attributes, and methods of a class 2.
More informationCPS122 Lecture: From Python to Java last revised January 4, Objectives:
Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.
More informationEncapsulation. Mason Vail Boise State University Computer Science
Encapsulation Mason Vail Boise State University Computer Science Pillars of Object-Oriented Programming Encapsulation Inheritance Polymorphism Abstraction (sometimes) Object Identity Data (variables) make
More informationCS 251 Intermediate Programming Methods and More
CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationDefining Classes and Methods
Defining Classes and Methods Chapter 4 Chapter 4 1 Basic Terminology Objects can represent almost anything. A class defines a kind of object. It specifies the kinds of data an object of the class can have.
More informationCS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output
CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output Last revised January 12, 2006 Objectives: 1. To introduce arithmetic operators and expressions 2. To introduce variables
More informationCOMP 250 Winter 2011 Reading: Java background January 5, 2011
Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP
More informationJava How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;
More informationCS112 Lecture: Introduction to Karel J. Robot
CS112 Lecture: Introduction to Karel J. Robot Last revised 1/17/08 Objectives: 1. To introduce Karel J. Robot as an example of an object-oriented system. 2. To explain the mechanics of writing simple Karel
More informationCPS122 Lecture: From Python to Java
Objectives: CPS122 Lecture: From Python to Java last revised January 7, 2013 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.
More informationClient Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module
Basic Class Design Goal of OOP: Reduce complexity of software development by keeping details, and especially changes to details, from spreading throughout the entire program. Actually, the same goal as
More informationStoring Data in Objects
Storing Data in Objects Rob Miles Department of Computer Science 28d 08120 Programming 2 Objects and Items I have said for some time that you use objects to represent things in your problem Objects equate
More informationAnatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation
Anatomy of a Method September 15, 2006 HW3 is due Today ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Midterm 1 Next Tuesday Sep 19 @ 6:30 7:45pm. Location:
More informationCS112 Lecture: Primitive Types, Operators, Strings
CS112 Lecture: Primitive Types, Operators, Strings Last revised 1/24/06 Objectives: 1. To explain the fundamental distinction between primitive types and reference types, and to introduce the Java primitive
More informationCOMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation
COMP-202 Unit 8: Defining Your Own Classes CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation Defining Our Own Classes (1) So far, we have been creating
More informationCS112 Lecture: Repetition Statements
CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general
More informationSoftware Design and Analysis for Engineers
Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:
More informationCS1004: Intro to CS in Java, Spring 2005
CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a
More informationWeiss Chapter 1 terminology (parenthesized numbers are page numbers)
Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement
More informationDefensive Programming
Defensive Programming Software Engineering CITS1220 Based on the Java1200 Lecture notes by Gordon Royle Lecture Outline Why program defensively? Encapsulation Access Restrictions Documentation Unchecked
More informationAnatomy of a Class Encapsulation Anatomy of a Method
Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and
More informationAP Computer Science Chapter 10 Implementing and Using Classes Study Guide
AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within
More informationIntroduction Welcome! Before you start Course Assessments The course at a glance How to pass M257
Introduction Unit 1: Java Everywhere Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 Introduction Welcome! Before you start Course Assessments The course at a glance How to pass M257 1. Java background 2.
More informationCSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017
This week the main activity was a quiz activity, with a structure similar to our Friday lecture activities. The retrospective for the quiz is in Quiz-07- retrospective.pdf This retrospective explores the
More informationChapter 6 Introduction to Defining Classes
Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of
More informationHandout 7. Defining Classes part 1. Instance variables and instance methods.
Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from
More informationDefining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5
Defining Classes and Methods Chapter 5 Objectives Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters in a method Use modifiers public,
More informationMethods. Methods. Mysteries Revealed
Methods Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private A method (a.k.a. func2on, procedure, rou2ne) is a piece of code
More informationCMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012
CMPSCI 187: Programming With Data Structures Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012 The StringLog ADT Data Abstraction Three Views of Data Java Interfaces Defining the StringLog
More informationObject Oriented Programming
Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented
More informationLesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014
Lesson 10B Class Design By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Encapsulation Inheritance and Composition is a vs has a Polymorphism Information Hiding Public
More informationUnit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW
Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 1. Introduction 2. Objects and classes 3. Information hiding 4. Constructors 5. Some examples of Java classes 6. Inheritance revisited 7. The class hierarchy
More informationCSE 142 Su 04 Computer Programming 1 - Java. Objects
Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request
More informationSoftware Development 2
Software Development 2 Course Map This module introduces some of the techniques programmers use to create applications and programs. Introduction Computer Principles and Components Software Development
More information5. Defining Classes and Methods
5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! Describe and define concepts of class and object! Describe use
More informationImplementing Classes
Implementing Classes Advanced Programming ICOM 4015 Lecture 3 Reading: Java Concepts Chapter 3 Fall 2006 Slides adapted from Java Concepts companion slides 1 Chapter Goals To become familiar with the process
More informationObjectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09
Objectives Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management
More informationCmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects
CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with
More informationClasses. Classes as Code Libraries. Classes as Data Structures
Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They
More informationCOMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading
COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables
More informationObjects and Classes -- Introduction
Objects and Classes -- Introduction Now that some low-level programming concepts have been established, we can examine objects in more detail Chapter 4 focuses on: the concept of objects the use of classes
More informationChapter 4: Writing Classes
Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own
More informationEECS168 Exam 3 Review
EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any
More informationObject-Oriented Programming Concepts
Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are
More information1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE
1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE LECTURE-1 Syllabus Introduction 1.1 Introduction to Object Oriented 1.2 Introduction to UML 1.3 Software Process and OOA&D 1.4 Component and CBSD 1.5 Patterns
More informationBasic Object-Oriented Concepts. 5-Oct-17
Basic Object-Oriented Concepts 5-Oct-17 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions, which could manipulate any data An object contains
More informationThe development of a CreditCard class
The development of a CreditCard class (an introduction to object-oriented design in C++) January 20, 2006 Rather than explain the many aspects involved in a fully-fledged, yet complex example, we will
More informationObject-Oriented Programming Paradigm
Object-Oriented Programming Paradigm Sample Courseware Object-Oriented Programming Paradigm Object-oriented programming approach allows programmers to write computer programs by representing elements of
More informationDefining Classes and Methods
Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe concepts of class, class
More informationThe name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.
Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that
More informationTOPIC 2 INTRODUCTION TO JAVA AND DR JAVA
1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared
More informationAgenda: Notes on Chapter 3. Create a class with constructors and methods.
Bell Work 9/19/16: How would you call the default constructor for a class called BankAccount? Agenda: Notes on Chapter 3. Create a class with constructors and methods. Objectives: To become familiar with
More informationCS112 Lecture: Inheritance and Polymorphism
CS112 Lecture: Inheritance and Polymorphism Last revised 4/10/08 Objectives: 1. To review the basic concept of inheritance 2. To introduce Polymorphism. 3. To introduce the notions of abstract methods,
More informationDefining Classes and Methods
Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)
More informationUNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED
UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance
More informationJAVA: A Primer. By: Amrita Rajagopal
JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently
More informationCpt S 122 Data Structures. Introduction to C++ Part II
Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function
More informationAPCS Semester #1 Final Exam Practice Problems
Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester
More informationUNDERSTANDING CLASS DEFINITIONS CITS1001
UNDERSTANDING CLASS DEFINITIONS CITS1001 Main concepts to be covered Fields / Attributes Constructors Methods Parameters Source ppts: Objects First with Java - A Practical Introduction using BlueJ, David
More informationACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1
ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1 Due to CMS by Tuesday, February 14. Social networking has caused a return of the dot-com madness. You want in on the easy money, so you have decided to make
More informationAssoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down
More information5. Defining Classes and Methods
5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe and define concepts of class, class object Describe use
More informationImplementing Classes (P1 2006/2007)
Implementing Classes (P1 2006/2007) Fernando Brito e Abreu (fba@di.fct.unl.pt) Universidade Nova de Lisboa (http://www.unl.pt) QUASAR Research Group (http://ctp.di.fct.unl.pt/quasar) Chapter Goals To become
More informationCSCE 156 Computer Science II
CSCE 156 Computer Science II Lab 04 - Classes & Constructors Dr. Chris Bourke Prior to Lab 1. Review this laboratory handout prior to lab. 2. Read Object Creation tutorial: http://download.oracle.com/javase/tutorial/java/javaoo/objectcreation.
More informationContainer Vs. Definition Classes. Container Class
Overview Abstraction Defining new classes Instance variables Constructors Defining methods and passing parameters Method/constructor overloading Encapsulation Visibility modifiers Static members 14 November
More informationClasses and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון
Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented
More informationGraphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java
More informationBuilding custom components IAT351
Building custom components IAT351 Week 1 Lecture 1 9.05.2012 Lyn Bartram lyn@sfu.ca Today Review assignment issues New submission method Object oriented design How to extend Java and how to scope Final
More informationLecture 7: Data Abstractions
Lecture 7: Data Abstractions Abstract Data Types Data Abstractions How to define them Implementation issues Abstraction functions and invariants Adequacy (and some requirements analysis) Towards Object
More informationLesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014
Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class
More informationData Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.
Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn
More informationChapter 4. Defining Classes I
Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes
More informationIntroduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview
Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple
More informationDefining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining
Defining Classes Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining Classes, fields, and constructors Methods and parameters, mutators and accessors Assignment
More informationClasses and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות
Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static,
More informationLecture 02, Fall 2018 Friday September 7
Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7 Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python
More information12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.
Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.
More informationClasses and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון
Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented
More informationExpress Yourself. Writing Your Own Classes
Java Programming 1 Lecture 5 Defining Classes Creating your Own Classes Express Yourself Use OpenOffice Writer to create a new document Save the file as LastFirst_ic05 Replace LastFirst with your actual
More informationClasses. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)
Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They
More informationHigh Performance Computing Prof. Matthew Jacob Department of Computer Science and Automation Indian Institute of Science, Bangalore
High Performance Computing Prof. Matthew Jacob Department of Computer Science and Automation Indian Institute of Science, Bangalore Module No # 09 Lecture No # 40 This is lecture forty of the course on
More informationJava. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)
Summary: Chapters 1 to 10 Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email: sharma@cse.uta.edu
More information3 ADT Implementation in Java
Object-Oriented Design Lecture 3 CS 3500 Spring 2010 (Pucella) Tuesday, Jan 19, 2010 3 ADT Implementation in Java Last time, we defined an ADT via a signature and a specification. We noted that the job
More informationEncapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects
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
More informationReview sheet for Final Exam (List of objectives for this course)
Review sheet for Final Exam (List of objectives for this course) Please be sure to see other review sheets for this semester Please be sure to review tests from this semester Week 1 Introduction Chapter
More informationCS112 Lecture: Making Choices
CS112 Lecture: Making Choices Objectives: Last revised 1/19/06 1. To review the Java if and if... statements 2. To introduce relational expressions and boolean operators 3. To discuss nested if statements
More information