Download presentation
Presentation is loading. Please wait.
1
Understanding class definitions Looking inside classes
2
21/10/2004 Lecture 2: Understanding Class Definitions 2 Main concepts to be covered fields constructors methods parameters assignment statements conditional statements
3
21/10/2004 Lecture 2: Understanding Class Definitions 3 Ticket machines – an external view Exploring the behavior of a typical ticket machine. Use the naive-ticket-machine project. Machines supply tickets of a fixed price. How is that price determined? How is ‘money’ entered into a machine? How does a machine keep track of the money that is entered? How is a ticket provided?
4
21/10/2004 Lecture 2: Understanding Class Definitions 4 Resulting Fields
5
21/10/2004 Lecture 2: Understanding Class Definitions 5 Resulting Methods
6
21/10/2004 Lecture 2: Understanding Class Definitions 6 Ticket machines – an internal view Interacting with an object gives us clues about its behavior. Looking inside allows us to determine how that behavior is provided or implemented. Looking at the source code All Java classes have a similar-looking internal view.
7
21/10/2004 Lecture 2: Understanding Class Definitions 7 The Source Code
8
21/10/2004 Lecture 2: Understanding Class Definitions 8 Basic class structure public class TicketMachine { Inner part of the class omitted. } public class ClassName { Fields Constructors Methods } The outer wrapper of TicketMachine The contents of a class
9
21/10/2004 Lecture 2: Understanding Class Definitions 9 Comments/Documentation Comments make source code easier to read for humans. No effect on the functionality. Three sorts: // comment: single-line comments /* comments */: multiple-lines – more detail /** */: similar to previous, but used when documentation software is used.
10
21/10/2004 Lecture 2: Understanding Class Definitions 10 Fields Fields store values for an object. They are also known as instance variables. Use the Inspect option to view an object’s fields. Fields define the state of an object. public class TicketMachine { private int price; private int balance; private int total; Constructor and methods omitted. } private int price; visibility modifiertypevariable name
11
21/10/2004 Lecture 2: Understanding Class Definitions 11 Constructors Constructors initialize an object. Then assign the necessary memory to the created object They have the same name as their class. They store initial values into the fields. They often receive external parameter values for this. public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; }
12
21/10/2004 Lecture 2: Understanding Class Definitions 12 Passing data via parameters
13
21/10/2004 Lecture 2: Understanding Class Definitions 13 Parameters Parameter names inside a constructor or method are referred to as Formal Parameters. Parameter values provided from the outside are referred to as Actual Parameters. In the example: ticketCost is a formal parameter and 500 is an actual parameter.
14
21/10/2004 Lecture 2: Understanding Class Definitions 14 Space The ticketCost box in the object representation is only created when the constructor is executed. Extra temporarily storage is provided to store a value for ticketCost. This is called the constructor space or method space. Values can only be used during the execution.
15
21/10/2004 Lecture 2: Understanding Class Definitions 15 Scope and Lifetime The scope of a variable/parameter defines the section of the code from where it can be accessed. For instance variables this is the entire class. For parameters, this is the constructor or method that declares it. Trick: find the enclosing {}, this is the scope The lifetime of a variable/parameter describes how long the variable continues to exist before it is destroyed.
16
21/10/2004 Lecture 2: Understanding Class Definitions 16 Assignment Values are stored into fields (and other variables) via assignment statements: variable = expression; price = ticketCost; Both sides of the assignment should have the same type, e.g. int, double, String, … A variable stores a single value, so any previous value is lost.
17
21/10/2004 Lecture 2: Understanding Class Definitions 17 Accessor methods Methods implement the behavior of objects. Accessors provide information about an object. Methods have a structure consisting of a header and a body. The header defines the method’s signature. public int getPrice() The body encloses the method’s statements.
18
21/10/2004 Lecture 2: Understanding Class Definitions 18 Accessor methods public int getPrice() { return price; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier
19
21/10/2004 Lecture 2: Understanding Class Definitions 19 Mutator methods Have a similar method structure: header and body. Used to mutate (i.e., change) an object’s state. Achieved through changing the value of one or more fields. Typically contain assignment statements. Typically receive parameters.
20
21/10/2004 Lecture 2: Understanding Class Definitions 20 Mutator methods public void insertMoney(int amount) { balance += amount; } return type ( void ) method name parameter visibility modifier assignment statement field being changed
21
21/10/2004 Lecture 2: Understanding Class Definitions 21 Printing from methods public void printTicket() { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the balance. total += balance; // Clear the balance. balance = 0; }
22
21/10/2004 Lecture 2: Understanding Class Definitions 22 Output
23
21/10/2004 Lecture 2: Understanding Class Definitions 23 Reflecting on the ticket machines Their behavior is inadequate in several ways: No checks on the amounts entered. No refunds. No checks for a sensible initialization. How can we do better? We need more sophisticated behavior.
24
21/10/2004 Lecture 2: Understanding Class Definitions 24 Making choices public void insertMoney(int amount) { if(amount > 0) { balance += amount; } else { System.out.println("Use a positive amount: " + amount); }
25
21/10/2004 Lecture 2: Understanding Class Definitions 25 Making choices 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 } ‘if’ keyword boolean condition to be tested - gives a true or false result actions if condition is true actions if condition is false ‘else’ keyword
26
21/10/2004 Lecture 2: Understanding Class Definitions 26 Boolean Tests == : equality > : greater than < : less than =< : less or equal than >= : greater or equal than != : not equal
27
21/10/2004 Lecture 2: Understanding Class Definitions 27 Local variables Fields are one sort of variable. They store values through the life of an object. They are accessible throughout the class. Methods can include shorter-lived variables. They exist only as long as the method is being executed. They are only accessible from within the method.
28
21/10/2004 Lecture 2: Understanding Class Definitions 28 Local variables public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } A local variable No visibility modifier
29
21/10/2004 Lecture 2: Understanding Class Definitions 29 Review Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors initialize objects. Methods implement the behavior of objects. Constructors are methods which do not return anything.
30
21/10/2004 Lecture 2: Understanding Class Definitions 30 Review Fields, parameters and local variables are all variables. Fields persist for the lifetime of an object. Parameters are used to receive values into a constructor or method. Local variables are used for short-lived temporary storage.
31
21/10/2004 Lecture 2: Understanding Class Definitions 31 Review Objects can make decisions via conditional (if) statements. A true or false test allows one of two alternative courses of actions to be taken.
32
21/10/2004 Lecture 2: Understanding Class Definitions 32 Terms Instance variables Local variables Parameters Formal Parameters Actual Parameters Scope Lifetime Constructors Methods If-statement Assignment = += =,, !=, ==
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.