Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.

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.
Fields, Constructors, Methods
Written by: Dr. JJ Shepherd
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.
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
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.
Grouping Objects 1 Introduction to Collections.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
More about inheritance Exploring polymorphism 5.0.
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.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
Understanding class definitions Looking inside classes.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
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,
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
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.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
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.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Looking inside classes Conditional Statements Week 4.
Written by: Dr. JJ Shepherd
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,
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.
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
SOFTWARE AND PROGRAMMING 1
CSC 222: Object-Oriented Programming Fall 2015
Class definitions CITS1001 week 2.
Understanding class definitions
public class BankAccount{
Java Programming with BlueJ
Understanding class definitions
An Introduction to Java – Part II
COS 260 DAY 3 Tony Gauvin.
2 The anatomy of class definitions
Understanding class definitions
Collections and iterators
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
JAVA CLASSES.
COS 260 DAY 6 Tony Gauvin.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Collections and iterators
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

Understanding class definitions – Part II –

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered mutator and accessor methods conditional statements local variables string concatenation

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Reflecting on the ticket machines Their behaviour is inadequate in several ways: No checks on the amounts entered. No refunds. No checks for a sensible initialisation. How can we do better? We need more sophisticated behaviour.

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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Making choices if (perform some test) { Do these statements if the test gives true } else { Do these statements if the test gives false } ‘if’ keyword boolean condition to be tested actions if condition is true actions if condition is false ‘else’ keyword

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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Local variables Fields are one sort of variable. They store values through the life of an object. They are accessible throughout the class. Methods can include shorter-lived variables. They exist only as long as the method is being executed. They are only accessible from within the method.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Scope and life time The scope of a local variable is the block it is declared in. The lifetime of a local variable is the time of execution of the block it is declared in.

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

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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review 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 Review Objects can make decisions via conditional (if) statements. A true or false test allows one of two alternative courses of actions to be taken.