Top Banner
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/ CSC 1051 M.A. Papalaskari, Villanova University Designing Classes
40

CSC 1051 – Data Structures and Algorithms I

Feb 20, 2016

Download

Documents

alamea

CSC 1051 – Data Structures and Algorithms I. Designing Classes. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu /~map/1051/. Where do objects come from?. Where do objects come from?. Good question! - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 1051 – Data Structures and Algorithms I

CSC 1051 – Data Structures and Algorithms I

Dr. Mary-Angela PapalaskariDepartment of Computing SciencesVillanova University

Course website:www.csc.villanova.edu/~map/1051/

CSC 1051 M.A. Papalaskari, Villanova University

Designing Classes

Page 2: CSC 1051 – Data Structures and Algorithms I

Where do objects come from?

CSC 1051 M.A. Papalaskari, Villanova University

Page 3: CSC 1051 – Data Structures and Algorithms I

Where do objects come from?Good question!

Defining our own classes will make it possible to design our own objectsWe need to learn:

1.What does it mean to define our own classes?

2.How do we define what happens when an object is instantiated?

3.How do we define methods that can be invoked through our objects?

CSC 1051 M.A. Papalaskari, Villanova University

Page 4: CSC 1051 – Data Structures and Algorithms I

1. Defining our own classes

Example: Defining the Account Class– A class to represent a generic bank account– A blueprint for an Account object

Data declarations

Method declarations

CSC 1051 M.A. Papalaskari, Villanova University

Constructor

deposit()

withdraw()

getBalance()

toString()

double balance;int acctNumber;

String name;

Page 5: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

72354acctNumber

102.56balance

name

"Ted Murphy"

Constructor

deposit()

withdraw()

getBalance()

toString()

double balance;int acctNumber;

String name;

Account classAccount object

• The object:– is like the house built from the blueprint– is an instance of the class– has its own data space– shares the methods defined by the class• The class is the blueprint

Classes define DATA and METHODS

1. Defining our own classes

Page 6: CSC 1051 – Data Structures and Algorithms I

Creating Objects – old example:• We have already seen something like this:

This invokes the Scanner constructor, which isa special method that sets up the object

CSC 1051 M.A. Papalaskari, Villanova University

2. What happens when an object is instantiated?

Scanner scan = new Scanner (System.in);

Page 7: CSC 1051 – Data Structures and Algorithms I

Account acct1 = new Account ("Ted Murphy", 72354, 102.56);

Creating Objects – our newly defined Account class:

Invokes the Account constructor, which isa special method that sets up the object

2. What happens when an object is instantiated?

Constructordeposit()

withdraw()

getBalance()

toString()

double balance;

int acctNumber;

String name;

Page 8: CSC 1051 – Data Structures and Algorithms I

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

Account acct1 = new Account ("Ted Murphy", 72354, 102.56);

Creating Objects – our newly defined Account class:

A new Account object is created!

2. What happens when an object is instantiated?

Page 9: CSC 1051 – Data Structures and Algorithms I

• 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();

CSC 1051 M.A. Papalaskari, Villanova University

3. What happens when an object’s method is invoked?

Page 10: CSC 1051 – Data Structures and Algorithms I

acct1 72354acctNumber

102.56balance

name "Ted Murphy"CSC 1051 M.A. Papalaskari, Villanova University

Meacct1.deposit (25.85);Method invocation

Transactions class

3. What happens when an object’s method is invoked?

Page 11: CSC 1051 – Data Structures and Algorithms I

acct1 72354acctNumber

128.41balance

name "Ted Murphy"CSC 1051 M.A. Papalaskari, Villanova University

Meacct1.deposit (25.85);

//---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------public void deposit (double x){ balance = balance + x;}

Method invocation

Method definition

Account class

Transactions class

3. What happens when an object’s method is invoked?

Page 12: CSC 1051 – Data Structures and Algorithms I

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– instance data– encapsulation and Java modifiers– method declaration and parameter passing– constructors– graphical objects– events and listeners– buttons and text fields

CSC 1051 M.A. Papalaskari, Villanova University

Page 13: CSC 1051 – Data Structures and Algorithms I

Account: Example of newly defined class

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

acct2 69713acctNumber

40.00balance

name "Jane Smith"

CSC 1051 M.A. Papalaskari, Villanova University

Page 14: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

//********************************************************************// 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;

//----------------------------------------------------------------- // 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; } //----------------------------------------------------------------- // Deposits the specified amount x into the account. //----------------------------------------------------------------- public void deposit (double x) { balance = balance + x; }continue

Constructordeposit()

withdraw()

getBalance()

toString()

double balance;

int acctNumber;

String name;

Page 15: CSC 1051 – Data Structures and Algorithms I

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. //----------------------------------------------------------------- public double getBalance () { return balance; }

//----------------------------------------------------------------- // Returns a one-line description of the account as a string. //----------------------------------------------------------------- public String toString () { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber + "\t" + name + "\t" + fmt.format(balance)); }}

CSC 1051 M.A. Papalaskari, Villanova University

Constructor

deposit()

withdraw()

getBalance()

toString()

double balance;

int acctNumber;

String name;

Page 16: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

//********************************************************************// 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); System.out.println (acct2); System.out.println (acct3); }}

Page 17: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

Account acct1 = new Account ("Ted Murphy", 72354, 102.56);

Transactions class: Creating Account objectsTransactions class

Page 18: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

Account acct1 = new Account ("Ted Murphy", 72354, 102.56);

Transactions class: Creating Account objects

public Account (String x, int y, double z) { name = x; acctNumber = y; balance = z; }

Constructor method

Transactions class

Account class

Constructordeposit()

withdraw()

getBalance()

toString()

double balance;

int acctNumber;

String name;

Page 19: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

Account acct1 = new Account ("Ted Murphy", 72354, 102.56);

Transactions class: Creating Account objects

public Account (String x, int y, double z) { name = x; acctNumber = y; balance = z; }

Constructor method

Transactions class

Account class

Constructordeposit()

withdraw()

getBalance()

toString()

double balance;

int acctNumber;

String name;

acct1 72354acctNumber

102.56balance

name

"Ted Murphy"

Page 20: CSC 1051 – Data Structures and Algorithms I

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

acct2 69713acctNumber

40.00balance

name "Jane Smith"CSC 1051 M.A. Papalaskari, Villanova University

Account acct2 = new Account ("Jane Smith", 69713, 40.00);

Account acct1 = new Account ("Ted Murphy", 72354, 102.56);

Transactions class: Creating more Account objectsTransactions class

Transactions class

Page 21: CSC 1051 – Data Structures and Algorithms I

Account class: Using methods

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

acct1.deposit (25.85);

Page 22: CSC 1051 – Data Structures and Algorithms I

Account class: Using methods

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

acct1.deposit (25.85);

//---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------public void deposit (double x){ balance = balance + x;}

Page 23: CSC 1051 – Data Structures and Algorithms I

Account class: Using methods

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

acct1.deposit (25.85);

//---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------public void deposit (double x){ balance = balance + x;}

Page 24: CSC 1051 – Data Structures and Algorithms I

Account class: Using methods

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

acct1.deposit (25.85);

//---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------public void deposit (double x){ balance = balance + x;}

Page 25: CSC 1051 – Data Structures and Algorithms I

Account class: Using methods

acct1 72354acctNumber

128.41balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

acct1.deposit (25.85);

//---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------public void deposit (double x){ balance = balance + x;}

Page 26: CSC 1051 – Data Structures and Algorithms I

Account class: Another Example

acct1 72354acctNumber

128.41balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

acct1.withdraw (60, 2.50);

Page 27: CSC 1051 – Data Structures and Algorithms I

Account class: Another Example

acct1 72354acctNumber

128.41balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

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;}

Page 28: CSC 1051 – Data Structures and Algorithms I

Account class: Another Example

acct1 72354acctNumber

65.91balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

//-------------------------------------------------// Withdraws the specified amount from the account// and applies the fee.//-----------------------------------------------public void withdraw (double x, double fee){ balance = balance - x - fee;}

acct1.withdraw (60, 2.50);

Page 29: CSC 1051 – Data Structures and Algorithms I

Class definitions• A class can contain data declarations and method

declarations

Data declarations(also called fields)

Method declarations(note: the constructor is also

a method)

CSC 1051 M.A. Papalaskari, Villanova University

Constructordeposit()

withdraw()

getBalance()

toString()

double balance;

int acctNumber;

String name;

Page 30: CSC 1051 – Data Structures and Algorithms I

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)

CSC 1051 M.A. Papalaskari, Villanova University

Page 31: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

//********************************************************************// 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()); }}

Page 32: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

//********************************************************************// 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); System.out.println (acct2); System.out.println (acct3); }}

Sample Run72354 Ted Murphy $102.5669713 Jane Smith $40.0093757 Edward Demsey $759.32

72354 Ted Murphy $65.9169713 Jane Smith $40.0093757 Edward Demsey $759.32

Page 33: CSC 1051 – Data Structures and Algorithms I

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

toString() method System.out.println(acct1.toString());

public String toString (){ NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber +"\t”+ name +"\t”+ fmt.format(balance));}

Page 34: CSC 1051 – Data Structures and Algorithms I

acct1 72354acctNumber

102.56balance

name "Ted Murphy"

CSC 1051 M.A. Papalaskari, Villanova University

toString() method System.out.println(acct1.toString());

public String toString (){ NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber +"\t”+ name +"\t”+ fmt.format(balance));}

can be omitted!

Page 35: CSC 1051 – Data Structures and Algorithms I

Examples of Classes

CSC 1051 M.A. Papalaskari, Villanova University

Page 36: CSC 1051 – Data Structures and Algorithms I

Another example: The Die Class• See RollingDice.java • See Die.java

CSC 1051 M.A. Papalaskari, Villanova University

Page 37: CSC 1051 – Data Structures and Algorithms I

UML Class DiagramsUML = Unified Modelling Language• Example: A UML class diagram for the RollingDice program:

RollingDice

main (args : String[]) : void

DiefaceValue : introll() : intsetFaceValue (value : int) : voidgetFaceValue() : inttoString() : String

Page 38: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

//********************************************************************// 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);

sum = die1.roll() + die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); System.out.println ("New sum: " + sum); }}

Sample RunDie One: 5, Die Two: 2Die One: 1, Die Two: 4Sum: 5Die One: 4, Die Two: 2New sum: 6

Page 39: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

//********************************************************************// 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

Page 40: CSC 1051 – Data Structures and Algorithms I

CSC 1051 M.A. Papalaskari, Villanova University

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; }}