msc it programming methodology (2). oblong easyscanner bankaccount
TRANSCRIPT
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.
// switch on light
// get building
// check if passed
main
boolean light;
int mark;
String roomNumber;
String roomNumber;
int mark;
// switch on light
// get building
// check if passed
Room Student Projector
boolean light;
Oblong
length height
Oblong( )
setLength( )
setHeight( )
getLength( )
getHeight( )
calculateArea( )
calculatePerimeter( )
Class Name
data
methods
attributes
Oblong
length : ? height : ?
Oblong( ? )
setLength( ? ): ?
setHeight( ? ): ?
getLength( ? ): ?
getHeight( ? ): ?
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : ?
Oblong( ? )
setLength( ? ): ?
setHeight( ? ): ?
getLength( ? ): ?
getHeight( ? ): ?
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( ? )
setLength( ? ): ?
setHeight( ? ): ?
getLength( ? ): ?
getHeight( ? ): ?
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( double, double )
setLength( ? ): ?
setHeight( ? ): ?
getLength( ? ): ?
getHeight( ? ): ?
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( double, double )
setLength( double )
setHeight( ? ): ?
getLength( ? ): ?
getHeight( ? ): ?
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( double, double )
setLength( double )
setHeight( double )
getLength( ? ): ?
getHeight( ? ): ?
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( double, double )
setLength( double )
setHeight( double )
getLength( ): double
getHeight( ? ): ?
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( double, double )
setLength( double )
setHeight( double )
getLength( ): double
getHeight( ): double
calculateArea( ? ): ?
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( double, double )
setLength( double )
setHeight( double )
getLength( ): double
getHeight( ): double
calculateArea( ): double
calculatePerimeter( ? ): ?
Oblong
length : double height : double
Oblong( double, double )
setLength( double )
setHeight( double )
getLength( ): double
getHeight( ): double
calculateArea( ): double
calculatePerimeter( ): double
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
}
public class Oblong
{
}
double length; double height;
privateprivate
Oblong ob1 = new Oblong (12.5, 7.5);
public class Oblong
{
}
double length; double height;
privateprivate
Oblong ob1 = new Oblong (12.5, 7.5);
Oblong(double ?, double ?) {
}
// more methods here
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 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
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
public class Oblong
{
}
double length; double height;
privateprivate
Oblong ob1 = new Oblong (12.5, 7.5);
Oblong(double ?, double ?) {
}
// more methods here
public
public class Oblong
{
}
double length; double height;
privateprivate
Oblong ob1 = new Oblong (12.5, 7.5);
Oblong(double lengthIn, double ?) {
}
// more methods here
public
public class Oblong
{
}
double length; double height;
privateprivate
Oblong ob1 = new Oblong (12.5, 7.5);
Oblong(double lengthIn, double ?) {
}
// more methods here
public
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
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
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
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
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
public class Oblong
{
}
double length; double height;
privateprivate
Oblong(){
}
public
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
Oblong(){
}
public
// more methods here
A default constructor has no code inside it.
public class Oblong
{
}
double length; double height;
privateprivate
Oblong(){
}
public
// more methods here
Oblong ob1 = new Oblong ( );
public class Oblong
{
}
double length; double height;
privateprivate
Oblong(){
}
public
// more methods here
Oblong ob1 = new Oblong (12.5, 7.5);
public class Oblong
{
}
double length; double height;
privateprivate
Oblong ob1 = new Oblong (12.5, 7.5);
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);
public class Oblong
{
}
double length; double height;
privateprivate
System.out.println( ob1.getLength() );
getLength(){ }
public double
return length;
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
System.out.println( ob1.getHeight() );
getHeight(){ }
public double
return height;
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setLength( 14.75);
setLength( double ?){ }
public
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setLength( 14.75);
setLength( double ?){ }
public
// more methods here
void
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setLength( 14.75);
setLength( double ?){ }
public
// more methods here
void
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setLength( 14.75);
setLength( double lengthIn){ }
public
// more methods here
void
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setLength( 14.75);
setLength( double lengthIn){ // reset length}
public
// more methods here
void
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setLength( 14.75);
setLength( double lengthIn){ }
public void
length = lengthIn;
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setHeight( 5.9 );
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
ob1.setHeight( 5.9 );
setHeight( double heightIn){ }
public void
height = heightIn;
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
System.out.println( ob1.calculateArea() );
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
System.out.println( ob1.calculateArea() );
// more methods here
calculateArea(){ }
public double
return length * height;
public class Oblong
{
}
double length; double height;
privateprivate
System.out.println( ob1.calculatePerimeter());
// more methods here
public class Oblong
{
}
double length; double height;
privateprivate
System.out.println( ob1.calculatePerimeter());
// more methods here
calculatePerimeter(){ }
public double
return 2 * (length + height);
BankAccount
accountNumber : String
accountName : String
balance : double
BankAccount (String, String)
getAccountNumber() : String
getAccountName() : String
getBalance() : double
deposit(double)
withdraw(double)
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public BankAccount(String numberIn, String nameIn)
{
accountNumber = numberIn;
accountName = nameIn;
balance = 0;
}
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public String getAccountName()
{
return accountName;
}
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public String getAccountNumber()
{
return accountNumber;
}
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public String getABalance()
{
return balance;
}
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public void deposit(double amountIn)
{
balance = balance + amountIn;
}
public class BankAccount
{
private String accountNumber;
private String accountName;
private double balance;
}
public void withdraw(double amountIn)
{
balance = balance – amountIn;
}
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());
}
}
“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 ();
“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 ();
“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();
“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 );
“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 );
“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);
“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);
“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%
“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 );
“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);
“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);
“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( );
“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.
“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.
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
}
public class BankAccount2
{
private String accountNumber;
private String accountName;
private double balance;
}
private static double interestRate;
public void addInterest()
{
balance = balance + (balance * interestRate)/100;
}
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;
}
public class BankAccount2
{
private String accountNumber;
private String accountName;
private double balance;
private static double interestRate ;
// methods here
}
= 0;
Remember how we used EasyScanner?
int x;
System.out.println(“Enter number”);
X = EasyScanner.nextInt() ;
This must be a class method!
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
}
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);
}
}
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);
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
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
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
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
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
}
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
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) {
}
}
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 */ }
}