File - Mr. Smith`s Classes

Download Report

Transcript File - Mr. Smith`s Classes

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
// 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
if sample
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
If (condition)
{
Commands;
}
Conditions
Pascal
=
Java
==
Example
(x==y)
<>
!=
(x!=y)
>
>
(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;
boolean test2 = 1 > 2;
boolean test3 = 3.5 != 1;
boolean test4 = 17*3.5 >= 67.0 - 42;
boolean test5 = 9.8*54 <= 654;
boolean test6 = 6*4 == 3*8;
boolean test7 = 6*4 <= 3*8;
boolean test8 = 6*4 < 3*8;
•
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Logical Operators Worksheet
If x = -2, y = 5, z = 0 , and t = -4, what is the value of each of the following logical
expressions?
Order of Operations
x+y<z+1
•
()
x-2*y+y<z*2/3
•
*, /, % Multiplicative operators
3 * y / 4 < 8 && y >= 4
t > 5 || z < 2
•
+, - Additive operators
x * y < 10 || y * z < 10
•
<, >, >=, <= Relational operators
(y + 2) / 3 > 3 && t < 0
•
==, != Then do any comparisons for
x * 3 > 0 || y + 5 / t < 2
equality and inequality
!(x > 0)
•
&& Logical and
!(x * t < 10) || y / x * 4 < y * 2
t > 5 || z < (y + 5) && y < 3
•
|| Logical or
!(4 + 5 * y >= z - 4) && (z - 2 < 7)
•
= Assignment operator
Write syntactically correct logical expressions for the following conditions:
1. m is less than 100
2. n is positive and greater than m
3. m is between 5 and 10 (inclusive)
4. k is less than 1 or greater than 2
5. j and k are both negative
6. i is an even number
If else
if (condition)
{
commands;
}
else
{
Commands;
}
You can
leave off the
{} if there is
only one
command
line.
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
 b 2  4ac
b 
 i
2a 
2a






• 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.