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

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

Where do objects come from? Good question!

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

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

ECE 122. Engineering Problem Solving with Java

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

Chapter 4: Writing Classes

Java Basic Introduction, Classes and Objects SEEM

Java Basic Introduction, Classes and Objects SEEM

Chapter 4 Writing Classes

writing classes objectives chapter

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

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

ECE 122. Engineering Problem Solving with Java

CS1004: Intro to CS in Java, Spring 2005

Packages & Random and Math Classes

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

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

CSC 1051 Data Structures and Algorithms I

Encapsulation. Mason Vail Boise State University Computer Science

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

Classes and Methods: Classes

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

Chapter 5: Writing Classes and Enums

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

Selection Statements and operators

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

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

Objects and Classes -- Introduction

Introduction to Arrays

Algorithms and Conditionals

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

Using Classes and Objects

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

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

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

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

CSC 1051 Data Structures and Algorithms I

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

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

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

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

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

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

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

Using Classes and Objects

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

Designing Classes Part 2

Selection and Repetition

Building Your Own Classes

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

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

CSC 1051 Data Structures and Algorithms I

Java I/O and Control Structures Algorithms in everyday life

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

Chapter 4 Defining Classes I

ECE 122. Engineering Problem Solving with Java

Java I/O and Control Structures

Selection and Repetition Revisited

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Software Design and Analysis for Engineers

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

Arrays, Part 3: Multidimensional arrays

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

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

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

CS1083 Week 2: Arrays, ArrayList

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

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

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

Programming II (CS300)

CH. 2 OBJECT-ORIENTED PROGRAMMING

Chapter 6 Lab Classes and Objects

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

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

Object-Oriented Programming (Java)

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

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

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Selection and Repetition

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

ECE 122. Engineering Problem Solving with Java

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Transcription:

Designing Classes 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! 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?

Example: Account datatype - represents a generic bank account acct1 72354 acctnumber balance 102.56 name "Ted Murphy" acct2 69713 acctnumber balance 40.00 name "Jane Smith"

1. Structure of class definition Account class Data Methods int acctnumber; double balance; String name; Constructor deposit() withdraw() Account object acctnumber 72354 balance 102.56 name "Ted Murphy" getbalance() tostring() The class is the blueprint Classes define DATA and METHODS i.e., a datatype The object: is like the house built from the blueprint is an instance of the class has its own data space & shares methods defined for this datatype

2. Object instantiation Old example: Scanner scan = new Scanner (System.in); Invokes the Scanner constructor, which is a special method that sets up the object

2. Object instantiation using our newly defined Account class: Account acct1 = new Account ("Ted Murphy", 72354, 102.56); int acctnumber; double balance; String name; Constructor deposit() withdraw() Invokes the Account constructor, which is a special method that sets up the object A new Account object is created! acct1 acctnumber 72354 getbalance() tostring() balance 102.56 name "Ted Murphy"

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(); acct1.deposit (25.85); acct1 72354 acctnumber balance 128.41 name "Ted Murphy"

Datatype / Client (also referred to as servant / driver classes) //******************************************************************** // Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example) // Demonstrates the creation and use of multiple Account objects. //******************************************************************** String name; public class Transactions //******************************************************************* // Account.java Author: Constructor Lewis/Loftus //----------------------------------------------------------------- // Simplified code by MA Papalaskari // Creates some bank accounts and requests various services. // Represents a bank account //----------------------------------------------------------------- deposit() with methods deposit and withdraw. // public static void main (String[] args) ******************************************************************** import java.text.numberformat; withdraw() Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Account acct2 = new Account ("Jane Smith", 69713, 40.00); public class Account getbalance() Account acct3 = new Transactions Account ("Edward Demsey", 93757, 759.32); int acctnumber; System.out.println (acct1); double balance; tostring() System.out.println (acct2); String name; System.out.println (acct3); Client //---------------------------------------------------------------- acct1.deposit (25.85); // Sets up the account Account by defining its owner's name, acct1.withdraw account (60, 2.50); // number, and initial balance. //---------------------------------------------------------------- System.out.println (); - System.out.println (acct1); public Account (String x, int y, double z) System.out.println (acct2); System.out.println (acct3); - name = x; acctnumber = y; balance = z; int acctnumber; double balance; Datatype //----------------------------------------------------------------

//******************************************************************** // 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 acctnumber; double balance; String name; Data, aka, Instance Variables //----------------------------------------------------------------- // Sets up the account by defining its owner's name, account // number, and initial balance. //----------------------------------------------------------------- public Account (String x, int y, double z) name = x; acctnumber = y; balance = z; //----------------------------------------------------------------- getbalance() // Deposits the specified amount x into the account. //----------------------------------------------------------------- tostring() public void deposit (double x) balance = balance + x; continue int acctnumber; double balance; String name; Constructor deposit() withdraw()

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 acctnumber; public double getbalance () double balance; return balance; String name; Constructor //----------------------------------------------------------------- deposit() // Returns a one-line description of the account as a string. withdraw() //----------------------------------------------------------------- public String tostring () getbalance() NumberFormat fmt = NumberFormat.getCurrencyInstance(); tostring() return (acctnumber + "\t" + name + "\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 ("Ted Murphy", 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.deposit (25.85); acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1.tostring()); System.out.println (acct2.tostring()); System.out.println (acct3.tostring()); Sample Run 72354 Ted Murphy $102.56 69713 Jane Smith $40.00 93757 Edward Demsey $759.32 72354 Ted Murphy $65.91 69713 93757 Jane Smith Edward Demsey $40.00 $759.32

Transactions class: Creating Account objects Transactions class Account acct1 = new Account ("Ted Murphy", 72354, 102.56); int acctnumber; double balance; String name; Constructor deposit() withdraw() getbalance() tostring() Account class public Account (String x, int y, double z) name = x; acctnumber = y; balance = z; Constructor method acct1 72354 acctnumber balance 102.56 name "Ted Murphy"

Transactions class: Creating more Account objects Transactions class Account acct1 = new Account ("Ted Murphy", 72354, 102.56); acct1 72354 acctnumber Transactions class balance 102.56 name "Ted Murphy" Account acct2 = new Account ("Jane Smith", 69713, 40.00); acct2 69713 acctnumber balance 40.00 name "Jane Smith"

Using methods: deposit() acct1.deposit (25.85); //--------------------------------------------------- // Deposits the specified amount into the account. //--------------------------------------------------- public void deposit (double x) balance = balance + x; acct1 72354 acctnumber balance 102.56 name "Ted Murphy"

Using methods: deposit() acct1.deposit (25.85); //--------------------------------------------------- // Deposits the specified amount into the account. //--------------------------------------------------- public void deposit (double x) balance = balance + x; acct1 72354 acctnumber balance 128.41 name "Ted Murphy"

Another example: withdraw() 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; acct1 72354 acctnumber balance 128.41 name "Ted Murphy"

Another example: withdraw() 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; acct1 72354 acctnumber balance 65.91 name "Ted Murphy"

returning a value: getbalance() double amount = acct1.getbalance(); // Note: this code is not used in Transactions.java public double getbalance () return balance; acct1 acctnumber 72354 balance 102.56 name "Ted Murphy"

returning a value: getbalance() double amount = acct1.getbalance(); // Note: this code is not used in Transactions.java public double getbalance () return balance; acct1 acctnumber 72354 balance 102.56 name "Ted Murphy"

returning a value: tostring () method can be omitted! System.out.println(acct1.toString()); 72354 Ted Murphy $102.56 public String tostring () NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctnumber +"\t + name +"\t + fmt.format(balance)) acct1 acctnumber 72354 balance 102.56 name "Ted Murphy"

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 withdraw() method is positive Some of these improvements are in the book examples Account.java, Transactions.java (simplified versions) Account.java, Transactions.java (book versions)

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 //----------------------------------------------------------------- // Constructor: 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); return result;

NEXT: Focus on Methods Method control flow Method definition Parameters return statement Different ways of thinking about methods

Method Control Flow If the called method is in the same class, only the method name is needed compute mymethod mymethod();

Method Control Flow The called method is often part of another class or object obj main doit helpme obj.doit(); helpme(); 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.name Color.black more on this later (encapsulation)

Invoking methods within the same class An object s method may access any of the object s other methods directly. Eg: public void addinterest(double rate) deposit (rate*balance); Client code, eg: compound the interest for acct2 over 10 years int year = 1; while (year <= 10) acct2.addinterest(0.03); year ++; effect: acct2.deposit(0.03*balance)

Method Control Flow: example acct2 main addinterest deposit acct2.addinterest(0.03); deposit();

Method definition: Example 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;

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 ). How do we invoke the method to compute & print: (14.8) 2 + (37.65) 2?

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: Account Die Shoe Person Clients (Driver classes): Transactions, OnePercent RollingDice YouVeGotShoes (Project) PeopleBeingPeople (Lab) Next: UML class diagrams, visibility modifiers, graphical objects,

UML Class Diagrams 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

UML class diagram for Transactions program?

Examples of datatypes (Classes)

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 Client Methods Violating Encapsulation It is possible for a class to access the instance data of another class directly 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! Account.java Bad Client acct1.name = Joe ; deposit() withdraw() getbalance() name acctnumber balance

Use Accessors & Mutators - RIGHT Indirect access through methods accessors and mutators ( getters and setters ) Usually named getx() and setx() Account.java Transactions.java int x1 = acct1.getbalance(); deposit() withdraw() getbalance() name acctnumber balance

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

Violating Encapsulation experiment Revisit the Account example (use our simplified versions from the course website or Lab 8) Add some code to the client (Transactions or Onepercent) to modify the value of an instance variable, eg: acct1.name = Bernie ; Run the program to verify that the name on that account has changed. Now modify Account.java insert the modifier private in front of the instance variable declaration: private String name; Without changing anything in the client, re-compile the Account class and run your program again. Note the error you get: Error:

Public Constants 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);

static Public Constants BETTER It is better to declare constants as static public final static double RATE = 0.015; This way, a client can access the constants without creating an object, using the class name: System.out.print ("Interest rate = " + Account.RATE);

Visibility Modifiers the RULES Variables Methods public NO (but OK for public constants) Yes private Yes Yes, for support methods only See also ImInUrClassMessingUrInstanceData.java