CSC 1051 – Data Structures and Algorithms I
description
Transcript of CSC 1051 – Data Structures and Algorithms I
![Page 1: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/1.jpg)
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 – part 2
![Page 2: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/2.jpg)
Getting to know classes so far• Predefined classes from the Java API.
• We are also defining a few classes of our own:
– Account
– Die
– Person
– Book
CSC 1051 M.A. Papalaskari, Villanova University
![Page 3: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/3.jpg)
Getting to know classes so far• Predefined classes from the Java API.
• We have defined a few classes of our own:
– Account
– Die
– Person
– Book
CSC 1051 M.A. Papalaskari, Villanova University
Driver classes:– Transactions
– RollingDice
– PeopleBeingPeople
– Bookshelf (Project 6)
![Page 4: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/4.jpg)
More Examples of Classes
CSC 1051 M.A. Papalaskari, Villanova University
![Page 5: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/5.jpg)
Next:• Review what we learned so far
• Focus on method definition
• Encapsulation
• UML diagrams
CSC 1051 M.A. Papalaskari, Villanova University
![Page 6: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/6.jpg)
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 Transactions1{ //----------------------------------------------------------------- // 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); System.out.println (acct1); System.out.println (acct2); acct1.deposit (25.85); System.out.println (); System.out.println (acct1); System.out.println (acct2); }}
Review• driver classes
![Page 7: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/7.jpg)
• class declaration
CSC 1051 M.A. Papalaskari, Villanova University
Constructor
deposit()
withdraw()
long acctNumber;double balance;String name;
Data declarations
Method declarations
toString()
Review
![Page 8: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/8.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
• using methods…
Review
![Page 9: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/9.jpg)
Account class: Using methods
acct1 72354acctNumber
102.56balance
name "Ted Murphy"
CSC 1051 M.A. Papalaskari, Villanova University
acct1.deposit (25.85);
Review
![Page 10: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/10.jpg)
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 amount){ balance = balance + amount;}
Review
![Page 11: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/11.jpg)
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 amount){ balance = balance + amount;}
Review
![Page 12: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/12.jpg)
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 amount){ balance = balance + amount;}
Review
![Page 13: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/13.jpg)
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 amount){ balance = balance + amount;}
Review
![Page 14: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/14.jpg)
Account class: Another Example
acct1 72354acctNumber
128.41balance
name "Ted Murphy"
CSC 1051 M.A. Papalaskari, Villanova University
acct1.withdraw (60,2);
Review
![Page 15: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/15.jpg)
Account class: Another Example
acct1 72354acctNumber
128.41balance
name "Ted Murphy"
CSC 1051 M.A. Papalaskari, Villanova University
acct1.withdraw (60,2);
//------------------------------------------------------- // Withdraws the specified amount from the account // and applies the fee. //----------------------------------------------------- public void withdraw (double amount, double fee) { balance = balance - amount - fee; }
Review
![Page 16: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/16.jpg)
Account class: Another Example
acct1 72354acctNumber
66.41balance
name "Ted Murphy"
CSC 1051 M.A. Papalaskari, Villanova University
acct1.withdraw (60,2);
//-------------------------------------------------// Withdraws the specified amount from the account// and applies the fee.//-----------------------------------------------public void withdraw (double amount, double fee){ balance = balance - amount - fee;}
Review
![Page 17: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/17.jpg)
acct1 72354acctNumber
102.56balance
name "Ted Murphy"
CSC 1051 M.A. Papalaskari, Villanova University
Object creation: Constructors Account acct1 = new Account ("Ted Murphy", 72354, 102.56);
public Account (String owner, long account, double initial){ acctNumber = account; balance = initial; name = owner;}
Review
![Page 18: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/18.jpg)
acct1 72354acctNumber
102.56balance
name "Ted Murphy"
CSC 1051 M.A. Papalaskari, Villanova University
toString() method System.out.prinltn(acct1);
public String toString (){ NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber +"\t”+ name +"\t”+ fmt.format(balance));}
Review
![Page 19: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/19.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
Invoking methods in same classpaintSnowman(page, 200, 150);
public void paintSnowman(Graphics page, int x, int y){ // snowman code goes here . . . }
Review
![Page 20: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/20.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
static methods: 1) In another class OtherClass.doSomething();
public static void doSomething(){ System.out.println(" At your service. " );}
Review
OtherClass
![Page 21: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/21.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
static methods: 2) In same class doSomething();
public static void doSomething(){ System.out.println(" At your service. " );}
Review
![Page 22: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/22.jpg)
• Method definition–parameters– return type– return statement
char calc (int num1, int num2, String message){ int sum = num1 + num2; char result = message.charAt (sum);
return result;}
ch = obj.calc (25, count, "Hello");
CSC 1051 M.A. Papalaskari, Villanova University
Review
![Page 23: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/23.jpg)
myMethod();
myMethodcompute
Method Control Flow• If the called method is in the same class, only the
method name is needed
CSC 1051 M.A. Papalaskari, Villanova University
![Page 24: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/24.jpg)
doIt
helpMe
helpMe();
obj.doIt();
main
Method Control Flow• The called method is often part of another class or
object
CSC 1051 M.A. Papalaskari, Villanova University
![Page 25: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/25.jpg)
More Method Examples:• Write a method that has one int parameter num,
and prints “Happy Birthday” num times• Write a method with two double paramenters a
and b that computes and returns the sum of squares: a2 + b2 of its two int parameters
• Write a method that has one int parameter num, and returns the String “Happy Birthday” num times
CSC 1051 M.A. Papalaskari, Villanova University
![Page 26: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/26.jpg)
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);
continue
Another Example: RollingDice.java (Driver)
![Page 27: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/27.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
continue
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); }}
Example: RollingDice.java (Driver)
![Page 28: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/28.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
continue
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
Example: RollingDice.java (Driver)
![Page 29: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/29.jpg)
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; }
Example: Die.java
4faceValue
![Page 30: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/30.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
continue
//----------------------------------------------------------------- // Rolls the die and returns the result. //----------------------------------------------------------------- public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; }
//----------------------------------------------------------------- // Face value mutator. //----------------------------------------------------------------- public void setFaceValue (int value) { faceValue = value; }
//----------------------------------------------------------------- // Face value accessor. //----------------------------------------------------------------- public int getFaceValue() { return faceValue; }
continue
Example: Die.java
4faceValue
![Page 31: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/31.jpg)
CSC 1051 M.A. Papalaskari, Villanova University
continue
//----------------------------------------------------------------- // Returns a string representation of this die. //----------------------------------------------------------------- public String toString() { String result = Integer.toString(faceValue);
return result; }}
Example: Die.java
4faceValue
![Page 32: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/32.jpg)
UML Class Diagrams• A UML class diagram for the RollingDice
program:
RollingDice
main (args : String[]) : void
DiefaceValue : introll() : intsetFaceValue (int value) : voidgetFaceValue() : inttoString() : String
![Page 33: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/33.jpg)
UML Class Diagrams• A UML class diagram for the RollingDice
program:
RollingDice
main (args : String[]) : void
DiefaceValue : introll() : intsetFaceValue (int value) : voidgetFaceValue() : inttoString() : String
die1 4faceValue
![Page 34: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/34.jpg)
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 and they manage the instance data
Methods
Data
Client
CSC 1051 M.A. Papalaskari, Villanova University
![Page 35: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/35.jpg)
Violating Encapsulation• It is possible for a class to access the instance data
of another class directly
Methods
Data
Client
CSC 1051 M.A. Papalaskari, Villanova University
![Page 36: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/36.jpg)
Violating Encapsulation - Example• It is possible for a class to access the instance data
of another class directly - but don’t do this!• See Account.java (modified)• See ImInUrClassMessingUrInstanceData.java
deposit()withdraw()
addInterest()
nameacctNumber
balance
Transactions.java
CSC 1051 M.A. Papalaskari, Villanova University
acct1.name = “Joe”;
Account.java
![Page 37: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/37.jpg)
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
CSC 1051 M.A. Papalaskari, Villanova University
![Page 38: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/38.jpg)
public constants are ok - Example
Account acct1 = new Account (”Sartre", 72354, 102.56);
System.out.println (acct1);
System.out.println ("Interest rate = " + acct1.RATE);
CSC 1051 M.A. Papalaskari, Villanova University
![Page 39: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/39.jpg)
public constants are ok - Example
Account acct1 = new Account (”Sartre", 72354, 102.56);
System.out.println (acct1);
System.out.println ("Interest rate = " + acct1.RATE);
• Normally, constants are declared as static. • If RATE had been declared as follows:final static double RATE = ;• Then the last statement in this program would have been:System.out.println ("Interest rate = " + Account.RATE);
CSC 1051 M.A. Papalaskari, Villanova University
![Page 40: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/40.jpg)
Visibility Modifiers – the RULES
public private
Variables
Methods Yes Yes, for support methods only
YesNO(but OK for public
constants)
CSC 1051 M.A. Papalaskari, Villanova University
![Page 41: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/41.jpg)
Encapsulation – Accessing the data• Indirect access through methods
• accessors and mutators (“getters” and “setters”)
• Usually named getX() and setX()
deposit()withdraw()
addInterest()
nameacctNumber
balance
Transactions.java
CSC 1051 M.A. Papalaskari, Villanova University
acct1.getBalance()
Account.java
Methods
Data
Example
![Page 42: CSC 1051 – Data Structures and Algorithms I](https://reader036.fdocuments.us/reader036/viewer/2022062520/56816204550346895dd229f6/html5/thumbnails/42.jpg)
Encapsulation – Mutators (setters) can restrict access to the data, as appropriate• Example: Say a class has an instance variable:
private int quantity;
• The mutator may also work to ensure that quantity, does not become negative:
public void setQuantity(int num){ if (num<0) { System.out.println("*Error in setQuantity()"); System.out.println("negative quantity."); System.out.println("quantity not changed."); } else quantity = num;}
CSC 1051 M.A. Papalaskari, Villanova University