1 / 61

INF 523Q

INF 523Q. Chapter 4: Writing Classes. Writing Classes. We've been using predefined classes. Now we will learn to write our own classes to define new objects Chapter 4 focuses on: class declarations method declarations instance variables encapsulation method overloading. Objects.

vivianjames
Download Presentation

INF 523Q

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. INF 523Q Chapter 4: Writing Classes

  2. Writing Classes • We've been using predefined classes. Now we will learn to write our own classes to define new objects • Chapter 4 focuses on: • class declarations • method declarations • instance variables • encapsulation • method overloading

  3. Objects • An object has: • state - descriptive characteristics • behaviors - what it can do (or be done to it) • For example, consider a coin that can be flipped so that it's face shows either "heads" or "tails" • The state of the coin is its current face (heads or tails) • The behavior of the coin is that it can be flipped • Note that the behavior of the coin might change its state

  4. Classes • A class is a blueprint of an object • It is the model or pattern from which objects are created • For example, the String class is used to define String objects • Each String object contains specific characters (its state) • Each String object can perform services (behaviors) such as toUpperCase

  5. Classes • The String class was provided for us by the Java standard class library • But we can also write our own classes that define specific objects that we need • For example, suppose we wanted to write a program that simulates the flipping of a coin • We could write a Coin class to represent a coin object

  6. int x, y; char ch; Classes • A class contains data declarations and method declarations Data declarations Method declarations

  7. Data Scope • The scope of data is the area in a program in which that data can be used (referenced) • Data declared at the class level can be used by all methods in that class • Data declared within a method can only be used in that method • Data declared within a method is called local data

  8. Writing Methods • A method declaration specifies the code that will be executed when the method is invoked (or called) • When a method is invoked, the flow of control jumps to the method and executes its code • When complete, the flow returns to the place where the method was called and continues • The invocation may or may not return a value, depending on how the method was defined

  9. compute myMethod myMethod(); Method Control Flow • The called method could be within the same class, in which case only the method name is needed

  10. main doIt helpMe obj.doIt(); helpMe(); Method Control Flow • The called method could be part of another class or object

  11. The Coin Class • In our Coin class we could define the following data: • face, an integer that represents the current face • HEADS and TAILS, integer constants that represent the two possible states • We might also define the following methods: • a Coin constructor, to set up the object • a flip method, to flip the coin • a getFace method, to return the current face • a toString method, to return a string description for printing • See Coin.java (page 180) • See CountFlips.java (page 179)

  12. Coin.java • //****************************************************************** • // Coin.java Author: Lewis and Loftus • // Represents a coin with two sides that can be flipped. • //****************************************************************** • publicclass Coin • { • publicfinalint HEADS = 0; • publicfinalint TAILS = 1; • privateint face; • // Sets up the coin by flipping it initially. • public Coin () • { • flip(); • } • // Flips the coin by randomly choosing a face. • publicvoid flip () • { • face = (int) (Math.random() * 2); • }

  13. Coin.java (cont.) • // Returns the current face of the coin as an integer. • publicint getFace () • { • return face; • } • // Returns the current face of the coin as a string. • public String toString() • { • String faceName; • if (face == HEADS) • faceName = "Heads"; • else • faceName = "Tails"; • return faceName; • } • }

  14. CountFlips.java • //****************************************************************** • // CountFlips.java Author: Lewis and Loftus • // Demonstrates the use of a programmer-defined class. • //****************************************************************** • import Coin; • publicclass CountFlips • { • // Flips a coin multiple times and counts the number of heads and tails that result. • publicstaticvoid main (String[] args) • { • finalint NUM_FLIPS = 1000; • int heads = 0, tails = 0; • Coin myCoin = new Coin(); // instantiate the Coin object • for (int count=1; count <= NUM_FLIPS; count++)

  15. CountFlips.java (cont.) • { • myCoin.flip(); • if (myCoin.getFace() == myCoin.HEADS) • heads++; • else • tails++; • } • System.out.println ("The number flips: " + NUM_FLIPS); • System.out.println ("The number of heads: " + heads); • System.out.println ("The number of tails: " + tails); • } • }

  16. The Coin Class • Once the Coin class has been defined, we can use it again in other programs as needed • Note that the CountFlips program did not use the toString method • A program will not necessarily use every service provided by an object

  17. Instance Data • The face variable in the Coin class is called instance data because each instance (object) of the Coin class has its own • A class declares the type of the data, but it does not reserve any memory space for it • Every time a Coin object is created, a new face variable is created as well • The objects of a class share the method definitions, but they have unique data space • That's the only way two objects can have different states

  18. class Coin coin1 coin2 int face; face face 0 1 Instance Data • See FlipRace.java (page 182)

  19. FlipRace.java • //******************************************************************** • // FlipRace.java Author: Lewis and Loftus • // Demonstrates the existence of separate data space in multiple • // instantiations of a programmer-defined class. • //******************************************************************** • import Coin; • publicclass FlipRace • { • // Flips two coins until one of them comes up heads a set number of times in a row. • publicstaticvoid main (String[] args) • { • finalint GOAL = 3; • int count1 = 0, count2 = 0; • // Create two separate coin objects • Coin coin1 = new Coin(); • Coin coin2 = new Coin(); • while (count1 < GOAL && count2 < GOAL) • { • coin1.flip(); • coin2.flip();

  20. FlipRace.java (cont.) • // Print the flip results (uses Coin's toString method) • System.out.print ("Coin 1: " + coin1); • System.out.println (" Coin 2: " + coin2); • // Increment or reset the counters • count1 = (coin1.getFace() == coin1.HEADS) ? count1+1 : 0; • count2 = (coin2.getFace() == coin2.HEADS) ? count2+1 : 0; • } • // Determine the winner • if (count1 < GOAL) • System.out.println ("Coin 2 Wins!"); • else • if (count2 < GOAL) • System.out.println ("Coin 1 Wins!"); • else • System.out.println ("It's a TIE!"); • } • }

  21. Encapsulation • You can take one of two views of an object: • internal - the structure of its data, the algorithms used by its methods • external - the interaction of the object with other objects in the program • From the external view, an object is an encapsulated entity, providing a set of specific services • These services define the interface to the object • Recall from Chapter 2 that an object is an abstraction, hiding details from the rest of the system

  22. Encapsulation • An object should be self-governing • Any changes to the object's state (its variables) should be accomplished by that object's methods • We should make it difficult, if not impossible, for one object to "reach in" and alter another object's state • The user, or client, of an object can request its services, but it should not have to be aware of how those services are accomplished

  23. Encapsulation • An encapsulated object can be thought of as a black box • Its inner workings are hidden to the client, which only invokes the interface methods Methods Client Data

  24. Visibility Modifiers • In Java, we accomplish encapsulation through the appropriate use of visibility modifiers • A modifier is a Java reserved word that specifies particular characteristics of a method or data value • We've used the modifier final to define a constant • Java has three visibility modifiers: public, private, and protected • We will discuss the protected modifier later

  25. Visibility Modifiers • Members of a class that are declared with public visibility can be accessed from anywhere • Members of a class that are declared with private visibility can only be accessed from inside the class • Members declared without a visibility modifier have default visibility and can be accessed by any class in the same package • Java modifiers are discussed in detail in Appendix F

  26. Visibility Modifiers • As a general rule, no object's data should be declared with public visibility • Methods that provide the object's services are usually declared with public visibility so that they can be invoked by clients • Public methods are also called service methods • A method created simply to assist a service method is called a support method • Since a support method is not intended to be called by a client, it should not be declared with public visibility

  27. Method Declarations Revisited • A method declaration begins with a method header char calc (int num1, int num2, String message) method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument return type

  28. Method Declarations • The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } sum and result are local data They are created each time the method is called, and are destroyed when it finishes executing The return expression must be consistent with the return type

  29. The return Statement • The return type of a method indicates the type of value that the method sends back to the calling location • A method that does not return a value has a void return type • The return statement specifies the value that will be returned • Its expression must conform to the return type

  30. char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } Parameters • Each time a method is called, the actual arguments in the invocation are copied into the formal arguments ch = obj.calc (25, count, "Hello");

  31. Constructors Revisited • Recall that a constructor is a special method that is used to set up a newly created object • When writing a constructor, remember that: • it has the same name as the class • it does not return a value • it has no return type, not even void • it often sets the initial values of instance variables • The programmer does not have to define a constructor for a class

  32. Writing Classes • See Account.java (page 189) • See BankAccounts.java (page 188)

  33. Account.java • //******************************************************************** • // Account.java Author: Lewis and Loftus • // Represents a bank account with basic services such as deposit and withdraw. • //******************************************************************** • import java.text.NumberFormat; • publicclass Account • { • private NumberFormat fmt = NumberFormat.getCurrencyInstance(); • privatefinaldouble RATE = 0.045; // interest rate of 4.5% • privatelong acctNumber; • privatedouble balance; • private String name; • // Sets up the account by defining its owner, account number, • // and initial balance. • public Account (String owner, long account, double initial) • { • name = owner; • acctNumber = account; • balance = initial; • }

  34. Account.java (cont.) • // Validates the transaction, then deposits the specified amount • // into the account. Returns the new balance. • publicdouble deposit (double amount) • { • if (amount < 0) // deposit value is negative • { • System.out.println (); • System.out.println ("Error: Deposit amount is invalid."); • System.out.println (acctNumber + " " + fmt.format(amount)); • } • else • balance = balance + amount; • return balance; • } • // Validates the transaction, then withdraws the specified amount • // from the account. Returns the new balance. • publicdouble withdraw (double amount, double fee) • { • amount += fee;

  35. Account.java (cont.) • if (amount < 0) // withdraw value is negative • { • System.out.println (); • System.out.println ("Error: Withdraw amount is invalid."); • System.out.println ("Account: " + acctNumber); • System.out.println ("Requested: " + fmt.format(amount)); • } • else • if (amount > balance) // withdraw value exceeds balance • { • System.out.println (); • System.out.println ("Error: Insufficient funds."); • System.out.println ("Account: " + acctNumber); • System.out.println ("Requested: " + fmt.format(amount)); • System.out.println ("Available: " + fmt.format(balance)); • } • else • balance = balance - amount; • return balance; • }

  36. Account.java (cont.) • // Adds interest to the account and returns the new balance. • publicdouble addInterest () • { • balance += (balance * RATE); • return balance; • } • // Returns the current balance of the account. • publicdouble getBalance () • { • return balance; • } • // Returns the account number. • publiclong getAccountNumber () • { • return acctNumber; • } • // Returns a one-line description of the account as a string. • public String toString () • { • return (acctNumber + "\t" + name + "\t" + fmt.format(balance)); } }

  37. BankAccounts.java • //******************************************************************** • // BankAccounts.java Author: Lewis and Loftus • // Driver to exercise the use of multiple Account objects. • //******************************************************************** • import Account; • publicclass BankAccounts • { • // 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); • acct1.deposit (25.85); • double smithBalance = acct2.deposit (500.00); • System.out.println ("Smith balance after deposit: " + • smithBalance);

  38. BankAccounts.java (cont.) • System.out.println ("Smith balance after withdrawal: " + • acct2.withdraw (430.75, 1.50)); • acct3.withdraw (800.00, 0.0); // exceeds balance • acct1.addInterest(); • acct2.addInterest(); • acct3.addInterest(); • System.out.println (); • System.out.println (acct1); • System.out.println (acct2); • System.out.println (acct3); • } • }

  39. Writing Classes • An aggregate object is an object that contains references to other objects • An Account object is an aggregate object because it contains a reference to a String object (that holds the owner's name) • An aggregate object represents a has-a relationship • A bank account has a name

  40. Writing Classes • Sometimes an object has to interact with other objects of the same type • For example, we might add two Rational number objects together as follows: r3 = r1.add(r2); • One object (r1) is executing the method and another (r2) is passed as a parameter • See Rational.java (page 197) • See RationalNumbers.java (page 196)

  41. Rational.java • //******************************************************************** • // Rational.java Author: Lewis and Loftus • // Represents one rational number with a numerator and denominator. • //******************************************************************** • publicclass Rational • { • privateint numerator, denominator; • // Sets up the rational number by ensuring a nonzero denominator • // and making only the numerator signed. • public Rational (int numer, int denom) • { • if (denom == 0) • denom = 1; • // Make the numerator "store" the sign • if (denom < 0) • { • numer = numer * -1; • denom = denom * -1; • } • numerator = numer; • denominator = denom; • reduce(); }

  42. Rational.java (cont.) • // Adds this rational number to the one passed as a parameter. • // A common denominator is found by multiplying the individual • // denominators. • public Rational add (Rational op2) • { • int commonDenominator = denominator * op2.getDenominator(); • int numerator1 = numerator * op2.getDenominator(); • int numerator2 = op2.getNumerator() * denominator; • int sum = numerator1 + numerator2; • returnnew Rational (sum, commonDenominator); • } • // Subtracts the rational number passed as a parameter from this • // rational number. • public Rational subtract (Rational op2) • { • int commonDenominator = denominator * op2.getDenominator(); • int numerator1 = numerator * op2.getDenominator(); • int numerator2 = op2.getNumerator() * denominator; • int difference = numerator1 - numerator2; • returnnew Rational (difference, commonDenominator); • }

  43. Rational.java (cont.) • // Multiplies this rational number by the one passed as a parameter. • public Rational multiply (Rational op2) • { • int numer = numerator * op2.getNumerator(); • int denom = denominator * op2.getDenominator(); • returnnew Rational (numer, denom); • } • // Divides this rational number by the one passed as a parameter • // by multiplying by the reciprocal of the second rational. • public Rational divide (Rational op2) • { • return multiply (op2.reciprocal()); • } • // Returns the reciprocal of this rational number. • public Rational reciprocal () • { • returnnew Rational (denominator, numerator); • } • // Returns the numerator of this rational number. • publicint getNumerator () • { return numerator; }

  44. Rational.java (cont.) • // Returns the denominator of this rational number. • publicint getDenominator () • { return denominator; } • // Determines if this rational number is equal to the one passed • // as a parameter. Assumes they are both reduced. • publicboolean equals (Rational op2) • { • return ( numerator == op2.getNumerator() && • denominator == op2.getDenominator() ); • } • // Returns this rational number as a string. • public String toString () • { • String result; • if (numerator == 0) • result = "0"; • else • if (denominator == 1) • result = numerator + ""; • else • result = numerator + "/" + denominator; • return result; }

  45. Rational.java (cont.) • // Reduces this rational number by dividing both the numerator • // and the denominator by their greatest common divisor. • privatevoid reduce () • { • if (numerator != 0) • { • int common = gcd (Math.abs(numerator), denominator); • numerator = numerator / common; • denominator = denominator / common; • } • } • // Computes and returns the greatest common divisor of the two • // positive parameters. Uses Euclid's algorithm. • privateint gcd (int num1, int num2) • { • while (num1 != num2) • if (num1 > num2) • num1 = num1 - num2; • else • num2 = num2 - num1; • return num1; • } }

  46. RationalNumbers.java • //******************************************************************** • // RationalNumbers.java Author: Lewis and Loftus • // Driver to exercise the use of multiple Rational objects. • //******************************************************************** • import Rational; • publicclass RationalNumbers • { • // Creates some rational number objects and performs various • // operations on them. • publicstaticvoid main (String[] args) • { • Rational r1 = new Rational (6, 8); • Rational r2 = new Rational (1, 3); • System.out.println ("First rational number: " + r1); • System.out.println ("Second rational number: " + r2);

  47. RationalNumbers.java (cont.) • if (r1.equals(r2)) • System.out.println ("r1 and r2 are equal."); • else • System.out.println ("r1 and r2 are NOT equal."); • Rational r3 = r1.add(r2); • Rational r4 = r1.subtract(r2); • Rational r5 = r1.multiply(r2); • Rational r6 = r1.divide(r2); • System.out.println ("r1 + r2: " + r3); • System.out.println ("r1 - r2: " + r4); • System.out.println ("r1 * r2: " + r5); • System.out.println ("r1 / r2: " + r6); • } • }

  48. Overloading Methods • Method overloading is the process of using the same method name for multiple methods • The signature of each overloaded method must be unique • The signature includes the number, type, and order of the parameters • The compiler must be able to determine which version of the method is being invoked by analyzing the parameters • The return type of the method is not part of the signature

  49. Version 1 Version 2 float tryMe (int x, float y) { return x*y; } float tryMe (int x) { return x + .375; } Invocation result = tryMe (25, 4.32) Overloading Methods

  50. Overloaded Methods • The println method is overloaded: println (String s) println (int i) println (double d) etc. • The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);

More Related