Java: overview by example



Similar documents
Inheritance, overloading and overriding

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

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

D06 PROGRAMMING with JAVA

Chapter 13 - Inheritance

CS193j, Stanford Handout #10 OOP 3

ICOM 4015: Advanced Programming

5. Advanced Object-Oriented Programming Language-Oriented Programming

Programming to Interfaces

D06 PROGRAMMING with JAVA

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

More on Objects and Classes

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

An Introduction to Classes

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

UML Class Diagrams. Lesson Objectives

CS 111 Classes I 1. Software Organization View to this point:

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Moving from CS 61A Scheme to CS 61B Java

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Lab 5: Bank Account. Defining objects & classes

High-Level Language. Building a Modern Computer From First Principles.

Design Patterns. Firuza Aibara. 11 August IIT Bombay (CSE) Firuza Aibara (IIT Bombay (CSE)) Design Patterns 11 August / 16

Introduction to Java Lecture Notes. Ryan Dougherty

Lecture J - Exceptions

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Unit Testing & JUnit

Java CPD (I) Frans Coenen Department of Computer Science

CS170 Lab 11 Abstract Data Types & Objects

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Crash Course in Java

Word Count Code using MR2 Classes and API

D06 PROGRAMMING with JAVA

LAB4 Making Classes and Objects

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

AP Computer Science Static Methods, Strings, User Input

1 Hour, Closed Notes, Browser open to Java API docs is OK

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

CSE 308. Coding Conventions. Reference

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Module 13 Implementing Java EE Web Services with JAX-WS

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Threads 1. When writing games you need to do more than one thing at once.

Object-Oriented Programming in Java

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

J a v a Quiz (Unit 3, Test 0 Practice)

AVRO - SERIALIZATION

Mutual Exclusion using Monitors

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Quick Introduction to Java

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

History OOP languages Year Language 1967 Simula Smalltalk

Python for Rookies. Example Examination Paper

Unit-testing with JML

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Using Two-Dimensional Arrays

Tutorial: Getting Started

Collections.sort(population); // Método de ordenamiento

Creating a Simple, Multithreaded Chat System with Java

Software Engineering 1 EEL5881 Spring Homework - 2

Basics of Java Programming Input and the Scanner class

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Introduction to Object-Oriented Programming

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

CPLEX Tutorial Handout

RentersPLUS Move In Special

Building Java Programs

Software Development with UML and Java 2 SDJ I2, Spring 2010

How to Install Java onto your system

JAVA ARRAY EXAMPLE PDF

Unit Testing and JUnit

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Object Oriented Software Design

Remote Method Invocation in JAVA

Java Classes. GEEN163 Introduction to Computer Programming

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Object-Oriented Programming in Java

Outline of this lecture G52CON: Concepts of Concurrency

Konzepte objektorientierter Programmierung

Object Oriented Software Design

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Java Interview Questions and Answers

Chapter 5 Aspect Oriented Programming

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Transcription:

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: overview by example

Bank Account A Bank Account maintain a balance (in CHF) of the total amount of money balance can go negative can open an account with an initial sum of money can deposit money on the account deposit is effective only for a nonnegative amount of money can withdraw money from the account withdraw is effective only for a nonnegative amount of money Java implementation: BankAccount class public class BankAccount { 2

Attribute balance maintain a balance (in CHF) of the total amount of money public class BankAccount { // Attribute 'balance inaccessible by clients private int balance; // Public getter for 'balance public int getbalance() { return balance; // Restricted setter for 'balance protected void setbalance(int balance){ this.balance = balance; 3

Constructor: open a new account can open an account with an initial sum of money public class BankAccount { // no-arg constructor public BankAccount() { balance = 0; // 1-arg constructor public BankAccount(int initialbalance) { if (initialbalance >= 0) { balance = initialbalance; else throw new BankAccountException( ) 4

Method deposit can deposit money on the account deposit is effective only for a nonnegative amount of money public class BankAccount { // deposit 'amount' // don't do anything if 'amount' < 0 public void deposit(int amount) { if (amount >= 0) { balance = balance + amount; 5

Method withdraw can withdraw money on the account withdraw is effective only for a nonnegative amount of money. public class BankAccount { // withdraw allowed amount // access restricted only to some clients protected int withdraw(int amount) { if (amount >= 0) { balance = balance - amount); return 0; else { return -1; 6

Premium Bank Account A special Bank Account: basic functionalities as in a regular Bank Account has a minimum balance and a fixed fee if the balance goes below the minimum balance, the fee is automatically deducted from the balance example: minimum balance = 200, fee = 15 if a withdrawal brings the balance down to 150, an additional 15 is deducted, so the final balance after the deposit is 135 Java implementation: PremiumBankAccount class inheriting from BankAccount public class PremiumBankAccount extends BankAccount { 7

New attributes has a minimum balance and a fee public class PremiumBankAccount extends BankAccount { public final int minimumbalance = 200; public final int lowbalancefee = 15; 8

New constructor public class PremiumBankAccount extends BankAccount { // constructor public PremiumBankAccount(int initialbalance) { if(initialbalance >= minimumbalance) { setbalance(initialbalance); else{ throw new BankAccountException( ); 9

Redefining withdraw if the balance goes below the minimum balance, the fee is automatically deducted from the balance public class PremiumBankAccount extends BankAccount { // overrides corresponding method in BankAccount protected int withdraw(int amount) { int res = super.withdraw (amount); if (res == 0 && getbalance() < minimumbalance) { setbalance (getbalance() - lowbalancefee); return 0; else { //handle other cases here 10

Clients of the BankAccount Class A client class which runs two instances of BankAccount public class BankClient { public static void main(string[] args) { BankAccount ba = new BankAccount(); BankAccount pba = new PremiumBankAccount(250); System.out.println(ba.getBalance()); System.out.println(pba.getBalance()); ba.deposit(1800); pba.withdraw(100); System.out.println(ba.getBalance()); System.out.println(bap.getBalance()); 11

Running a Java application > javac BankAccount.java PremiumBankAccount.java BankClient.java > java BankClient 0 250 1800 135 12