It has a parameter list Account(String n, double b) in the creation of an instance of this class.



Similar documents
CISC 181 Project 3 Designing Classes for Bank Accounts

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

CS170 Lab 11 Abstract Data Types & Objects

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

Moving from CS 61A Scheme to CS 61B Java

Computer Programming C++ Classes and Objects 15 th Lecture

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

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

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Curriculum Map. Discipline: Computer Science Course: C++

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Help on the Embedded Software Block

Sample CSE8A midterm Multiple Choice (circle one)

Ch 7-1. Object-Oriented Programming and Classes

C++FA 5.1 PRACTICE MID-TERM EXAM

Programming Database lectures for mathema

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

El Dorado Union High School District Educational Services

Certified PHP Developer VS-1054

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

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Visual Basic Programming. An Introduction

PHP Tutorial From beginner to master

Part I. Multiple Choice Questions (2 points each):

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Program to solve first and second degree equations

So far we have considered only numeric processing, i.e. processing of numeric data represented

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

USING MAGENTO TRANSLATION TOOLS

Explain the relationship between a class and an object. Which is general and which is specific?

Delphi Developer Certification Exam Study Guide

14 Triggers / Embedded SQL

C++ Programming Language

CS193j, Stanford Handout #10 OOP 3

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:

>

Lecture 5: Java Fundamentals III

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

AP Computer Science File Input with Scanner

AP Computer Science Java Subset

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

Inheritance, overloading and overriding

! 2!!$ ,)!$- %$0. Baskı-2 ! "! #$ % #$#!&'! '! (&&)!! &!! #.! &)!$#$! /&)!!! 0! &)!$!.!! 0$! #! &)!$ &.!!#$!! 3!&!#!!3! #&!'! &! 4!!

Script Handbook for Interactive Scientific Website Building

The C Programming Language course syllabus associate level

D06 PROGRAMMING with JAVA

Government Girls Polytechnic, Bilaspur

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE CISY 233 INTRODUCTION TO PHP

Facebook Twitter YouTube Google Plus Website

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Web development... the server side (of the force)

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

An Incomplete C++ Primer. University of Wyoming MA 5310

7-1. This chapter explains how to set and use Event Log Overview Event Log Management Creating a New Event Log...

Bash shell programming Part II Control statements

PL / SQL Basics. Chapter 3

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

Web Programming Step by Step

Chapter 13 - Inheritance

Sources: On the Web: Slides will be available on:

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

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

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

Programming Languages CIS 443

How to Create a Custom TracDat Report With the Ad Hoc Reporting Tool

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

Example of a Java program

N3458: Simple Database Integration in C++11

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CS A250 (CRN ) C++ Programming Language 2. Syllabus Fall 2015

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

C Compiler Targeting the Java Virtual Machine

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Computer Programming I

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Advanced Data Structures

7 Why Use Perl for CGI?

Basic Programming and PC Skills: Basic Programming and PC Skills:

Hands-On UNIX Exercise:

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Introduction to Programming II Winter, 2014 Assignment 2

Continuous Integration Part 2

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

PHP Integration Kit. Version User Guide

Oracle Database: SQL and PL/SQL Fundamentals

Systems Programming & Scripting

CSE 308. Coding Conventions. Reference

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Transcription:

Lecture 10 Private Variables Let us start with some code for a class: String name; double balance; // end Account // end class Account The class we are building here will be a template for an account at a bank. There are two "class" variables, name and balance. The "constructor" for the class, which has the same name Account as the class is // end Account It has a parameter list Account(String n, double b) in the creation of an instance of this class. which will be used The code for the class is kept in a separate file Account.java. The next program, Bank.java, will eventually be a collection of accounts. Right now it has only one. The file Bank.java is kept in the same directory as "Account": Account a = new Account("Ronald", 3000); System.out.println("\n\nbalance = "+ a.balance); //end main() // end class Bank In this application we are constructing an account for "Ronald", with an initial deposit of $3000. When the application is run its' output is: 1

In reviewing this program one sees that the variable balance can be accessed by the program Bank. As a matter of fact it can be accesed by any number of programs. The access can be restricted by declaring the two class variables in Account to be private: private String name; private double balance; // end Account // end class Account When Bank.java is compiled (after the above change has been made and saved to the Account.java file) one gets a "variable balance.. not accessible" error message: It is not accessible because it has been declared to be private. The variable balance can only be manipulated within the class Account". So, how does the bank find out what is in each account? This is done by adding a toprint() method to the class Account: private String name; private double balance; 2

public String toprint() String output; output = name +", balance = " + balance; return output; Then, Bank is modified by a call to the a.toprint() method, which prints out the name and the balance : Account a = new Account("Ronald", 3000); //end main() // end class Bank The output is: Deposits, Withdrawals Next, we add two methods to to permit the bank to make deposits and withdrawls; private String name; private double balance; public String toprint() String output; 3

output = name +", balance = " + balance; return output; public double deposit(double d) if ( d >= 0) balance = balance + d; if (d < 0) System.out.println("Your deposit has to be >= 0"); return balance; public double withdrawl(double w) if (w >= 0 && w <= balance) balance = balance -w; if (w > balance) System.out.println("You don't have sufficient funds!"); if (w < 0) System.out.println("Withdrawl must be >= 0"); return balance; // end Account We modify Bank to test deposit() and withdrawl() : Account a = new Account("Ronald", 3000); System.out.println("Started with $3000"); System.out.println("\nThen deposited $450"); a.deposit(450); System.out.println("\nThen tried to deposit -200"); a.deposit(-200); System.out.println("\nWithdrew $650"); a.withdrawl(650); System.out.println("\nTried to get $4000"); a.withdrawl(4000); //end main() // end class Bank The output is: 4

5

Overloading a Constructor Suppose the bank wishes to include the social security number of all new accounts. This can be done by "overloading" the constructor for the class, as folows. private String name; private String socialsecurity = ""; private double balance; public Account(String n, String s, double b) socialsecurity = s; public String toprint() public double deposit(double d) public double withdrawl(double w) public String printss() String ssnumber ; if (socialsecurity.equals("")) ssnumber = "no social security number for this account"; else ssnumber = socialsecurity; return ssnumber; // end account This class has a new class variable private String socialsecurity = "";, 6

initialized as the null string, and two different constructors for Account, the previously defined one Account(String n, double b), and a newly defined one Account(String n, String s, double b). A class can have any number of constructors. Each constructor has to have the name of the class, Account in this case. Constructors with different parameter lists are considered to be distinct constructors. The text of the methods that were defined earlier toprint(), deposit(), and withdrawl() was deleted above for space and readability. It has to be replaced when the program is run. The next version of the class Bank shows the two different ways an account can now be constructed. The first account a is constructed without a social security number; the second, b, with one. The method printss() is used to print out this number, provided it has been entered. Account a = new Account("Ronald", 3000); Account b = new Account("David","123-45-678", 4000); System.out.println("Ronald Started with $3000"); System.out.println(a.printSS()); System.out.println("\nDavid Started with $4000"); System.out.println(b.toPrint()); System.out.print("His social security number is "); System.out.println(b.printSS()); //end main() // end class Bank The output is: 7

An Array of Accounts We shall show the syntax for building a bank with 3 accounts. We will be assuming that all accounts do have a social security number so, we will add another print method, toprint2(), to the class Account to simplify the output. It is: public String toprint2() String output = "name: "+name + "\n"; output = output + "Social Security Number: "+socialsecurity; output = output +"\nbalance: " + balance; return output; Once this method has been added to Account we then define Bank as an array of Accounts: Account[] b ; b = new Account[3]; b[0] = new Account("Edward", "234-56-789", 300); System.out.println(b[0].toPrint2()+ " \n"); b[1] = new Account("Amy", "34-567-891", 50000); System.out.println(b[1].toPrint2()+ "\n"); b[2] = new Account("Charlie", "45-678-912",897.45); System.out.println(b[2].toPrint2()); The output is: 8