1 / 18

Understanding Class Definitions II Ticket Machine Improvements

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.

pia
Télécharger la présentation

Understanding Class Definitions II Ticket Machine Improvements

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. Understanding Class Definitions IITicket Machine Improvements

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

  3. Demo click to activate

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

  5. 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); } }

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

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

  8. Local variables A local variable public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } No visibility modifier

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

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

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

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

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

  14. Variable Declarations • Variables must be “declared” prior to usage • String greeting = “Hello, World!”; • PrintStream printer = System.out; • int luckyNumber = 21;

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

  16. 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{…}

  17. 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);

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

More Related