Multiple if-else boolean Data

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Advertisements

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.
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.
Chapter 5 Loops.
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.
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.
Chapter 4: Control Structures II
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
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.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
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.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
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.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
(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)
Chapter 2 Elementary Programming
Class Definitions and Writing Methods
Multiple if-else boolean Data
Dialogues and Wrapper Classes
Switch Statement.
Computer Programming Methodology Input and While Loop
Repetition.
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,
Topic 11 Scanner object, conditional execution
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/
AP Java Review If else.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
Self study.
Lecture Notes – Week 2 Lecture-2
CHAPTER 4: Conditional Structures
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
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 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 1 1

Programs Program 6 is being graded. Program 7 is on my website. Bonus point for evaluation. 2

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

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

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

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("CS117")) System.out.println("Intro to C++"); else System.out.println("Number not found."); scan.close(); }

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 serious of if-statements in a chain to determine the price, given the age.

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. double price = 0.0; if(age <= 14) price = 6.00; else if(age < 55) price = 12.00; else price = 8.00; DecimalFormat formatter = new DecimalFormat("$0.00"); System.out.println(formatter.format(price) + " please."); }

Boolean Variables Skip this Spring ‘16

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.”);

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

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;

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”