Today s Agenda. Quick Review

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

Understanding class definitions

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

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

Ticket Machine Project(s)

UNDERSTANDING CLASS DEFINITIONS CITS1001

CSC 222: Object-Oriented Programming. Fall 2015

CSCI 161 Introduction to Computer Science

CSC 222: Object-Oriented Programming. Fall 2017

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.

Chapter 4 Defining Classes I

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

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

Introduction to Programming Using Java (98-388)

Objects as a programming concept

CS 302: Introduction to Programming in Java. Lecture 15

EECS168 Exam 3 Review

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Lab: PiggyBank. Defining objects & classes

Chapter 6 Introduction to Defining Classes

Chapter 4. Defining Classes I

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

Anatomy of a Class Encapsulation Anatomy of a Method

Object Oriented Design: Identifying Objects

MIDTERM REVIEW. midterminformation.htm

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Declarations and Access Control SCJP tips

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

MID-SEMESTER TEST 2014

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

TaxiBot New attributes Variables Math! TaxiBot

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Chapter 4: Writing Classes

Encapsulation. Mason Vail Boise State University Computer Science

Principles of Object Oriented Programming. Lecture 4

AP COMPUTER SCIENCE A

Computer Science II (20073) Week 1: Review and Inheritance

Robots. Byron Weber Becker. chapter 6

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

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

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

// 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 }

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

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

Programming Language Concepts: Lecture 2

CS 302 Week 9. Jim Williams

Objects and Iterators

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

Short Notes of CS201

Chapter 7 User-Defined Methods. Chapter Objectives

And Even More and More C++ Fundamentals of Computer Science

Programming Language Concepts: Lecture 2

Example: Monte Carlo Simulation 1

CSC Java Programming, Fall Java Data Types and Control Constructs

CS201 - Introduction to Programming Glossary By

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Java and OOP. Part 2 Classes and objects

Do not start the test until instructed to do so!

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CS111: PROGRAMMING LANGUAGE II

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Computer Science 1 Ah

About this exam review

Sample Paper of Computer for Class 10

Objects and Classes Lecture 2

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

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

Java Review. Fundamentals of Computer Science

Chapter 4. Defining Classes I

In Java there are three types of data values:

Object-oriented programming. CSC 222: Computer Programming II. Spring Java classes. Simple example: Die class. public static void main

Packages Inner Classes

What will this print?

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Computer Science II Data Structures

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Classes and Methods: Classes

Notes on Chapter Three

Chapter 6 Lab Classes and Objects

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Java Primer 1: Types, Classes and Operators

C# Programming for Developers Course Labs Contents

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Class, Variable, Constructor, Object, Method Questions

Java Identifiers, Data Types & Variables

Handout 7. Defining Classes part 1. Instance variables and instance methods.

CMSC 341 Lecture 7 Lists

Transcription:

Today s Agenda TA Information Homework 1, Due on 6/17 Quick Review Finish Objects and Classes Understanding class definitions 1 Quick Review What is OOP? How is OOP different from procedural programming? What is an object? What is a class? 2 1

Understanding class definitions Looking inside classes 4.0 Main concepts to be covered fields constructors methods parameters assignment statements 4 2

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. Methods insertmoney, getbalance, and printticket are used to enter money, keep track of balance, and print out tickets. 5 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. 6 3

Basic class structure public class TicketMachine Inner part of the class omitted. The outer wrapper of TicketMachine public class ClassName Fields Constructors Methods The contents of a class 7 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. visibility modifier type variable name private int price; 8 4

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; 9 Passing data via parameters 10 5

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. 11 Main concepts to be covered mutator and accessor methods conditional statements local variables string concatenation 12 6

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. 13 Accessor methods visibility modifier return type public int getprice() return price; method name start and end of method body (block) parameter list (empty) return statement 14 7

Test public class CokeMachine private price; public CokeMachine() price = 300 public int getprice return Price; What is wrong here? (there are five errors!) 15 Test public class CokeMachine int private price; public CokeMachine() price = 300 ; public int getprice () return - Price; What is wrong here? (there are five errors!) 16 8

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. 17 Mutator methods visibility modifier return type method name parameter public void insertmoney(int amount) balance = balance + amount; field being mutated assignment statement 18 9

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; 19 String concatenation 4 + 5 9 "wind" + "ow" "window" "Result: " + 6 "Result: 6" "# " + price + " cents" "# 500 cents" overloading" 20 10

Quiz System.out.println(5 + 6 + "hello"); 11hello! System.out.println("hello" + 5 + 6); hello56! 21 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. 22 11

Making choices public void insertmoney(int amount) if(amount > 0) balance = balance + amount; else System.out.println("Use a positive amount: " + amount); 23 Making choices if keyword boolean condition to be tested actions if condition is true if(perform some test) Do these statements if the test gave a true result else Do these statements if the test gave a false result else keyword actions if condition is false 24 12

How do we write 'refundbalance'? 25 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. 26 13

Scope and life time The scope of a local variable is the block it is declared in. The lifetime of a local variable is the time of execution of the block it is declared in. 27 Local variables A local variable No visibility modifier public int refundbalance() int amounttorefund; amounttorefund = balance; balance = 0; return amounttorefund; 28 14

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. 29 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. 30 15

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. 31 16