Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount."— Presentation transcript:

1 MSc IT Programming Methodology (2)

2 Oblong EasyScanner BankAccount

3 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.

4 Structured programming…

5 // switch on light // get building // check if passed main boolean light; int mark; String roomNumber;

6 Object-oriented programming…

7 String roomNumber; int mark; // switch on light // get building // check if passed Room Student Projector boolean light;

8 Looking inside a class….

9

10 Student

11 data Student

12 name number marks data

13 name number marks Student data methods

14 name number marks Student data methods getName getNumber setMarks Student

15 Oblong

16 data

17 Oblong data length height

18 Oblong data length height methods

19 Oblong data length height methods getLength calculateArea Oblong

20 The notation of the Unified Modelling Language (UML)

21 Oblong length height Oblong( ) setLength( ) setHeight( ) getLength( ) getHeight( ) calculateArea( ) calculatePerimeter( ) Class Name data methods attributes

22 Oblong length : ? height : ? Oblong( ? ) setLength( ? ): ? setHeight( ? ): ? getLength( ? ): ? getHeight( ? ): ? calculateArea( ? ): ? calculatePerimeter( ? ): ?

23 Oblong length : double height : ? Oblong( ? ) setLength( ? ): ? setHeight( ? ): ? getLength( ? ): ? getHeight( ? ): ? calculateArea( ? ): ? calculatePerimeter( ? ): ?

24 Oblong length : double height : double Oblong( ? ) setLength( ? ): ? setHeight( ? ): ? getLength( ? ): ? getHeight( ? ): ? calculateArea( ? ): ? calculatePerimeter( ? ): ?

25 Oblong length : double height : double Oblong( double, double ) setLength( ? ): ? setHeight( ? ): ? getLength( ? ): ? getHeight( ? ): ? calculateArea( ? ): ? calculatePerimeter( ? ): ?

26 Oblong length : double height : double Oblong( double, double ) setLength( double ) setHeight( ? ): ? getLength( ? ): ? getHeight( ? ): ? calculateArea( ? ): ? calculatePerimeter( ? ): ?

27 Oblong length : double height : double Oblong( double, double ) setLength( double ) setHeight( double ) getLength( ? ): ? getHeight( ? ): ? calculateArea( ? ): ? calculatePerimeter( ? ): ?

28 Oblong length : double height : double Oblong( double, double ) setLength( double ) setHeight( double ) getLength( ): double getHeight( ? ): ? calculateArea( ? ): ? calculatePerimeter( ? ): ?

29 Oblong length : double height : double Oblong( double, double ) setLength( double ) setHeight( double ) getLength( ): double getHeight( ): double calculateArea( ? ): ? calculatePerimeter( ? ): ?

30 Oblong length : double height : double Oblong( double, double ) setLength( double ) setHeight( double ) getLength( ): double getHeight( ): double calculateArea( ): double calculatePerimeter( ? ): ?

31 Oblong length : double height : double Oblong( double, double ) setLength( double ) setHeight( double ) getLength( ): double getHeight( ): double calculateArea( ): double calculatePerimeter( ): double

32 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 }

33 public class Oblong { } double length; double height; Attributes declared outside of any methods

34 public class Oblong { } double length; double height;

35 public class Oblong { } double length; double height; Attributes should be hidden inside the class

36 public class Oblong { } double length; double height; private

37 Implementing the Oblong methods…

38 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5);

39 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double ?, double ?) { } // more methods here

40 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double ?, double ?) { } // more methods here Methods should be visible outside of the class

41 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double ?, double ?) { } // more methods here Methods should be visible outside of the class public

42 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double ?, double ?) { } // more methods here public A constructor has no return type

43 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double ?, double ?) { } // more methods here public

44 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double lengthIn, double ?) { } // more methods here public

45 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double lengthIn, double ?) { } // more methods here public

46 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double lengthIn, double heightIn) { } // more methods here public

47 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double lengthIn, double heightIn) { // set the values of length and height } // more methods here public

48 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double lengthIn, double heightIn) { // set the values of length and height } // more methods here public

49 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double lengthIn, double heightIn) { length = lengthIn; } // more methods here public

50 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5); Oblong(double lengthIn, double heightIn) { length = lengthIn; height = heightIn; } // more methods here public

51 What happens if you don’t write a constructor?

52 A default constructor is added for you!

53 public class Oblong { } double length; double height; private Oblong() { } public // more methods here

54 public class Oblong { } double length; double height; private Oblong() { } public // more methods here A default constructor has no code inside it.

55 public class Oblong { } double length; double height; private Oblong() { } public // more methods here Oblong ob1 = new Oblong ( );

56 public class Oblong { } double length; double height; private Oblong() { } public // more methods here Oblong ob1 = new Oblong (12.5, 7.5);

57 public class Oblong { } double length; double height; private Oblong ob1 = new Oblong (12.5, 7.5);

58 public class Oblong { } double length; double height; private public Oblong(double lengthIn, double heightIn) { } // more methods here length = lengthIn; height = heightIn; Oblong ob1 = new Oblong (12.5, 7.5);

59 public class Oblong { } double length; double height; private System.out.println( ob1.getLength() ); getLength() { } publicdouble return length; // more methods here

60 public class Oblong { } double length; double height; private System.out.println( ob1.getHeight() ); getHeight() { } publicdouble return height; // more methods here

61 public class Oblong { } double length; double height; private ob1.setLength( 14.75); setLength( double ?) { } public // more methods here

62 public class Oblong { } double length; double height; private ob1.setLength( 14.75); setLength( double ?) { } public // more methods here void

63 public class Oblong { } double length; double height; private ob1.setLength( 14.75); setLength( double ?) { } public // more methods here void

64 public class Oblong { } double length; double height; private ob1.setLength( 14.75); setLength( double lengthIn) { } public // more methods here void

65 public class Oblong { } double length; double height; private ob1.setLength( 14.75); setLength( double lengthIn) { // reset length } public // more methods here void

66 public class Oblong { } double length; double height; private ob1.setLength( 14.75); setLength( double lengthIn) { } publicvoid length = lengthIn; // more methods here

67 public class Oblong { } double length; double height; private ob1.setHeight( 5.9 ); // more methods here

68 public class Oblong { } double length; double height; private ob1.setHeight( 5.9 ); setHeight( double heightIn) { } publicvoid height = heightIn; // more methods here

69 public class Oblong { } double length; double height; private System.out.println( ob1.calculateArea() ); // more methods here

70 public class Oblong { } double length; double height; private System.out.println( ob1.calculateArea() ); // more methods here calculateArea() { } publicdouble return length * height;

71 public class Oblong { } double length; double height; private System.out.println( ob1.calculatePerimeter()); // more methods here

72 public class Oblong { } double length; double height; private System.out.println( ob1.calculatePerimeter()); // more methods here calculatePerimeter() { } publicdouble return 2 * (length + height);

73 The BankAccount class….

74 BankAccount accountNumber : String accountName : String balance : double BankAccount (String, String) getAccountNumber() : String getAccountName() : String getBalance() : double deposit(double) withdraw(double)

75 public class BankAccount { private String accountNumber; private String accountName; private double balance; }

76 public class BankAccount { private String accountNumber; private String accountName; private double balance; }

77 public class BankAccount { private String accountNumber; private String accountName; private double balance; } public BankAccount(String numberIn, String nameIn) { accountNumber = numberIn; accountName = nameIn; balance = 0; }

78 public class BankAccount { private String accountNumber; private String accountName; private double balance; } public String getAccountName() { return accountName; }

79 public class BankAccount { private String accountNumber; private String accountName; private double balance; } public String getAccountNumber() { return accountNumber; }

80 public class BankAccount { private String accountNumber; private String accountName; private double balance; } public String getABalance() { return balance; }

81 public class BankAccount { private String accountNumber; private String accountName; private double balance; } public void deposit(double amountIn) { balance = balance + amountIn; }

82 public class BankAccount { private String accountNumber; private String accountName; private double balance; } public void withdraw(double amountIn) { balance = balance – amountIn; }

83 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()); } }

84 Amending the BankAccount class…

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

86 “0012765” “Funmi Odulopo” £1200.49 “09887254” “Mary Stephenson” £987.31 “07721009” “Dilraj Mann” £3975.75 acc1 acc2 acc3 1.25% acc1.getBalance(); acc1.getInterestRate(); acc2.addInterest ();

87 “0012765” “Funmi Odulopo” £1200.49 “09887254” “Mary Stephenson” £987.31 “07721009” “Dilraj Mann” £3975.75 acc1 acc2 acc3 1.25% acc1.getBalance(); acc1.getInterestRate(); acc2.addInterest (); acc2.getInterestRate();

88 “0012765” “Funmi Odulopo” £1200.49 “09887254” “Mary Stephenson” £987.31 “07721009” “Dilraj Mann” £3975.75 acc1 acc2 acc3 1.25% acc1.getBalance(); acc1.getInterestRate(); acc2.addInterest (); acc2.getInterestRate(); acc3.deposit( 500 );

89 “0012765” “Funmi Odulopo” £1200.49 “09887254” “Mary Stephenson” £987.31 “07721009” “Dilraj Mann” £4475.75 acc1 acc2 acc3 1.25% acc1.getBalance(); acc1.getInterestRate(); acc2.addInterest (); acc2.getInterestRate(); acc3.deposit( 500 );

90 “0012765” “Funmi Odulopo” £1200.49 “09887254” “Mary Stephenson” £987.31 “07721009” “Dilraj Mann” £4475.75 acc1 acc2 acc3 1.25% acc1.getBalance(); acc1.getInterestRate(); acc2.addInterest (); acc2.getInterestRate(); acc3.deposit( 500 ); acc3.setInterestRate(1.5);

91 “0012765” “Funmi Odulopo” £1200.49 “09887254” “Mary Stephenson” £987.31 “07721009” “Dilraj Mann” £4475.75 acc1 acc2 acc3 1.25% 1.5% acc1.getBalance(); acc1.getInterestRate(); acc2.addInterest (); acc2.getInterestRate(); acc3.deposit( 500 ); acc3.setInterestRate(1.5);

92 “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%

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

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

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

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

97 “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.

98 “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.

99 How do we create class attributes and class methods?

100 Use the static keyword!

101 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 }

102 The addInterest method ….

103 public class BankAccount2 { private String accountNumber; private String accountName; private double balance; } private static double interestRate; public void addInterest() { balance = balance + (balance * interestRate)/100; }

104 The BankAccount2 constructor ?

105 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; }

106 What about the interestRate attribute?

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

108 Initializing attributes int double char Objects 0 boolean false null

109 public class BankAccount2 { private String accountNumber; private String accountName; private double balance; private static double interestRate ; // methods here } = 0;

110 The EasyScanner class…

111 Remember how we used EasyScanner? int x; System.out.println(“Enter number”); X = EasyScanner.nextInt() ; This must be a class method!

112 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 }

113 Sending objects as parameters….

114 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); } }

115 Account Number: 1 Account Name: Samsun Okoyo Balance: 2500.0

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

117 public static void main (String[] args) { BankAccount acc = new BankAccount(…) ; } Computer MemoryJava Instructions acc a BankAccount object private static void test(BankAccount accIn) { } accIn test(acc); accIn.deposit(2500);

118 Practical Work

119 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : int getScienceMark() : int calculateAverageMark() : double public class Student { // attributes // methods } Implement this class in your practical

120 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : int getScienceMark() : int calculateAverageMark() : double public class Student { // attributes // methods } Implement this class in your practical

121 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : int getScienceMark() : int calculateAverageMark() : double public class Student { private int markForMaths; // more attributes here // methods } Implement this class in your practical

122 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : int getScienceMark() : int calculateAverageMark() : double public class Student { private int markForMaths; // more attributes here // methods } Implement this class in your practical

123 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : 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 }

124 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : int getScienceMark() : int calculateAverageMark() : double Then write a tester to check your class

125 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : int getScienceMark() : int calculateAverageMark() : double Then write a tester to check your class public class StudentTester { public static void main(String[ ] args) { } }

126 Student studentNumber : String studentName : String markForMaths : int markForEnglish : int markForScience : int Student(String, String) getNumber() : String getName() : String enterMarks(int, int, int) getMathsMark() : int getEnglishMark() : 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 */ } }

127


Download ppt "MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount."

Similar presentations


Ads by Google