Web & Systems Developer, 30 credits

Slides:



Advertisements
Similar presentations
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Advertisements

Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Introduction to Computer Programming Decisions If/Else Booleans.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
LAB 10.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
Chapter 5 Loops.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures II
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS0007: Introduction to Computer Programming
CompSci 230 S Programming Techniques
Lecture 5: Program Logic
Introduction to programming in java
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Repetition-Sentinel,Flag Loop/Do_While
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Topic 11 Scanner object, conditional execution
Control Statement Examples
Chapter 6 More Conditionals and Loops
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Selection (if-then-else)
Outline Boolean Expressions The if Statement Comparing Data
Know for Quiz Everything through Last Week and Lab 7
AP Java Review If else.
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Self study.
Building Java Programs
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
CS2011 Introduction to Programming I Selections (I)
AP Java Review If else.
Building Java Programs
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Question 1a) What is printed by the following Java program? int s;
Conditionals and Loops
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

Web & Systems Developer, 30 credits Software development, part 2 2017-10-22 Magnus Wärja

Selection (if statement) fals code true selection if (selection) expression 2017-10-22 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-10-22 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-10-22 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 (=) 2017-10-22 Magnus Wärja

Boolska uttryck 56 == 56 true ’a’ == ’a’ ’a’ == ’A’ false ’x’ != ’z’ 45 < 80 2 >= 6 2 >= 2 2017-10-22 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 2017-10-22 Magnus Wärja

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 2017-10-22 Magnus Wärja

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 2017-10-22 Magnus Wärja

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 2017-10-22 Magnus Wärja

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 2017-10-22 Magnus Wärja

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! 2017-10-22 Magnus Wärja

Iteration (while statement) The while statement has the following syntax: code true selection false while (selection) code; 2017-10-22 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-10-22 Magnus Wärja

Iteration (for statement) for (int index = 1; index <= 5; index ++) sats; counter Initialization Condition 2017-10-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); Exenpel på en for-sats för att förtydliga föregående OH. Skriv in ett heltal: 9 1+2+3+...9=45 2017-10-22 Magnus Wärja

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 2017-10-22 Magnus Wärja