Presentation is loading. Please wait.

Presentation is loading. Please wait.

SOFTWARE AND PROGRAMMING 1

Similar presentations


Presentation on theme: "SOFTWARE AND PROGRAMMING 1"— Presentation transcript:

1 SOFTWARE AND PROGRAMMING 1
Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site Course Assistant: Lab/WebCT/Tests/Assignments: Mr Martin O’Shea

2 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

3 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

4 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 -getting refunded

5 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

6 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

7 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(); 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

8 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?

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

10 Accessor methods public int getPrice() { return price; } return type
visibility modifier method name parameter list (empty) public int getPrice() { return price; } return statement start and end of method’s body (block)

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

12 Mutator methods public void insertMoney(int amount) {
return type (void) visibility modifier method name parameter public void insertMoney(int amount) { balance = balance + amount; } field being changed assignment statement

13 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

14 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; }

15 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

16 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

17 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

18 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


Download ppt "SOFTWARE AND PROGRAMMING 1"

Similar presentations


Ads by Google