CS 1054 Lecture 3 Chapter 2 Understanding class




















- Slides: 20
CS 1054: Lecture 3, Chapter 2 Understanding class definitions 1. 0
Example of a Ticket Machine Function: Prints a ticket when a customer inserts the correct money for their fare. Note: A ticket machine may offer tickets of more than one type from which a customer chooses one.
Storing Information … - - Price of the ticket Balance (amount of money entered by customer) Total (amount of money collected by the machine so far)
Operations on the machine n n Get the price of the ticket? What is the balance so far? Insert Money What is the total amount of money collected?
public class Ticket. Machine { private int price; private int balance; private int total; public Ticket. Machine(int ticket. Cost) { price = ticket. Cost; balance = 0; total = 0; } public int get. Price() { return price; } public int get. Balance() { return balance; } public void insert. Money(int amount) { balance += amount; } } public void print () { total += balance; balance =0; }
Basic Elements n Here are three basic elements of class definitions: public class Class. Name { Fields Constructors Methods } The contents of a class
Methods n Methods have two parts: n n a header and a body Here is an example header /** * Return the price of a ticket */ public int get. Price() n
Types of Methods n Accessor Methods n n Methods that simply return values from the object Mutator Methods n Methods that change the state of the object
Accessor methods return type visibility modifier method name parameter list (empty) get. Price() public int { return price; } start and end of method body (block) return statement
Mutator methods visibility modifier return type (void) method name parameter public void insert. Money(int amount) { assignment statement balance += amount; } field being changed
Method vs. Constructor You can tell a method apart from a constructor from the signature for each public Ticket. Machine( int ticket. Cost ) public int get. Price() n n n The constructor can have no return type The constructor must match the name of the class
Local variables n Fields are one sort of variable. n n n They store values through the life of an object. They are accessible throughout the class. Methods can include shorter-lived variables. n n They exist only as long as the method is being executed. They are only accessible from within the method.
Local variables A local variable No visibility modifier public int refund. Balance() { int amount. To. Refund; amount. To. Refund = balance; balance = 0; return amount. To. Refund; }
Scope n n n The scope of a variable means where the variable can be accessed The scope of a parameter is only in the method that it has been passed to The scope of an instance variable is the entire class
Lifetime n n n The lifetime of a variable means how long a variable can be accessed The lifetime for a parameter to a method is only the duration of the method. The lifetime for an instance variable is the same as for the object
Assignment n n n Values are stored into fields (and other variables) via assignment statements: n variable = expression; n price = ticket. Cost; A variable stores a single value, so any previous value is lost. variable = variable + expression; Can be shortened to: variable += expression;
Examples of assignment int A; int B; A = 10; B = 20; A = B; What will be the value of A? What will be the value of B? A = A + B; What will be the value of A? What will be the Value of B? A -= B; What will be the value of A? What will be the value of B? A *= B; What will be the value of A? What will be the value of B?
Reflecting on the ticket machines n Their behavior is inadequate in several ways: n n No checks on the amounts entered. No refunds. No checks for a sensible initialization. How can we do better? n We need more sophisticated behavior.
Making choices public void insert. Money(int amount) { if(amount > 0) { balance += amount; } else { System. out. println("Use a positive amount: " + amount); } }
Making choices ‘if’ keyword boolean condition to be tested - gives a true or false result actions if condition is true if(perform some test) { Do the statements here if the test gave a true result } else { Do the statements here if the test gave a false result } ‘else’ keyword actions if condition is false