1 / 40

CSC 1051 – Data Structures and Algorithms I

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!

alamea
Download Presentation

CSC 1051 – Data Structures and Algorithms I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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/ CSC 1051 M.A. Papalaskari, Villanova University

  2. Where do objects come from? CSC 1051 M.A. Papalaskari, Villanova University

  3. Where do objects come from? Good question! Defining our own classes will make it possible to design our own objects We need to learn: • What does it mean to define our own classes? • How do we define what happens when an object is instantiated? • How do we define methods that can be invoked through our objects? CSC 1051 M.A. Papalaskari, Villanova University

  4. Constructor deposit() withdraw() 1. Defining our own classes Example: Defining the AccountClass • A class to represent a generic bank account • A blueprint for an Account object intacctNumber; Data declarations double balance; String name; Method declarations getBalance() toString() CSC 1051 M.A. Papalaskari, Villanova University

  5. 72354 acctNumber 102.56 balance name "Ted Murphy" Constructor deposit() withdraw() 1. Defining our own classes Accountclass • 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 Accountobject intacctNumber; double balance; String name; getBalance() toString() • The class is the blueprint • Classes define DATA and METHODS CSC 1051 M.A. Papalaskari, Villanova University

  6. 2. What happens when an object is instantiated? Creating Objects – old example: • We have already seen something like this: • Scanner scan = new Scanner (System.in); This invokes the Scannerconstructor, which is a special method that sets up the object CSC 1051 M.A. Papalaskari, Villanova University

  7. 2. What happens when an object is instantiated? Constructor deposit() withdraw() Creating Objects – our newly defined Accountclass: Account acct1 = new Account ("Ted Murphy", 72354, 102.56); intacctNumber; double balance; String name; Invokes the Accountconstructor, which is a special method that sets up the object getBalance() toString()

  8. 2. What happens when an object is instantiated? acct1 72354 acctNumber 102.56 balance "Ted Murphy" name Creating Objects – our newly defined Accountclass: Account acct1 = new Account ("Ted Murphy", 72354, 102.56); A new Account object is created! CSC 1051 M.A. Papalaskari, Villanova University

  9. 3. What happens when an object’s method is invoked? • As we have seen, once an object has been created, we can use the dot operatorto invoke its methods: ans = scan.nextLine(); numChars = title.length(); CSC 1051 M.A. Papalaskari, Villanova University

  10. 3. What happens when an object’s method is invoked? acct1 72354 acctNumber 102.56 balance "Ted Murphy" name Transactionsclass Method invocation Me acct1.deposit (25.85); CSC 1051 M.A. Papalaskari, Villanova University

  11. 3. What happens when an object’s method is invoked? acct1 72354 acctNumber 128.41 balance "Ted Murphy" name Transactionsclass Method invocation Me acct1.deposit (25.85); Accountclass Method definition //---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------publicvoid deposit (double x){ balance = balance + x;} CSC 1051 M.A. Papalaskari, Villanova University

  12. 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

  13. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name acct2 69713 acctNumber 40.00 balance "Jane Smith" name Account: Example of newlydefined class CSC 1051 M.A. Papalaskari, Villanova University

  14. //********************************************************************// Account.java Author: Lewis/Loftus// Simplified code by MA Papalaskari// Represents a bank account with methods deposit and withdraw.//********************************************************************importjava.text.NumberFormat;publicclass Account{intacctNumber;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. //-----------------------------------------------------------------publicvoid deposit (double x) { balance = balance + x;} continue Constructor deposit() withdraw() intacctNumber; double balance; String name; getBalance() toString() CSC 1051 M.A. Papalaskari, Villanova University

  15. continue //-----------------------------------------------------------------// Withdraws the specified amount from the account and applies// the fee. //-----------------------------------------------------------------publicvoid withdraw (double x, double fee) { balance = balance - x - fee; }//-----------------------------------------------------------------// Returns the current balance of the account.//-----------------------------------------------------------------publicdoublegetBalance () {return balance; }//-----------------------------------------------------------------// Returns a one-line description of the account as a string.//-----------------------------------------------------------------public String toString () {NumberFormatfmt = NumberFormat.getCurrencyInstance();return (acctNumber + "\t" + name + "\t" + fmt.format(balance)); }} Constructor deposit() withdraw() intacctNumber; double balance; String name; getBalance() toString() CSC 1051 M.A. Papalaskari, Villanova University

  16. //********************************************************************// Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example)// Demonstrates the creation and use of multiple Account objects.//********************************************************************publicclassTransactions{//-----------------------------------------------------------------// Creates some bank accounts and requests various services.//-----------------------------------------------------------------publicstaticvoid 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); }} CSC 1051 M.A. Papalaskari, Villanova University

  17. Transactions class: Creating Account objects Transactionsclass Account acct1 = newAccount ("Ted Murphy", 72354, 102.56); CSC 1051 M.A. Papalaskari, Villanova University

  18. Constructor deposit() withdraw() Transactions class: Creating Account objects Transactionsclass Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Accountclass public Account (String x, inty, double z) { name = x;acctNumber = y; balance = z; } intacctNumber; double balance; Constructor method String name; getBalance() toString() CSC 1051 M.A. Papalaskari, Villanova University

  19. acct1 72354 acctNumber 102.56 balance name "Ted Murphy" Constructor deposit() withdraw() Transactions class: Creating Account objects Transactionsclass Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Accountclass public Account (String x, inty, double z) { name = x;acctNumber = y; balance = z; } intacctNumber; double balance; Constructor method String name; getBalance() toString() CSC 1051 M.A. Papalaskari, Villanova University

  20. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name acct2 69713 acctNumber 40.00 balance "Jane Smith" name Transactions class: Creating more Account objects Transactionsclass Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Transactionsclass Account acct2 = new Account ("Jane Smith", 69713, 40.00); CSC 1051 M.A. Papalaskari, Villanova University

  21. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name Account class: Using methods acct1.deposit (25.85); CSC 1051 M.A. Papalaskari, Villanova University

  22. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name Account class: Using methods acct1.deposit (25.85); //---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------publicvoid deposit (double x){ balance = balance + x;} CSC 1051 M.A. Papalaskari, Villanova University

  23. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name Account class: Using methods acct1.deposit (25.85); //---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------publicvoid deposit (double x){ balance = balance + x;} CSC 1051 M.A. Papalaskari, Villanova University

  24. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name Account class: Using methods acct1.deposit (25.85); //---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------publicvoid deposit (double x){ balance = balance + x;} CSC 1051 M.A. Papalaskari, Villanova University

  25. acct1 72354 acctNumber 128.41 balance "Ted Murphy" name Account class: Using methods acct1.deposit (25.85); //---------------------------------------------------// Deposits the specified amount into the account. //---------------------------------------------------publicvoid deposit (double x){ balance = balance + x;} CSC 1051 M.A. Papalaskari, Villanova University

  26. acct1 72354 acctNumber 128.41 balance "Ted Murphy" name Account class: Another Example acct1.withdraw (60, 2.50); CSC 1051 M.A. Papalaskari, Villanova University

  27. acct1 72354 acctNumber 128.41 balance "Ted Murphy" name Account class: Another Example acct1.withdraw (60, 2.50); //------------------------------------------------// Withdraws the specified amount from the // account and applies the fee.//------------------------------------------------publicvoid withdraw (double x, double fee) { balance = balance - x- fee;} CSC 1051 M.A. Papalaskari, Villanova University

  28. acct1 72354 acctNumber 65.91 balance "Ted Murphy" name Account class: Another Example acct1.withdraw (60, 2.50); //-------------------------------------------------// Withdraws the specified amount from the account// and applies the fee.//-----------------------------------------------publicvoid withdraw (double x, double fee){ balance = balance - x- fee;} CSC 1051 M.A. Papalaskari, Villanova University

  29. Constructor deposit() withdraw() Class definitions • A class can contain data declarations and method declarations Data declarations (also called fields) intacctNumber; double balance; String name; Method declarations (note: the constructor is also a method) getBalance() toString() CSC 1051 M.A. Papalaskari, Villanova University

  30. 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

  31. //********************************************************************// Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example)// Demonstrates the creation and use of multiple Account objects.//********************************************************************publicclassTransactions{//-----------------------------------------------------------------// Creates some bank accounts and requests various services.//-----------------------------------------------------------------publicstaticvoid 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()); }} CSC 1051 M.A. Papalaskari, Villanova University

  32. //********************************************************************// Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example)// Demonstrates the creation and use of multiple Account objects.//********************************************************************publicclassTransactions{//-----------------------------------------------------------------// Creates some bank accounts and requests various services.//-----------------------------------------------------------------publicstaticvoid 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 Run 72354 Ted Murphy $102.5669713 Jane Smith $40.0093757 Edward Demsey $759.3272354 Ted Murphy $65.9169713 Jane Smith $40.0093757 Edward Demsey $759.32 CSC 1051 M.A. Papalaskari, Villanova University

  33. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name toString() method System.out.println(acct1.toString()); public String toString () {NumberFormatfmt = NumberFormat.getCurrencyInstance();return (acctNumber +"\t”+ name +"\t”+ fmt.format(balance));} CSC 1051 M.A. Papalaskari, Villanova University

  34. acct1 72354 acctNumber 102.56 balance "Ted Murphy" name can be omitted! toString() method System.out.println(acct1.toString()); public String toString () {NumberFormatfmt = NumberFormat.getCurrencyInstance();return (acctNumber +"\t”+ name +"\t”+ fmt.format(balance));} CSC 1051 M.A. Papalaskari, Villanova University

  35. Examples of Classes CSC 1051 M.A. Papalaskari, Villanova University

  36. Another example: The Die Class • See RollingDice.java • See Die.java CSC 1051 M.A. Papalaskari, Villanova University

  37. RollingDice Die faceValue : int main (args: String[]) : void roll() : int setFaceValue(value : int) : void getFaceValue() : int toString() : String UML Class Diagrams UML = Unified Modelling Language • Example: A UML class diagram for the RollingDice program:

  38. //********************************************************************//******************************************************************** // 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; intsum; 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 Run Die One: 5, Die Two: 2 Die One: 1, Die Two: 4 Sum: 5 Die One: 4, Die Two: 2 New sum: 6 CSC 1051 M.A. Papalaskari, Villanova University

  39. //********************************************************************//******************************************************************** // Die.java Author: Lewis/Loftus // // Represents one die (singular of dice) with faces showing values // between 1 and 6. //******************************************************************** public class Die { private final intMAX = 6; // maximum face value private intfaceValue; // current value showing on the die //----------------------------------------------------------------- // Constructor: Sets the initial face value. //----------------------------------------------------------------- public Die() { faceValue = 1; } //----------------------------------------------------------------- // Rolls the die and returns the result. //----------------------------------------------------------------- public introll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } continue CSC 1051 M.A. Papalaskari, Villanova University

  40. continue //----------------------------------------------------------------- // Face value mutator. //----------------------------------------------------------------- public void setFaceValue (intvalue) { faceValue = value; } //----------------------------------------------------------------- // Face value accessor. //----------------------------------------------------------------- public intgetFaceValue() { return faceValue; } //----------------------------------------------------------------- // Returns a string representation of this die. //----------------------------------------------------------------- public String toString() { String result = Integer.toString(faceValue); return result; } } CSC 1051 M.A. Papalaskari, Villanova University

More Related