Work with Data and Decision Structure. Slide 2 Note: String and Date are classes.

Slides:



Advertisements
Similar presentations
Complex Conditions. Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F.
Advertisements

Work with Data and Decision Structure. Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 2.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
IF Function Decision: Action based on condition. Examples SF State Tuition calculation: – Fees_and_Expenses.
Murach’s Java SE 6, C6© 2007, Mike Murach & Associates, Inc.Slide 1.
Murach’s Java SE 6, C5© 2007, Mike Murach & Associates, Inc.Slide 1.
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Nested IF and Complex Condition. Nested IF Example: –Rules to determine bonus: JobCode = 1, Bonus=500 JobCode = 2, Bonus = 700 JobCode = 3, Bonus = 1000.
VB.Net Introduction - 2. Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: Dim Counter As Integer Need.
IF Function Decision: Action based on condition. Examples SF State Tuition calculation: –
Complex Conditions. Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F.
Loops ISYS 350. Three Types of Loops while loop do while loop for loop.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to.
Expression and Decision Structure ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators.
Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to.
Decisions Action based on condition. Examples Simple condition: –If total sales exceeds $300 then applies 5% discount; otherwise, no discount. More than.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Functions BUS 782. What are functions? Functions are prewritten formulas. We use functions to perform calculations. Enclose arguments within parentheses.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
IF Function Decision: Action based on condition. Examples SF State Tuition calculation: –
CS0007: Introduction to Computer Programming
Decision Structure - 2 ISYS 350.
Decision Structure ISYS 350.
Decision Structure - 1 ISYS 350.
Elementary Programming
User input We’ve seen how to use the standard output buffer
Java Programming: From Problem Analysis to Program Design, 4e
TK1114 Computer Programming
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)
Lecture 4: Conditionals
Decision Structure - 2 ISYS 350.
Decision Structure - 1 ISYS 350.
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
The for-loop and Nested loops
Work with Data and Decision Structure
Decision Structure - 2 ISYS 350.
Chapter 2: Basic Elements of Java
SELECTION STATEMENTS (2)
Java Variables, Types, and Math Getting Started
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
Decision Structures ISYS 350.
Decision Structure - 2 ISYS 350.
Decision Structure - 2 ISYS 350.
Decision Structure - 1 ISYS 350.
Lecture Notes – Week 2 Lecture-2
Decision: Action based on condition
Decision Structure - 2 ISYS 350.
Functions BUS 782.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Decision Structure - 1 ISYS 350.
Problem 1 Given n, calculate 2n
Presentation transcript:

Work with Data and Decision Structure

Slide 2 Note: String and Date are classes.

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 3

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 4

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 5

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 6

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 7 Enter Floating and Long Integer Values

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 8

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 9

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 10

Modulus Example Long division: – Quotient – Remainder Enter length in inches and determine the equivalent feet and inches: – 57 inches = 4 feet and 9 inches

Return Smallest Number of Coins Input: Changes in penny from 0 to 99 Output: Smallest number of coins System.out.print("enter changes between 0 to 99: "); Scanner sc = new Scanner(System.in) ; int Changes = sc.nextInt(); int qtr = Changes/25; int dime = (Changes - qtr*25)/10; int nickle = (Changes - qtr*25 - dime*10)/5; int penny = (Changes - qtr*25-dime*10-nickle*5); System.out.print("qtr= " + qtr + " dime=" + dime + " Nickle=" + nickle + " penny= " + penny); Examples: 26 cents: 1 Q, 1 P 57 cents: 2 Q, 1 N, 2 P 63 cents: 2 Q, 1 D, 3 P

String Concatenation with the concat method or “+” String firstName, lastName, fullName; System.out.println("Enter first name"); firstName=sc.next(); System.out.println("Enter last name"); lastName=sc.next(); fullName= firstName.concat(" ").concat(lastName); // fullName=firstName + " " + lastName; System.out.println("Full name is: " + fullName) ;

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 14

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 15

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 16

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 17

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 18

Decision Structure

Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount; otherwise, no discount. More than one condition: Taxable Income < =3000 no tax 3000 < taxable income <= % tax Taxable income > % tax Complex condition: – If an applicant’s GPA > 3.0 and SAT > 1200: admitted

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 21

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 22

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 23

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 24

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 25

Example: If total sales is larger than 1000, then give 5% discount Scanner sc = new Scanner(System.in); double totalSales; double discountRate=0; System.out.println("Enter a total sales: "); totalSales=sc.nextDouble(); if (totalSales > 1000) discountRate=0.05; double netPay=totalSales*(1-discountRate); System.out.println("Net pay is: " + netPay); double discountRate; System.out.println("Enter a total sales: "); totalSales=sc.nextDouble(); if (totalSales > 1000) discountRate=0.05; else discountRate=0;

Even integer or odd integer Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); if (N % 2==0) System.out.println("Even"); else System.out.println("Odd");

Example: If with a block of statements if (totalSales > 1000) { discountRate=0.05; System.out.println("Thank you so much! "); } else { discountRate=0; System.out.println("Thank you! "); }

Variable Scope Block-level scope: declared within a block of code. Method (Procedural) level scope: declared in a procedure Class-level: declared in a class but outside any procedure.

Scanner sc = new Scanner(System.in); System.out.println("Enter a city name"); String City=sc.next(); if (City.equalsIgnoreCase("Paris")) { String Country="France"; System.out.println("It is in " + Country); } else System.out.println("It is not in France");

What output you will see? public class Main { public static String projLevelVar="Project level"; String testVar="Class Level Var"; public static void main(String[] args) { demoScope(); } public static void demoScope(){ String testVar="Method Level Var"; System.out.println(testVar); }

More than one condition Rules for bonus: JobCode = 1300 JobCode = 2500 JobCode = 3700 JobCode = 41000

Scanner sc = new Scanner(System.in); double jobCode, bonus; System.out.println("Enter job code: "); jobCode = sc.nextDouble(); if (jobCode==1) bonus=300; else if (jobCode==2) bonus=500; else if (jobCode==3) bonus=700; else bonus=1000; System.out.println("Bonus is: " + bonus);

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 34

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 35 Case Structure with the Switch Statement

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 36

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 37

Code Examle System.out.println("enter job code:"); double jobCode=sc.nextDouble(); Switch((int) jobCode) { case 1: bonus=300; break; case 2: bonus=500; break; case 3: bonus=700; break; case 4: bonus=1000; break; default: bonus=0; break; }

Explicit Casting Since the Switch structure requires an integer expression and the jobCode is declared as a double, we need to convert it to integer: From double to integer: int code = (int) jobCode From integer to double: int counter = 0; double dcounter = (double) counter;

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 40

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 41

Nested if statements if (customerType.equals("R")) { // begin nested if if (subtotal >= 100) discountPercent =.2; else discountPercent =.1; } // end nested if else discountPercent =.4;

Type = “R” Or not Discount=.2Subtota>=100? Discount=.1 Discount=.4

Example State University calculates students tuition based on the following rules: – State residents: Total units taken <=12, tuition = 1200 Total units taken > 12, tuition = per additional unit. – Non residents: Total units taken <= 9, tuition = 3000 Total units taken > 9, tuition = per additional unit.

Decision Tree Resident or Not Units <= 12 or Not Units <= 9 or Not

Consider the following code snippet. if (aNumber >= 0) if (aNumber == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string"); Does the “else” pair with the first or the second if? What output do you think the code will produce if aNumber is 3?

Complex Condition Examples: – A theater charges admission fee based on customer’s age: 12 <= Age <= 65:Fee = $5 Otherwise: Fee = $3 – X University admission rules: If GPA > 3.5 or SAT > 1500: Admitted – Y University admission rules: If GPA > 3.0 and SAT > 1200: Admitted

Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F

Examples Write a complex condition for: 12 <= Age <= 65 Use a complex condition to describe age not between 12 and 65. X <= 15 is equivalent to: X<15 AND X =15? (T/F) This complex condition is always false: – X 10 This complex condition is always true: – X >= 5 OR X <= 10

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 50

Examples of Logical Operators if (Age>=12 && Age <=65) Fee=20; else Fee=0; 1. If 12 <= Age <= 65, admission fee = 20, otherwise, free admission The rules to compute employee bonus are: If JobCode = 1 or Salary < 50000, then bonus = 500 Otherwise, bonus = 1000 if (jobCode==1 || Salary<50000) Bonus=500; else Bonus=1000;

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 52

Example Electric Company charges customers based on KiloWatt-Hour used. The rules are: – First 100 KH,20 cents per KH – Each of the next 200 KH ( up to 300 KH), 15 cents per KH – All KH over 300, 10 cents per KH

More Complex Condition University admission rules: Applicants will be admitted if meet one of the following rules: – 1. Income >= 100,000 – 2. GPA > 2.5 AND SAT > 900 An applicant’s Income is 150,000, GPA is 2.9 and SAT is 800. Admitted? – Income >= 100,000 OR GPA > 2.5 AND SAT >900 How to evaluate this complex condition?

Scholarship: Business students with GPA at least 3.2 and major in Accounting or CIS qualified to apply: – 1. GPA >= 3.2 – 2. Major in Accounting OR CIS Is a CIS student with GPA = 2.0 qualified? – GPA >= 3.2 AND Major = “Acct” OR Major = “CIS” Is this complex condition correct?

Examples SAT = 800, Income 60,000, GPA 3.0, admitted? – (SAT > 900 OR Income >= 50,000) AND Not GPA < 2.5 A=2, B=3 – (A=3 OR NOT (B < A)) AND B=A+1

NOT Set 1: Young: Age < 30 Set 2: Rich: Income >= 100,000 YoungRich

Condition with Not University admission rules: Applicants will be admitted if meet all the rules: – 1. SAT > 900 OR Income >= 50,000 – 2. Not GPA < 2.5 Condition: – SAT > 900 OR Income >= 50,000 AND Not GPA < 2.5 – Correct?

Order of Evaluation 1. () 2. Not 3. AND 4. OR

Examples SAT = 800, Income 60,000, GPA 3.0, admitted? – (SAT > 900 OR Income >= 50,000) AND Not GPA < 2.5 A=2, B=3 – (A=3 OR NOT (B < A)) AND B=A+1

Java Examples (SAT > 900 OR Income >= 50,000) AND Not GPA < 2.5 – (SAT > 900 | Income >= 50,000) & !GPA < 2.5 GPA >= 3.2 AND (Major = “Acct” OR Major = “CIS”) – GPA>=3.2 & (Major.equals(“Acct”) | Major.equals(“CIS”)) (A=3 OR NOT (B < A)) AND B=A+1 – (A==3 | !(B < A)) & B=A+1

Leap Year The complex condition to determine if a year is leap year is: If (the year is divisible by 4 and not divisible by 100) or (the year is divisible by 400), then it is a leap year. Write a equivalent Java complex condition.