|
|
| Chapter 5 |
Copyright M.A.Smith 1999 may not be reproduced without written permission
5. The class
| Mike's account | Corinna's account |
![]() |
![]() |
| Class Diagram | Components |
![]() |
The class Account is composed of the instance variables: the_balance and the_min_balance and the methods: deposit, withdraw, account_balance and set_min_balance. |
| class | A collective name for all objects that have the same instance methods and variables. However, the contents of the instance variables may be different. |
| instance variables | The variables contained within the object that represent its internal state. |
| message | A request sent to an object to obey one of its hidden methods. |
| method | An action that manipulates or accesses the internal state of the object. The implementation of this action is however hidden from a client who sends messages to this object. |
| object | An item that has a hidden internal structure. The hidden structure is manipulated or accessed by messages sent to the object. |
Account mike = new Account(); |
| 1 | The declaration of the object | Account mike |
| 2 | The allocation of storage for the object. | = new Account() |
mike.deposit(100.00); |
| The components of this Java statement are illustrated in Figure 5.3 opposite. |
Figure 5.3 Sending a message to an object.
|
class Main
{
public static void main(String args[])
{
Account mike = new Account();
Account corinna = new Account();
double obtained;
System.out.println( "Mike's Balance = " +
mike.account_balance() );
mike.deposit(100.00);
System.out.println( "Mike's Balance = " +
mike.account_balance() );
obtained = mike.withdraw(20.00);
System.out.println( "Mike has withdrawn : " + obtained );
System.out.println( "Mike's Balance = " +
mike.account_balance() );
corinna.deposit(50.00);
System.out.println( "Corinna's Balance = " +
corinna.account_balance() );
}
}
|
Mike's Balance = 0.0 Mike's Balance = 100.0 Mike has withdrawn : 20.0 Mike's Balance = 80.0 Corinna's Balance = 50.0 |
| Name | Remarks |
| account_balance | Returns the current balance of the account. |
| withdraw | Withdraws money from the account. Naturally money can only be withdrawn if the account balance stays above or equal to the minimum balance allowed. |
| deposit | Deposits money into the account. |
| set_min_balance | Sets the minimum balance that the account must hold. A negative amount indicates an overdraft is allowed. |
| Name | Remarks |
| the_balance | Holds the current balance of the account. |
| the_min_balance | The minimum balance that the account is allowed to go to. If the account holder was allowed an overdraft of £200.00 then the_min_balance would contain -200.00. |
class Account
{
|
private double the_balance = 0.0d; //Balance of account private double the_min_balance = 0.0d; //Minimum bal (Overdraft) |
public Account() {
the_balance = the_min_balance = 0.00;
}
|
public double account_balance()
{
return the_balance;
}
|

public double withdraw( final double money)
{
if ( the_balance - money >= the_min_balance )
{
the_balance = the_balance - money;
return money;
} else {
return 0.00;
}
}
|

public void deposit( final double money )
{
the_balance = the_balance + money;
}
|

public void set_min_balance( final double money )
{
the_min_balance = money;
}
|
} |

Account mike = new Account();
Account shallow_copy_mike = mike;
mike.deposit(100.00);
System.out.println("Balance account mike = " +
mike.account_balance() );
System.out.println("Balance account shallow copy mike = " +
shallow_copy_mike.account_balance() );
|
Balance account mike = 100.0 Balance account shallow copy mike = 100.0 |

Account mike = null; // The default value
if ( mike == null )
{
// No storage allocated for object
}
|
public void process()
{
Account mike = new Account(); // Processing on the object
}
|
| Allocation of the handle | Account mike |
| Allocation of the objects storage | = new Account(); |
System.gc(); |
[a] Deposit [b] Withdraw [c] Balance Input selection: |
Input selection: a Amount to deposit = 100 |
Input selection : c Balance = #100.0 |
| Method | Responsibility |
| menu | Sets up the menu that will be displayed to the user. Each menu item is described by a string. |
| chose_option | Returns the menu item selected by a user of the TUI. |
| message(s) | Display the message s to a user. |
| dialogue(s) | Displays the message prompt s to a user returning the string they type as a response. |
| dialogue_int(s) | Displays the message prompt s to a user returning the number they type as an int. |
| dialogue_double(s) | Displays the message prompt s to a user returning the number they type as a double value. |
class Menu_item
{
public static final int M_1 = 1;
public static final int M_2 = 2;
public static final int M_3 = 3;
public static final int M_4 = 4;
public static final int M_5 = 5;
public static final int NONE = 0;
}
|
class TUI
{
private String men_1 = ""; //Name of 1st menu item
private String men_2 = ""; //Name of 2nd menu item
private String men_3 = ""; //Name of 3rd menu item
private String men_4 = ""; //Name of 4th menu item
private String men_5 = ""; //Name of 5th menu item
private static final int EOF = -1;
}
|
public void menu( final String m1, final String m2,
final String m3, final String m4,
final String m5 )
{
men_1 = m1; men_2 = m2; men_3 = m3;
men_4 = m4; men_5 = m5; //Store names
|
public int chose_option()
{
int choice = Menu_item.NONE;
|
while ( choice == Menu_item.NONE ) //While no valid reply
{
char selection = ' '; System.out.println("");
display_menu_item( "[a] ", men_1 ); //First menu item
display_menu_item( "[b] ", men_2 ); //Second ..
display_menu_item( "[c] ", men_3 ); //Third
display_menu_item( "[d] ", men_4 ); //Fourth
display_menu_item( "[e] ", men_5 ); //Fifth
|
String res = dialogue("Input selection"); //Response
res = res.toLowerCase().trim(); //Lower case
if ( res.length() >= 1 )
selection = res.charAt(0); //First char
|
switch ( selection )
{
case 'a' :
if ( ! men_1.equals( "" ) ) choice = Menu_item.M_1;
break;
case 'b' :
if ( ! men_2.equals( "" ) ) choice = Menu_item.M_2;
break;
case 'c' :
if ( ! men_3.equals( "" ) ) choice = Menu_item.M_3;
break;
case 'd' :
if ( ! men_4.equals( "" ) ) choice = Menu_item.M_4;
break;
case 'e' :
if ( ! men_5.equals( "" ) ) choice = Menu_item.M_5;
break;
}
if ( choice == Menu_item.NONE )
message( "Invalid response" );
return choice; //User selection
}
|
private void display_menu_item( String prompt, String name )
{
if ( ! name.equals( "" ) ) //Not null String so print
{
System.out.println( prompt + name );
System.out.println( "" );
}
}
|
public void message( final String mes )
{
System.out.println( mes );
}
|
public String dialogue( final String mes )
{
String line = ""; //Line read
int ch;
System.out.print( mes + " : " ); //Prompt
try
{
ch = System.in.read(); //Read ch
while ( ch != '\n' && ch != EOF ) //While !EOL
{
line += (char) ch; // append ch
ch = System.in.read(); // next ch
if ( ch == '\r' ) ch = System.in.read();// Skip ''
}
if ( line.equals("") && ch == EOF )
System.exit(-1); //Exit **
return line; //return line
}
catch( IOException exp ) //Problem
{
System.exit(-1); //Exit **
}
return ""; //Blank line
}
|
public double dialogue_double( final String mes)
{
String res = dialogue( mes ); //Read line
double value = 0.0; //
try
{
value = Double.parseDouble( res ); //Convert
}
catch ( NumberFormatException ex ) //Problem
{ // ignore
}
return value; //return
}
|
public int dialogue_int( final String mes )
{
String res = dialogue( mes ); //Read line
int value = 0; //
try
{
value = Integer.parseInt( res ); //Convert
}
catch ( NumberFormatException ex ) //Problem
{ // ignore
}
return value; //return
}
}
|
class Main
{
public static void main( String args[] )
{
Account mine = new Account(); // My Account
TUI screen = new TUI(); // Interaction screen
double amount; // money
screen.menu("Deposit", "Withdraw", "Balance", "", "" );
while ( true )
{
switch ( screen.chose_option() )
{
|
case Menu_item.M_1 :
amount = screen.dialogue_double("Amount to deposit");
if ( amount >= 0.0 )
{
mine.deposit( amount );
} else {
screen.message("Amount must be positive");
}
break;
|
case Menu_item.M_2 :
amount = screen.dialogue_double("Amount to withdraw");
if ( amount > 0.0 )
{
double get = mine.withdraw( amount );
if ( get <= 0.0 )
screen.message("Sorry not enough funds");
} else {
screen.message("Amount not valid");
}
break;
|
case Menu_item.M_3 :
{
String mes = "Balance = #" +
String.valueOf(mine.account_balance());
screen.message( mes ); //"Bal ... "
}
break;
}
}
}
}
|
|
Deposit an initial 100.00 into the account. |
[a] Deposit [b] Withdraw [c] Balance Input selection : a Amount to deposit : 100.00 |
| Try and take 200.00 from the account. |
[a] Deposit [b] Withdraw [c] Balance Input selection : b Amount to withdraw : 200.00 Sorry not enough funds |
| Show the balance |
[a] Deposit [b] Withdraw [c] Balance Input selection : c Balance = #100.0 |
interface Account_protocol
{
public double account_balance();
public void deposit( final double money);
public double withdraw( final double money );
}
|
class Simple_Account implements Account_protocol
{
private double the_balance = 0.0d; //Balance of account
public double account_balance()
{
return the_balance;
}
public double withdraw( final double money )
{
if ( the_balance - money >= 0.00 )
{
the_balance = the_balance - money;
return money;
} else {
return 0.00;
}
}
public void deposit( final double money )
{
the_balance = the_balance + money;
}
}
|
public double transfer( Account_protocol other, final double money )
{
if ( money > 0.00 )
{
double obtain = other.withdraw( money );
if ( obtain != 0.00 )
{
deposit( money );
return money;
}
}
return 0.00;
}
}
|
class Main
{
public static void main( String args[] )
{
Simple_Account mike = new Simple_Account();
Simple_Account corinna = new Simple_Account();
double obtained;
mike.deposit(100.00);
System.out.println( "Mike's Balance = " +
mike.account_balance() );
corinna.deposit(150.00);
System.out.println( "Corinna's Balance = " +
corinna.account_balance() );
corinna.transfer( mike, 80.00 );
System.out.println( "Mike's Balance = " +
mike.account_balance() );
System.out.println( "Corinna's Balance = " +
corinna.account_balance() );
}
}
|
Mike's Balance = 100.0 Corinna's Balance = 150.0 Mike's Balance = 20.0 Corinna's Balance = 230.0 |
| A static nested class | This may only access the enclosing classes class variables or methods. |
| A non static nested class also called an inner class. | This may access all the enclosing classes member variables or methods. |
class Car
{
class Engine //Inner class
{
private double the_capacity; //Engine Capacity
public Engine( double engine_capacity ) //Constructor
{
the_capacity = engine_capacity;
}
public double engine_size() //Return Engine size
{
return the_capacity;
}
}
Engine the_engine = new Engine( 1.3 ); //Instance of engine
public double capacity() //Return capacity
{
return the_engine.engine_size();
}
}
|
| Method | Responsibility |
| book_seats | Books n seats at the performance. |
| cancel | Un-books n seats. |
| seats_free | Returns the number of seats that are still unsold. |
| seats_booked | Returns the number of seats sold for this performance. |