Top Banner
MSc IT Programming Methodology (2)
127

MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Jan 01, 2016

Download

Documents

Amos Wright
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

MSc IT

Programming Methodology (2)

Page 2: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

EasyScanner

BankAccount

Page 3: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Implementing Classes

Learning objectives

• design classes using the notation of the Unified Modelling Language (UML);

• write the Java code for a specified class;

• explain the difference between public and private access to attributes and methods;

• explain the use of the static keyword;

• pass objects as parameters.

Page 4: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Structured programming…

Page 5: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

// switch on light

// get building

// check if passed

main

boolean light;

int mark;

String roomNumber;

Page 6: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Object-oriented programming…

Page 7: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

String roomNumber;

int mark;

// switch on light

// get building

// check if passed

Room Student Projector

boolean light;

Page 8: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Looking inside a class….

Page 9: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.
Page 10: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Student

Page 11: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

data

Student

Page 12: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

name

number

marks

Student

data

Page 13: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

name

number

marks

Student

data methods

Page 14: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

name

number

marks

Student

data methods

getName

getNumber

setMarks

Student

Page 15: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

Page 16: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

data

Page 17: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

data

length

height

Page 18: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

data

length

height

methods

Page 19: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

data

length

height

methods

getLength

calculateArea

Oblong

Page 20: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

The notation of the

Unified Modelling Language (UML)

Page 21: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length height

Oblong( )

setLength( )

setHeight( )

getLength( )

getHeight( )

calculateArea( )

calculatePerimeter( )

Class Name

data

methods

attributes

Page 22: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : ? height : ?

Oblong( ? )

setLength( ? ): ?

setHeight( ? ): ?

getLength( ? ): ?

getHeight( ? ): ?

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 23: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : ?

Oblong( ? )

setLength( ? ): ?

setHeight( ? ): ?

getLength( ? ): ?

getHeight( ? ): ?

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 24: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( ? )

setLength( ? ): ?

setHeight( ? ): ?

getLength( ? ): ?

getHeight( ? ): ?

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 25: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( ? ): ?

setHeight( ? ): ?

getLength( ? ): ?

getHeight( ? ): ?

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 26: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( double )

setHeight( ? ): ?

getLength( ? ): ?

getHeight( ? ): ?

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 27: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( double )

setHeight( double )

getLength( ? ): ?

getHeight( ? ): ?

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 28: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( double )

setHeight( double )

getLength( ): double

getHeight( ? ): ?

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 29: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( double )

setHeight( double )

getLength( ): double

getHeight( ): double

calculateArea( ? ): ?

calculatePerimeter( ? ): ?

Page 30: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( double )

setHeight( double )

getLength( ): double

getHeight( ): double

calculateArea( ): double

calculatePerimeter( ? ): ?

Page 31: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( double )

setHeight( double )

getLength( ): double

getHeight( ): double

calculateArea( ): double

calculatePerimeter( ): double

Page 32: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Oblong

length : double height : double

Oblong( double, double )

setLength( double )

setHeight( double )

getLength( ): double

getHeight( ): double

calculateArea( ): double

calculatePerimeter( ): double

public class Oblong

{

// attributes

// methods

}

Page 33: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

Attributes declared outside of any methods

Page 34: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

Page 35: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

Attributes should be hidden inside the class

Page 36: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Page 37: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Implementing the Oblong methods…

Page 38: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Page 39: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double ?, double ?) {

}

// more methods here

Page 40: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double ?, double ?) {

}

// more methods here

Methods should be visible outside of the class

Page 41: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double ?, double ?) {

}

// more methods here

Methods should be visible outside of the class

public

Page 42: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double ?, double ?) {

}

// more methods here

public

A constructor has no return type

Page 43: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double ?, double ?) {

}

// more methods here

public

Page 44: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double lengthIn, double ?) {

}

// more methods here

public

Page 45: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double lengthIn, double ?) {

}

// more methods here

public

Page 46: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double lengthIn, double heightIn)

{

}

// more methods here

public

Page 47: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double lengthIn, double heightIn)

{

// set the values of length and height}

// more methods here

public

Page 48: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double lengthIn, double heightIn)

{

// set the values of length and height}

// more methods here

public

Page 49: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double lengthIn, double heightIn)

{ length = lengthIn;

}

// more methods here

public

Page 50: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Oblong(double lengthIn, double heightIn)

{ length = lengthIn;

height = heightIn;}

// more methods here

public

Page 51: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

What happens if you don’t write a constructor?

Page 52: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

A default constructor is added for you!

Page 53: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong(){

}

public

// more methods here

Page 54: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong(){

}

public

// more methods here

A default constructor has no code inside it.

Page 55: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong(){

}

public

// more methods here

Oblong ob1 = new Oblong ( );

Page 56: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong(){

}

public

// more methods here

Oblong ob1 = new Oblong (12.5, 7.5);

Page 57: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

Oblong ob1 = new Oblong (12.5, 7.5);

Page 58: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

public Oblong(double lengthIn, double heightIn)

{

}

// more methods here

length = lengthIn;

height = heightIn;

Oblong ob1 = new Oblong (12.5, 7.5);

Page 59: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

System.out.println( ob1.getLength() );

getLength(){ }

public double

return length;

// more methods here

Page 60: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

System.out.println( ob1.getHeight() );

getHeight(){ }

public double

return height;

// more methods here

Page 61: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setLength( 14.75);

setLength( double ?){ }

public

// more methods here

Page 62: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setLength( 14.75);

setLength( double ?){ }

public

// more methods here

void

Page 63: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setLength( 14.75);

setLength( double ?){ }

public

// more methods here

void

Page 64: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setLength( 14.75);

setLength( double lengthIn){ }

public

// more methods here

void

Page 65: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setLength( 14.75);

setLength( double lengthIn){ // reset length}

public

// more methods here

void

Page 66: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setLength( 14.75);

setLength( double lengthIn){ }

public void

length = lengthIn;

// more methods here

Page 67: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setHeight( 5.9 );

// more methods here

Page 68: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

ob1.setHeight( 5.9 );

setHeight( double heightIn){ }

public void

height = heightIn;

// more methods here

Page 69: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

System.out.println( ob1.calculateArea() );

// more methods here

Page 70: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

System.out.println( ob1.calculateArea() );

// more methods here

calculateArea(){ }

public double

return length * height;

Page 71: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

System.out.println( ob1.calculatePerimeter());

// more methods here

Page 72: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class Oblong

{

}

double length; double height;

privateprivate

System.out.println( ob1.calculatePerimeter());

// more methods here

calculatePerimeter(){ }

public double

return 2 * (length + height);

Page 73: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

The BankAccount class….

Page 74: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

BankAccount

accountNumber : String

accountName : String

balance : double

BankAccount (String, String)

getAccountNumber() : String

getAccountName() : String

getBalance() : double

deposit(double)

withdraw(double)

Page 75: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

Page 76: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

Page 77: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

public BankAccount(String numberIn, String nameIn)

{

accountNumber = numberIn;

accountName = nameIn;

balance = 0;

}

Page 78: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

public String getAccountName()

{

return accountName;

}

Page 79: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

public String getAccountNumber()

{

return accountNumber;

}

Page 80: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

public String getABalance()

{

return balance;

}

Page 81: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

public void deposit(double amountIn)

{

balance = balance + amountIn;

}

Page 82: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount

{

private String accountNumber;

private String accountName;

private double balance;

}

public void withdraw(double amountIn)

{

balance = balance – amountIn;

}

Page 83: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccountTester

{

public static void main(String[ ] args)

{

BankAccount account1

= new BankAccount("99786754","Susan Richards");

account1.deposit(1000);

System.out.println("Account number: "

+ account1.getAccountNumber());

System.out.println("Account name: " +

account1.getAccountName());

System.out.println("Current balance: " + account1.getBalance());

}

}

Page 84: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Amending the BankAccount class…

Page 85: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £975.12

“07721009” “Dilraj Mann” £3975.75

acc1

acc2

acc3

1.25%

1.25%

1.25%

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

Page 86: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £3975.75

acc1

acc2

acc3

1.25%

1.25%

1.25%

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

Page 87: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £3975.75

acc1

acc2

acc3

1.25%

1.25%

1.25%

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

acc2.getInterestRate();

Page 88: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £3975.75

acc1

acc2

acc3

1.25%

1.25%

1.25%

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

acc2.getInterestRate();

acc3.deposit( 500 );

Page 89: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.25%

1.25%

1.25%

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

acc2.getInterestRate();

acc3.deposit( 500 );

Page 90: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.25%

1.25%

1.25%

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

acc2.getInterestRate();

acc3.deposit( 500 );

acc3.setInterestRate(1.5);

Page 91: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.25%

1.25%

1.5%

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

acc2.getInterestRate();

acc3.deposit( 500 );

acc3.setInterestRate(1.5);

Page 92: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

acc1.getBalance();

acc1.getInterestRate();

acc2.addInterest ();

acc2.getInterestRate();

acc3.deposit( 500 );

acc3.setInterestRate(1.5);

1.5%

Page 93: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.5%

acc1.getBalance();

acc2.addInterest ();

acc3.deposit( 500 );

Page 94: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.5%

acc1.getBalance();

acc2.addInterest ();

acc3.deposit( 500 );

BankAccount.setInterestRate(1.4);

Page 95: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.4%

acc1.getBalance();

acc2.addInterest ();

acc3.deposit( 500 );

BankAccount.setInterestRate(1.4);

Page 96: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.4%

acc1.getBalance();

acc2.addInterest ();

acc3.deposit( 500 );

BankAccount.setInterestRate(1.4);

BankAccount.getInterestRate( );

Page 97: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.4%

acc1.getBalance();

acc2.addInterest ();

acc3.deposit( 500 );

BankAccount.setInterestRate(1.4);

BankAccount.getInterestRate( );

This is a class attribute.

Page 98: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

“0012765” “Funmi Odulopo” £1200.49

“09887254” “Mary Stephenson” £987.31

“07721009” “Dilraj Mann” £4475.75

acc1

acc2

acc3

1.4%

acc1.getBalance();

acc2.addInterest ();

acc3.deposit( 500 );

BankAccount.setInterestRate(1.4);

BankAccount.getInterestRate( );

This is a class method.

Page 99: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

How do we create class attributes and class methods?

Page 100: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Use the static keyword!

Page 101: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

private static double interestRate;

public static void setInterestRate(double rateIn)

{

interestRate = rateIn;

}

public static double getInterestRate()

{

return interestRate;

}

public class BankAccount2{ // attributes and methods as before plus addInterest

}

Page 102: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

The addInterest method ….

Page 103: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount2

{

private String accountNumber;

private String accountName;

private double balance;

}

private static double interestRate;

public void addInterest()

{

balance = balance + (balance * interestRate)/100;

}

Page 104: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

The BankAccount2 constructor ?

Page 105: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount2

{

private String accountNumber;

private String accountName;

private double balance;

}

private static double interestRate;

public BankAccount2(String numberIn, String nameIn)

{

accountNumber = numberIn;

accountName = nameIn;

balance = 0;

}

Page 106: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

What about the interestRate attribute?

Page 107: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Java does not give an initial value to local variables but does initialize attributes!

Page 108: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Initializing attributes

int

double

char

Objects0

boolean false null

Page 109: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class BankAccount2

{

private String accountNumber;

private String accountName;

private double balance;

private static double interestRate ;

// methods here

}

= 0;

Page 110: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

The EasyScanner class…

Page 111: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Remember how we used EasyScanner?

int x;

System.out.println(“Enter number”);

X = EasyScanner.nextInt() ;

This must be a class method!

Page 112: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

import java.util.*;

public class EasyScanner

{

public static int nextInt()

{

Scanner sc = new Scanner(System.in);

int i = sc.nextInt();

return i;

}

// more methods here

}

Page 113: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Sending objects as parameters….

Page 114: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public class ParameterTest

{

public static void main(String[ ] args)

{

BankAccount acc = new BankAccount("1", "Samsun Okoyo");

test(acc);

System.out.println("Account Number: " + acc.getAccountNumber());

System.out.println("Account Name: " + acc.getAccountName());

System.out.println("Balance: " + acc.getBalance());

}

private static void test(BankAccount accIn)

{

accIn.deposit(2500);

}

}

Page 115: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Account Number: 1

Account Name: Samsun Okoyo

Balance: 2500.0

Page 116: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Effect on computer memory of sending an object as a parameter…

Page 117: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

public static void main (String[] args)

{

BankAccount acc = new BankAccount(…) ;

}

Computer Memory Java Instructions

acc

a BankAccount

object

private static void test(BankAccount accIn)

{

}

accIn

test(acc);

accIn.deposit(2500);

Page 118: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

Practical Work

Page 119: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

public class Student

{

// attributes

// methods

}

Implement this class in your practical

Page 120: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

public class Student

{

// attributes

// methods

}

Implement this class in your practical

Page 121: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

public class Student

{

private int markForMaths;

// more attributes here

// methods

}

Implement this class in your practical

Page 122: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

public class Student

{

private int markForMaths;

// more attributes here

// methods

}

Implement this class in your practical

Page 123: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

Implement this class in your practical

public class Student

{

private int markForMaths;

// more attributes here

public int getMathsMark( ) {

return markForMaths; }

// more methods here

}

Page 124: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

Then write a tester to check your class

Page 125: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

Then write a tester to check your class

public class StudentTester

{

public static void main(String[ ] args) {

}

}

Page 126: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.

StudentstudentNumber : StringstudentName : StringmarkForMaths : intmarkForEnglish : intmarkForScience : int

Student(String, String)getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : intgetEnglishMark() : int getScienceMark() : int calculateAverageMark() : double

Then write a tester to check your class

public class StudentTester

{

public static void main(String[ ] args) {

/* code to create at least two Student objects and call

their methods */ }

}

Page 127: MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.