Understanding class definitions

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.
Fields, Constructors, Methods
Written by: Dr. JJ Shepherd
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.
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.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
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.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
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.
OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes.
Looking inside classes Conditional Statements Week 4.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
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.
SOFTWARE AND PROGRAMMING 1
CSC 222: Object-Oriented Programming Fall 2015
CSC 222: Object-Oriented Programming Fall 2017
Class definitions CITS1001 week 2.
Creating Your OwnClasses
COS 260 DAY 19 Tony Gauvin.
Objects First with Java
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
Objects First with Java A Practical Introduction using BlueJ
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
JAVA CLASSES.
Objects First with Java A Practical Introduction using BlueJ
Classes CS 21a: Introduction to Computing I
COS 260 DAY 6 Tony Gauvin.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

Understanding class definitions Looking inside classes 1.0

Main concepts to be covered fields constructors methods parameters assignment statements conditional statements Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Ticket machines – an external view Exploring the behavior of a typical ticket machine. Use the naive-ticket-machine project. Machines supply tickets of a fixed price. How is that price determined? How is ‘money’ entered into a machine? How does a machine keep track of the money that is entered? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Ticket machines – an internal view Interacting with an object gives us clues about its behavior. Looking inside allows us to determine how that behavior is provided or implemented. All Java classes have a similar-looking internal view. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Basic class structure The outer wrapper of TicketMachine public class TicketMachine { Inner part of the class omitted. } public class ClassName { Fields Constructors Methods } The contents of a class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Fields Fields store values for an object. They are also known as instance variables. Use the Inspect option to view an object’s fields. Fields define the state of an object. public class TicketMachine { private int price; private int balance; private int total;   Constructor and methods omitted. } visibility modifier type variable name private int price; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Constructors Constructors initialize an object. They have the same name as their class. They store initial values into the fields. They often receive external parameter values for this. public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Passing data via parameters Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Assignment Values are stored into fields (and other variables) via assignment statements: variable = expression; price = ticketCost; A variable stores a single value, so any previous value is lost. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Accessor methods Methods implement the behavior of objects. Accessors provide information about 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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

Mutator methods public void insertMoney(int amount) { return type (void) visibility modifier method name parameter public void insertMoney(int amount) { balance += amount; } assignment statement field being changed Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Printing from methods public void printTicket() { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println();   // Update the total collected with the balance. total += balance; // Clear the balance. balance = 0; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

Making choices public void insertMoney(int amount) { if(amount > 0) { 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 boolean condition to be tested - gives a true or false result ‘if’ keyword actions if condition is true if(perform some test) { Do the statements here if the test gave a true result } else { Do the statements here if the test gave a false result actions if condition is false ‘else’ keyword 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

Local variables A local variable public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } 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 initialize objects. Methods implement the behavior 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling