Understanding class definitions Looking inside classes.

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.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Fields, Constructors, Methods
Written by: Dr. JJ Shepherd
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7: User-Defined Functions II
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Understanding class definitions Looking inside classes 3.0.
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.
Writing Classes (Chapter 4)
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.
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.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
OOP Basics Classes & Methods (c) IDMS/SQL News
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.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
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.
Java Programming: Guided Learning with Early Objects
Intro To Classes Review
Understanding class definitions
public class BankAccount{
Java Programming with BlueJ
Understanding class definitions
Classes, Encapsulation, Methods and Constructors (Continued)
COS 260 DAY 3 Tony Gauvin.
CHAPTER 6 GENERAL-PURPOSE METHODS
2 The anatomy of class definitions
Defining Classes and Methods
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)
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
JAVA CLASSES.
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

Understanding class definitions Looking inside classes

21/10/2004 Lecture 2: Understanding Class Definitions 2 Main concepts to be covered fields constructors methods parameters assignment statements conditional statements

21/10/2004 Lecture 2: Understanding Class Definitions 3 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? How is a ticket provided?

21/10/2004 Lecture 2: Understanding Class Definitions 4 Resulting Fields

21/10/2004 Lecture 2: Understanding Class Definitions 5 Resulting Methods

21/10/2004 Lecture 2: Understanding Class Definitions 6 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. Looking at the source code All Java classes have a similar-looking internal view.

21/10/2004 Lecture 2: Understanding Class Definitions 7 The Source Code

21/10/2004 Lecture 2: Understanding Class Definitions 8 Basic class structure public class TicketMachine { Inner part of the class omitted. } public class ClassName { Fields Constructors Methods } The outer wrapper of TicketMachine The contents of a class

21/10/2004 Lecture 2: Understanding Class Definitions 9 Comments/Documentation Comments make source code easier to read for humans. No effect on the functionality. Three sorts: // comment: single-line comments /* comments */: multiple-lines – more detail /** */: similar to previous, but used when documentation software is used.

21/10/2004 Lecture 2: Understanding Class Definitions 10 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. } private int price; visibility modifiertypevariable name

21/10/2004 Lecture 2: Understanding Class Definitions 11 Constructors Constructors initialize an object. Then assign the necessary memory to the created 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; }

21/10/2004 Lecture 2: Understanding Class Definitions 12 Passing data via parameters

21/10/2004 Lecture 2: Understanding Class Definitions 13 Parameters Parameter names inside a constructor or method are referred to as Formal Parameters. Parameter values provided from the outside are referred to as Actual Parameters. In the example: ticketCost is a formal parameter and 500 is an actual parameter.

21/10/2004 Lecture 2: Understanding Class Definitions 14 Space The ticketCost box in the object representation is only created when the constructor is executed. Extra temporarily storage is provided to store a value for ticketCost. This is called the constructor space or method space. Values can only be used during the execution.

21/10/2004 Lecture 2: Understanding Class Definitions 15 Scope and Lifetime The scope of a variable/parameter defines the section of the code from where it can be accessed. For instance variables this is the entire class. For parameters, this is the constructor or method that declares it. Trick: find the enclosing {}, this is the scope The lifetime of a variable/parameter describes how long the variable continues to exist before it is destroyed.

21/10/2004 Lecture 2: Understanding Class Definitions 16 Assignment Values are stored into fields (and other variables) via assignment statements: variable = expression; price = ticketCost; Both sides of the assignment should have the same type, e.g. int, double, String, … A variable stores a single value, so any previous value is lost.

21/10/2004 Lecture 2: Understanding Class Definitions 17 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.

21/10/2004 Lecture 2: Understanding Class Definitions 18 Accessor methods public int getPrice() { return price; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier

21/10/2004 Lecture 2: Understanding Class Definitions 19 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.

21/10/2004 Lecture 2: Understanding Class Definitions 20 Mutator methods public void insertMoney(int amount) { balance += amount; } return type ( void ) method name parameter visibility modifier assignment statement field being changed

21/10/2004 Lecture 2: Understanding Class Definitions 21 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("##################"); System.out.println(); // Update the total collected with the balance. total += balance; // Clear the balance. balance = 0; }

21/10/2004 Lecture 2: Understanding Class Definitions 22 Output

21/10/2004 Lecture 2: Understanding Class Definitions 23 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.

21/10/2004 Lecture 2: Understanding Class Definitions 24 Making choices public void insertMoney(int amount) { if(amount > 0) { balance += amount; } else { System.out.println("Use a positive amount: " + amount); }

21/10/2004 Lecture 2: Understanding Class Definitions 25 Making choices 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 } ‘if’ keyword boolean condition to be tested - gives a true or false result actions if condition is true actions if condition is false ‘else’ keyword

21/10/2004 Lecture 2: Understanding Class Definitions 26 Boolean Tests == : equality > : greater than < : less than =< : less or equal than >= : greater or equal than != : not equal

21/10/2004 Lecture 2: Understanding Class Definitions 27 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.

21/10/2004 Lecture 2: Understanding Class Definitions 28 Local variables public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } A local variable No visibility modifier

21/10/2004 Lecture 2: Understanding Class Definitions 29 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. Constructors are methods which do not return anything.

21/10/2004 Lecture 2: Understanding Class Definitions 30 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.

21/10/2004 Lecture 2: Understanding Class Definitions 31 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.

21/10/2004 Lecture 2: Understanding Class Definitions 32 Terms Instance variables Local variables Parameters Formal Parameters Actual Parameters Scope Lifetime Constructors Methods If-statement Assignment = += =,, !=, ==