180 likes | 280 Views
SOFTWARE AND PROGRAMMING 1. Lecture 3: 26.01.11 Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site http:// www.dcs.bbk.ac.uk /~mirkin/sp109 Course Assistant: Lab/WebCT/Tests/Assignments : Mr Martin O’Shea E-mail: martin@dcs.bbk.ac.uk.
E N D
SOFTWARE AND PROGRAMMING 1 Lecture 3: 26.01.11 Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site http://www.dcs.bbk.ac.uk/~mirkin/sp109 Course Assistant: Lab/WebCT/Tests/Assignments: Mr Martin O’Shea E-mail: martin@dcs.bbk.ac.uk
Test1 9/2/11 awareness Test1 subjects: • Variable: type, declaration, initialisation • Expression • Loop for • Loop while • if( )… else if( ) ... else • Simple class structure • Strings • Simple methods
TM with BlueJ and with JDK • Ticket Machine without and with a menu or main method • Method: mutator (setter), accessor (getter) • Constructor • Calling a method • Working over a menu with if/else and while
Ticket Machine Imitates issuing flat-rate tickets • Three variables needed: price – for ticket price balance – for the user’s money total – for money received from customers • Three accessor methods for getting each of the variables • Three mutator methods for -entering customer’s money -issuing a ticket -gettingrefunded
Ticket Machine (1) /* * TicketMachine models a ticket machine that issues * flat-fare tickets. */ public class TM{ private int price; private int balance; private int total; public TM(int ticketCost) //constructor { price = ticketCost; balance = 0; total = 0; } public int getPrice() { return price; } public int getBalance() { return balance; } public int getTotal() { return total; } // see next page for continuation
Ticket Machine (2) // TicketMachine’s continuation public void insertMoney(int amount) { if(amount > 0) balance = balance + amount; else { System.out.println(“This is not positive "+ amount); } } public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } // continued on the next page
Blue-J Ticket Machine (3) public void printTicket() { if(balance >= price) { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " pence."); System.out.println("##################"); System.out.println(); total = total + price; // Update the total balance = balance - price; // Update the balance } else { System.out.println("You must insert at least: " + (price - balance) + " more pence."); } } // further methods to follow
A comment I consider printTicket()method as somewhat inconsistent: printing (an accessing activity) is mixed up with changing the balance and total (mutating activities) Any suggestions?
Accessor methods • Accessors provide information about the state of 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.
Accessor methods return type visibility modifier method name parameter list (empty) public int getPrice() { return price; } return statement start and end of method’s body (block)
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.
Mutator methods return type (void) visibility modifier method name parameter public void insertMoney(int amount) { balance = balance + amount; } field being changed assignment statement
Questions • How many methods are in TicketMachine? - five • If there is any syntactic difference between a method and constructor? – two: absence of the output type, compulsory name • Which of the methods are accessors and which are mutators? - Two in the beginning are accessors, three in the end are mutators
Organising a menu //--- ‘menu’ method for choosing action----------- public static int menu() { TextIO.putln(); TextIO.putln("Please enter a number: "); TextIO.putln(" 0 - to quit "); TextIO.putln(" 1 - to get a ticket price "); TextIO.putln(" 2 - to put money and get a ticket "); TextIO.putln(" 3 - to get refunded "); TextIO.putln(" 4 - to get statistics "); int action=TextIO.getInt(); return action; }
Main method using menu: right? • public static void main(String[ ] args){ • int MeItem=1; • while (MeItem!=0){ • MeItem=menu();// a method • if (MeItem==1){ • int pp=getPrice(); // a method • System.out.println("The ticket price is "+pp+" pence ");} • else if (MeItem==2) { • System.out.println("Please key in the money inserted, in pence"); • int money.insert=TextIO.getInt(); • insertMoney(money_insert); • printTicket();} • else if (MeItem==3) { • int refund=refundBalance(); • System.out.println("Please take your refund " + refund);} • else • {int tt=getTotal(); • int bb=getBalance(); • System.out.println("The total for tickets: "+tt);} • }//end of while for choosing action • } //end of main
Main method using menu: WRONG! • WHY? • There are some deficiencies in the program: no difference between option 4 and !(1 | 2 | 3) – but this wouldn’t make the class fail • Because main method is static, but other methods and variables are not: • A Java Commandment: You shalt not utilise non static items in a static method! • What to do? Either • Make other methods and variables static too; this works but may be at odds with flexibility considerations • Introduce an instance of the class into main method – make the constructor working – and use all methods and variables from the instance
Main method using menu: Right (I) public static void main(String[ ] args){ System.out.println("Please enter a ticket price "); int pi=TextIO.getInt(); TM atm=new TM(pi); int MeItem=1; while (MeItem!=0){ MeItem=menu();// a method if (MeItem==1){ int pp=atm.getPrice(); // a method System.out.println("Ticket price is "+pp);} // continued next page
Main method using menu: Right (II) else if (MeItem==2) { System.out.println(“Key in the money inserted in pence"); int money_insert=TextIO.getInt(); atm.insertMoney(money_insert); atm.printTicket();} else if (MeItem==3) { int refund=atm.refundBalance(); System.out.println("Please take your refund " + refund);} else {int tt=atm.getTotal(); int bb=atm.getBalance(); System.out.println("The total for tickets: "+tt);} }//end of loop while for choosing action } //end of main