Multiple if-else boolean Data

Slides:



Advertisements
Similar presentations
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
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:
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
(Dreaded) Quiz 2 Next Monday.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Chapter 2 Elementary Programming
Class Definitions and Writing Methods
Dialogues and Wrapper Classes
Switch Statement.
Computer Programming Methodology Input and While Loop
Repetition.
TK1114 Computer Programming
Topic 11 Scanner object, conditional execution
Chapter 2.
Boolean Expressions And if…else Statements.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Java so far Week 7.
Branching Chapter 5 11/14/16 & 11/15/
Multiple if-else boolean Data
Self study.
Building Java Programs
Lecture Notes – Week 2 Lecture-2
CHAPTER 4: Conditional Structures
Repetition Statements
Outline Boolean Expressions The if Statement Comparing Data
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Computer Science Club 1st November 2019.
Presentation transcript:

Multiple if-else boolean Data Chapter 5 12/5/16 & 12/6/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 1 1 1

Topics Multiple if-else(5.3) Boolean Data (5.6) 2 2 2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 2 2 2

Multiple Outcomes if-else can produce two possible outcomes. One outcome for true Another for false condition Sometimes there can be several possible outcomes. 3

Example Course Description Write a program to accept a course number then print the description for the course. 4

import java.util.Scanner; //Outputs a course description for a course number public class CourseDescription { public static void main(String[] args) { //Input the course number Scanner scan = new Scanner(System.in); System.out.print("Enter Course Number:"); String number = scan.next(); //Output correct description if(number.equals("CS119")) System.out.println("Intro to Java"); else if(number.equals("CS115")) System.out.println("Intro to C"); else if(number.equals("CS111")) System.out.println("Intro to Python"); else System.out.println("Number not found."); scan.close(); } 5

Movie Ticket Example Move tickets sell at $6 for children under 14, $12 for adults, and $8 for seniors over 55. Three outcomes Can use an a series of if-statements in a chain to determine the price, given the age. 6

import java.util.Scanner; import java.text.DecimalFormat; //Determine movie price based on age public class Movie{ public static void main(String []args){ //Input movie-goer's age Scanner scan = new Scanner(System.in); System.out.print("What is your age?"); int age = scan.nextInt(); //Decide price, based on age.**YOU WRITE THE IF-ELSE CHAIN double price = 0.0; DecimalFormat formatter = new DecimalFormat("$0.00"); System.out.println(formatter.format(price) + " please."); } 7

Participation Write a program to accept an integer for placement in a hog show at the fair. Use a multiple if-else statement to output the ribbon color for the placement “Blue” “Red” “White” Any other number is “No ribbon” 8

Participation 2 Write a program to input sales. Using the follow chart, write an if-else chain that assigns .10, .15 or .20 to a commission rate depending on sales. Output the commission rate. Sales Commission Rate Up to $10,000 10% $10,000 to $15,000 15% Over $15,000 20%

Boolean Variables 10

Boolean Variables boolean is a primitive type. boolean variables can store either true or false. Can use to flag condition. Example: boolean icyRoads = true; if(icyRoads) System.out.println(“Stay home.”); else System.out.println(“Drive on.”); 11

Boolean data field in Class public class AutoRepair{ private String name; private String make; private String model; private boolean done; . . . 12

What is the value in y? int y, x = 5; boolean flag = true; if (x > 10 || x < 2) y = 1; else if(flag) y = 2; else if(x > -2) y = 3; else y = 4; 13