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

Similar documents
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

Designing Classes part 2

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

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

Anatomy of a Class Encapsulation Anatomy of a Method

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

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

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

Chapter 4: Writing Classes

ECE 122. Engineering Problem Solving with Java

Java Basic Introduction, Classes and Objects SEEM

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

Java Basic Introduction, Classes and Objects SEEM

Chapter 4 Writing Classes

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

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

writing classes objectives chapter

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

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

Assignment 1 due Monday at 11:59pm

CS1004: Intro to CS in Java, Spring 2005

ECE 122. Engineering Problem Solving with Java

CSC 1051 Data Structures and Algorithms I

Using Classes and Objects

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

Selection Statements and operators

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

Selection Statements and operators

Writing Classes Chapter 5. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Chapter 5: Writing Classes and Enums

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

Algorithms and Conditionals

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

Introduction to Arrays

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

Objects and Classes -- Introduction

Classes and Methods: Classes

Using Classes and Objects

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

Encapsulation. Mason Vail Boise State University Computer Science

COMP 202. Building Your Own Classes. CONTENTS: Anatomy of a class Constructors and Methods (parameter passing) COMP 202 Objects 2 1

Selection and Repetition Revisited

CSC 1051 Data Structures and Algorithms I

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Java I/O and Control Structures Algorithms in everyday life

Selection and Repetition

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Using Classes and Objects

Java I/O and Control Structures

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

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

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

CSC 1051 Data Structures and Algorithms I

Selection and Repetition Revisited

COMP 202. Building Your Own Classes. CONTENTS: Anatomy of a class Constructors and Methods (parameter passing) Instance Data. COMP Objects 2 1

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

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

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

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

Designing Classes Part 2

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

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Building Your Own Classes

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

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

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

CS1083 Week 2: Arrays, ArrayList

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

Chapter 4 Defining Classes I

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Selection and Repetition

Review. these are the instance variables. these are parameters to the methods

Arrays, Part 3: Multidimensional arrays

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Review for Midterm. Instructor: Scott Kristjanson CMPT 135 SFU Surrey, Spring 2016

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Object-Oriented Design. Chapter. Class Relationships. Classes in a software system can have various types of relationships to each other

Software and Programming 1

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

2: Basics of Java Programming

Assignment 1 is due tonight at 11:59pm. Assignment 2 is due next Thursday at 11:59pm

Midterms Save the Dates!

Chapter 5: Classes and Objects in Depth. Introduction to methods

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

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

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Introduction to Classes and Objects

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

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

Transcription:

Designing Classes Where do objects come from? CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects come from? Good question! Example: Account datatype - represents a generic bank account We will learn how to create a class that defines a new datatype, i.e., a new type of objects We need to learn: 1. What is the structure of a class definition? 2. How to specify what happens when an object is instantiated (i.e., when the new operator is used)? 3. How do we define the methods that can be invoked through objects of this class? acct2 69713 balance 40.00 "Jane Smith" Dr. Papalaskari 1

Data Methods 1. Structure of class definition Account class int ; String ; Account object balance 72354 102.56 getbalance() The object: is like the house built from the tostring() blueprint is an instance of the class The class is the blueprint has its own data space & shares Classes define DATA and METHODS methods defined for this datatype i.e., a datatype 2. Object instantiation Creating Objects old example: We have already seen something like this: Scanner scan = new Scanner (System.in); This invokes the Scanner constructor, which is a special method that sets up the object 2. Object instantiation 2. Object instantiation Creating Objects our newly defined Account class: Creating Objects our newly defined Account class: Account acct1 = new Account (, 72354, 102.56); Account acct1 = new Account (, 72354, 102.56); int ; Invokes the Account constructor, which is a special method that sets up the object A new Account object is created! String ; acct1 72354 getbalance() tostring() Dr. Papalaskari 2

3. Method invocation As we have seen, once an object has been created, we can use the dot operator to invoke its methods: ans = scan.nextline(); numchars = title.length(); amount = acct1.getbalance(); Chapter 4: Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. class definitions constructors instance data method declaration and parameter passing encapsulation and Java modifiers more about creating graphical objects (next week) balance 128.41 Datatype / Client (also referred to as slave / driver classes) // Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example) int ; // Demonstrates the creation and use of multiple Account objects. String ; public class Transactions // ******************************************************************** // Account.java Author: Lewis/Loftus // Creates some bank accounts and requests various services. // Simplified code by MA Papalaskari // Represents a bank account with methods deposit public and withdraw. static void main (String[] args) // ******************************************************************** Account acct1 = new Account (, 72354, 102.56); import java.text.numberformat; Account acct2 = new Account ("Jane Smith", 69713, 40.00); getbalance() Account acct3 = new Account ("Edward Demsey", 93757, 759.32); public class Account System.out.println (acct1); int ; tostring() System.out.println (acct2); System.out.println (acct3); String ; Client ------------ acct1.withdraw (60, 2.50); -- // Sets up the account Account by defining its owner's, System.out.println account (); // number, and initial balance. System.out.println (acct1); ------------ System.out.println (acct2); -- System.out.println (acct3); public Account (String x, Datatype int y, double z) = x; = y; balance = z; Transactions // Account.java Author: Lewis/Loftus // Simplified code by MA Papalaskari // Represents a bank account with methods deposit and withdraw. import java.text.numberformat; public class Account int ; String ; // Sets up the account by defining its owner's, account int ; // number, and initial balance. public Account (String x, int y, double z) String ; = x; = y; balance = z; getbalance() // Deposits the specified amount x into the account. tostring() public void deposit (double x) balance = balance + x; continue Dr. Papalaskari 3

continue // Withdraws the specified amount from the account and applies // the fee. public void withdraw (double x, double fee) balance = balance - x - fee; // Returns the current balance of the account. int ; public double getbalance () return balance; String ; // Returns a one-line description of the account as a string. public String tostring () getbalance() NumberFormat fmt = NumberFormat.getCurrencyInstance(); tostring() return ( + "\t" + + "\t" + fmt.format(balance)); // Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example) // Demonstrates the creation and use of multiple Account objects. public class Transactions // Creates some bank accounts and requests various services. public static void main (String[] args) Account acct1 = new Account (, 72354, 102.56); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, 759.32); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); // Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example) // Demonstrates the creation and use of multiple Account objects. public class Transactions // Creates some bank accounts and requests various services. public static void main (String[] args) Account acct1 = new Account (, 72354, 102.56); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, 759.32); Transactions class: Creating Account objects Transactions class Account acct1 = new Account (, 72354, 102.56); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); Sample Run acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); 72354 Ted Murphy $102.56 69713 Jane Smith $40.00 93757 Edward Demsey $759.32 72354 Ted Murphy $65.91 69713 Jane Smith $40.00 93757 Edward Demsey $759.32 Dr. Papalaskari 4

Transactions class: Creating Account objects Transactions class Account acct1 = new Account (, 72354, 102.56); Transactions class: Creating Account objects Transactions class Account acct1 = new Account (, 72354, 102.56); int ; String ; getbalance() Account class public Account (String x, int y, double z) = x; method = y; balance = z; int ; String ; getbalance() Account class public Account (String x, int y, double z) = x; method = y; balance = z; tostring() tostring() Transactions class: Creating more Account objects Transactions class Account acct1 = new Account (, 72354, 102.56); Account class: Using methods Transactions class acct2 69713 balance 40.00 Account acct2 = new Account ("Jane Smith", 69713, 40.00); " "Jane Smith" Dr. Papalaskari 5

Account class: Using methods Account class: Using methods // Deposits the specified amount into the account. public void deposit (double x) balance = balance + x; // Deposits the specified amount into the account. public void deposit (double x) balance = balance + x; Account class: Using methods Account class: Using methods // Deposits the specified amount into the account. public void deposit (double x) balance = balance + x; // Deposits the specified amount into the account. public void deposit (double x) balance = balance + x; balance 128.41 Dr. Papalaskari 6

Account class: Another Example acct1.withdraw (60, 2.50); Account class: Another Example acct1.withdraw (60, 2.50); //------------------------------------------------ // Withdraws the specified amount from the // account and applies the fee. //------------------------------------------------ public void withdraw (double x, double fee) balance = balance - x - fee; balance 128.41 balance 128.41 Account class: Another Example acct1.withdraw (60, 2.50); Class definitions A class can contain data declarations and method declarations //------------------------------------------------- // Withdraws the specified amount from the account // and applies the fee. //----------------------------------------------- public void withdraw (double x, double fee) balance = balance - x - fee; int ; String ; Data declarations (also called fields) balance 65.91 getbalance() Method declarations (note: the constructor is also a method) tostring() Dr. Papalaskari 7

tostring() method can be omitted! getbalance () method System.out.println(acct1.toString()); double amount = acct1.getbalance(); // Note: this code is not used in Transactions.java public String tostring () NumberFormat fmt = NumberFormat.getCurrencyInstance(); return ( +"\t + +"\t + fmt.format(balance)) public double getbalance () return balance; acct1 72354 acct1 72354 Bank Account Example There are some improvements that can be made to the Account class The design of some methods could also be more robust, such as verifying that the amount parameter to the method is positive Examples of datatypes (Classes) Some of these improvements are in the book examples Account.java, Transactions.java (simplified versions) Account.java, Transactions.java (book versions) Dr. Papalaskari 8

Another Example: RollingDice.java // RollingDice.java Author: Lewis/Loftus // Demonstrates the creation and use of a user-defined class. public class RollingDice // Creates two Die objects and rolls them several times. public static void main (String[] args) Die die1, die2; int sum; die1 = new Die(); die2 = new Die(); die1.roll(); die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); die1.roll(); die2.setfacevalue(4); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); sum = die1.getfacevalue() + die2.getfacevalue(); System.out.println ("Sum: " + sum); Sample Run Die One: 5, Die Two: 2 Die One: 1, Die Two: 4 Sum: 5 Die One: 4, Die Two: 2 New sum: 6 sum = die1.roll() + die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); System.out.println ("New sum: " + sum); // Die.java Author: Lewis/Loftus // // Represents one die (singular of dice) with faces showing values // between 1 and 6. public class Die private final int MAX = 6; // maximum face value private int facevalue; // current value showing on the die // : Sets the initial face value. public Die() facevalue = 1; // Rolls the die and returns the result. public int roll() facevalue = (int)(math.random() * MAX) + 1; return facevalue; continue continue // Face value mutator. public void setfacevalue (int value) facevalue = value; // Face value accessor. public int getfacevalue() return facevalue; // Returns a string representation of this die. public String tostring() String result = Integer.toString(faceValue); Next: Focus on Method definition parameters return type return statement char ch = obj.calc (start, 2, "ABCDE"); char calc (int num1, int num2, String message) int sum = num1 + num2; char result = message.charat (sum); return result; return result; Dr. Papalaskari 9

Method Control Flow If the called method is in the same class, only the method is needed Method Control Flow The called method is often part of another class or object main doit helpme compute mymethod obj.doit(); helpme(); mymethod(); Thus the dot operator is an addressing mechanism. Note that it can also be used to access an object s or class s data directly, for example acct1. Color.black more on this later (encapsulation) Invoking methods within the same class An object s method may access any of its other methods directly. Eg: public void addinterest(double rate) deposit (rate*balance); Invoking methods within the same class The main method can do this too! public static void main (String[] args) printmanygreetings(5); assumes printmanygreetings() is defined in the same class è A convenient way to test methods Dr. Papalaskari 10

Invoking methods within the same class Implementing printmanygreetings() method: The main method can do this too! public static void main (String[] args) printmanygreetings(5); assumes printmanygreetings() is defined in the same class NOTE: Since main() is static, printmanygreetings() must also be static Invoking the method: printmanygreetings(5); Method Control Flow revisited Recall, if the called method is in the same class, only the method is needed More Method Examples: Write a method with two double parameters a and b that computes and returns the sum of squares of its two parameters (i.e., a 2 + b 2 ). main printmanygreetings printmanygreetings(5); How do we invoke the method to compute & print: (14.8) 2 + (37.65) 2? Dr. Papalaskari 11

More Method Examples: Write a method with one int parameter num, that returns a String composed of Happy Birthday num times How do we invoke the method to print happy birthday 4 times? Getting to know classes so far Using predefined classes from the Java API. Defining classes for our own datatypes. datatypes: Clients (Driver classes): Account Transactions, OnePercent Die RollingDice Shoe YouVeGotShoes (Project) Person PeopleBeingPeople (Lab) Next: Focus on method definition/invocation UML Class Diagrams UML class diagram for Transactions program? UML = Unified Modelling Language Example: A UML class diagram for the RollingDice program: RollingDice main (args : String[]) : void facevalue : int Die roll() : int setfacevalue (value : int) : void getfacevalue() : int tostring() : String Dr. Papalaskari 12

Encapsulation 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 which in turn manage the instance data Violating Encapsulation - WRONG It is possible for a class to access the instance data of another class directly but it s not a good idea! See Account.java See ImInUrClassMessingUrInstanceData.java Account.java Client Methods ImInUrClassMessingUrInstanceData.java acct1. = Joe ; addinterest() Violating Encapsulation It is possible for a class to access the instance data of another class directly Data balance Use Accessors & Mutators - RIGHT Indirect access through methods accessors and mutators ( getters and setters ) Usually d getx() and setx() Transactions.java Example int x1 = acct1.getbalance(); Account.java addinterest() Methods balance Data Visibility Modifiers In Java, we enforce encapsulation through the appropriate use of visibility modifiers: public can be referenced from other classes private can be referenced only within that class: protected involves inheritance (discussed later) Data 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 Dr. Papalaskari 13

Violating Encapsulation experiment Revisit your solution for the Account Class Exercise Add some code to the OnePercent.java class to modify the value of an instance variable, eg: acct1. = Joe ; This should work as expected Now modify Account.java insert the modifier private in front of that variable declaration: private String ; Re-compile the Account class and run your program again. Note the error you get. public constants are ok Example: The Account class can have a constant for the interest rate: public final double RATE = 0.015; A client (eg, OnePercent.java) can access this constant directly: System.out.print ("Interest rate = " + acct1.rate); public constants are ok Visibility Modifiers the RULES Example: The Account class can have a constant for the interest rate: Usually, constants are declared static public final static double RATE = 0.015; A driver class (eg, OnePercent.java) can access this constant directly without creating an object: Variables Methods public NO (but OK for public constants) Yes private Yes Yes, for support methods only System.out.print ("Interest rate = " + acct1.rate); System.out.print ("Interest rate = " + Account.RATE); Dr. Papalaskari 14