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

Post on 31-Mar-2015

241 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

Your first program..

Hello worldpublic class Hello{

}

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

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

Adding comments to a program..

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

}

Simple data types in Java …..

Price : for example £4.75

Total sold: for example 187

A real number

An integer

In Java the simple types are referred to as

scalar types or

primitive types

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

Variables in Java

Declaring variables in Java

variableNamedataType +

variableName ;dataType

JAVA

variableName ;

JAVA

double

JAVA

double x ;

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)

ticket

cinema ticket

cinemaTicket cinema_ticket

void Ticket

score

B

int ;

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

int score;

Computer Memory Java Instruction

score

int score;

B

int player;

int score, player;

B

char level;

The effect of declaring many variables in Java

int score, player;

char level;

Java InstructionsComputer Memory

playerscore

level

Assignments in Java

score 0

score = 0;

score 0

score = 0;

score 5

score = 5;

score 3

score = 3;

score = 5;

score 0

int score;

score = 0;

score 0

int score = 0;

Spot the error!

int score = 2.5 ;

A real number cannot be placed into an integer

variable!

Assigning a character to a variable

char level = 'A';

Creating constants

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

final int HOURS = 24;;

final int HOURS = 24;;

final int HOURS = 24;

final int HOURS = 24;HOURS = 10;

Cannot change value inside a constant!

final int HOURS = 24;

Carrying out calculations

int x;x = 10 + 25;

x 35

The arithmetic operators of Java

Operation Java operator

addition +

subtraction -

multiplication *

division /

remainder %

Examples of the modulus operator in Java

Expression Value

2

6

0

0

29 % 9

6 % 8

40 % 40

10 % 2

Calculations: another example

Cost = 500 Sales tax = 17.5%

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

cost 587.5

Expressions in Java

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

cost

tax

price 500

17.5

587.5

int x = 10;x = x + 1;

x 10

?

int x = 10;x = x + 1;

x

11

int x = 10;x++;

x

11

int x = 10;x = x - 1;

x 10

?

int x = 10;x = x - 1;

x

9

int x = 10;x--;

x

9

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

cost

tax

price 500

17.5

587.5

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

tax

price 500

17.5

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

tax

price 500

17.5

?

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

tax

price

17.5

587.5

A complete program..

RUN

public class FindCost

{

}

public static void main (String [] args)

{

}

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

Output in Java

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

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

Hello world_

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

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

Hello world _

Examples of println and print

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 ");

System.out.print(10*10);

100

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

cost = 225.0

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

225.0

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

cost = 225.0

Modifying the FindCost program

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

RUN

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

Keyboard input

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

score 35

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

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

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

Scanner sc = new Scanner(System.in);

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

Scanner sc = new Scanner(System.in);

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

x = sc.nextInt();

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

y = sc.nextDouble();

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

z = sc.next.charAt(0);

Revisiting the FindCost program

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

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

More about strings

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

String name;

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

String name;

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

name = “Mike";

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

name = sc.next();

Strings: A sample program

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

Program design

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.

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

BEGIN

DISPLAY prompt for value in pounds

ENTER value in pounds

SET value to old value 2.2

DISPLAY value in kilos

END

top related