AP Java 9-17-2015 Review If else.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
Introduction to Computer Programming Decisions If/Else Booleans.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Java Review if Online Time For loop Quiz on Thursday.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
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.
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.
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Intro to Programming STARS College of Communication and Information Florida State University Written by: Hannah Brock Alissa Ovalle Nicolaus Lopez Martin.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures I
Chapter 2 Clarifications
CSC111 Quick Revision.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Chapter 4: Control Structures I
Yanal Alahmad Java Workshop Yanal Alahmad
Introduction to programming in java
Chapter 6 More Conditionals and Loops
Goals Understand how to create and compare Strings.
SELECTION STATEMENTS (1)
Java Methods Making Subprograms.
Control Statement Examples
Chapter 4: Control Structures I
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Java Enter your code from FRQ to Shell
Java Fix a program that has if Online time for Monday’s Program
Goals Understand how to create and compare Strings.
Goals Understand how to create and compare Strings.
SELECTION STATEMENTS (2)
Java Variables, Types, and Math Getting Started
Java Methods Making Subprograms.
Outline Boolean Expressions The if Statement Comparing Data
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
Fundamentals 2.
CS110D Programming Language I
Java Fix a program that has if Online time for Monday’s Program
Self study.
Building Java Programs
Building Java Programs
Introduction to Java Applications
Truth tables: Ways to organize results of Boolean expressions.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
CSC 1051 – Data Structures and Algorithms I
Array Review Selection Sort
AP CS Be able to fix a program written in Java
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

AP Java 9-17-2015 Review If else

Learning Objectives Be able to implement Java’s if and if..else into a program. Be able to evaluate boolean conditions.

What do you recall about… Types in Java Math operations Getting information from the user Showing information on the screen Byte Code, Java Virtual Machine, Compiling Class Method JavaDocs

if sample number1 = input.nextInt(); // read first number from user // Fig. 2.15: Comparison.java // Compare integers using if statements, relational operators // and equality operators. import java.util.Scanner; // program uses class Scanner public class Comparison { // main method begins execution of Java application public static void main( String args[] ) // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int number1; // first number to compare int number2; // second number to compare System.out.print( "Enter first integer: " ); // prompt number1 = input.nextInt(); // read first number from user System.out.print( "Enter second integer: " ); // prompt number2 = input.nextInt(); // read second number from user if ( number1 == number2 ) System.out.printf( "%d == %d\n", number1, number2 ); if ( number1 != number2 ) System.out.printf( "%d != %d\n", number1, number2 ); if ( number1 < number2 ) System.out.printf( "%d < %d\n", number1, number2 ); if ( number1 > number2 ) System.out.printf( "%d > %d\n", number1, number2 ); if ( number1 <= number2 ) System.out.printf( "%d <= %d\n", number1, number2 ); if ( number1 >= number2 ) System.out.printf( "%d >= %d\n", number1, number2 ); } // end method main } // end class Comparison if sample

If If (condition) { Commands; }

Conditions Pascal Java Example = == (x==y) <> != (x!=y) > >= (x>=y) <= (x<=y) AND && (x<y)&&(y<z) OR II (x>y)||(x<z)

Order of operations for comparisons () *, /, % Multiplicative operators +, - Additive operators <, >, >=, <= Relational operators ==, != Then do any comparisons for equality and inequality && Logical and || Logical or = Assignment operator

Is it true or false? boolean test1 = 1 < 2;

*, /, % Multiplicative operators +, - Additive operators Logical Operators Worksheet If x = -2, y = 5, z = 0 , and t = -4, what is the value of each of the following logical expressions? x + y < z + 1 x - 2 * y + y < z * 2 / 3 3 * y / 4 < 8 && y >= 4 t > 5 || z < 2 x * y < 10 || y * z < 10 (y + 2) / 3 > 3 && t < 0 x * 3 > 0 || y + 5 / t < 2 !(x > 0) !(x * t < 10) || y / x * 4 < y * 2 t > 5 || z < (y + 5) && y < 3 !(4 + 5 * y >= z - 4) && (z - 2 < 7) Write syntactically correct logical expressions for the following conditions: m is less than 100 n is positive and greater than m m is between 5 and 10 (inclusive) k is less than 1 or greater than 2 j and k are both negative i is an even number Order of Operations () *, /, % Multiplicative operators +, - Additive operators <, >, >=, <= Relational operators ==, != Then do any comparisons for equality and inequality && Logical and || Logical or = Assignment operator

You can leave off the {} if there is only one command line. If else You can leave off the {} if there is only one command line. if (condition) { commands; } else Commands;

System.out.println("Young 'en"); else if (age<20) import java.util.Scanner; //Smith's if..else sample public class IfElseSample { public static void main(String[] args) { int age=0; //create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); System.out.println("Please enter your age"); age = input.nextInt(); if (age<=12) System.out.println("Young 'en"); else if (age<20) System.out.println("Teeny bopper"); else if (age < 65) System.out.println("Time for work"); System.out.println("prep for retirement"); } else System.out.println("Retire"); System.out.println("second career"); System.out.println("Pay off kids college");

Programs: Complete the following. Modify the quadratic equation program to also calculate imaginary roots. If b2 – 4ac >= 0 then display the real roots Else show the imaginary roots Input three real numbers, and print the numbers in sorted order. Write a program that inputs a persons name, rate of pay, and the number of hours worked during the week. The program will calculate the pay for the person. For every hour of overtime work (over 40 hours in the week) the person gets paid 150 percent of the regular wage. Example: Input: Sue, $10.00 per hour rate, 45 hours Calculation: pay = 10*40 + 1.5*10*5 = $475 Input a value representing a person’s percent score (example inputting 80 represents an 80% score), write a program that will output the corresponding letter grade. ‘A’ for 90-100, ‘B’ for 80 to 89, etc.