Professional Documents
Culture Documents
Intro Java v5.4 Workshop Solutions
Intro Java v5.4 Workshop Solutions
Java
Programming
Workshop
Solutions
Introduction to Java Programing: Workshop Solutions Page i
Table of Contents
Chapter 1:
HelloWorld Workshop
HelloWorld.java
/*
* HelloWorld
*/
public class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello World!!");
}
}
Chapter 2:
Control Structures Workshop
MultTable.java
/*
* MultTable class
*/
} // end class
MultTableLabel.java
/*
* MultTableLabel
*
* Multiplication table program with row & column labels
* for "If Time" portion of lab.
*/
System.out.print(" ");
for(int c = 1; c <= iCols; ++c) {
if(c < 10)
System.out.print(" ");
System.out.print(" " + c);
}
System.out.println();
System.out.print(" ");
for(int c = 1; c <= iCols; ++c)
System.out.print("====");
System.out.println();
Chapter 2:
HelloWorld Arguments Workshop
HelloWorld.java
/*
* HelloWorld
*/
class HelloWorld {
public static void main (String[] args) {
Chapter 4:
Using Objects Workshop
DateDisplay.java
/*
* DateDisplay
*/
import java.util.*;
public class DateDisplay {
public static void main (String[] args) {
String[] month = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
String [] dayOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
System.out.print(month[myDate.get(Calendar.MONTH) ]);
System.out.print(" ");
System.out.print(myDate.get(Calendar.DATE));
System.out.print(", ");
System.out.println(myDate.get(Calendar.YEAR));
Chapter 4:
More Objects Workshop
DateDisplayXtra.java
/*
* Date DisplayXtra
*/
import java.util.*;
public class DateDisplayXtra {
public static void main (String[] args) {
String[] month = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
String [] dayOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
GregorianCalendar myDate;
String myString;
if (args.length == 3){
int userYear = Integer.parseInt(args[0]);
int userMonth = Integer.parseInt(args[1]);
int userDay = Integer.parseInt(args[2]);
myDate = new GregorianCalendar(userYear, userMonth , userDay);
myString = "The date you input was ";
}
else {
myString = "The date I chose was ";
myDate = new GregorianCalendar(2003, 2, 6);
}
System.out.println(myString
+ dayOfWeek[myDate.get(Calendar.DAY_OF_WEEK)-1]
+ ", "
+ month[myDate.get(Calendar.MONTH) ]
+ " "
+ myDate.get(Calendar.DATE)
+ ", "
+ myDate.get(Calendar.YEAR));
Chapter 5:
Create Classes Workshop A
BigCorpCreateClassA.java
/*
* BigCorpCreateClassA - Create Class workshop A
*/
import java.util.*;
public class BigCorpCreateClassA {
public static void main (String[] args) {
Person myPerson1 = new Person();
myPerson1.setFName("Charlie");
myPerson1.setLName("Tuna");
System.out.println("My person is "
+ myPerson1.getFName()
+ " " + myPerson1.getLName());
myPerson1.walk();
myPerson1.talk();
}
}
class Person {
String lName;
String fName;
public void setLName(String inLName) {
lName = inLName;
}
public void setFName(String inFName) {
fName = inFName;
}
public String getLName() {
return lName;
}
public String getFName() {
return fName;
}
public void walk() {
System.out.println(getFName() + " " + getLName() + " is walking");
}
public void talk(){
System.out.println(getFName() + " " + getLName() + " is talking");
}
}
Chapter 5:
Create Class Workshop B
BigCorpCreateClassB.java
/*
* BigCorpCreateClassB - Using Static variables
*/
import java.util.*;
myPerson1.finalize();
System.out.println("the population is " + Person.getPopulation());
}
}
class Person {
String lName;
String fName;
static long population;
public Person () {
population++;
}
public static long getPopulation () {
return population;
}
Chapter 5:
Create Class Workshop C
BigCorpCreateClassC.java
/*
* BigCorpCreateClassC - constructors, this & overloaded methods
*/
import java.util.*;
myPerson1.finalize();
System.out.println("Remove " + myPerson1.getName()
+ " height was " + myPerson1.getHeight());
System.out.println("the population is " + Person.getPopulation());
System.out.println("the average height is "
+ Person.getAverageHeight());
System.out.println(" ");
}
}
class Person {
String lName;
String fName;
int height;
public Person () {
population++;
}
public Person (String inFName, String inLName) {
this();
setLName(inLName);
setFName(inFName);
}
return lName;
}
public String getFName() {
return fName;
}
public String getName() {
return (fName + " " + lName);
}
public int getHeight() {
return height;
}
void setHeight(int inHeight) {
height = inHeight;
totalHeight += height;
setAverageHeight(height);
}
Chapter 6:
Packages Workshop
Setenvpack.bat
Changes only applied to this statement:
set
CLASSPATH=.;c:\JavaIntro\workshops;c:\JavaIntro\lib\introcourse.jar;c:\J
avaIntro\lib\sbcourse.jar
BigCorpPack.java
/*
* BigCorpPack - Building Packages
*/
package com.skillbuilders;
…
Below this all is the same as BigVOrpCreateClassC
Chapter 7:
Inheritance Workshop A
BigCorpInheritA.java
/*
* BigCorpInheritA - Inheritance workshop A
*/
import java.util.*;
}
}
class Person {
private String lName;
private String fName;
private int height;
public Person () {
population++;
}
public Person (String inFName, String inLName) {
this();
setLName(inLName);
setFName(inFName);
}
else
averageHeight = 0;
}
void walk() {
System.out.println(getFName() + " " + getLName() + " is walking");
}
void talk() {
System.out.println(getFName() + " " + getLName() + " is talking");
}
public Employee () {
}
void pay() {
System.out.println(getFName() + " " + getLName() + " was paid");
}
public void talk() {
System.out.println("Speaking as an employee");
}
void personalOpinion() {
super.talk();
System.out.println("That is my personal opinion");
}
}
Chapter 7:
Inheritance Workshop B
BigCorpInheritB.java
/*
* BigCorpInheritB - Inheritance - abstract - final
*/
import java.util.*;
// here's a Client
Client myClient1 = new Client("john", "Constructor", true);
System.out.println("My client is " + myClient1.getName()
+ ", their status is "
+ myClient1.isActive());
myClient1.setActive(false);
System.out.println("Now my client, " + myClient1.getName()
+ ", has status "
+ myClient1.isActive());
// Can't pay them - they're not an EMPLOYEE!
myClient1.talk();
myClient1.walk();
System.out.println(" ");
// pay my employees
myEmpl1.pay();
System.out.println(myEmpl1.getName() + "will be paid "
+ myEmpl1.getWallet());
myEmpl2.pay();
System.out.println(myEmpl2.getName() + " will be paid "
+ myEmpl2.getWallet());
myEmpl3.pay();
System.out.println(myEmpl3.getName() + " will be paid "
+ myEmpl3.getWallet());
}
}
public Person () {
population++;
}
public Person (String inFName, String inLName) {
this();
setLName(inLName);
setFName(inFName);
}
void walk() {
System.out.println(getFName() + " " + getLName()
+ " is walking");
}
void talk() {
System.out.println(getFName() + " " + getLName()
+ " is talking");
}
public Employee () {
}
public WageEmployee () {
}
void pay() {
wallet += (wage * hours);
}
}
public SalariedEmployee () {
}
void pay() {
wallet = salary;
}
}
public TempEmployee () {
}
void pay() {
wallet = pay + overtime;
}
}
Chapter 8:
Polymorphism Workshop
BigCorpPoly.java
/*
* BigCorpPoly - Polymorphism workshop
*/
package com.skillbuilders;
import java.util.*;
// here's a Client
Client myClient1 = new Client("john", "Constructor", true);
System.out.println("My client is " + myClient1.getName()
+ ", their status is "
+ myClient1.isActive());
myClient1.setActive(false);
System.out.println("Now my client, " + myClient1.getName()
+ ", has status "
+ myClient1.isActive());
// Can't pay them - they're not an EMPLOYEE! ==> myClient1.pay();
myClient1.talk();
myClient1.walk();
System.out.println(" ");
int i;
for (i = 0; i < staff.length; i++) {
staff[i].pay();
System.out.println(staff[i].getName() + " will be paid "
+ staff[i].getWallet());
}
}
}
public Person () {
population++;
}
public Person (String inFName, String inLName) {
this();
setLName(inLName);
setFName(inFName);
}
void walk() {
System.out.println(getFName() + " " + getLName()
+ " is walking");
}
void talk() {
System.out.println(getFName() + " " + getLName()
+ " is talking");
}
public Employee () {
}
public WageEmployee () {
}
void pay() {
wallet += (wage * hours);
}
}
public SalariedEmployee () {
}
void pay() {
wallet = salary;
}
}
public TempEmployee () {
}
void pay() {
wallet = pay + overtime;
}
}
Chapter 11:
HelloWorld Applet Workshop
HelloWorld.java (starter)
/*
HelloWorld.java
import java.applet.Applet;
import javax.swing.*;
HelloWorld.html
<HTML>
<APPLET CODE="HelloWorld.class"
WIDTH=300
HEIGHT=200>
</APPLET>
</HTML>
/*
HelloWorld.java
import java.applet.Applet;
import javax.swing.*;
Chapter 12:
Components Workshop
Sampler.java
/*
* Sampler - JButton, JTextField, JLabel
*/
package mylabs;
import javax.swing.*;
add(btnCancel);
Chapter 13:
Colors Workshop
Sampler.java
// Sampler.java
// Exercise after lesson on Colors, Fonts & Borders
package mylabs;
import java.awt.*;
import javax.swing.*;
// Set colors
setBackground( Color.lightGray ); // Window background
setForeground( Color.black ); // and foreground
_tfName.setBackground( Color.white );
_tfName.setForeground( Color.red );
_tfPass.setBackground( _tfName.getBackground() );
_tfPass.setForeground( _tfName.getForeground() );
_taAddress.setBackground( Color.white );
// Set fonts
_tfName.setFont( new Font( "Serif", Font.ITALIC, 20 ));
_tfPass.setFont( _tfName.getFont() );
_taAddress.setFont( new Font( "SansSerif", Font.BOLD, 16 ));
_taAddress.setBorder( BorderFactory.createTitledBorder("Address") );
Chapter 14:
Event Handling Workshop
Sampler.java
/*
* Sampler.java - Event handling: Main part
*/
package mylabs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Set colors
setBackground( Color.lightGray ); // Applet background
setForeground( Color.black ); // and foreground
tfName.setBackground( Color.white );
tfName.setForeground( Color.red );
tfPass.setBackground( tfName.getBackground() );
tfPass.setForeground( tfName.getForeground() );
taAddress.setBackground( Color.white );
// Set fonts
tfName.setFont( new Font( "Serif", Font.ITALIC, 20 ));
tfPass.setFont( tfName.getFont() );
taAddress.setFont( new Font( "SansSerif", Font.BOLD, 16 ));
// Register listeners
btnOK.addActionListener( this );
btnCancel.addActionListener( this );
tfName.addActionListener( this );
tfPass.addActionListener( this );
}
package mylabs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Set colors
setBackground( Color.lightGray ); // Applet background
setForeground( Color.black ); // and foreground
tfName.setBackground( Color.white );
tfName.setForeground( Color.red );
tfPass.setBackground( tfName.getBackground() );
tfPass.setForeground( tfName.getForeground() );
taAddress.setBackground( Color.white );
// Set fonts
tfName.setFont( new Font( "Serif", Font.ITALIC, 20 ));
tfPass.setFont( tfName.getFont() );
taAddress.setFont( new Font( "SansSerif", Font.BOLD, 16 ));
// Register listeners
btnOK.addActionListener( this );
btnCancel.addActionListener( this );
tfName.addActionListener( this );
tfPass.addActionListener( this );
tfName.addKeyListener( this );
tfPass.addKeyListener( this );
}
btnOK.setEnabled( false );
else
btnOK.setEnabled( true );
}
Chapter 15:
Collections Workshop
Using Collections
ListSystemProperties.java
import java.util.*;
Properties p = System.getProperties();
Enumeration e = p.propertyNames();
while( e.hasMoreElements() ) {
String strKey = (String) e.nextElement();
String strValue = p.getProperty( strKey, "Not found" );
System.out.println( strKey + " = " + strValue );
}
} // end of main()
} // end of class
of class
Chapter 16:
Inner Classes Workshop
LinkedList.java
/*
LinkedList.java
package mylabs;
import java.io.*;
import java.util.Enumeration;
// Methods
public void add( Object objData ) {
ListItem liNewItem = new ListItem( objData, null );
// Inner classes
private class ListItem {
private Object _objData;
private ListItem _liNext;
// Constructor
public ListItem( Object objData, ListItem liNext ) {
_objData = objData; // Assign data.
setNext( liNext ); // Assign links.
}
// Methods
public Object getData( ) {
return _objData;
}
public ListItem getNext( ) {
return _liNext;
}
public void setNext( ListItem liNext ) {
_liNext = liNext;
}
} // End of ListItem class
Presidents.txt
Herbert Hoover
Franklin Roosevelt
Harry Truman
Dwight Eisenhower
John Kennedy
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George Bush
Bill Clinton
George W. Bush
ListApp.java
// ListApp.java
package mylabs;
Chapter 17:
Throwing an Exception Workshop
BigCorpET.java
/*
* BigCorpThrow - Throw an Exception workshop
*/
package com.skillbuilders;
import java.util.*;
// here's a Client
Client myClient1 = new Client("john", "Constructor", true);
System.out.println("My client is " + myClient1.getName()
+ ", their status is "
+ myClient1.isActive());
myClient1.setActive(false);
System.out.println("Now my client, " + myClient1.getName()
+ ", has status "
+ myClient1.isActive());
// Can't pay them - they're not an EMPLOYEE! ==> myClient1.pay();
myClient1.talk();
myClient1.walk();
System.out.println(" ");
int i;
for (i = 0; i < staff.length; i++) {
staff[i].pay();
System.out.println(staff[i].getName() + " will be paid "
+ staff[i].getWallet());
}
}
}
public Person () {
population++;
}
public Person (String inFName, String inLName) {
this();
setLName(inLName);
setFName(inFName);
}
void walk() {
System.out.println(getFName() + " " + getLName()
+ " is walking");
}
void talk() {
System.out.println(getFName() + " " + getLName()
+ " is talking");
}
public Employee () {
}
public WageEmployee () {
}
void pay() {
wallet += (wage * hours);
}
}
public SalariedEmployee () {
}
void pay() {
wallet = salary;
}
}
public TempEmployee () {
}
void pay() {
wallet = pay + overtime;
}
}
Chapter 17:
Catching an Exception Workshop
BigCorpCatch.java
/*
* BigCorpCatch - Catch an Exception workshop
*/
package com.skillbuilders;
import java.util.*;
catch(IllegalArgumentException i) {
System.out.println
("My salaried employee had a negative salary");
return;
}
// here's a Client
Client myClient1 = new Client("john", "Constructor", true);
System.out.println("My client is " + myClient1.getName()
+ ", their status is "
+ myClient1.isActive());
myClient1.setActive(false);
System.out.println("Now my client, " + myClient1.getName()
+ ", has status "
+ myClient1.isActive());
// Can't pay them - they're not an EMPLOYEE! ==> myClient1.pay();
myClient1.talk();
myClient1.walk();
System.out.println(" ");
int i;
for (i = 0; i < staff.length; i++) {
staff[i].pay();
System.out.println(staff[i].getName() + " will be paid "
+ staff[i].getWallet());
}
}
}
public Person () {
population++;
}
public Person (String inFName, String inLName) {
this();
setLName(inLName);
setFName(inFName);
}
public Employee () {
}
return active;
}
void setActive(boolean active){
this.active = active;
}
public WageEmployee () {
void pay() {
wallet += (wage * hours);
}
}
void pay() {
wallet = salary;
}
}
public TempEmployee () {
}
void pay() {
wallet = pay + overtime;
}
Chapter 18:
Multi-Threading Workshop
HelloWorldMT.java
/*
* HelloWorld
*
* Thread exercise: Rainbow background on HelloWorld applet
*/
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
sb.setUnitIncrement(20);
sb.setBlockIncrement(100);
}
public void stop() {
System.out.println("stop invoked");
}
public void destroy() {
System.out.println("destroy invoked");
}
HelloWorldMT.html
<HTML>
<APPLET CODE="HelloWorldMT.class"
WIDTH=300
HEIGHT=200>
</APPLET>
</HTML>
Chapter 19:
Swing Containers Workshop
Sampler.java
/*
* Sampler - containers
*/
package mylabs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Create panels
JPanel pnlRed = new JPanel();
JPanel pnlCyan = new JPanel();
// Set colors
tfName.setBackground( Color.lightGray );
tfName.setForeground( Color.red );
tfPass.setBackground( tfName.getBackground() );
tfPass.setForeground( tfName.getForeground() );
taAddress.setBackground( Color.white );
// Set fonts
tfName.setFont( new Font( "Serif", Font.ITALIC, 20 ));
tfPass.setFont( tfName.getFont() );
taAddress.setFont( new Font( "SansSerif", Font.BOLD, 16 ));
// Second panel
// pnlCyan.add(new JLabel("Address:"));
pnlCyan.add(taAddress);
pnlCyan.setBackground(Color.cyan);
pnlMain.add(pnlCyan); // Add panel to main
// Third panel
pnlYellow.add(btnOK);
pnlYellow.add(btnCancel);
pnlYellow.setBackground(Color.yellow);
pnlCyan.add(pnlYellow); // Add 3rd panel to 2nd panel
// Register listeners
btnOK.addActionListener( this );
btnCancel.addActionListener( this );
tfName.addActionListener( this );
tfPass.addActionListener( this );
tfName.addKeyListener( this );
tfPass.addKeyListener( this );
btnOK.setEnabled( false );
else
btnOK.setEnabled( true );
}
Chapter 20:
Layout Manger Workshop
Sampler.java
/*
* Sampler - Layout Manager
*/
package mylabs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Create panels
JPanel pnlRed = new JPanel();
JPanel pnlCyan = new JPanel();
JPanel pnlYellow = new JPanel();
// Set colors
tfName.setBackground( Color.lightGray );
tfName.setForeground( Color.red );
tfPass.setBackground( tfName.getBackground() );
tfPass.setForeground( tfName.getForeground() );
taAddress.setBackground( Color.white );
// Set fonts
tfName.setFont( new Font( "Serif", Font.ITALIC, 20 ));
tfPass.setFont( tfName.getFont() );
taAddress.setFont( new Font( "SansSerif", Font.BOLD, 16 ));
// Second panel
// pnlCyan.add(new JLabel("Address:"));
pnlCyan.add(taAddress);
pnlCyan.setBackground(Color.cyan);
pnlMain.add(pnlCyan); // Add panel to main
// Third panel
pnlYellow.add(btnOK);
pnlYellow.add(btnCancel);
pnlYellow.setBackground(Color.yellow);
pnlCyan.add(pnlYellow); // Add 3rd panel to 2nd panel
// Register listeners
btnOK.addActionListener( this );
btnCancel.addActionListener( this );
tfName.addActionListener( this );
tfPass.addActionListener( this );
tfName.addKeyListener( this );
tfPass.addKeyListener( this );
btnOK.setEnabled( false );
else
btnOK.setEnabled( true );
}
Chapter 21:
Windowed Application Workshop
WinApp.java
/*
* WinApp
*
* Windowed application workshop
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
show();
}
mFile.addSeparator();
JMenuItem miExit = new JMenuItem("Exit");
miExit.setActionCommand("FileExit");
miExit.setMnemonic('x');
mFile.add(miExit);
; // Save here...
// If they said to quit, do so
if( bQuit )
System.exit(0);
return bQuit;
}
Bank.java
package mycode;
import course.bankapp.util.*;
import java.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Constructors
//================================================
//================================================
// Instance Methods
//================================================
// Various methods...
// More methods...
}
BankApp.java
package mycode;
import course.bankapp.audit.Auditable;
import course.bankapp.util.AccountValue;
import com.sb.application.ApplicationBase;
import java.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Instance methods invoked by GUI
//================================================
// Various methods...
// More methods...
Account.java
package mycode;
import course.bankapp.util.*;
import java.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Constructors
//================================================
//================================================
// Instance Methods
//================================================
return strDescription;
}
CheckingAccount.java
package mycode;
import course.bankapp.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Constructors
//================================================
//================================================
// Instance Methods
//================================================
iCheckNum++;
return super.debit(amount);
}
Bank.java
public AccountValue createAccount(AccountValue values) {
Account acct = null;
return values;
}
SavingsAccount.java
package mycode;
import course.bankapp.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Constructors
//================================================
//================================================
// Instance Methods
//================================================
fCurrInterest = BankUtil.getCurrentInterest();
AccountValue av = super.credit(amount);
return av;
}
Account.java
public class Account implements java.io.Serializable, Auditable {
// Various methods...
//================================================
// Instance Methods related to Auditing
//================================================
Bank.java
public class Bank implements java.io.Serializable, Auditable {
// Various methods...
//================================================
// Instance Methods related to Auditing
//================================================
BankApp.java
public Auditable auditBank() {
return bank;
}
Chapter 15 Advanced:
Using Collections
Account.java
/*
Account.java
*/
package mycode;
import course.bankapp.audit.*;
import course.bankapp.transaction.Transaction;
import course.bankapp.util.*;
import java.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Constructors
//================================================
strNumber = BankUtil.getNextAccountId();
strOwner = values.getOwner();
strType = values.getType();
setDescription(values.getDescription());
this.bank = bank;
//================================================
// Instance Methods
//================================================
//================================================
// Instance Methods related to Auditing
//================================================
Bank.java
/*
Bank.java
=====================================
After lab Collections
=====================================
package mycode;
import course.bankapp.audit.*;
import course.bankapp.util.*;
import java.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Constructors
//================================================
//================================================
// Instance Methods
//================================================
return values;
}
return alTrans;
}
//================================================
// Instance Methods related to Auditing
//================================================
BankApp.java
/*
BankApp.java
=====================================
After lab Collections
=====================================
*/
package mycode;
import course.bankapp.audit.Auditable;
import course.bankapp.util.AccountValue;
import com.sb.application.ApplicationBase;
import java.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Instance methods invoked by GUI
//================================================
CheckingAccount.java
/*
Account.java
*/
package mycode;
import course.bankapp.util.*;
//================================================
// Instance Variables
//================================================
//================================================
// Constructors
//================================================
//================================================
// Instance Methods
//================================================
iCheckNum++;
return super.debit(amount);
}
SavingsAccount.java
/*
Account.java
*/
package mycode;
import course.bankapp.util.*;
//================================================
// Instance Variables
//================================================
// Constructors
//================================================
//================================================
// Instance Methods
//================================================
fCurrInterest = BankUtil.getCurrentInterest();
AccountValue av = super.credit(amount);
return av;
}
Account.java
public AccountValue credit(double amount) {
if(amount < 0) {
String strMsg =
"Cannot perform credit; negative amounts not allowed";
throw new IllegalArgumentException(strMsg);
}
dBalance += amount;
recordTransaction(amount);
return new AccountValue(this);
}
// Prevent overdraft
if(amount > dBalance) {
String strMsg =
"Cannot perform debit; insufficient funds in account";
throw new InsufficientFundsException(getNumber(),
amount - dBalance, strMsg);
}
// Perform debit
dBalance -= amount;
recordTransaction(-amount);
return new AccountValue(this);
}
InsufficientFundsException.java
package mycode;
super(msg);
strAcctNum = acctNum;
dShortfall = shortfall;
}
CheckingAccount.java
public AccountValue debit(double amount)
throws InsufficientFundsException {
Bank.java
public AccountValue debitCurrentAccount(double amount)
throws InsufficientFundsException {
BankApp.java
public AccountValue debitCurrentAccount(double amount)
throws InsufficientFundsException {
Chapter 18 Advanced:
Multi-Threading
BankApp.java
public boolean createBank(String name) {
bank = new Bank(name);
new ApplyInterestThread();
return true;
}
//...
//...
SavingsAccount.java
public void applyInterest() {
double dInterest =
BankUtil.calculateInterest(getCurrentInterest(), getBalance());
credit(dInterest);
}
Bank.java
public int applyInterest() {
SavingsAccount[] sa =
BankUtil.getSavingsAccounts(hmAccounts.values().toArray());
for( int i = 0; i < sa.length; i++ )
sa[i].applyInterest();
return sa.length;
}