Looking inside classes Fields, Constructors & Methods Week 3.

Slides:



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

Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Understanding class definitions Looking inside classes 5.0.
Fields, Constructors, Methods
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Road Map Introduction to object oriented programming. Classes
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Enhancing classes Visibility modifiers and encapsulation revisited
Understanding class definitions Looking inside classes 3.0.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
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.
Understanding class definitions Looking inside classes.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
Object Oriented Design: Identifying Objects
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
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,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 8: User-Defined Classes and ADTs
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
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 3 Introduction to Classes and Objects Definitions Examples.
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.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Understanding class definitions Exploring source code 6.0.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
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.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
SOFTWARE AND PROGRAMMING 1
Re-Intro to Object Oriented Programming
CSC 222: Object-Oriented Programming Fall 2015
Class definitions CITS1001 week 2.
Classes (Part 1) Lecture 3
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
More on Classes and Objects
Implementing Classes Chapter 3.
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
JAVA CLASSES.
Classes CS 21a: Introduction to Computing I
COS 260 DAY 6 Tony Gauvin.
Dr. R Z Khan Handout-3 Classes
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Chapter 4 Test Review First day
Presentation transcript:

Looking inside classes Fields, Constructors & Methods Week 3

Fields Constructors Methods Parameters Printing Java Class Definitions CONCEPTS COVERED

Exploring the behavior of a 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? Java Class Definitions TICKET MACHINES – AN EXTERNAL VIEW

BlueJ Demonstration A first look at the naive-ticket-machine project

TicketMachine int price int balance int total int getPrice() int getBalance() void insertMoney(int amount) void printTicket()

Interacting with an object gives us clues about its behavior. Looking inside the objects class definition (source code) allows us to determine how that behavior is provided or implemented. All Java classes have a similar-looking internal view/structure. Java Class Definitions SOURCE CODE – AN INTERNAL VIEW

Java Class Definitions BASIC STRUCTURE OF A JAVA CLASS public class ClassName { Fields Constructors Methods } public class TicketMachine { Inner part of the class omitted. }

Java Class Definitions FIELDS Fields store values for an object. They are also known as instance variables, and/or attributes. Use Inspect in BlueJ to view an objects fields. The values held in an objects fields constitute the state of that object.

Java Class Definitions STRUCTURE OF FIELDS IN A JAVA CLASS public class TicketMachine { private int price; private int balance; private int total; Constructor and methods omitted. } private int price; visibility modifier data type variable name

Java Class Definitions CONSTRUCTORS Constructors initialize an object They have the same name as their class They do not have a return type They can store initial values into the fields They often receive external parameter values that are used to set initial field values

Methods The Java language is built from many classes, each with its own inbuilt methods. Much of programming in Java is about using inbuilt methods as well as being able to create your own methods. The method header or signature describes the methods: –accessibility modifiers, public or private, –whether or not it returns a value and if so what type, –the name of the method, starting with a lowercase letter, –input needed (formal parameters) for the method to fulfill its task.

Encapsulation Normally we make fields that hold the objects data private While the methods that can access those fields are made public This way we provide controlled access to the data, thus protecting it This is called encapsulation

Java Class Definitions STRUCTURE OF A CLASS CONSTRUCTOR public class TicketMachine { private int price; private int balance; private int total; public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; }

Methods implement the behavior of objects. Accessors provide information about an object. Methods have a structure consisting of a header and a body. Method header defines the methods access modifier, return type and signature. public int getPrice() Method body encloses a methods statements. Java Class Definitions ACCESSOR METHODS

Java Class Definitions 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

Mutators have a standard method structure of method header and method body. Mutators are used to mutate (i.e. change) an objects state. Mutators achieve this through changing the value of one or more object fields (attributes). –Typically contain assignment statements. –Typically receive parameters. Java Class Definitions MUTATOR METHODS

Java Class Definitions MUTATOR METHODS public void insertMoney(int amount) { balance = balance + amount; } return type ( void ) method name parameter visibility modifier assignment statement field being changed start and end of method body (block)

Java Class Definitions PASSING DATA VIA PARAMETERS

Java Class Definitions 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; }

Required Reading Objects First With Java – A Practical Introduction using BlueJ Related reading for this lecture Chapter 2 pp (Constructors & Methods) Reading for next weeks lecture Chapter 2 pp (Choices)