msc it programming methodology (2). number name number

129
MSc IT Programming Methodology (2)

Upload: kenna-tomlinson

Post on 14-Dec-2015

236 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: MSc IT Programming Methodology (2). number name number

MSc IT

Programming Methodology (2)

Page 2: MSc IT Programming Methodology (2). number name number
Page 3: MSc IT Programming Methodology (2). number name number

number

Page 4: MSc IT Programming Methodology (2). number name number

namenumber

Page 5: MSc IT Programming Methodology (2). number name number

namenumber

balance

Page 6: MSc IT Programming Methodology (2). number name number

abbey

namenumber

balance

Page 7: MSc IT Programming Methodology (2). number name number

namenumber

balance

abbey

Page 8: MSc IT Programming Methodology (2). number name number

deposit

abbey

Page 9: MSc IT Programming Methodology (2). number name number

deposit withdraw

abbey

Page 10: MSc IT Programming Methodology (2). number name number

getBalance

deposit withdraw

abbey

Page 11: MSc IT Programming Methodology (2). number name number

getBalance

deposit withdraw

abbey

Page 12: MSc IT Programming Methodology (2). number name number

BankAccount

Page 13: MSc IT Programming Methodology (2). number name number

abbey

BankAccount

Page 14: MSc IT Programming Methodology (2). number name number

abbeyhalifax

BankAccount

Page 15: MSc IT Programming Methodology (2). number name number

Class

abbeyhalifax

Page 16: MSc IT Programming Methodology (2). number name number

Class

objectobject

Page 17: MSc IT Programming Methodology (2). number name number

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

Object-oriented programming…

Page 19: MSc IT Programming Methodology (2). number name number

Classes….

Page 20: MSc IT Programming Methodology (2). number name number

Student

data

methods

Class

objects

?

s1

s2

s3

s4

Page 21: MSc IT Programming Methodology (2). number name number

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
Page 23: MSc IT Programming Methodology (2). number name number

How do you create objects from a Class ?

Page 24: MSc IT Programming Methodology (2). number name number

Object creation is a two-step process:

Page 25: MSc IT Programming Methodology (2). number name number

STEP 1:Declare object type.

Page 26: MSc IT Programming Methodology (2). number name number

Student

data

methods

? s1

s1; Student

Class

object

Page 27: MSc IT Programming Methodology (2). number name number

The effect on computer memory of declaring an object……

Page 28: MSc IT Programming Methodology (2). number name number

Student s1 ;

Computer Memory Java Instructions

s1

? Reference variable

Page 29: MSc IT Programming Methodology (2). number name number

Student s1 ;

Computer Memory Java Instructions

s1

null Reference variable

Page 30: MSc IT Programming Methodology (2). number name number

STEP 2:Allocating memory to store the object data!

Page 31: MSc IT Programming Methodology (2). number name number

This is sometimes called instantiating an object!

Page 32: MSc IT Programming Methodology (2). number name number

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

The effect on computer memory of instantiating an object……

Page 34: MSc IT Programming Methodology (2). number name number

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

You can combine steps 1 & 2!

Page 36: MSc IT Programming Methodology (2). number name number

s1Student

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

; = new Student (”071202”, “Kans”);

Page 37: MSc IT Programming Methodology (2). number name number

You can instantiate many objects from one Class !

Page 38: MSc IT Programming Methodology (2). number name number

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

Calling the methods of another class….

Page 40: MSc IT Programming Methodology (2). number name number

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

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

Revisiting the Scanner

class ……

Page 43: MSc IT Programming Methodology (2). number name number

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

Scanner methods…..

Page 45: MSc IT Programming Methodology (2). number name number

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

The String class

Page 47: MSc IT Programming Methodology (2). number name number

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

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

String methods ……

Page 50: MSc IT Programming Methodology (2). number name number

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

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

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

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

Comparing strings …..

Page 55: MSc IT Programming Methodology (2). number name number

When checking two strings for equality use the equals method!

Page 56: MSc IT Programming Methodology (2). number name number

Do not use the equality operator (==)

Page 57: MSc IT Programming Methodology (2). number name number

if(firstString == secondString){ // more code here}

String firstString = “Hello”;

String secondString = “Goodbye”;

Page 58: MSc IT Programming Methodology (2). number name number

if(firstString == secondString){ // more code here}

String firstString = “Hello”;

String secondString = “Goodbye”;

Page 59: MSc IT Programming Methodology (2). number name number

if(firstString.equals(secondString)){ // more code here}

String firstString = “Hello”;

String secondString = “Goodbye”;

Page 60: MSc IT Programming Methodology (2). number name number

if(firstString.equals(secondString)){ // more code here}

String firstString = “Hello”;

String secondString = “Goodbye”;

Page 61: MSc IT Programming Methodology (2). number name number

When comparing two strings alphabetically use the compareTo method!

Page 62: MSc IT Programming Methodology (2). number name number

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

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

Using some classes written for you

Page 65: MSc IT Programming Methodology (2). number name number

Using some classes written for you

Oblong

Page 66: MSc IT Programming Methodology (2). number name number

Using some classes written for you

Oblong

EasyScanner

Page 67: MSc IT Programming Methodology (2). number name number

Using some classes written for you

Oblong

EasyScanner

BankAccount

Page 68: MSc IT Programming Methodology (2). number name number

Oblong

Page 69: MSc IT Programming Methodology (2). number name number

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

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

Methods of the Oblong class..

Page 72: MSc IT Programming Methodology (2). number name number

Oblong

Oblong myOblong

12.5

20

= new Oblong(12.5, 20);

Page 73: MSc IT Programming Methodology (2). number name number

setLength

myOblong.setlength(17.5);

12.5

20

17.5

Page 74: MSc IT Programming Methodology (2). number name number

setHeight

myOblong.setHeight(12);

20

17.5

12

Page 75: MSc IT Programming Methodology (2). number name number

getHeight

System.out.println( );

17.5

12

myOblong.getHeight( )

Page 76: MSc IT Programming Methodology (2). number name number

getLength

System.out.println( );

17.5

12

myOblong.getLength( )

Page 77: MSc IT Programming Methodology (2). number name number

calculateArea

System.out.println( );

17.5

12

myOblong.calculateArea( )

Page 78: MSc IT Programming Methodology (2). number name number

calculatePerimeter

System.out.println( );

17.5

12

myOblong.calculatePerimeter( )

Page 79: MSc IT Programming Methodology (2). number name number

Issues with using the Scanner class for keyboard input….

Page 80: MSc IT Programming Methodology (2). number name number

• 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

The EasyScanner class resolves these problems!

Page 82: MSc IT Programming Methodology (2). number name number

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

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

Page 84: MSc IT Programming Methodology (2). number name number

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

The BankAccount class ….

Page 86: MSc IT Programming Methodology (2). number name number

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

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

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

Account number: 99786754

Account name: Susan Richards

Current balance: 1000.0

Page 90: MSc IT Programming Methodology (2). number name number

Arrays of objects ..

Page 91: MSc IT Programming Methodology (2). number name number

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

The effect on memory of creating an array of objects …

Page 93: MSc IT Programming Methodology (2). number name number

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

Adding bank accounts to the array …

Page 95: MSc IT Programming Methodology (2). number name number

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

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

Accessing objects in an array ….

Page 98: MSc IT Programming Methodology (2). number name number

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

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

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

Practical Work

Page 102: MSc IT Programming Methodology (2). number name number

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

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

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
Page 106: MSc IT Programming Methodology (2). number name number

This program needs access to the Oblong class

Page 107: MSc IT Programming Methodology (2). number name number

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

Page 108: MSc IT Programming Methodology (2). number name number

Choose Add >Add Exisiting Files

Page 109: MSc IT Programming Methodology (2). number name number
Page 110: MSc IT Programming Methodology (2). number name number

Select the file(s) you need.

Page 111: MSc IT Programming Methodology (2). number name number

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

Select Add to add the file into your project.

Page 113: MSc IT Programming Methodology (2). number name number

File has been added and your program will now compile!

Page 114: MSc IT Programming Methodology (2). number name number

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

Page 115: MSc IT Programming Methodology (2). number name number

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

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

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

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

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

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

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

***Bank Program***

Which bank account?:

Page 123: MSc IT Programming Methodology (2). number name number

***Bank Program***

Which bank account?: a123

Page 124: MSc IT Programming Methodology (2). number name number

***Bank Program***

Which bank account?: a123

How much to deposit?

Page 125: MSc IT Programming Methodology (2). number name number

***Bank Program***

Which bank account?: a123

How much to deposit? 250

Page 126: MSc IT Programming Methodology (2). number name number

***Bank Program***

Which bank account?: a123

How much to deposit? 250

More deposits?

Page 127: MSc IT Programming Methodology (2). number name number

***Bank Program***

Which bank account?: a123

How much to deposit? 250

More deposits? n

Page 128: MSc IT Programming Methodology (2). number name number

***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