Understanding class definitions Looking inside classes 3.0.

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
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Lab 10: Bank Account II Transaction List, error checking & aggregation.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
Improving structure with inheritance
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 Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Objects First with Java A Practical Introduction using BlueJ
String Concatenation (operator overloading) 3.0.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Grouping Objects 1 Introduction to Collections.
Object Interaction 1 Creating cooperating objects.
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.
Object interaction Creating cooperating objects 5.0.
Object interaction Creating cooperating objects 5.0.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
Object Interaction 2 Creating cooperating objects.
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
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
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.
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.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
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.
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.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
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.
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
Class definitions CITS1001 week 2.
Objects and Classes CITS1001 week 1.
Objects First with Java A Practical Introduction using BlueJ
Objects First with Java
Understanding class definitions
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
Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
F II 3. Classes and Objects Objectives
Objects First with Java Creating cooperating objects
COS 260 DAY 4 Tony Gauvin.
Objects First with Java Creating cooperating objects
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.
Presentation transcript:

Understanding class definitions Looking inside classes 3.0

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

Time for study A full-time student week is 40 hours!

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Examination Course work: 3 assignments Programming test in lab; at computer practical task exam conditions Programming test MUST be passed (pass/fail mark; hurdle requirement) Final mark calculated from coursework marks

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Why BlueJ Why Java? Why BlueJ?

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling And, by the way: Greenfoot

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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Ticket machines Demo

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 behaviour. Looking inside allows us to determine how that behaviour 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 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

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; Further details omitted. } private int price; visibility modifier type variable name

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Constructors Constructors initialise 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 behaviour 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 method name parameter list (empty) start and end of method body (block) return statement visibility modifier

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Test What is wrong here? public class CokeMachine { private price; public CokeMachine() { price = 300 } public int getPrice { return Price; } (there are five errors!)

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Test What is wrong here? public class CokeMachine { private price; public CokeMachine() { price = 300 } public int getPrice { return Price; } } ; () int p (there are five errors!)

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. mutate 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) { balance = balance + amount; } return type method name parameter visibility modifier assignment statement field being mutated

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("##################"); System.out.println(); // Update the total collected with the balance. total = 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 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.