Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Web & Systems Developer, 30 credits Software development, part 2 2017-05-09 sida 1 Magnus Wärja Selection (if statement) if (selection) expression selection true fals expression 2017-05-09 sida 2 Magnus Wärja 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: 7000.0 SEK 2017-05-09 sida 3 Magnus Wärja 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 } 2017-05-09 sida 4 Magnus Wärja 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 System.out.print ("Enter your rate:"); int result = scan.nextInt (); if (result > 0 && result < 10) { if (result > 4 && result < 8 ) { System.out.println ("Result = G"); } else if (result> 7) { … 2017-05-09 sida 5 Magnus Wärja 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 (=) System.out.print ("Enter your rate:"); int result = scan.nextInt (); if (result >= 1 && result <= 10) { if (result >= 5 && result <= 7 ) { System.out.println ("Result = G"); } else if (result> 7) { 2017-05-09 sida 6 Magnus Wärja Boolska uttryck 2017-05-09 56 == 56 true ’a’ == ’a’ true ’a’ == ’A’ false ’x’ != ’z’ true 45 < 80 true 2 >= 6 false 2 >= 2 true sida 7 Magnus Wärja Blockstatement 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 2017-05-09 sida 8 Magnus Wärja Nested statements 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 = } else if (result> 7) { System.out.println ("Result = } else { System.out.println ("Result = } } else { System.out.println("You have entered an } } } 2017-05-09 sida 9 G"); VG"); U"); incorrect value!"); Enter your rate: 11 You have entered an incorrect value! Magnus Wärja Exercises 3.1 Selection What is the result when you execute public static void main (String[] args){ int x = 5; int y = 10; if (x > y) System.out.println (x + ” is greater "); else if (x < y) System.out.println (y + " is greater"); else System.out.println ("The numbers are equal"); } 3.1 Selection What is the result when you execute public static void main (String[] args){ int x = 5; int y = 10; if (x == y) System.out.println ("The numbers are equal"); else System.out.println ("The numbers are not equal"); } 2017-05-09 sida 10 Magnus Wärja Exercises 3.3 Selection What is the result when you execute public static void main (String[] args){ int x = 5; int y = 10; if (x != y) System.out.println ("The numbers are not equal"); else System.out.println ("The numbers are equal"); } 3.4 Selection What is the result when you execute public static void main (String[] args){ int x = 5; int y = 10; if (x == 6 && y == 10) System.out.println ("x = 6 andy = 10"); else if (x==6 || y==10) System.out.println ("Either is x = 6 or y = 10"); else System.out.println ("x is not 6 and y is not 10"); } 2017-05-09 sida 11 Magnus Wärja Exercises 3.5 Selection What is the result when you execute public static void main (String[] args){ int resultat = 7; if (resultat >= 9) System.out.println ("Excellent"); if (resultat >= 8) System.out.println (”Excellent"); if (resultat >= 7) System.out.println (”Excellent"); if (resultat >= 6) System.out.println (”Past"); if (resultat >= 5) System.out.println (”Past"); if (resultat < 5) System.out.println (”Fail"); } 2017-05-09 sida 12 Magnus Wärja Exercises 3.6 Selection What is the result when you execute public static void main (String[] args){ int num = 80; int max = 25; if (num == 80 && max != 25) System.out.println (”Apple"); System.out.println ("Pear"); System.out.println ("Banana"); } Pear Banana 2017-05-09 sida 13 Magnus Wärja Exercises 3.7 Selection Write a program that computes the following, depending on the age of the person: Years left to retirement Number of years the person has been retired If the person has been retired this year You can assume that the retirement age is 65 years. Below are three examples of what your program should return depending on input value: Example 1 Enter your age: 45 You retire if: 20 years Example 2. Enter your age: 75 You have been retired in: 10 years Example 3. Enter your age: 65 Congratulations, you were retired in the year !!! 2017-05-09 sida 14 Magnus Wärja Exercises 3.8 Selection Write a program that takes two integers and prints out the largest number Below are three examples of what your program should return depending on input value: Example 1 Enter the first number: 40 Enter the second number: 62 62 is greater than 40 Example 2 Enter the first number: 88 Enter the second number: 53 88 is greater than 53 Example 3 Enter the first number: 10 Enter the second number: 10 The numbers are equal 2017-05-09 sida 15 Magnus Wärja Exercises 3.7 Selection Write a program that computes the following, depending on the age of the person: Years left to retirement Number of years the person has been retired If the person has been retired this year You can assume that the retirement age is 65 years. Below are three examples of what your program should return depending on input value: Example 1 Enter your age: 45 You retire if: 20 years Example 2. Enter your age: 75 You have been retired in: 10 years Example 3. Enter your age: 65 Congratulations, you were retired in the year !!! 2017-05-09 sida 16 Magnus Wärja Exercises 4.1 Iteration Write a program that computes the following, depending on the age of the person: Years left to retirement Number of years the person has been retired If the person has been retired this year You can assume that the retirement age is 65 years. Below are three examples of what your program should return depending on input value: Example 1 Enter your age: 45 You retire if: 20 years Example 2. Enter your age: 75 You have been retired in: 10 years Example 3. Enter your age: 65 Congratulations, you were retired in the year !!! 2017-05-09 sida 17 Magnus Wärja Iteration (while statement) The while statement has the following syntax: while (selection) code; selection true false code 2017-05-09 sida 18 Magnus Wärja 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 ("1 + 2 + 3 + ..." + number + "=" + sum); } Enter an integer:9 1 + 2 + 3 + ...9=45 } 2017-05-09 sida 19 Magnus Wärja Assign operators Operator Example Equivalent += -= *= /= %= x += y x -= y x *= y x /= y x %= y x=x+y x=x-y x=x*y x=x/y x=x%y Exempel: int sum = 100; int num = 50; sum += num //Equivalent to: sum = sum + num System.out.println (sum);//150 while (index <= number) { sum += index; // sum = sum + index index ++; // index = index + 1; } 2017-05-09 sida 20 Magnus Wärja Operators of increase and decrease The expression: i++; is the same as: i = i + 1; Expression Operation The term's value num++ ++num num---num add 1 add 1 subtract 1 subtract 1 gamla värdet nya värdet gamla värdet nya värdet while (index <= number) { sum += index; // sum = sum + index index ++; // index = index + 1; } 2017-05-09 sida 21 Magnus Wärja What is printed in the console window? public class ÖkaMinska{ public static void main (String[] args){ int a = 5; int b = 5; int c = 10; System.out.println(a++); System.out.println(++b); System.out.println(a); System.out.println(b); System.out.println(c++ + ++c); } } 2017-05-09 sida 22 Magnus Wärja 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 ("1 + 2 + 3 + ..." + number + "=" + sum); } } Skriv in ett heltal: 9 1+2+3+...9=45 2017-05-09 sida 23 Magnus Wärja Iteration (for statement) for (int index = 1; index <= 5; index ++) sats; Initialization 2017-05-09 Condition sida 24 counter Magnus Wärja Nested statements 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 <=21) { System.out.println ("Throw (Yes = 0 No = 1):"); result = scan.nextInt (); if (result!= 0) { play = false; } } else { play = false; } } System.out.println ("Result:" + sum); } } 2017-05-09 sida 25 Total:5 Throw (Yes = 0 No = 1): 0 Total:8 Throw (Yes = 0 No = 1): 0 Total:12 Throw (Yes = 0 No = 1): 0 Total:15 Throw (Yes = 0 No = 1): 1 Result:15 Magnus Wärja Exercises 4.1 Iteration What happens during execution: public static void main (String[] args){ int x = 0; while (x < 3){ x++; System.out.println (x); }} 4.2 Iteration What happens during execution: public static void main (String[] args){ for(int i = 1; i <= 3; i++){ System.out.println (i); }} 4.3 Iteration What happens during execution: public static void main (String[] args){ int x = 10; while (x != 3){ System.out.println (x); x--; }} 2017-05-09 sida 26 Magnus Wärja Exercises 4.4 Iteration What happens during execution: public static void main (String[] args){ int x = 0; while (x != 3 && x < 10){ x += 2; System.out.println (x); } } 4.5 Iteration What happens during execution: public static void main (String[] args){ for(int i = 15; i >= 3; i -=3){ System.out.println (i); } 2017-05-09 sida 27 Magnus Wärja Exercises 4.6 Iteration What happens during execution: public static void main (String[] args){ Scanner scan = new Scanner(System.in); double sum = 0; double average = 0; int numbers = 0; int num; System.out.print("Enter an integer: "); num = scan.nextInt(); while (tal != 0){ System.out.print ("Enter a new integer: "); num = scan.nextInt(); sum += tal; numbers++; } if (numbers != 0) average = sum / numbers; System.out.println ("Average: " + average ); } 2017-05-09 sida 28 Magnus Wärja Exercises 4.7 Iteration What happens during execution: public static void main (String[] args){ Scanner scan = new Scanner(System.in); double sum = 0; double average = 0; int numbers = 0; int num; System.out.print("Enter an integer: : "); num = scan.nextInt(); while (num != 0){ sum += tal; numbers++; System.out.print ("Enter a new integer: : "); num = scan.nextInt(); } if (numbers != 0) average = sum / numbers; System.out.println ("Average: " + average ); } } 2017-05-09 sida 29 Magnus Wärja Exercises 4.8 Mixed Write a program that prints all the odd numbers from 1 to 99 (use a for loop). 4.9 Mixed Write a program that computes the sum of all integers from 1 to 100 (use for loop). 4.10 Mixed Write a program using a while loop that prints the text "ALARM!" repeatedly at separate lines. The user shall be able to specify the number of times the "ALARM" to be written out. 4.11 Mixed Write a program that reads five numbers and prints out the larger of the given numbers. 4.12 Mixed Write a program that reads five numbers and prints the smallest and the largest of the given numbers. 4.13 Mixed Write a program that reads a series of optional numbers and then prints the minimum and the maximum of the given numbers. The process should be stopped by the number zero. If the first number is zero, the text shall be printed "No numbers loaded" 4:14 Mixed Write a program that reads a text string using the Scanner class. The program writes then the number of times the letter 'a’ appears in the loaded text string. 3:25 Mixed Write a program that reads a name using the Scanner class. The program writes then every other character in the name beginning with the first letter. 2017-05-09 sida 30 Magnus Wärja Exercises 4.1 Iteration What happens during execution public static void main (String[] args){ int x = 0; do{ x++; System.out.println (x); } while (x < 3); } 4.2 Iteration What happens during execution public static void main (String[] args){ int x = 0; while (x < 3){ x++; System.out.println (x); }} 4.3 Iteration Ange resultatet vid exekvering public static void main (String[] args){ for(int i = 1; i <= 3; i++){ System.out.println (i); }} 2017-05-09 sida 31 Magnus Wärja Array An array is an ordered list of elements The array name Each element has an index 0 points 1 2 3 79 87 94 82 4 5 6 67 98 87 7 8 9 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9 2017-05-09 sida 32 Magnus Wärja Array A particular value in an array can be accessed with the array name followed by the index in brackets For example: points [2] gives the number 94 (the third number in our array) The expression represents a place to store a single integer and can be used anywhere int variable is used 2017-05-09 sida 33 Magnus Wärja Array Ett element i en array kan tilldelas ett värde, skrivas ut, eller användas i en beräkning, osv public class MyArray{ public static void main (String[] args){ int[] points = new int[10]; points [3] = 82; points [2] = points[3] + 12; System.out.println(points[2]); System.out.println(points[2] / 2); } } 94 47 2017-05-09 sida 34 Magnus Wärja Deklarera en array Några deklarationer av arrayer: int[] result= new int[10]; float[] price = new float[500]; boolean[] flag = new boolean[20]; char[] code = new char[1750]; 2017-05-09 sida 35 Magnus Wärja Arrayens gränser myArray [100] have indices 0 to 99 Specifies one another index, you get: ArrayIndexOutOfBoundsException ?? System.out.println (minArray [100]); // This produces an error? It is common to accidentally counting too far in loops problem for (int index = 0; index <= 100; index ++) minArray [index] = index * 50; 2017-05-09 sida 36 Magnus Wärja Användning av Array import java.util.Scanner; public class Array{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int num = 0; int sum = 0; int[] list = new int[5]; System.out.println("Enter five numbers: "); for (int i = 0; i < 5; i ++){ System.out.print(”Enter a number: " + (i + 1) + ":"); num = scan.nextInt(); list[i] = num; Enter five numbers:: } Enter number 1:5 Enter number 2:6 for (int i = 0; i < 5; i ++){ Enter number 3:4 sum += list[i]; Enter number 4:8 } Enter number 5:2 System.out.println("The total is: " + sum); } The total is: 25 } 2017-05-09 sida 37 Magnus Wärja Exercises 5.1 Array Write a Java program to sum values of an array. 5.2 Array Write a Java program to calculate the average value of array elements. 5.3 Array Write a Java program to test if an array contains a specific value. 5.4 Array Write a Java program to find the index of an array element. 5.5 Array Write a Java program to remove a specific element from an array. 2017-05-09 sida 38 Magnus Wärja