Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

Similar documents
public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

STUDENT LESSON A5 Designing and Using Classes

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Encapsulation. Mason Vail Boise State University Computer Science

Imperative Languages!

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

Outline. Quick Introduction to Database Systems. Data Manipulation Tasks. What do they all have in common? CSE142 Wi03 G-1

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

About this exam review

BankAccount account1 = new BankAccount(...);,this

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Implementing Classes (P1 2006/2007)

Object-Oriented Programming

CSC Inheritance. Fall 2009

Introduction to Computer Science and Object-Oriented Programming

APCS Semester #1 Final Exam Practice Problems

I. Variables and Data Type week 3

CHAPTER 10 INHERITANCE

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

Program Fundamentals

Implementing Classes

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Chapter Goals. Chapter 7 Designing Classes. Discovering Classes Actors (end in -er, -or) objects do some kinds of work for you: Discovering Classes

Introduction to Programming Using Java (98-388)

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

1.00 Lecture 4. Promotion

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Section we will not cover section 2.11 feel free to read it on your own

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Lecture 08: Static Fields and Methods

Inheritance and Subclasses

CS112 Lecture: Defining Instantiable Classes

CPS122 Lecture: Defining a Class

Practical Session 2 Correction Don't forget to put the InputValue.java file in each project

Lecture 3 Tao Wang 1

Intro to Computer Science 2. Inheritance

CSE 413 Winter 2001 Midterm Exam

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2)

Classes and Methods: Classes

CIS133J. Working with Numbers in Java

EXPRESSIONS AND ASSIGNMENT CITS1001

$ % $ % BankAccount. public class BankAccount {... private??? balance; } "commands, transformers, mutators! "abstract state! "queries, accessors!

CS 1302 Chapter 9 (Review) Object & Classes

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Chapter 5: Arrays. Chapter 5. Arrays. Java Programming FROM THE BEGINNING. Copyright 2000 W. W. Norton & Company. All rights reserved.

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

What will this print?

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Building Java Programs

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

CS112 Lecture: Working with Numbers

Building Java Programs

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

CIS 110: Introduction to Computer Programming

Software Design and Analysis for Engineers

COMP 110 Introduction to Programming. What did we discuss?

ICOM 4015: Advanced Programming

Handout 8 Classes and Objects Continued: Static Variables and Constants.

Introduction to Inheritance

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Inheritance (P1 2006/2007)

Using the Xcode Debugger

This exam is open book. Each question is worth 3 points.

Building Java Programs

IT 374 C# and Applications/ IT695 C# Data Structures

CS 106X, Lecture 14 Classes and Pointers

Ch. 12: Operator Overloading

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

1B1b Classes in Java Part I

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CS 101 Fall 2006 Midterm 3 Name: ID:

Introduction to Classes and Objects

Programming with Java

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Arrays and Array Lists

4. Inputting data or messages to a function is called passing data to the function.

2/9/2012. Chapter Three: Implementing Classes. Chapter Goals

9/2/2011. Chapter Goals

Overview of Eclipse Lectures. Module Road Map

Assignment 2 - Specifications and Modeling

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Operators. Java operators are classified into three categories:

Chapter 8. Arrays and Array Lists. Chapter Goals. Chapter Goals. Arrays. Arrays. Arrays

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Chapter 3: Operators, Expressions and Type Conversion

CSE 143 Lecture 3. More ArrayList; object-oriented programming. reading: 10.1;

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

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

Programming Abstractions

Transcription:

Outline CSE 142 Class Implementation in Java Implementing classes in Java Instance variables properties Value-returning methods for queries Void methods for commands Return statement Assignment statement and arithmetic expressions Method parameters Constructors 1/10/2003 (c) 2001-3, University of Washington F-1 1/10/2003 (c) 2001-3, University of Washington F-2 Specification vs Implementation - Review Specification external view of an object/class View of the class as seen by client code (i.e., other code that creates or uses instances objects of this class) Class name and method names, parameters, and descriptions Implementation internal details private to the class Instance variables properties Statements that describe algorithms carried out by methods Instance Variables private int number; // account number private String name; // account name private double balance; // current balance These are instance variable declarations private <type> <identifier> private part of the implementation, not visible outside <type> - the type of the variable <identifier> - a (hopefully meaningful) name for the variable Each object of class BankAccount will have its own set of instance variables 1/10/2003 (c) 2001-3, University of Washington F-3 1/10/2003 (c) 2001-3, University of Washington F-4 CSE142 Wi03 F-1

Implementing Methods for Simple Queries /** return the current balance of this BankAccount */ public double getbalance( ) { return balance; When this method is executed, it replies with the value of the instance variable balance checking.getbalance() More About Value-Returning (Query) Methods Form /** Comment specifying the method */ public <result type> <identifier> ( ) { list of statements Details public this method is part of the public specification of the class (methods can also be private; we ll see examples eventually) <result type> the type of the value returned by this query <identifier> the (hopefully meaningful) name of this method This is the name of the query that the method implements list of statements the body of the method These make up the algorithm that the method executes when it is called 1/10/2003 (c) 2001-3, University of Washington F-5 1/10/2003 (c) 2001-3, University of Washington F-6 Return Statement First example of a statement return expression ; Meaning Evaluate the expression to get a value In getbalance, the expression is the name of the instance variable balance For a variable, evaluation means get its current value Then, finish execution of this method (query), replying with the value of the expression Arithmetic Expressions Basic components Literals 17, 3.0, 1.023e23 Variable names value is the current value of the variable Operators (see book for all the details) +, -, *, /, % (remainder) Gotchas: for ints, x/y yields integer part, dropping any fraction; x%y gives the remainder Operators have the usual precedence For example, a + b * c is understood to mean a + (b * c) Binary operators (ones that have two components) are left associative : a * b / c means (a * b) / c Use parentheses where needed to override: a * (b / c) Mixing ints and doubles is normally ok the int is converted to a double and the calculation is done as a double 1/10/2003 (c) 2001-3, University of Washington F-7 1/10/2003 (c) 2001-3, University of Washington F-8 CSE142 Wi03 F-2

Exercise Another Query Complete the query in class BankAccount /** return the name of this BankAccount */ public double getname( ) { Implementing Methods for Simple Commands /** Set this BankAccount s name to newname */ public void setname(string newname) { name = newname; When this method is executed, it changes the name instance variable; it does not return a value Executed only for its effect 1/10/2003 (c) 2001-3, University of Washington F-9 1/10/2003 (c) 2001-3, University of Washington F-10 More About Command Methods Form /** Comment specifying the method */ public void <identifier> ( parameters ) { list of statements Details public, <identifier>, and list of statements same as for queries void Indicates that this is a command that doesn t return a value (as opposed to the result type of a query) parameters information supplied with command message Same form as a variable declaration (Note: Queries can also have parameters, but they have not been needed in the simple cases we ve seen so far) Assignment Statement Second example of a statement variable = expression ; Meaning First, evaluate the expression to get a value Second, bind that value to the variable whose name appears on the left These two steps are done in that order, not simultaneously Question: what does this mean (or do)? count = count + 1; 1/10/2003 (c) 2001-3, University of Washington F-11 1/10/2003 (c) 2001-3, University of Washington F-12 CSE142 Wi03 F-3

Exercise Another Simple Command Complete the command in class BankAccount /** Set this BankAccount s number to newnumber */ public void setnumber(int newnumber) { 1/10/2003 (c) 2001-3, University of Washington F-13 Constructor /** Construct a new BankAccount with balance=number=0 and no name */ public BankAccount( ) { number = 0; name = ; balance = 0.0; This is a lot like a command method. Difference it is executed automatically each time a new BankAccount instance is created Idea: Use the constructor to initialize newly created objects to some sensible state Syntax difference from other methods: no result type or void 1/10/2003 (c) 2001-3, University of Washington F-14 Creating and Using BankAccount Objects Before going further, we d better test what we ve done BankAccount savings = new BankAccount(); savings.setname( A. Hacker ); savings.setnumber(4200); savings.getname(); A Smarter Constructor Better would be to provide initial values for name, account number, and balance when we create a BankAccount Solution: use parameters in the constructor /** Construct a new BankAccount with given account name, number, and balance */ public BankAccount(String accountname, int accountnumber, double initialbalance) { number = accountnumber; name = accountname; balance = initialbalance; Test BankAccount checking = new BankAccount( E. Fudd, 4179, 42.17); checking.getname( ); checking.getbalance( ); 1/10/2003 (c) 2001-3, University of Washington F-15 1/10/2003 (c) 2001-3, University of Washington F-16 CSE142 Wi03 F-4

Deposit Another Command In class BankAccount /** Deposit given amount in this BankAccount */ public void deposit(double amount) { balance = balance + amount; Meaning is clear since expression in assignment statement is evaluated before balance is changed 1/10/2003 (c) 2001-3, University of Washington F-17 Transfer Objects as Parameters From class BankAccount /** Transfer the given amount from otheraccount to this BankAccount */ public void transfer(double amount, BankAccount otheraccount) { balance = balance + amount; otheraccount.balance = otheraccount.balance amount; Instance variable (field) access objectname.variablename is a reference to the given instance variable of the given object Legal in the example because otheraccount is another instance of BankAccount. Since transfer is part of class BankAccount, it can access private information in any BankAccount 1/10/2003 (c) 2001-3, University of Washington F-18 Summary Implementation of classes Instance variables type plus name Methods statements that make up the body of each method Statements return Assignment & arithmetic expressions Creating objects and calling methods Coming attractions More details about objects, method calls, and variables More complex statements conditionals and loops 1/10/2003 (c) 2001-3, University of Washington F-19 CSE142 Wi03 F-5