SOFTWARE AND PROGRAMMING 1

Slides:



Advertisements
Similar presentations
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
Advertisements

Understanding class definitions Looking inside classes 5.0.
Looking inside classes Fields, Constructors & Methods Week 3.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (except 11& ) Lab: B43, MB321, MB536 6:00-7:30 (from ) [each student must have obtained.
Lab 10: Bank Account II Transaction List, error checking & aggregation.
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Access to Names Namespaces, Scopes, Access privileges.
Understanding class definitions Looking inside classes 3.0.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 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.
Understanding class definitions Looking inside classes.
UML Basics & Access Modifier
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Writing Classes (Chapter 4)
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
Classes CS 21a: Introduction to Computing I First Semester,
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald.
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (except 11& ) Lab: B43, MB321, MB536 6:00-7:30 (from ) [each student must have obtained.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
1 CSC 221: Computer Programming I Fall 2004 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (except 11& ) Lab: B43, MB321, MB536 6:00-7:30 (from ) [each student must have obtained.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: (from ) Ms Mihaela Cocea Room London Knowledge Lab Emerald.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
By Mr. Muhammad Pervez Akhtar
Looking inside classes Conditional Statements Week 4.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
SOFTWARE AND PROGRAMMING 1 Lecture 4: Ticketing machine details: Constructor, method, static/instance variables Instructor: Prof. Boris Mirkin
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
Understanding class definitions Exploring source code 6.0.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
SOFTWARE AND PROGRAMMING 1 Advert : NO TEST1 on 7/02: TEST1 will be 14/02 Lab: SH131, BBK536 6:00-7:30 (from ) [each student must have obtained.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Re-Intro to Object Oriented Programming
CSC 222: Object-Oriented Programming Fall 2015
CSC 222: Object-Oriented Programming Fall 2017
Class definitions CITS1001 week 2.
Module Road Map Refactoring Why Refactoring? Examples
Yanal Alahmad Java Workshop Yanal Alahmad
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Understanding class definitions
public class BankAccount{
Java Programming with BlueJ
Understanding class definitions
Overview of Eclipse Lectures
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
COS 260 DAY 3 Tony Gauvin.
CHAPTER 6 GENERAL-PURPOSE METHODS
2 The anatomy of class definitions
More on Classes and Objects
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Namespaces, Scopes, Access privileges
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
JAVA CLASSES.
Classes CS 21a: Introduction to Computing I
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

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

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

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

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