180 likes | 311 Views
Understanding Class Definitions II Ticket Machine Improvements. Administrivia. Assigned reading: Starting Out With Java 5 , pp 1-28 HWxx (due Wed, 1/xx): p 29-30, pbms 1-14 Labxx (due Fri, 1/xx): see Blackboard. Demo. click to activate. Reflecting on the ticket machines.
E N D
Understanding Class Definitions IITicket Machine Improvements
Administrivia Assigned reading: Starting Out With Java 5, pp 1-28HWxx (due Wed, 1/xx): p 29-30, pbms 1-14Labxx (due Fri, 1/xx): see Blackboard
Demo click to activate
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.
B Smith: 8/28/06: Stopped here with Sect 01 Making choices public void insertMoney(int amount) { if(amount > 0) { balance = balance + amount; } else { System.out.println("Use a positive amount: " + amount); } }
Making choices boolean condition to be tested ‘if’ keyword actions if condition is true if(perform some test) { Do these statements if the test gave a true result } else { Do these statements if the test gave a false result } actions if condition is false ‘else’ keyword
B Smith: Stopped here on 8/28/06 w/ night class. Used notes sparingly, worked from ticket-machine example. 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.
Local variables A local variable public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } No visibility modifier
what we know • 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.
what we know • 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.
what we know • Objects can make decisions via conditional (if) statements. • A true or false test allows one of two alternative courses of actions to be taken.
B Smith: From here onward was created Sun 8/27, and not given to M130-01/03, but was given to night class Types and Variables • In Java, every value has a type • “Hello World” has type String • System.out has type PrintStream • 13 has type int • Type tells you what you can do with the values
Variables • You may want to use a value later on in your program • A “variable” • stores values for use at a later time • a storage location • has a type, a name, and contents • stores the type used in original decl
Variable Declarations • Variables must be “declared” prior to usage • String greeting = “Hello, World!”; • PrintStream printer = System.out; • int luckyNumber = 21;
Variable Names • Identifiers for variables, methods and classes • composed of letters, digits, and underscore character • can’t use other symbols as identifiers • can’t have spaces • can’t use reserved words • are case sensitive
Identifier Naming Conventions • variables and method names should start with a lower case letter String employeeName; • Class names should start with uppercase letter public class TicketMachine{…}
The Assignment Operator • Use the assignment operator to change the value of a variable • Initialize variables before using: int luckyNumber; luckyNumber = 21; System.out.println(luckyNumber);
Method Parameters and Return Values • Methods can accept inputs and deliver outputs • Inputs are called “parameters”. void horizontalMove(int dist); • Outputs return information to you int getPrice(); //returns price