programming methodology (1). public class hello { public static void main(string[] args) {...

96
Programming Methodology (1)

Upload: annabella-clegg

Post on 31-Mar-2015

241 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Programming Methodology (1)

Page 2: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

public class Hello{ public static void main(String[] args) { System.out.println("Hello world"); }}

Hello world

Page 3: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Learning objectives

write Java programs that display text on the screen;

distinguish between the eight built-in scalar types of Java;

declare and assign values to variables;

create constant values with the keyword final;

join messages and values in output commands by using the concatenation (+) operator;

use the input methods of the Scanner class to get data from the keyboard;

design the functionality of a method using pseudocode.

Page 4: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Your first program..

Page 5: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Hello worldpublic class Hello{

}

public static void main(String[] args) { }

System.out.println("Hello world");

Page 6: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Adding comments to a program..

Page 7: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

// this is a short comment, so we use the first method

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world");

}

/* this is the second method of including comments –

it is more convenient to use this method here,

because the comment is longer and goes over more

than one line */

}

Page 8: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Simple data types in Java …..

Page 9: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Price : for example £4.75

Total sold: for example 187

A real number

An integer

Page 10: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

In Java the simple types are referred to as

scalar types or

primitive types

Page 11: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

The scalar types of JavaJava type Allows for Range of values

byte very small integers -128 to 127

short small integers -32768 to 32767

int big integers -2147483648 to 2147483647

long very big integers -9223372036854775808 to 9223372036854775807

float real numbers +/- 1.4 * 10-45 to 3.4 * 1038

double very big real numbers +/- 4.9 * 10-324 to 1.8 * 10308

char characters Unicode character set

boolean true or false not applicable

Page 12: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Variables in Java

Page 13: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Declaring variables in Java

Page 14: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

variableNamedataType +

Page 15: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

variableName ;dataType

JAVA

Page 16: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

variableName ;

JAVA

double

Page 17: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

JAVA

double x ;

Page 18: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

You can choose almost any name for your variables as long as:

the name does not have spaces in it

the name is not already used in Java (such as class or static)

Page 19: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

ticket

cinema ticket

cinemaTicket cinema_ticket

void Ticket

Page 20: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

score

B

int ;

Page 21: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

The effect of declaring a variable on the computer's memory

int score;

Computer Memory Java Instruction

score

Page 22: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int score;

B

int player;

Page 23: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int score, player;

B

char level;

Page 24: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

The effect of declaring many variables in Java

int score, player;

char level;

Java InstructionsComputer Memory

playerscore

level

Page 25: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Assignments in Java

Page 26: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

score 0

score = 0;

Page 27: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

score 0

score = 0;

Page 28: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

score 5

score = 5;

Page 29: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

score 3

score = 3;

score = 5;

Page 30: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

score 0

int score;

score = 0;

Page 31: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

score 0

int score = 0;

Page 32: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Spot the error!

int score = 2.5 ;

A real number cannot be placed into an integer

variable!

Page 33: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Assigning a character to a variable

Page 34: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

char level = 'A';

Page 35: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Creating constants

Page 36: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

• the maximum score in an exam (100); • the number of hours in a day (24);• the mathematical value of (3.14176).

Page 37: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

final int HOURS = 24;;

Page 38: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

final int HOURS = 24;;

Page 39: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

final int HOURS = 24;

Page 40: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

final int HOURS = 24;HOURS = 10;

Cannot change value inside a constant!

Page 41: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

final int HOURS = 24;

Page 42: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Carrying out calculations

Page 43: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int x;x = 10 + 25;

x 35

Page 44: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

The arithmetic operators of Java

Operation Java operator

addition +

subtraction -

multiplication *

division /

remainder %

Page 45: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Examples of the modulus operator in Java

Expression Value

2

6

0

0

29 % 9

6 % 8

40 % 40

10 % 2

Page 46: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Calculations: another example

Cost = 500 Sales tax = 17.5%

double cost; cost = 500 * (1 + 17.5/100);

cost 587.5

Page 47: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Expressions in Java

Page 48: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

double price, tax, cost; price = 500; tax = 17.5; cost = price * (1 + tax/100);

cost

tax

price 500

17.5

587.5

Page 49: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int x = 10;x = x + 1;

x 10

?

Page 50: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int x = 10;x = x + 1;

x

11

Page 51: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int x = 10;x++;

x

11

Page 52: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int x = 10;x = x - 1;

x 10

?

Page 53: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int x = 10;x = x - 1;

x

9

Page 54: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

int x = 10;x--;

x

9

Page 55: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

double price, tax, cost; price = 500; tax = 17.5; cost = price * (1 + tax/100);

cost

tax

price 500

17.5

587.5

Page 56: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

double price, tax; price = 500; tax = 17.5;

tax

price 500

17.5

Page 57: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100);

tax

price 500

17.5

?

Page 58: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100);

tax

price

17.5

587.5

Page 59: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

A complete program..

Page 60: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

RUN

public class FindCost

{

}

public static void main (String [] args)

{

}

double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100);

Page 61: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Output in Java

Page 62: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

System.out.println(message to be printed on screen);

System.out.println("Hello world");

Hello world_

Page 63: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

System.out.print(message to be printed on screen);

System.out.print("Hello world");

Hello world _

Page 64: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Examples of println and print

Page 65: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world");

System.out.println("Hello world again!");

}

}

Hello world

Hello world again!

RUN

System.out.print("Hello world");

Hello worldHello world again!Hello world Hello world again!

System.out.print("Hello world ");

Page 66: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

System.out.print(10*10);

100

Page 67: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

System.out.print("cost = " + (30*7.5) );

cost = 225.0

Page 68: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

double cost = 30 * 7.5;System.out.print(cost);

225.0

Page 69: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

double cost = 30 * 7.5;System.out.print("cost = " + cost);

cost = 225.0

Page 70: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Modifying the FindCost program

Page 71: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

public class FindCost2{ public static void main(String[] args) { double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100); System.out.println("*** Product Price Check ***"); System.out.println("Cost after tax = " + price); }}

RUN

Page 72: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

RUN

*** Product Price Check ***Cost after tax = 587.5

Page 73: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Keyboard input

Page 74: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

The Scanner class has recently been added into Java to simplify keyboard input.

score 35

Page 75: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

In order to have access to the Scanner class you have to place the following line at the beginning of your program:

import java.util.*;

Page 76: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

In order to have access to the Scanner class you have to place the following line at the beginning of your program:

import java.util.*;

Page 77: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

You will then need to write the following instruction inside your program:

Scanner sc = new Scanner(System.in);

Page 78: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

You will then need to write the following instruction inside your program:

Scanner sc = new Scanner(System.in);

Page 79: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

If we want a user to type in an integer at the keyboard, into the variable x :

x = sc.nextInt();

Page 80: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

If we want a user to type in a double at the keyboard, into the variable y :

y = sc.nextDouble();

Page 81: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

If we want a user to type in a character at the keyboard, into the variable z :

z = sc.next.charAt(0);

Page 82: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Revisiting the FindCost program

Page 83: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

import java.util.*;

public class FindCost3{ public static void main(String[] args ) {

Scanner sc = new Scanner(System.in); double price, tax;

System.out.println("*** Product Price Check ***");System.out.print("Enter initial price: "); price = sc.nextDouble();

System.out.print("Enter tax rate: "); tax = sc.nextDouble();

price = price * (1 + tax/100); System.out.println("Cost after tax = " + price);

}}

RUN

*** Product Price Check ***Enter initial price: _1000

Enter tax rate: _12.5

Cost after tax = 1125.0

Page 84: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

import java.util.*;

public class FindCost3{ public static void main(String[] args ) {

Scanner sc = new Scanner(System.in); double price, tax;

System.out.println("*** Product Price Check ***");System.out.print("Enter initial price: "); price = sc.nextDouble();

System.out.print("Enter tax rate: "); tax = sc.nextDouble();

price = price * (1 + tax/100); System.out.println("Cost after tax = " + price);

}}

RUN

*** Product Price Check ***Enter initial price: _50

Enter tax rate: _17.5

Cost after tax = 58.75

Page 85: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

More about strings

Page 86: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

You can declare a String in a similar way to variables of type int or char:

String name;

Page 87: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

You can declare a String in a similar way to variables of type int or char:

String name;

Page 88: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Java allows you to use the normal assignment operator ( = ) with strings:

name = “Mike";

Page 89: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

To obtain a string from the keyboard you can use the next method of Scanner

name = sc.next();

Page 90: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Strings: A sample program

Page 91: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

import java.util.*;public class StringTest{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name; int age; System.out.print("What is your name? "); name = sc.next(); System.out.print("What is your age? "); age = sc.nextInt(); System.out.println(); System.out.println("Hello " + name); System.out.println("When I was your age I was " +(age +1)); }}

RUN

What is your name? _Aaron

What is your age? _15

Hello Aaron

When I was your age I was 16

Page 92: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

Program design

Page 93: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

public class FindCost3{ public static void main(String[] args ) { Scanner …… }}

When you sketch out the code for your methods a general purpose "coding language" can be used.

Code expressed in this way is referred to as pseudocode.

Page 94: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

BEGIN

DISPLAY program title

DISPLAY prompt for price

ENTER price

DISPLAY prompt for tax

ENTER tax

SET price TO price * (1 + tax/100)

DISPLAY new price

END

Page 95: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world
Page 96: Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world

BEGIN

DISPLAY prompt for value in pounds

ENTER value in pounds

SET value to old value 2.2

DISPLAY value in kilos

END