Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.

Similar presentations


Presentation on theme: "MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee."— Presentation transcript:

1 MSc IT Programming Methodology (2)

2 MODULE TEAM

3 Dr Aaron Kans Dr Sin Wee Lee

4 Recommended Text

5

6 Module Web Site

7 www.aaronkans.com

8 Review of Programming Methodology (1) Classes and Objects Implementing Classes Inheritance Graphics and event-driven programming Assignment Workshop Exceptions Two dimensional arrays Review Weekly Schedule

9 Review of first semester…

10 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

11 Declaring variables in Java

12 variableName ;dataType JAVA

13 variableName ; JAVA double

14 JAVA doublex ;

15 score B int ;

16 int score; B int player;

17 int score, player; B char level;

18 Assignments in Java

19 score 0 score = 0;

20 0 score = 0;

21 score 5 score = 5;

22 Creating constants

23 final int HOURS = 24; ;

24 ;

25

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

27 Output in Java

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

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

30 Keyboard input

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

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

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

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

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

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

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

38 The FindCost program import java.util.*; public class FindCost { 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

39 Selection In Java there are three forms of selection you can use: an if statement; an if…else statement; a switch statement.

40 import java.util.*; public class SelectionQ5 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); } else { System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _10 Blue Red

41 import java.util.*; public class SelectionQ5 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); } else { System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _20 Green Red

42 Iteration There are three types of loops in Java: for loop; while loop; do…while loop

43 import java.util.*; public class IterationQ2 { public static void main(String[] args) { int num; Scanner sc = new Scanner(System.in); System.out.print("Enter a number "); num = sc.nextInt(); for (int i= 1; i< num; i++) { System.out.println("YES"); System.out.println("NO"); } System.out.println("OK"); } Enter a number5 YES NO OK YES NO YES NO YES NO i =12345

44 public static void main(String[ ] args) { int num; Scanner sc = new Scanner (System.in); System.out.println("Size of square?"); num = sc.nextInt(); for (int i = 1; i <= num ; i++) { for (int j = 1; j<= num ; j++) { System.out.print("*"); } System.out.println(); } } private static void showStars( ) { } showStars( ); } Let’s use a method

45 public static void main(String[ ] args) { int num; Scanner sc = new Scanner (System.in); System.out.println("Size of square?"); num = sc.nextInt(); for (int i = 1; i <= ; i++) { for (int j = 1; j<= ; j++) { System.out.print("*"); } System.out.println(); } private static void showStars( ) { } showStars( ); } num showStars( num ); int ) numIn numIn num numIn actual parameter formal parameter

46 Creating arrays Java Instructions double[ ] temperature ; Computer Memory temperature ? item of type 'double' temperature = new double[7];

47 Naming the array elements….

48 0123456 temperature First item is temperature[0] Last item is temperature[6]

49 Passing arrays as parameters…

50 private static void enterTemps(double[ ] temperatureIn) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); } public static void main (String [ ] args) { double[ ] temperature = new double[7]; enterTemps( temperature ); displayTemps( temperature ); } temperature temperatureIn

51 The enhanced 'for' loop for ( ) { } int i = 0; i < temperatureIn.length ; i++ System.out.println( );temperatureIn[ i ] temperatureInitem :double item “For every item in the temperatureIn array”

52 Write a program that takes in the length and height of a rectangle from the user and then uses methods to calculate the area and perimeter of that rectangle.

53 Rectangle Program Enter length:

54 Rectangle Program Enter length: 2.5

55 Rectangle Program Enter length: 2.5 Enter height:

56 Rectangle Program Enter length: 2.5 Enter height: 8

57 Rectangle Program Enter length: 2.5 Enter height: 8 Area = 20

58 Rectangle Program Enter length: 2.5 Enter height: 8 Area = 20 Perimeter = 21

59


Download ppt "MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee."

Similar presentations


Ads by Google