Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSc IT Programming Methodology (2). number name number.

Similar presentations


Presentation on theme: "MSc IT Programming Methodology (2). number name number."— Presentation transcript:

1 MSc IT Programming Methodology (2)

2

3 number

4 name number

5 name number balance

6 abbey name number balance

7 name number balance abbey

8 deposit abbey

9 deposit withdraw abbey

10 getBalance deposit withdraw abbey

11 getBalance deposit withdraw abbey

12 BankAccount

13 abbey BankAccount

14 abbey halifax BankAccount

15 Class abbey halifax

16 Class object

17 Classes and objects Learning objectives explain the meaning of the term object-oriented; explain the concept of encapsulation; explain the terms class and object; create objects in Java; call the methods of an object; Use a number of methods of the String class; create and use arrays of objects.

18 Object-oriented programming…

19 Classes….

20 Student data methods Class objects ? s1 s2 s3 s4

21 Student data methods Class objects ? s1 s2 s3 s4 Encapsulation: The hiding of data within a class

22

23 How do you create objects from a Class ?

24 Object creation is a two-step process:

25 STEP 1: Declare object type.

26 Student data methods ? s1 s1;Student Class object

27 The effect on computer memory of declaring an object……

28 Student s1 ; Computer MemoryJava Instructions s1 ? Reference variable

29 Student s1 ; Computer MemoryJava Instructions s1 null Reference variable

30 STEP 2: Allocating memory to store the object data!

31 This is sometimes called instantiating an object!

32 Student data methods ? Class s1 object Student( )String, String A constructor method is required to generate object data from a class s1 = newStudent ( );“071202”, “Kans”

33 The effect on computer memory of instantiating an object……

34 Student s1 ; Computer Memory Java Instructions s1 This is the space for the new Student object s1 = new Student (“071202”, “Kans”); null

35 You can combine steps 1 & 2!

36 s1Student s1 = newStudent ( “071202”, “Kans”); ; = new Student (”071202”, “Kans”);

37 You can instantiate many objects from one Class !

38 Robot ? moveRight(int) moveLeft(int) moveUp(int) moveDown(int) Robot( )int, int Robot r1; r1 = new Robot(5, 50); Robot r2; r2 = new Robot(10, 2);

39 Calling the methods of another class….

40 Robot ? moveRight(int) moveLeft(int) moveUp(int) moveDown(int) Robot( )int, int Robot r1; r1 = new Robot(5, 50); Robot r2; r2 = new Robot(10, 2);

41 Robot ? moveRight(int) moveLeft(int) moveUp(int) moveDown(int) Robot( )int, int Robot r1; r1 = new Robot(5, 50); Robot r2; r2 = new Robot(10, 2); r1.moveRight(20); r2.moveDown(25);

42 Revisiting the Scanner class ……

43 To create a Scanner object we call the Scanner constructor: Scanner sc = new Scanner (System.in);

44 Scanner methods…..

45 int x = sc.nextInt(); double y = sc.nextDouble(); char reply = sc.next().charAt(0); this is a String methodthis is a String object

46 The String class

47 The String class is different from all other classes in Java. We can create String objects without a call to a constructor: String day = “Wednesday”;new String (“Wednesday”);

48 To obtain a string from the keyboard you can use the next method of Scanner day = sc.next();

49 String methods ……

50 charAt Accepts an integer and returns the character at that position in the string. Note that indexing starts from zero, not 1! An item of type int An item of type char length Returns the length of the string. None An item of type int substring Accepts two integers (eg m and n) and returns a copy of a chunk of the string, from position m to position n-1. Two items of type int A String object concat Accepts a string and returns a new string consisting of the that string joined to the end of the original string. A String object MethodDescriptionInputsOutput

51 toUpperCase Returns a copy of the original string, all upper case. None A String object toLowerCase Returns a copy of the original string, all lower case. None A String object compareTo Accepts a string and compares it to the object's string. It returns zero if the strings are identical, a negative number if the object's string comes first in the alphabet, and a positive number if it comes later. A String object An item of type int. equals Accepts an object and compares this to this to the string. It returns true if these are identical, otherwise returns false. An object of any class A boolean value. MethodDescriptionInputsOutput

52 import java.util.*; public class StringTest { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); String str = new String(); System.out.print("Enter a string: "); str = sc.next(); System.out.println("The length of the string is " + str.length()); System.out.println("The character at position 3 is " + str.charAt(2)); System.out.println("Characters 2 to 4 are " + str.substring(1,4)); System.out.println(str.concat(" was the string entered")); System.out.println("This is upper case: " + str.toUpperCase()); System.out.println("This is lower case: " + str.toLowerCase()); } } ;

53 Enter a string:Europe The length of the string is 6 The character at position 3 is r Characters 2 to 4 are uro Europe was the string entered This is upper case: EUROPE This is lower case: europe RUN import java.util.*; public class StringTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = new String(); System.out.print("Enter a string: "); str = sc.next(); System.out.println("The length of the string is " + str.length()); System.out.println("The character at position 3 is " + str.charAt(2)); System.out.println("Characters 2 to 4 are " + str.substring(1,4)); System.out.println(str.concat(" was the string entered")); System.out.println("This is upper case: " + str.toUpperCase()); System.out.println("This is lower case: " + str.toLowerCase()); }

54 Comparing strings …..

55 When checking two strings for equality use the equals method!

56 Do not use the equality operator (==)

57 if(firstString == secondString) { // more code here } String firstString = “Hello”; String secondString = “Goodbye”;

58 if(firstString == secondString) { // more code here } String firstString = “Hello”; String secondString = “Goodbye”;

59 if(firstString.equals(secondString)) { // more code here } String firstString = “Hello”; String secondString = “Goodbye”;

60 if(firstString.equals(secondString)) { // more code here } String firstString = “Hello”; String secondString = “Goodbye”;

61 When comparing two strings alphabetically use the compareTo method!

62 import java.util.*; public class StringComp { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); String string1, string2; int comparison; System.out.print("Enter a String: "); string1 = sc.next(); System.out.print("Enter another String: "); string2 = sc.next(); comparison = string1.compareTo(string2); // check comparison } }

63 if (comparison < 0) { System.out.println(string1 + " comes before " + string2 + " in the alphabet"); } else if (comparison > 0) { System.out.println(string2 + " comes before " + string1 + " in the alphabet"); } else { System.out.println("The strings are identical"); } RUN Enter a String: Enter another String: goodbye comes before hello in the alphabet hello goodbye

64 Using some classes written for you

65 Oblong

66 Using some classes written for you Oblong EasyScanner

67 Using some classes written for you Oblong EasyScanner BankAccount

68 Oblong

69 public class RectangleCalculations { public static void main(String[ ] args) { double length, height; // code to get length and height System.out.println(“Area = " + area(length,height) ); System.out.println(“Perimeter = " + perimeter(length,height) ); private static double area (double lengthIn, double heightIn) { return lengthIn * heightIn; } private static double area (double lengthIn, double heightIn) { return 2* (lengthIn * heightIn_; }

70 public class RectangleCalculations { public static void main(String[ ] args) { double length, height; // code to get length and height System.out.println(“Area = " + area(length,height) ); System.out.println(“Perimeter = " + perimeter(length,height) ); } private static double area (double lengthIn, double heightIn) { return lengthIn * heightIn; } private static double area (double lengthIn, double heightIn) { return 2* (lengthIn + heightIn); }

71 Methods of the Oblong class..

72 Oblong Oblong myOblong = new Oblong(12.5, 20);

73 setLength myOblong.setlength(17.5);

74 setHeight myOblong.setHeight(12);

75 getHeight System.out.println( ); myOblong.getHeight( )

76 getLength System.out.println( ); myOblong.getLength( )

77 calculateArea System.out.println( ); myOblong.calculateArea( )

78 calculatePerimeter System.out.println( ); myOblong.calculatePerimeter( )

79 Issues with using the Scanner class for keyboard input….

80 it is necessary to create a new Scanner object in every method that uses the Scanner class; there is no simple method such as nextChar for getting a single character like there is for the int and double types; the next method doesn't allow us to enter strings containing spaces.

81 The EasyScanner class resolves these problems!

82 The input methods of the EasyScanner class Java typeEasyScanner method intnextInt() doublenextDouble() charnextChar() StringnextString()

83 To use the methods of EasyScanner we do not need to create an object!

84 char c; System.out.print("Enter a character: "); c = EasyScanner.nextChar(); String s; System.out.print("Enter a string: "); s = EasyScanner.nextString();

85 The BankAccount class ….

86 The BankAcount class contains methods to process a bank account. BankAccount Susan Richards Sumana Khan 150.0

87 The methods of the BankAccount class MethodDescriptionInputsOutput BankAccount A constructor. It accepts two strings and assigns them to the account number and account name respectively. It also sets the account balance to zero. Two String objects Not applicable getAccountNumber Returns the account number.NoneAn item of type String getAccountName Returns the account name.NoneAn item of type String getBalance Returns the balance.NoneAn item of type double deposit Accepts an item of type double and adds it to the balance An item of type double None withdraw Accepts an item of type double and subtracts it from the balance An item of type double None

88 public class BankAccountTester { public static void main(String[ ] args) { BankAccount account1 = new BankAccount(" ","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()); } }

89 Account number: Account name: Susan Richards Current balance:

90 Arrays of objects..

91 int[] someArray = new int[3]; An array of 3 integers An array of 3 BankAccount objects BankAccount[] accountList= new BankAccount[3];

92 The effect on memory of creating an array of objects …

93 null accountList accountList = new BankAccount [3]; reference to bank account Java instructionComputer memory BankAccount[] accountList; null

94 Adding bank accounts to the array …

95 accountList[0] = new BankAccount(" ","Susan Richards"); accountList[1] = new BankAccount(" ","Delroy Jacobs"); accountList[2] = new BankAccount(" ","Sumana Khan");

96 accountList[1] BankAccount with account number " " and name "Sumana Khan" accountList[2] accountList[0] BankAccount with account number " " and name "Susan Richards" BankAccount with account number " " and name "Delroy Jacobs" accountList

97 Accessing objects in an array ….

98 accountList[0].deposit(1000); returns a BankAccount object calls a BankAccount method accountList[0].withdraw(500); accountList[2].deposit(150);

99 public class BankAccountTester2 { public static void main(String[ ] args) { BankAccount[ ] accountList = new BankAccount[3]; accountList[0] = new BankAccount(" ","Susan Richards"); accountList[1] = new BankAccount(" ","Delroy Jacobs"); accountList[2] = new BankAccount(" ","Sumana Khan"); accountList[0].deposit(1000); accountList[2].deposit(150); accountList[0].withdraw(500); for(BankAccount item : accountList) { System.out.println("Account number: " + item.getAccountNumber()); System.out.println("Account name: " + item.getAccountName()); System.out.println("Current balance: " + item.getBalance()); System.out.println(); } } }

100 Account number: Account name: Susan Richards Current balance: Account number: Account name: Delroy Jacobs Current balance: 0.0 Account number: Account name: Sumana Khan Current balance: 150.0

101 Practical Work

102 Follow these steps to make use of classes written for you (such as Oblong and EasyScanner)..

103 First, go to this module’s web site and download the classes we have written for you..

104 Now, every time your program needs to use a file (such as Oblong), add it to your project!

105

106 This program needs access to the Oblong class

107 Right click your project (or choose Project from the menu)

108 Choose Add >Add Exisiting Files

109

110 Select the file(s) you need.

111 You will be asked if you want this file to be added into your project or kept outside.

112 Select Add to add the file into your project.

113 File has been added and your program will now compile!

114 Remember the programs you write should not have the same name as our files!

115 public class TestOblong { public static void main (String[ ] args) { Oblong ob1 = new Oblong (4.2, 10); System.out.println("area = " + ob1.calculateArea()); System.out.println("permeter = " + ob1.calculatePerimeter()); }

116 public class TestOblong { public static void main (String[ ] args) { Oblong ob1 = new Oblong (4.2, 10); System.out.println("area = " + ob1.calculateArea()); System.out.println("permeter = " + ob1.calculatePerimeter()); }

117 public class TestOblong { public static void main (String[ ] args) { Oblong ob1 = new Oblong (4.2, 10); System.out.println("area = " + ob1.calculateArea()); System.out.println("permeter = " + ob1.calculatePerimeter()); }

118 public class TestOblong { public static void main (String[ ] args) { Oblong ob1 = new Oblong (4.2, 10); System.out.println("area = " + ob1.calculateArea()); System.out.println("permeter = " + ob1.calculatePerimeter()); } area = 42.0

119 public class TestOblong { public static void main (String[ ] args) { Oblong ob1 = new Oblong (4.2, 10); System.out.println("area = " + ob1.calculateArea()); System.out.println("perimeter = " + ob1.calculatePerimiter()); }

120 public class TestOblong { public static void main (String[ ] args) { Oblong ob1 = new Oblong (4.2, 10); System.out.println("area = " + ob1.calculateArea()); System.out.println("perimeter = " + ob1.calculatePerimiter()); } area = 42.0 perimeter = 28.4

121 Design and implement a program that performs in the following way: when the program starts two bank accounts are created, using names and numbers which are written into the code; the user is then asked to enter an account number, followed by an amount to deposit in that account; the balance of the appropriate account is then updated accordingly – or if an incorrect account number was entered a message to this effect is displayed; the user is then asked if he or she wishes to make more deposits; if the user answers 'y' (for yes), the process continues; if the user answers 'n' (for no), then both account details (account number, account name and balance) are displayed.

122 ***Bank Program*** Which bank account?:

123 ***Bank Program*** Which bank account?: a123

124 ***Bank Program*** Which bank account?: a123 How much to deposit?

125 ***Bank Program*** Which bank account?: a123 How much to deposit? 250

126 ***Bank Program*** Which bank account?: a123 How much to deposit?250 More deposits?

127 ***Bank Program*** Which bank account?: a123 How much to deposit?250 More deposits? n

128 ***Bank Program*** Which bank account?: a123 How much to deposit?250 More deposits?n number: a123 name: Aaron balance: number: a456 name: Obama balance: 0.0

129


Download ppt "MSc IT Programming Methodology (2). number name number."

Similar presentations


Ads by Google