CS-0401 INTERMEDIATE PROGRAMMING USING JAVA

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
The if Statement The code in methods executes sequentially.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
The switch Statement, DecimalFormat, and Introduction to Looping
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Chapter 3 Decision Structures 1. Contents 1.The if Statement 2.The if-else Statement 3.The if-else-if Statement 4.Nested if Statement 5.Logical Operators.
Lecture 3 Decision Structures. 4-2 Topics – The if Statement – The if - else Statement – The PayRoll class – Nested if Statements – The if - else - if.
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
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.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2013.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Control Stuctures Module 4 Copyright © Copyright © Slide #2 Decision Structures Chapter Objectives To understand: how to write boolean expressions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3: Decision Structures
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Chapter Making Decisions 4. Relational Operators 4.1.
Week 4 Program Control Structure
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Ltd.
Chapter 4: Control Structures I
Lecture 2b- Java Fundamentals
INTERMEDIATE PROGRAMMING USING JAVA
Chapter 3 Control Statements
CS0007: Introduction to Computer Programming
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Chapter 4: Control Structures I
Chapter 6 Conditionals.
Chapter 4: Making Decisions.
Chapter 3: Decision Structures
Decisions Chapter 4.
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Introduction to programming in java
Chapter 1: Introduction to Computers and Java
Chapter 1: Introduction to Computers and Java
DKT121: Fundamental of Computer Programming
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Making Decisions.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Chapter 6: Conditional Statements and Loops
SELECTION STATEMENTS (1)
Chapter 3: Decision Structures
if-else-if Statements
Chapter 3: Decision Structures
Chapter 4: Control Structures I
Starting Out With Java 5 (Control Structures to Objects) Chapter 3
Chapter 3:Decision Structures
The System.exit() Method
Control Structure Chapter 3.
SELECTIONS STATEMENTS
Lecture 2b- Java Fundamentals
Week 3 – Program Control Structure
Control Structure.
Chapter 6 Conditionals.
Presentation transcript:

CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Spring 2018

Chapter 3 Decision Structures

Chapter 3 Presentation Outline The if Statement The if-else Statement Nested if statements The if-else-if Statement Logical Operators Comparing String Objects The Conditional Operators The Switch Operator Displaying formatted output .

The IF statement

The IF statement Condition (true or false) I need to go to the lab! If it’s cold outside, I’ll wear a coat The “action” if the “condition” is true

What kind of statements can generate “true” or “false” values? The if Statement The general form if (condition is true) execute next statement What kind of statements can generate “true” or “false” values?

Boolean Expressions A boolean expression is any variable or calculation that results in a true or false condition. Expression Meaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y. x == y Is x equal to y? x != y Is x not equal to y?

if Statements and Boolean Expressions double x = 10, y = 5, z = 10; if (x > y) System.out.println("X is greater than Y"); if(x == z) System.out.println("X is equal to Y"); if(x != y) { System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is."); } boolean isElegible = age > 16; if (isElegible) { takeDriverLicenseTest();

String Comparison String name1 = “John”; String name2 = “john”; String name3 = “John”; String name4 = “Mary”; if(name1 == name3) // #1 true or false? if(name1.equals(name3)) // #2 true or false? if(name1.equals(name2)) // #3 true or false? if(name1.equalsIgnoreCase(name2) // #4 true or false? name1.compareTo(name4) // <, ==, or > 0 ? name1.compareTo(name3) // <, ==, or > 0 ? name1.compareTo(name2) // <, ==, or > 0 ? name2.compareTo(name4) // <, ==, or > 0 ? How last statement could be changed if case sensitive is not an issue?

String is an Object, not a primitive variable! The right and wrong way of comparing objects, such as a String

A reference to where the elephant object is placed in the memory Elephant@0x12EF05 long

Comparing References instead of Values String name1 = “John”; String@87 J o h n name1 J o h n String name3 = “John”; String@A9 if(name1 == name3) if(x87 == xA9) if(name1.equals(name2) “John” is equal to “John” name2

This was true for Strings until recently…

Flowcharts IF statements can be modeled as a flow chart. Get Ready Wear a coat Yes Is it cold outside? No getReady(); if (coldOutside) wearCoat(); takeTheBus(); Take the bus

Flowcharts If you have multiple tasks to perform if the condition is true, then use a block! Wear a coat. Yes Is it cold outside? Wear a hat. Wear gloves. if (coldOutside) { wearCoat(); wearHat(); wearGloves(); } Note the use of curly braces to block several statements together as one

The IF-ELSE statement

if-else Statements The if-else statement adds the ability to conditionally execute code when the if condition is false. if (expression) statementOrBlockIfTrue; else statementOrBlockIfFalse;

if-else Statement Flowcharts Wear a coat Yes Is it cold outside? Wear shorts Take the bus No

Nested IF statements

Nested if Statement Flowcharts Wear a jacket Yes Is it cold outside? Wear shorts Is it snowing? Wear a parka No

Nested if Statements if (coldOutside) { if (snowing) { wearParka(); } else { wearJacket(); } wearShorts();

if-else-if Statements

if-else-if Statements if (expression_1) { statement; etc. } else if (expression_2) { }   Insert as many else if clauses as necessary } else { If expression_1 is true these statements are executed, and the rest of the structure is ignored. Otherwise, if expression_2 is true these statements are executed, and the rest of the structure is ignored. These statements are executed if none of the expressions above are true.

if-else-if Flowchart

Do they produce the same results? if (score < 60) { grade = “F”; } else if (score < 70) { grade = “D”; } else if (score < 80) { grade = “C”; } else if (score < 90) { grade = “B”; } else if (score <= 100) { grade = “A”; } else { // invalid score } if (score < 60) { grade = “F”; } if (score < 70) { grade = “D”; if (score < 80) { grade = “C”; if (score < 90) { grade = “B”; if (score <= 100) { grade = “A”; } else { \\ invalid score; Do they produce the same results? Even if they did, is there any advantage in using one over the other?

Coding Examples AverageScore.java ConsultantCharges.java LoanQualifier.java TestResults.java StringCompare.java StringCompareTo.java

Logical Operators

The AND Operator AND

In what pair of conditions will Jenny will go to NY? got paid Megabus ticket is still available Is Jenny going to NY? true false

OR

The OR Operator See LogicalOr.java program Suzan is going with Jenny Megabus ticket is still available Is Jenny going to NY? true false See LogicalOr.java program

The NOT Operator gotSick !gotSick true false

Logical Operators && || ! Operator Meaning Example AND if (ticketIsAvailable && gotPaid) || OR if (SuzanIsGoing || ticketIsAvailable) ! NOT if (!gotSick) LogicalAnd.java LogicalOr.java

The switch Statement

The switch Statement switch(Expression) { case CaseExpression: // place one or more statements here break; // case statements may be repeated //as many times as necessary default: }

switch(weekDay) { case monday: case tuesday: case wednesday: case thursday: case friday: System.out.println(weekday + “ is a “ + working day! Get out of the bed now”); break; case saturday: doGroceries(); cutGrass(); case sunday: goToChurch(); stayTheRestOfTheDayHome(); // case statements may be repeated //as many times as necessary default: // holiday!!!! Go to the beach!!! }

Switch Examples NoBreaks.java PetFood.java SwitchDemo.java

Formatting the Output

A common problem… double grossPay = 874.12; System.out.println("Your pay is “ + grossPay);

The DecimalFormat Class

Decimal Format Format1.java Format2.java Format3.java Format4.java

The printf method

The printf Method double grossPay = 874.12; System.out.println("Your pay is “ + grossPay);

The printf Method System.out.printf(FormatString, ArgList); FormatString is a string that contains text and/or special formatting specifiers. ArgList is optional. It is a list of additional arguments that will be formatted according to the format specifiers listed in the format string.

The printf Method double grossPay = 874.12; System.out.printf("Your pay is %f.\n", grossPay); The contents of the grossPay variable will be printed in the location of the %f format specifier. The %f format specifier indicates that a floating-point value will be printed.

The printf Method double grossPay = 874.12; System.out.printf("Your pay is %.2f.\n", grossPay); The %.2f format specifier indicates that a floating-point value will be printed, rounded to two decimal places.

The printf Method int hours = 40; System.out.printf("I worked %d hours.\n", hours); The contents of the hours variable will be printed in the location of the %d format specifier. The %d format specifier indicates that a decimal integer will be printed.

The printf Method int dogs = 2, cats = 4; System.out.printf("We have %d dogs and %d cats.\n", dogs, cats);

The %s format specifier indicates that a string will be printed. The printf Method String name = "Ringo"; System.out.printf("Your name is %s.\n", name); The %s format specifier indicates that a string will be printed.

That’s the End of Chapter 3

Any Questions?