MSc IT Programming Methodology (2). number name number.
-
Upload
kenna-tomlinson -
Category
Documents
-
view
227 -
download
2
Transcript of MSc IT Programming Methodology (2). number name number.
![Page 1: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/1.jpg)
MSc IT
Programming Methodology (2)
![Page 2: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/2.jpg)
![Page 3: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/3.jpg)
number
![Page 4: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/4.jpg)
namenumber
![Page 5: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/5.jpg)
namenumber
balance
![Page 6: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/6.jpg)
abbey
namenumber
balance
![Page 7: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/7.jpg)
namenumber
balance
abbey
![Page 8: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/8.jpg)
deposit
abbey
![Page 9: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/9.jpg)
deposit withdraw
abbey
![Page 10: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/10.jpg)
getBalance
deposit withdraw
abbey
![Page 11: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/11.jpg)
getBalance
deposit withdraw
abbey
![Page 12: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/12.jpg)
BankAccount
![Page 13: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/13.jpg)
abbey
BankAccount
![Page 14: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/14.jpg)
abbeyhalifax
BankAccount
![Page 15: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/15.jpg)
Class
abbeyhalifax
![Page 16: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/16.jpg)
Class
objectobject
![Page 17: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/17.jpg)
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.
![Page 18: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/18.jpg)
Object-oriented programming…
![Page 19: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/19.jpg)
Classes….
![Page 20: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/20.jpg)
Student
data
methods
Class
objects
?
s1
s2
s3
s4
![Page 21: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/21.jpg)
Student
data
methods
Class
objects
?
s1
s2
s3
s4
Encapsulation:
The hiding of data within a class
![Page 22: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/22.jpg)
![Page 23: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/23.jpg)
How do you create objects from a Class ?
![Page 24: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/24.jpg)
Object creation is a two-step process:
![Page 25: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/25.jpg)
STEP 1:Declare object type.
![Page 26: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/26.jpg)
Student
data
methods
? s1
s1; Student
Class
object
![Page 27: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/27.jpg)
The effect on computer memory of declaring an object……
![Page 28: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/28.jpg)
Student s1 ;
Computer Memory Java Instructions
s1
? Reference variable
![Page 29: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/29.jpg)
Student s1 ;
Computer Memory Java Instructions
s1
null Reference variable
![Page 30: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/30.jpg)
STEP 2:Allocating memory to store the object data!
![Page 31: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/31.jpg)
This is sometimes called instantiating an object!
![Page 32: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/32.jpg)
Student
data
methods
?
Class
s1
objectStudent( )String, String
A constructor method is required to generate object data from a classs1 = new Student ( );“071202”, “Kans”
![Page 33: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/33.jpg)
The effect on computer memory of instantiating an object……
![Page 34: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/34.jpg)
Student s1 ;
Computer Memory Java Instructions
s1
This is the space for the new Student
object
s1 = new Student (“071202”, “Kans”);
null
![Page 35: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/35.jpg)
You can combine steps 1 & 2!
![Page 36: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/36.jpg)
s1Student
s1 = new Student ( “071202”, “Kans”);
; = new Student (”071202”, “Kans”);
![Page 37: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/37.jpg)
You can instantiate many objects from one Class !
![Page 38: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/38.jpg)
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);
![Page 39: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/39.jpg)
Calling the methods of another class….
![Page 40: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/40.jpg)
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);
![Page 41: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/41.jpg)
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);
![Page 42: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/42.jpg)
Revisiting the Scanner
class ……
![Page 43: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/43.jpg)
To create a Scanner object we call the Scanner constructor:
Scanner sc = new Scanner (System.in);
![Page 44: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/44.jpg)
Scanner methods…..
![Page 45: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/45.jpg)
int x = sc.nextInt();
double y = sc.nextDouble();
char reply = sc.next().charAt(0);
this is a String methodthis is a String object
![Page 46: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/46.jpg)
The String class
![Page 47: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/47.jpg)
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”);
![Page 48: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/48.jpg)
To obtain a string from the keyboard you can use the next method of Scanner
day = sc.next();
![Page 49: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/49.jpg)
String methods ……
![Page 50: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/50.jpg)
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
A String
object
Method Description Inputs Output
![Page 51: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/51.jpg)
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.
Method Description Inputs Output
![Page 52: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/52.jpg)
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());
}
}
;
![Page 53: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/53.jpg)
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());
}
}
![Page 54: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/54.jpg)
Comparing strings …..
![Page 55: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/55.jpg)
When checking two strings for equality use the equals method!
![Page 56: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/56.jpg)
Do not use the equality operator (==)
![Page 57: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/57.jpg)
if(firstString == secondString){ // more code here}
String firstString = “Hello”;
String secondString = “Goodbye”;
![Page 58: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/58.jpg)
if(firstString == secondString){ // more code here}
String firstString = “Hello”;
String secondString = “Goodbye”;
![Page 59: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/59.jpg)
if(firstString.equals(secondString)){ // more code here}
String firstString = “Hello”;
String secondString = “Goodbye”;
![Page 60: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/60.jpg)
if(firstString.equals(secondString)){ // more code here}
String firstString = “Hello”;
String secondString = “Goodbye”;
![Page 61: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/61.jpg)
When comparing two strings alphabetically use the compareTo method!
![Page 62: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/62.jpg)
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
}
}
![Page 63: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/63.jpg)
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
![Page 64: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/64.jpg)
Using some classes written for you
![Page 65: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/65.jpg)
Using some classes written for you
Oblong
![Page 66: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/66.jpg)
Using some classes written for you
Oblong
EasyScanner
![Page 67: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/67.jpg)
Using some classes written for you
Oblong
EasyScanner
BankAccount
![Page 68: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/68.jpg)
Oblong
![Page 69: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/69.jpg)
public class RectangleCalculations{ public static void main(String[ ] args) {
double length, height;// code to get length and heightSystem.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_; }}
![Page 70: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/70.jpg)
public class RectangleCalculations{ public static void main(String[ ] args) {
double length, height;// code to get length and heightSystem.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); }}
![Page 71: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/71.jpg)
Methods of the Oblong class..
![Page 72: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/72.jpg)
Oblong
Oblong myOblong
12.5
20
= new Oblong(12.5, 20);
![Page 73: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/73.jpg)
setLength
myOblong.setlength(17.5);
12.5
20
17.5
![Page 74: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/74.jpg)
setHeight
myOblong.setHeight(12);
20
17.5
12
![Page 75: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/75.jpg)
getHeight
System.out.println( );
17.5
12
myOblong.getHeight( )
![Page 76: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/76.jpg)
getLength
System.out.println( );
17.5
12
myOblong.getLength( )
![Page 77: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/77.jpg)
calculateArea
System.out.println( );
17.5
12
myOblong.calculateArea( )
![Page 78: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/78.jpg)
calculatePerimeter
System.out.println( );
17.5
12
myOblong.calculatePerimeter( )
![Page 79: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/79.jpg)
Issues with using the Scanner class for keyboard input….
![Page 80: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/80.jpg)
• 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.
![Page 81: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/81.jpg)
The EasyScanner class resolves these problems!
![Page 82: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/82.jpg)
The input methods of the EasyScanner class
Java type EasyScanner method
int nextInt()
double nextDouble()
char nextChar()
String nextString()
![Page 83: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/83.jpg)
To use the methods of EasyScanner we do not need to create an object!
![Page 84: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/84.jpg)
char c;
System.out.print("Enter a character: ");
c = EasyScanner.nextChar();
String s;
System.out.print("Enter a string: ");
s = EasyScanner.nextString();
![Page 85: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/85.jpg)
The BankAccount class ….
![Page 86: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/86.jpg)
The BankAcount class contains methods to process a bank account.
BankAccount
99786754Susan Richards
500.0
46376205 Sumana Khan
150.0
![Page 87: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/87.jpg)
The methods of the BankAccount classMethod Description Inputs Output
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. None An item of type String
getAccountName Returns the account name. None An item of type String
getBalance Returns the balance. None An 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
![Page 88: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/88.jpg)
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 89: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/89.jpg)
Account number: 99786754
Account name: Susan Richards
Current balance: 1000.0
![Page 90: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/90.jpg)
Arrays of objects ..
![Page 91: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/91.jpg)
int[] someArray = new int[3];
An array of 3 integers
An array of 3 BankAccount objects
BankAccount[] accountList= new BankAccount[3];
![Page 92: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/92.jpg)
The effect on memory of creating an array of objects …
![Page 93: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/93.jpg)
null
null
null
accountList
accountList
= new BankAccount [3];
reference to bank account
reference to bank account
reference to bank account
Java instructionComputer memory
BankAccount[] accountList;
null
![Page 94: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/94.jpg)
Adding bank accounts to the array …
![Page 95: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/95.jpg)
accountList[0] = new BankAccount("99786754","Susan Richards");
accountList[1] = new BankAccount("44567109","Delroy Jacobs");
accountList[2] = new BankAccount("46376205","Sumana Khan");
![Page 96: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/96.jpg)
accountList[1]
BankAccountwith account number
"46376205" and name "Sumana Khan"
accountList[2]
accountList[0]
BankAccount with account number "99786754" and name
"Susan Richards"
BankAccount with account number "44567109" and name
"Delroy Jacobs"
accountList
![Page 97: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/97.jpg)
Accessing objects in an array ….
![Page 98: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/98.jpg)
accountList[0].deposit(1000);
returns a BankAccount object
calls a BankAccount method
accountList[0].withdraw(500);
accountList[2].deposit(150);
![Page 99: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/99.jpg)
public class BankAccountTester2
{
public static void main(String[ ] args)
{
BankAccount[ ] accountList = new BankAccount[3];
accountList[0] = new BankAccount("99786754","Susan Richards");
accountList[1] = new BankAccount("44567109","Delroy Jacobs");
accountList[2] = new BankAccount("46376205","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();
}
}
}
![Page 100: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/100.jpg)
Account number: 99786754
Account name: Susan Richards
Current balance: 500.0
Account number: 44567109
Account name: Delroy Jacobs
Current balance: 0.0
Account number: 46376205
Account name: Sumana Khan
Current balance: 150.0
![Page 101: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/101.jpg)
Practical Work
![Page 102: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/102.jpg)
Follow these steps to make use of classes written for you (such as Oblong and EasyScanner)..
![Page 103: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/103.jpg)
First, go to this module’s web site and download the classes we have written for you..
![Page 104: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/104.jpg)
Now, every time your program needs to use a file (such as Oblong), add it to your project!
![Page 105: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/105.jpg)
![Page 106: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/106.jpg)
This program needs access to the Oblong class
![Page 107: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/107.jpg)
Right click your project (or choose Project from the menu)
![Page 108: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/108.jpg)
Choose Add >Add Exisiting Files
![Page 109: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/109.jpg)
![Page 110: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/110.jpg)
Select the file(s) you need.
![Page 111: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/111.jpg)
You will be asked if you want this file to be added into your project or kept outside.
![Page 112: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/112.jpg)
Select Add to add the file into your project.
![Page 113: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/113.jpg)
File has been added and your program will now compile!
![Page 114: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/114.jpg)
Remember the programs you write should not have the same name as our files!
![Page 115: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/115.jpg)
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());
}
}
![Page 116: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/116.jpg)
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());
}
}
![Page 117: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/117.jpg)
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());
}
}
![Page 118: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/118.jpg)
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
![Page 119: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/119.jpg)
area = 42.0
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());
}
}
![Page 120: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/120.jpg)
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
![Page 121: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/121.jpg)
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.
![Page 122: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/122.jpg)
***Bank Program***
Which bank account?:
![Page 123: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/123.jpg)
***Bank Program***
Which bank account?: a123
![Page 124: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/124.jpg)
***Bank Program***
Which bank account?: a123
How much to deposit?
![Page 125: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/125.jpg)
***Bank Program***
Which bank account?: a123
How much to deposit? 250
![Page 126: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/126.jpg)
***Bank Program***
Which bank account?: a123
How much to deposit? 250
More deposits?
![Page 127: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/127.jpg)
***Bank Program***
Which bank account?: a123
How much to deposit? 250
More deposits? n
![Page 128: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/128.jpg)
***Bank Program***
Which bank account?: a123
How much to deposit? 250
More deposits? n
number: a123 name: Aaron balance: 250.0
number: a456 name: Obama balance: 0.0
![Page 129: MSc IT Programming Methodology (2). number name number.](https://reader031.fdocuments.us/reader031/viewer/2022020921/56649c7d5503460f94931d9e/html5/thumbnails/129.jpg)