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

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

Encapsulation. Mason Vail Boise State University Computer Science

Practice problem on defining and using Class types. Part 4.

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

CHAPTER 10 INHERITANCE

Classes and Methods: Classes

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

Implementing Classes (P1 2006/2007)

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

Implementing Classes

What will this print?

public void creditaccount(string accountnumber, float amount) { this.accounts.get(accountnumber).credit(amount); }

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

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

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

ICOM 4015: Advanced Programming

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

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

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

Class 09 Slides: Polymorphism Preconditions. Table of Contents. Postconditions

Inheritance (P1 2006/2007)

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

Intro to Computer Science 2. Inheritance

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

9/2/2011. Chapter Goals

EXAMINATIONS 2012 END-OF-YEAR SWEN222. Software Design. Question Topic Marks 1. Design Quality Design Patterns Design by Contract 12

Handout 9 OO Inheritance.

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

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

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

What This Course Is About Design-by-Contract (DbC)

STUDENT LESSON A5 Designing and Using Classes

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

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

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

Lecture 7 Objects and Classes

Client, Supplier, Contract in OOP (1) Design-by-Contract (Dbc) Test-Driven Development (TDD) Terminology: Contract, Client, Supplier

Introduction to Objects. James Brucker

Chapter 9. Designing Classes. Chapter Goals. Chapter Goals. Choosing Classes. Choosing Classes. Self Test

Lecture 06: Classes and Objects

Design-by-Contract (Dbc) Test-Driven Development (TDD)

Designing Classes Advanced Programming ICOM 4015 Lecture 8 Reading: Java Concepts Chapter 8

Eduardo M. Breijo Baullosa May 2, Assignment 17. Report

Creating and Using Objects

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

Arrays and Array Lists

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

COSC This week. Will Learn

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

Catching Defects: Design or Implementation Phase? Design-by-Contract (Dbc) Test-Driven Development (TDD) Motivation of this Course

CMSC 202. Exceptions

Lecture 20: Implementation () / 33

Inheritance and Subclasses

Chapter Goals. T To understand the concept of regression testing. Chapter 6 Arrays and Array Lists. Arrays Array: Sequence of values of the same type

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

CSC 222: Object-Oriented Programming. Fall Inheritance & polymorphism

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

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

IT 313 Advanced Application Development

Fundamentos de programação

Chapter Seven: Arrays and Array Lists. Chapter Goals

The Object Oriented Paradigm

Quarter 1 Practice Exam

Chapter Goals. Chapter Eight: Designing Classes Contined. Choosing Classes

Programming Abstractions

SWC test question #01. Using objects part I

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

Inheritance: Definition

Object-Oriented Programming (Java)

CSE 413 Winter 2001 Midterm Exam

Chapter 8 Designing Classes. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Container Vs. Definition Classes. Container Class

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

GROUP - A <10 questions x 4 points each = 40 points >

CSC 175 Intermediate Programming

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object oriented programming

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

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Designing Classes. Check out DesigningClasses project from SVN

Imperative Languages!

ESc101 : Fundamental of Computing. Object Oriented programming

CPS122 Lecture: Defining a Class

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

Chapter 4 Defining Classes I

Practice Midterm Examination

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

The Singleton Pattern. Design Patterns In Java Bob Tarr

BloomBank Financial Software Design

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Java Persistence API (JPA) Entities

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Chapter 4. Defining Classes I

Review. these are the instance variables. these are parameters to the methods

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

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

Storing Data in Objects

CS 1302 Chapter 9 (Review) Object & Classes

CS112 Lecture: Defining Instantiable Classes

Object-Oriented Programming

Transcription:

1 6 ',,

: (.new ) BankAccount account1 = new BankAccount(...); פ, פ,this,, ) \ פ\( פ פ פ 2

) ( " פ : :? public class BankAccount {... private double balance;... 3

:),, ( 3 )queries, accessors( ), ( :)observers( פ, ( :)producers( פ ). )immutable( " )commands, transformers, mutators(, : )constructors( : 4

- : precondition - " " -.postcondition " " - פ 5

)preconditions( -., " )AND( @pre : 6

)postconditions(..., / @post :?, :?!) ( : 7

? @pre, @post, $prev, $ret, $implies ) )x >=0( :) ( x [0,1]( )"M is a diagonal square matrix ( : ', " ) (!,,! 8

BankAccount public class BankAccount { public double getbalance() {...; public long getaccountnumber() {...; פ public Customer getowner )( {...; private double balance; private long accountnumber; private Customer owner; field :.getfield() JavaBeans - GUI Builders 9

getter/setter?. )private( getter/setter balance :?getter,?setter public void setbalance(double balance) { this.balance = balance;,, 10

פ : פ public void deposit(double amount) { balance += amount; /** * Withdraw amount from the account * * @pre 0 < amount <= getbalance() * @post getbalance() == $prev(getbalance()) - amount */ public void withdraw(double amount) {?????????????????????????????????????????????????????????????????????????????? balance -= amount; this.balance :' - 11

פ : פ public void deposit(double amount) { balance += amount; /** * Withdraw amount from the account * * @pre 0 < amount <= getbalance() * @post getbalance() == $prev(getbalance()) - amount */ public void withdraw(double amount) { balance if (amount -= amount; < 0 amount > getbalance()) { System.out.println("Invalid withdrawal amount: "+ amount); return; balance -= amount; : :' 12

: : : /** * Makes a transfer of amount from one account to the other * @pre 0 < amount <= from.getbalance() * @post to.getbalance() == $prev(to.getbalance()) + amount * @post from.getbalance() == $prev(from.getbalance()) amount */ public static void transfer(double amount, BankAccount from, BankAccount to) { from.withdraw(amount); to.deposit(amount); 13

, ( : ) /** * Makes a transfer of amount from the current * account to the other one */ public void transferto(double withdraw amount, BankAccount other) { other.deposit(amount); withdraw(amount); withdraw 14

:!... ',null, - :.null :BankAccount!,,, :. ) (?? 15

BankAccount /** * Constructs a new account and sets its owner and identifier * @pre id > 0 * @pre customer!= null * @post getowner() == customer * @post getaccountnumber() == id * @post getbalance() == 0 */ public BankAccount(Customer customer, long id) { accountnumber = id; owner = customer;! new BankAccount - null 16

/** * Constructs a new account and sets its owner and identifier * @pre id > 0 * @pre customer!= null * @pre initialbalance >= 0 * @post getowner() == customer * @post getaccountnumber() == id * @post getbalance() == initialbalance */ public BankAccount(Customer customer, long id, double initialbalance) { this(customer, id); balance = initialbalance;. = :. this().! 17

עצמים, מחלקות, נראות ומה שביניהם 18

CurrentClass public class CurrentClass { public static void mypublicstaticmethod() { System.out.println("In mypublicstaticmethod"); private static void myprivatestaticmethod() { System.out.println("In myprivatestaticmethod"); public void mypublicmethod() { System.out.print("In mypublicmethod >> "); myprivatemethod(); private void myprivatemethod() { System.out.println("In myprivatemethod"); ) ( 19

OtherClass public class OtherClass { public static void otherspublicstaticmethod() { System.out.println("In otherspublicstaticmethod"); private static void othersprivatestaticmethod() { System.out.println("In othersprivatestaticmethod"); public void otherspublicmethod() { System.out.print("In otherspublicmethod >> "); othersprivatemethod(); private void othersprivatemethod() { System.out.println("In othersprivatemethod"); 20

CurrentClass- main public class CurrentClass { public static void main(string[] args) { CurrentClass.myPublicStaticMethod(); mypublicstaticmethod(); CurrentClass.myPrivateStaticMethod(); CurrentClass.myPublicMethod(); CurrentClass currentclass = new CurrentClass(); currentclass.mypublicmethod(); // Prints: In mypublicmethod >> In myprivatemethod currentclass.myprivatemethod(); // Prints: In myprivatemethod currentclass.mypublicstaticmethod(); //Has a warning, Prints: In mypublicstaticmethod OtherClass.othersPublicStaticMethod(); otherspublicstaticmethod(); OtherClass.othersPrivateStaticMethod(); // Prints: In mypublicstaticmethod // Prints: In mypublicstaticmethod // Prints: In myprivatestaticmethod // Prints: In otherspublicstaticmethod... OtherClass otherclass = new OtherClass(); otherclass.otherspublicmethod(); // Prints: In otherspublicmethod >> In othersprivatemethod otherclass.othersprivatemethod(); 21

) (! mypublicmethod() currentclass.mypublicmethod() = ( = )import, 22

Instance vs. Class (static) Fields Instance fields? Class (static) fields???? 0(! 1 )??!? ( ) 23?

public class BankAccount { public static final String BANK_NAME = "BNP"; //static constant private static int lastaccountid = 0; //static field private int id; public BankAccount() { id = ++lastaccountid; // unique ID for every account /* static method */ public static void main(string[] args) { System.out.println(lastAccountId); System.out.println(id); BankAccount account = new BankAccount(); System.out.println(account.id); Why?? /* instance method */ public void printstuff() { System.out.println(lastAccountId); System.out.println(id); 24

... 25