Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Programming Methodology (1)

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

3 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.

4 Your first program..

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

6 Adding comments to a program..

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

8 Simple data types in Java …..

9 Price : for example £4.75 Total sold: for example 187 A real number An integer

10 In Java the simple types are referred to as scalar types or primitive types

11 The scalar types of Java Java typeAllows forRange 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 * 10 38 double very big real numbers +/- 4.9 * 10 -324 to 1.8 * 10 308 char charactersUnicode character set boolean true or falsenot applicable

12 Variables in Java

13 Declaring variables in Java

14 variableNamedataType +

15 variableName ;dataType JAVA

16 variableName ; JAVA double

17 JAVA doublex ;

18 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)

19 ticket cinema ticket cinemaTicketcinema_ticket void Ticket

20 score B int ;

21 The effect of declaring a variable on the computer's memory int score ; Computer MemoryJava Instruction score

22 int score; B int player;

23 int score, player; B char level;

24 The effect of declaring many variables in Java int score, player; char level ; Java InstructionsComputer Memory player score level

25 Assignments in Java

26 score 0 score = 0;

27 0 score = 0;

28 score 5 score = 5;

29 3 score = 3; score = 5;

30 score 0 int score; score = 0;

31 0 int score = 0;

32 Spot the error! int score = 2.5 ; A real number cannot be placed into an integer variable!

33 Assigning a character to a variable

34 char level = 'A';

35 Creating constants

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

37 final int HOURS = 24; ;

38 ;

39

40 final int HOURS = 24; HOURS = 10; Cannot change value inside a constant!

41

42 Carrying out calculations

43 int x; x = 10 + 25; x 35

44 The arithmetic operators of Java OperationJava operator addition+ subtraction- multiplication* division/ remainder%

45 Examples of the modulus operator in Java ExpressionValue 2 6 0 0 29 % 9 6 % 8 40 % 40 10 % 2

46 Calculations: another example Cost = 500Sales tax = 17.5% double cost; cost = 500 *(1 + 17.5/100); cost 587.5

47 Expressions in Java

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

49 int x = 10; x = x + 1; x 10 ?

50 int x = 10; x = x + 1; x 11

51 int x = 10; x++; x 11

52 int x = 10; x = x - 1; x 10 ?

53 int x = 10; x = x - 1; x 9

54 int x = 10; x--; x 9

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

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

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

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

59 A complete program..

60 RUN public class FindCost { } public static void main (String [] args) { } double price, tax; price = 500; tax = 17.5; price = price * (1 + tax/100);

61 Output in Java

62 System.out.println(message to be printed on screen); System.out.println("Hello world"); Hello world _

63 System.out.print(message to be printed on screen); System.out.print("Hello world"); Hello world _

64 Examples of println and print

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

66 System.out.print(10*10); 100

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

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

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

70 Modifying the FindCost program

71 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

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

73 Keyboard input

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

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

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

77 You will then need to write the following instruction inside your program: Scanner sc = new Scanner(System.in);

78 You will then need to write the following instruction inside your program: Scanner sc = new Scanner(System.in);

79 If we want a user to type in an integer at the keyboard, into the variable x : x = sc.nextInt();

80 If we want a user to type in a double at the keyboard, into the variable y : y = sc.nextDouble();

81 If we want a user to type in a character at the keyboard, into the variable z : z = sc.next.charAt(0);

82 Revisiting the FindCost program

83 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

84 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

85 More about strings

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

87 String name;

88 Java allows you to use the normal assignment operator ( = ) with strings: name = Mike";

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

90 Strings: A sample program

91 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

92 Program design

93 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.

94 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

95

96 BEGIN DISPLAY prompt for value in pounds ENTER value in pounds SET value to old value 2.2 DISPLAY value in kilos END


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

Similar presentations


Ads by Google