Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web & Systems Developer, 30 credits

Similar presentations


Presentation on theme: "Web & Systems Developer, 30 credits"— Presentation transcript:

1 Web & Systems Developer, 30 credits
Software development, part 2 Magnus Wärja

2 Selection (if statement)
fals code true selection if (selection) expression Magnus Wärja

3 Selection (if else statement)
import java.util.Scanner; public class IfStatement{ public static void main (String [] args) { Scanner scan = new Scanner (System.in); double hourlySalary = 100; int hours; double payment; System.out.print ("Enter the weekly hours worked:"); hours = scan.nextInt(); if (hours > 40) { payment = 40 * hourlySalary + (hours - 40) * (hourlySalary * 1.5); } else { payment = hours * hourlySalary; System.out.println ("Weekly salary:" + payment + ” SEK"); Enter the weekly hours worked:60 Weekly salary: SEK Magnus Wärja

4 Selection (if else statement)
import java.util.Scanner; public class IfStatement2{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int score; int ratings; System.out.print("Enter your exam result (0 to 100 points):"); score = scan.nextInt(); ratings = score / 10; if (ratings > 7){ System.out.println("Your rate is: VERY GOOD"); } else if (ratings < 5){ System.out.println("Your rate is: FAIL"); else{ System.out.println("Your rate is: GOOD"); Enter your exam result (0 to 100 points):65 Your rate is: GOOD Magnus Wärja

5 Boolean expressions A conditional statement often uses one of Java's relational operators, which all return boolean answer (true or false): == Equal to != Not equal < Less than > Greater than <= Less than or equal to >= Greater than or equal to Note the difference between "equals" -operator (==) and the assignment operator (=) Magnus Wärja

6 Boolska uttryck 56 == 56 true ’a’ == ’a’ ’a’ == ’A’ false ’x’ != ’z’
45 < 80 2 >= 6 2 >= 2 Magnus Wärja

7 Logical expressions The logical NOT - expression:
!a s true if a is false and false if a is true The logical AND - expression: a && b is true if both a and b are true. otherwise false The logical OR- expression: a || b is true if a or b or both are true. otherwise false Magnus Wärja

8 Operatorer för att öka och minska
The expression: i++; is the same as: i = i + 1; Expression num++ ++num num-- --num Operation add 1 subtract 1 The term's value gamla värdet nya värdet Magnus Wärja

9 Operatorer för att öka och minska
public class ÖkaMinska{ public static void main (String[] args){ int a = 5; int b = 5; int c = 10; System.out.println(a++); //5 System.out.println(++b); //6 System.out.println(a); //6 System.out.println(b); //6 System.out.println(c c); //10+12 } Sista raden i varje beräkningssats har finns där för att visa hur man gör en tom rad vid utskriften av resultatet. 5 6 22 Magnus Wärja

10 Tilldelningsoperatorer
+= -= *= /= %= Exempel x += y x -= y x *= y x /= y x %= y Motsvarar x = x + y x = x - y x = x * y x = x / y x = x % y Exempel: int summa = 100; int vara = 50; summa = summa + vara; // summa = 150 är samma som: summa += vara; // summa = 150 Magnus Wärja

11 Blockstatement Block Guess a number between 1 and 10: 6
import java.util.Scanner; public class Guess{ public static void main (String [] args) { Scanner scan = new Scanner (System.in); int rightNumber = 4; int guessedNumber; System.out.print ("Guess a number between 1 and 10:"); guessedNumber = scan.nextInt(); if (rightNumber == guessedNumber) System.out.println ("GOOD, RIGHT NUMBER !!!"); else { System.out.println ("Your answer was wrong."); System.out.println ("The correct number was:" + rightNumber); } Block Guess a number between 1 and 10: 6 Your answer was wrong. The correct number was:4 Magnus Wärja

12 Nested statements Ange betyg: 10 Du har angivit ett felaktigt värde!
import java.util.Scanner; public class IfStatement3 { public static void main (String [] args) { Scanner scan = new Scanner (System.in); int result; System.out.print ("Enter your rate:"); result = scan.nextInt (); if (result> 0 && result <10) { if (result> 4 && result <= 7) { System.out.println ("Result = G"); } else if (result> 7) { System.out.println ("Result = VG"); else { System.out.println ("Result = U"); System.out.println("You have entered an incorrect value!");     } Ange betyg: 10 Du har angivit ett felaktigt värde! Magnus Wärja

13 Iteration (while statement)
The while statement has the following syntax: code true selection false while (selection) code; Magnus Wärja

14 Iteration (while- sats)
import java.util.Scanner; public class Counter { public static void main (String [] args) { Scanner scan = new Scanner (System.in); int sum = 0; int number; int index = 1; System.out.print ("Enter an integer:"); number = scan.nextInt (); while (index <= number) { sum += index; // sum = sum + index index ++; // index = index + 1; } System.out.println (" " + number + "=" + sum); Enter an integer:9 =45 Magnus Wärja

15 Iteration (for statement)
for (int index = 1; index <= 5; index ++) sats; counter Initialization Condition Magnus Wärja

16 Iteration (for statement)
import java.util.Scanner; public class Counter { public static void main (String [] args) { Scanner scan = new Scanner (System.in); int sum = 0; int number; System.out.print ("Enter an integer:"); number = scan.nextInt (); for (int index = 1; index <= number; index ++) { sum = sum + index; } System.out.println (" " + number + "=" + sum); Exenpel på en for-sats för att förtydliga föregående OH. Skriv in ett heltal: 9 =45 Magnus Wärja

17 Nested statements Total:5 Throw (Yes = 0 No = 1): Total:8 Total:12
import java.util.Random; import java.util.Scanner; public class TwentyOne { public static void main (String [] args) { Random random = new Random (); Scanner scan = new Scanner (System.in); int sum = 0; int result; boolean play = true; while (play) { sum += random.nextInt (6) + 1; System.out.println ("Total:" + sum); if (sum <22) { System.out.println ("Throw (Yes = 0 No = 1):"); result = scan.nextInt (); if (result!= 0) { play = false; } else { System.out.println ("Result:" + sum); Total:5 Throw (Yes = 0 No = 1): Total:8 Total:12 Total:15 1 Result:15 Magnus Wärja


Download ppt "Web & Systems Developer, 30 credits"

Similar presentations


Ads by Google