software and programming 1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
SOFTWARE AND PROGRAMMING 1 PowerPoint Presentation
Download Presentation
SOFTWARE AND PROGRAMMING 1

Loading in 2 Seconds...

play fullscreen
1 / 18

SOFTWARE AND PROGRAMMING 1 - PowerPoint PPT Presentation


  • 81 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'SOFTWARE AND PROGRAMMING 1' - zaide


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
software and programming 1

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 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
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
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
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
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
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
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
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 methods1
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
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 methods1
Mutator methods

return type (void)

visibility modifier

method name

parameter

public void insertMoney(int amount)

{

balance = balance + amount;

}

field being changed

assignment statement

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