UNDERSTANDING CLASS DEFINITIONS CITS1001

Similar documents
public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Today s Agenda. Quick Review

Understanding class definitions

Ticket Machine Project(s)

CSC 222: Object-Oriented Programming. Fall 2015

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CSCI 161 Introduction to Computer Science

Chapter 4 Defining Classes I

If too much money is inserted the machine takes it all - no refund. If there isn't enough money inserted, it still prints out the ticket.

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CSC 222: Object-Oriented Programming. Fall 2017

Chapter 6 Introduction to Defining Classes

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Lab: PiggyBank. Defining objects & classes

Object Oriented Design: Identifying Objects

CS112 Lecture: Defining Instantiable Classes

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Scope of this lecture

Scope of this lecture. Repetition For loops While loops

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

OBJECT INTERACTION CITS1001

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CPS122 Lecture: Defining a Class

ECE 122. Engineering Problem Solving with Java

CITS1001 week 6 Libraries

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS112 Lecture: Working with Numbers

Defining Classes and Methods

G52CPP C++ Programming Lecture 9

Grouping objects. Main concepts to be covered

class objects instances Fields Constructors Methods static

Objects First with Java

OBJECT INTERACTION. CITS1001 week 3

EECS168 Exam 3 Review

Slide 1 CS 170 Java Programming 1

MID-SEMESTER TEST 2014

CITS1001 week 4 Grouping objects

Classes, interfaces, & documentation. Review of basic building blocks

Some miscellaneous concepts

Encapsulation. Mason Vail Boise State University Computer Science

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

CPS122 Lecture: Detailed Design and Implementation

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

User Interface Programming OOP/Java Primer. Step 3 - documentation

Inheritance. Exploring Polymorphism. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Java and OOP. Part 2 Classes and objects

Robots. Byron Weber Becker. chapter 6

CSI33 Data Structures

Methods and Data (Savitch, Chapter 5)

Week 3 Classes and Objects

CPS122 Lecture: Introduction to Java

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Objects as a programming concept

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 3

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

The Object Oriented Paradigm

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

CSCE 156 Computer Science II

Robots. Byron Weber Becker. chapter 6

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

B-Trees. Based on materials by D. Frey and T. Anastasio

Initial Coding Guidelines

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

SELECTION. (Chapter 2)

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Instance Method Development Demo

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

Chapter 4. Defining Classes I

Software and Programming 1

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

The first program: Little Crab

BM214E Object Oriented Programming Lecture 8

Project 1 Balanced binary

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Lesson 12: OOP #2, Accessor Methods (W03D4)

Core Competency DOO (Design Object-Oriented)

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

CSIS 10B Lab 2 Bags and Stacks

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

Main loop structure. A Technical Support System. The exit condition. Main loop body

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CS 302: Introduction to Programming in Java. Lecture 15

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Chapter 4: Writing Classes

Foundations of object orientation

Storing Data in Objects

Objects and Classes Lecture 2

Objects First with Java A Practical Introduction using BlueJ

Classes. Brahm Capoor

Transcription:

UNDERSTANDING CLASS DEFINITIONS CITS1001

Main concepts to be covered Fields / Attributes Constructors Methods Parameters Source ppts: Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling

Source Code In Java, classes are defined by text files of source code Source code is designed to be human readable and machine readable Source code must specify everything about how each of the objects of a class behave Turn to the first code examples in the handouts Java Class File: Student.java Q: Where does the name of the class appear?

A very important point Program code is designed to be human readable Familiar words are used for programming constructs (if, else, while, repeat, for) Indented format is similar to paragraphs and sections in text Meaningful variable names suggest what is intended (eg. price, mark, studentname) AND program code is also executed by a computer The computer will do exactly what it is TOLD to do The RULES of the language determine EXACTLY what happens when the program is run THE COMPUTER DOES NOT KNOW WHAT YOU INTENDED THE PROGRAM TO DO

Programming can be difficult at first. It is annoying when your program doesn t work, and you spend ages trying to figure our why. Bugs can seem to come from nowhere, for no reason. But there is always a logical reason behind a bug. It is incredibly satisfying when your program does work.

Fields / Attributes Source code specifies the list of attributes that each object has Each object in a class has the same attributes, but the values of those attributes may be different for different objects e.g. each student has some student number, but different students have different student numbers Q: Identify the attributes of the Student class.

Constructor and Methods It specifies how the objects are constructed It specifies what methods the objects have, and exactly what each method does Each object in a class has the same collection of methods Constructor is a special method (maybe more than one) It has the same name as the class It has no return type Other methods have a return type or void Q: identify the constructor and other methods of the student class

Ticket machines an external view Turn to example 3 in the code handouts: TicketMachine.java 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?

Ticket machines Demo See Code Handouts in the Lecture Notes

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.

Basic class structure public class TicketMachine { Inner part omitted. } The outer wrapper of TicketMachine public class ClassName { Fields Constructors Methods } The inner contents of a class

Keywords Words with a special meaning in the language: public class private int Also known as reserved words.

Fields Fields store values for an object. They are also known as instance variables or attributes. Fields define the state of an object. Use Inspect to view the state in BlueJ Some values change often. Some change rarely (or not at all). public class TicketMachine { private int price; private int balance; private int total; } Further details omitted. visibility modifier type variable name private int price;

Constructors public TicketMachine(int cost) { price = cost; balance = 0; total = 0; } Constructors initialize an object. Have the same name as their class. Close association with the fields. Store initial values into the fields. External parameter values for this object.

Passing data via parameters Parameters are another sort of variable.

Choosing variable names There is a lot of freedom over choice of names. Use it wisely! Choose expressive names to make code easier to understand: price, amount, name, age, etc. Avoid single-letter or cryptic names: w, t5, xyz123

Methods Methods implement the behavior of objects. Methods have a consistent structure comprised of a header and a body. Accessor methods provide information about an object. Mutator methods alter the state of an object. Other sorts of methods accomplish a variety of tasks.

Method structure The header provides the method s signature: public int getprice() The header tells us: the name of the method what parameters it takes whether it returns a result its visibility to objects of other classes The body encloses the method s statements

Review Class bodies contain fields, constructors and methods. Fields store values that determine an object s state. Constructors initialize objects particularly their fields. Methods implement the behavior of objects.

Accessor (get) methods visibility modifier return type public int getprice() { return price; } method name parameter list (empty) return statement start and end of method body (block)

Accessor methods An accessor method always has a return type that is not void. An accessor method returns a value (result) of the type given in the header. The method will contain a return statement to return the value. NB: Returning is not printing!

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. Often receive parameters.

Mutator methods visibility modifier return type method name parameter public void insertmoney(int amount) { balance = balance + amount; } field being mutated assignment statement

set mutator methods Fields often have dedicated set mutator methods. These have a simple, distinctive form: void return type method name related to the field name single parameter, with the same type as the type of the field a single assignment statement

A typical set method public void setdiscount(int amount) { discount = amount; } We can infer that discount is a field of type int, i.e: private int discount;

Protective mutators A set method does not have to assign the parameter to the field. The parameter may be checked for validity and rejected if inappropriate. Mutators thereby protect fields. Mutators support encapsulation.

Method summary Methods implement all object behavior. A method has a name and a return type. The return-type may be void. A non-void return type means the method will return a value to its caller. A method might take parameters. Parameters bring values in from outside for the method to use.

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.

Review Methods have a return type. void methods do not return anything. non-void methods return a value. non-void methods have a return statement. String values can be printed to the terminal using System.out.println() String values can be concatenated using + For example cat + fish is catfish