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.

Slides:



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

JAVA Revision Lecture Electronic Voting System Marina De Vos.
Understanding class definitions Looking inside classes 5.0.
Looking inside classes Fields, Constructors & Methods Week 3.
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Fields, Constructors, Methods
Written by: Dr. JJ Shepherd
Chapter 7: User-Defined Functions II
Lab 10: Bank Account II Transaction List, error checking & aggregation.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Grouping Objects 3 Iterators, collections and the while loop.
Objects First with Java A Practical Introduction using BlueJ
String Concatenation (operator overloading) 3.0.
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
More about inheritance Exploring polymorphism 5.0.
Understanding class definitions Looking inside classes.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Object Oriented Design: Identifying Objects
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.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
1 CSC 221: Computer Programming I Fall 2004 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
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.
More about inheritance Exploring polymorphism 5.0.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
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.
Looking inside classes Conditional Statements Week 4.
Written by: Dr. JJ Shepherd
Grouping objects Introduction to collections 5.0.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
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.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
SOFTWARE AND PROGRAMMING 1
CSC 222: Object-Oriented Programming Fall 2015
Class definitions CITS1001 week 2.
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Objects First with Java
Understanding class definitions
public class BankAccount{
Understanding class definitions
COS 260 DAY 3 Tony Gauvin.
2 The anatomy of class definitions
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
CS2011 Introduction to Programming I Selections (I)
Program Flow.
Classes CS 21a: Introduction to Computing I
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

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 initialization. How can we do better? –We need more sophisticated behavior. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

2 Making choices in everyday life If I have enough money left, then I will go out for a meal otherwise I will stay home and watch a movie.

3 Making a choice in everyday life if(I have enough money left) { go out for a meal; } else { stay home and watch a movie; }

4 Making choices in Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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 } ‘if’ keyword boolean condition to be tested actions if condition is true actions if condition is false ‘else’ keyword

5 Making a choice in the ticket machine Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public void insertMoney(int amount) { if(amount > 0) { balance = balance + amount; } else { System.out.println( "Use a positive amount: " + amount); }

6 How do we write 'refundBalance'? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 Variables – a recap Fields are one sort of variable. –They store values through the life of an object. –They are accessible throughout the class. Parameters are another sort of variable: –They receive values from outside the method. –They help a method complete its task. –Each call to the method receives a fresh set of values. –Parameter values are short lived. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 Local variables Methods can define their own, local variables: –Short lived, like parameters. –The method sets their values – unlike parameters, they do not receive external values. –Used for ‘temporary’ calculation and storage. –They exist only as long as the method is being executed. –They are only accessible from within the method.

9 Scope highlighting

10 Scope and lifetime Each block defines a new scope. –Class, method and statement. Scopes may be nested: –statement block inside another block inside a method body inside a class body. Scope is static (textual). Lifetime is dynamic (runtime). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 Local variables Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } A local variable No visibility modifier

12 Scope and lifetime The scope of a local variable is the block in which it is declared. The lifetime of a local variable is the time of execution of the block in which it is declared. The scope of a field is its whole class. The lifetime of a field is the lifetime of its containing object.

13 Review (1) Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors initialize objects – particularly their fields. Methods implement the behavior of objects. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 Review (2) 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15 Review (3) Methods have a return type. void methods do not return anything. non-void methods return a value. non-void methods have a return statement.

16 Review (4) ‘Correct’ behavior often requires objects to make decisions. Objects can make decisions via conditional (if) statements. A true-or-false test allows one of two alternative courses of actions to be taken. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling