Top Banner
Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling 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; implement collection classes based on arrays.
52

Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Dec 16, 2015

Download

Documents

Ronald Boyd
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: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Implementing Classes

Learning objectives

By the end of this lecture you should be able to:

• design classes using the notation of the Unified Modeling 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;

• implement collection classes based on arrays.

Page 2: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Implementing classes in Java

A class consists of:

a set of attributes (the data);

a set of methods

Oblong

length

height : double

: double

setLength

setHeight

getLength

getHeight

calculateArea

calculatePerimeter

Oblong

()

: double

(double)

(double)

()

()

()

: double

: double

: double

(double, double)

Page 3: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

The notation of the Unified Modeling Language (UML)

Oblong

length : double

height : double

Oblong(double, double)

setLength(double)

setHeight(double)

getLength() : double

getHeight() : double

calculateArea() : double

calculatePerimeter() : double

Page 4: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public class Oblong

{

double length;

double height;

private

private

public Oblong(double lengthIn, double heightIn)

{

}

// more methods here

}

length = lengthIn;

height = heightIn;

Page 5: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

getLength()

{

}

public double

return length;

getHeight()

{

}

public double

return height;

Page 6: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

setLength( )

{

}

public void

length = lengthIn;

double lengthIn

setHeight( )

{

}

public void

height = heightIn;

double heightIn

Page 7: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

calculateArea()

{

}

public double

return length * height;

calculatePerimeter()

{

}

public double

return 2 * (length + height);

Page 8: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

The BankAccount class

BankAccount

accountNumber : String

accountName : String

balance : double

BankAccount (String, String)

getAccountNumber() : String

getAccountName() : String

getBalance() : double

deposit(double)

withdraw(double)

Page 9: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

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 10: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public String getAccountName()

{

return accountName;

}

public String getAccountNumber()

{

return accountNumber;

}

public double getBalance()

{

return balance;

}

Page 11: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public void deposit(double amountIn)

{

balance = balance + amountIn;

}

public void withdraw(double amountIn)

{

balance = balance – amountIn;

}

}

Page 12: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

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 13: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Amending the BankAccount class

“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 ();

acc2.getInterestRate();

acc3.deposit( 500 );

acc3.setInterestRate(1.5);

Bank.setInterestRate(1.4);

Bank.getInterestRate();

Page 14: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

The static keyword

private static double interestRate;

public static void setInterestRate(double rateIn)

{

interestRate = rateIn;

}

public static double getInterestRate()

{

return interestRate;

}

Page 15: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

The addInterest method

public void addInterest()

{

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

}

Page 16: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public class BankAccountTester2

{

public static void main(String[] args)

{

BankAccount2 account1 =

new BankAccount2("99786754","Varinder Singh");

BankAccount2 account2 =

new BankAccount2("99887776","Lenny Roberts");

account1.deposit(1000);

account2.deposit(2000);

BankAccount2.setInterestRate(10);

account1.addInterest();

Page 17: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

System.out.println("Account number: " + account1.getAccountNumber());

System.out.println("Account name: " + account1.getAccountName());

System.out.println("Interest Rate " + BankAccount2.getInterestRate());

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

System.out.println();

System.out.println("Account number: " + account2.getAccountNumber());

System.out.println("Account name: " + account2.getAccountName());

System.out.println("Interest Rate " + BankAccount2.getInterestRate());

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

}

}

Page 18: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Account number: 99786754

Account name: Varinder Singh

Interest rate: 10.0

Current balance: 1100.0

Account number: 99887776

Account name: Lenny Roberts

Interest rate: 10.0

Current balance: 2000.0

Page 19: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Initializing attributes

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

int

double

char

Objects0

boolean false null

private static double interestRate = 0;

Page 20: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

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 21: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public static double nextDouble()

{

Scanner sc = new Scanner(System.in);

double d = sc.nextDouble();

return d;

}

public static String nextString()

{

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

return s;

}

Page 22: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public static char nextChar()

{

Scanner sc = new Scanner(System.in);

char c = sc.next().charAt(0);

return c;

}

Page 23: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

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 24: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Account Number: 1

Account Name: Samsun Okoyo

Balance: 2500.0

Page 25: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Effect on computer memory

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 26: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Collection classes

When one object itself consists of other objects, this relationship is called aggregation;

A collection class is an implementation of the aggregation relationship.

BankAccountBank *

Page 27: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Bank

list: BankAccount [ ]

total : int

Bank(int)

search(String) : int

getTotal() : int

isEmpty() : boolean

isFull() : boolean

add(BankAccount) : boolean

getItem(String) : BankAccount

depositMoney(String, double) : boolean

withdrawMoney(String, double) : boolean

remove(String) : boolean

Page 28: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public class Bank

{

private BankAccount[] list;

private int total;

public Bank(int sizeIn)

{

list = new BankAccount[sizeIn];

total = 0;

}

Page 29: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

private int search(String accountNumberIn)

{

for(int i = 0; i ; i++)

{

}

return -999;

}

BankAccount tempAccount = list[i];

String tempNumber

= tempAccount.getAccountNumber();

if(tempNumber.equals(accountNumberIn))

{

return i;

}

< total

Page 30: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public int getTotal(){ return total;}

public boolean isEmpty(){ if (total == 0) { return true; } else { return false; }}

Page 31: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public boolean isFull()

{

if (total == list.length)

{

return true;

}

else

{

return false;

}

}

Page 32: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public boolean add(BankAccount accountIn)

{

if( )

{

return true;

}

else

{

return false;

}

}

(!isFull()

list[total] = accountIn;

total++;

Page 33: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public BankAccount getItem(String accountNumberIn)

{

int index;

index = search(accountNumberIn);

if(index == -999)

{

return null;

}

else

{

return list[index];

}

}

Page 34: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public boolean depositMoney

(String accountNumberIn, double amountIn)

{

int index = search(accountNumberIn);

if(index == -999)

{

return false;

}

else

{

list[index].deposit(amountIn);

return true;

}

}

Page 35: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public boolean remove(String numberIn)

{

int index = search(numberIn);

if(index == -999)

{

return false;

}

else

{

// remove item from list

return true;

}

}

Page 36: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

4th item

3rd item

2nd item

1st item

Item to delete

5th item Smith

Adams

Patel

Okoya

Ling

Patel

Adams

Smith

list

list[index] = list[index+1];

list[index+1] = list[index+2];

list[index+2] = list[index+3];

total--;

list[i] = list[i+1];

for(int i = index; i<= total-2; i++)

{

}

Page 37: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

public class BankProgram

{

public static void main(String[] args)

{

char choice;

int size;

System.out.print("Maximum number of accounts? ");

size = EasyScanner.nextInt();

Bank myBank = new Bank(size);

Page 38: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

do

{

System.out.println();

System.out.println("1. Create new account");

System.out.println("2. Remove an account");

System.out.println("3. Deposit money");

System.out.println("4. Withdraw money");

System.out.println("5. Check account details");

System.out.println("6. Quit");

System.out.println();

System.out.print("Enter choice [1-6]: ");

choice = EasyScanner.nextChar();

System.out.println();

Page 39: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

switch (choice)

{

case '1': option1(myBank); break;

case '2': option2(myBank); break;

case '3': option3(myBank); break;

case '4': option4(myBank); break;

case '5': option5(myBank); break;

case '6': break;

default: System.out.println("Invalid entry");

}

}while (choice != '6');

}

Page 40: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

// add account

private static void option1(Bank bankIn)

{

System.out.print("Enter account number: ");

String number = EasyScanner.nextString();

System.out.print("Enter account name: ");

BankAccount account = new BankAccount(number, name);

boolean ok = bankIn.add(account);

if (!ok)

{

System.out.println("The list is full");

}

else

{

System.out.println("Account created");

}

}

Page 41: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

// remove account

private static void option2(Bank bankIn)

{

System.out.print("Enter account number: ");

String number = EasyScanner.nextString();

boolean ok = bankIn.remove(number);

if (!ok)

{

System.out.println("No such account number");

}

else

{

System.out.println("Account removed");

}

}

Page 42: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

// deposit money

private static void option3(Bank bankIn)

{

System.out.print("Enter account number: ");

String number = EasyScanner.nextString();

System.out.print("Enter amount to deposit: ");

double amount = EasyScanner.nextDouble();

boolean ok = bankIn.depositMoney(number, amount);

if (!ok)

{

System.out.println("No such account number");

}

else

{

System.out.println("Money deposited");

}

}

Page 43: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

// withdraw money from an account

private static void option4(Bank bankIn)

{

System.out.print("Enter account number: ");

String number = EasyScanner.nextString();

System.out.print("Enter amount to withdraw: ");

double amount = EasyScanner.nextDouble();

boolean ok = bankIn.withdrawMoney(number, amount);

if (!ok)

{

System.out.println("No such account number");

}

else

{

System.out.println("Money withdrawn");

}

}

Page 44: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

// check account detailsprivate static void option5(Bank bankIn){ System.out.print("Enter account number "); String number = EasyScanner.nextString(); BankAccount account = bankIn.getItem(number); if (account == null) { System.out.println("No such account number"); } else { System.out.println("Account number: " +

account.getAccountNumber()); System.out.println("Account name: " +

account.getAccountName()); System.out.println("Balance: " +

account.getBalance()); System.out.println(); }}

Page 45: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

Maximum number of accounts? 100

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]: 1

Page 46: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]:

Enter account number:

Enter account name:

Account created

63488965

Paula Wilkins

1

Page 47: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]:

Enter account number:

Enter account name:

Account created

14322508

Sydney Isaacs

1

Page 48: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]:

Enter account number:

Enter account name:

Account created

90871435

Delroy Joseph

3

Page 49: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]:

Enter account number:

Enter amount to deposit:

Money deposited

90871435

1500

2

Page 50: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]:

Enter account number:

Account removed

14322508

5

Page 51: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]:

Enter account number:

No such account number

14322508

5

Page 52: Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.

1. Create new account

2. Remove an account

3. Deposit money

4. Withdraw money

5. Check account details

6. Quit

Enter choice [1-6]:

Enter account number:

Account number: 90871435

Account name: Delroy Joseph

Balance: 1500.0

90871435

6