warm-up: monday, march 24
DESCRIPTION
Warm-Up: Monday, March 24. List as many commands in Java as you can remember (at least 10) . Review. PAP Computer Science Cycle 5. Output. System.out.print ( ) System.out.println ( ) Escape sequences \n newline \t tab \\ backslash \’ single quote - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/1.jpg)
Warm-Up: Monday, March 24
List as many commands in Java as you can remember (at least 10)
![Page 2: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/2.jpg)
ReviewPAP Computer Science
Cycle 5
![Page 3: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/3.jpg)
OutputSystem.out.print( )System.out.println( )
Escape sequences\n newline\t tab\\ backslash\’ single quote\” double quote
![Page 4: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/4.jpg)
7 Mathematical Operators Addition (+)Subtraction (-)Multiplication (*)Division (/)Modulus (%)Increment (++)Decrement (--)
![Page 5: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/5.jpg)
Declaring/Initializing a Variable
Example 1
int x;
x = 5;
Example 2
int x = 5;
![Page 6: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/6.jpg)
Java Programming: Guided Learning with Early Objects 6
Input (Read) StatementThe Scanner class puts data into variables from the
standard input device static Scanner console = new Scanner(System.in);
Next input is: Integer: console.nextInt()Double: console.nextDouble()String: console.next()or console.nextLine()
![Page 7: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/7.jpg)
Math Methodsabs(x) – absolute value
abs(-96.5) 95.6ceil(x) – ceiling operator
ceil(54.13) 55floor(x) – floor operator
floor(54.13) 54exp(x) – returns ex
exp(3) e3 20.0855369232
![Page 8: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/8.jpg)
Math Methodslog(x) – natural logarithm (base e) of
x
log(2) ln(2) 0.69314718056log10(x) – base-10 logarithm of x
log10(100) log10(100) 2.0
max(x,y) – maximum of two numbers
max(15, 25) 25min(x,y) – minimum of two numbers
min(3, 4) 3
![Page 9: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/9.jpg)
Math Methodspow(x,y) – returns xy
pow(2,3) 23 8round(x) – rounds x to the nearest
whole #
round(34.4) 34
round(34.7) 35sqrt(x) – square root of a number
sqrt(121) √121 11
![Page 10: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/10.jpg)
String MethodsString.length( ) – returns the length of
the string (how many letters long it is)String word = “dog”; word.length( ) 3
String.toLowerCase( ) – changes all capital letters to lowercaseString word = “HAHA”; word.toLowerCase( ) “haha”;
String.toUpperCase( ) – changes all lowercase letters to capital lettersString word = “kitty”; word.toUpperCase( ) “KITTY”
![Page 11: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/11.jpg)
More String MethodsString.charAt(x) – returns the letter at
position x in the StringString word = “happy”;word.charAt(2) ‘p’;
String.replace(x,y) – replaces each instance of character ‘x’ with character ‘y’String word = “happy”;word.replace(‘p’,’ r’) “harry”
![Page 12: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/12.jpg)
indexOf( ) indexOf(char) – returns the index, or list
location, of the first instance of letter char String word = “happy”; word.indexOf(‘h’) 0
indexOf(str) – returns the index, or list location, of the first instance of a String str String name = “Alexander”; name.indexOf(“and”) 4
Note: Both of these commands will return -1 if the character or String is not found
![Page 13: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/13.jpg)
String.substring( )String.substring(x, y) – Converts the
characters between list location x of the String (inclusive) and list location y of the String (exclusive) into a new String
String word = “Spring Break”;
word.substring(0,6) “Spring”word.substring(7, 12) “Break”word.substring(3, 9) “ing Br”
![Page 14: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/14.jpg)
TODAYIF YOU ARE MISSING WORK…USE THIS
TIME TO COMPLETE SOME OF YOUR MISSING WORK
IF YOU ARE NOT MISSING WORKcodingbat.com/javaComplete codes within any section
(though String-1 will likely be the easiest)
1 ML:A pass per SECTION STAR completed
![Page 15: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/15.jpg)
Warm-Up: Tuesday, March 25
Give an example of a program that would require you to use an IF statement
EXAMPLE: Output the phrase “Be sure to wear a jacket” if the temperature is less than 55°
![Page 16: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/16.jpg)
IF StatementsPAP Computer Science
Cycle 5
![Page 17: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/17.jpg)
IF Statements
IF statements are used to add branching structure to your codes
They are used to make decisions
![Page 18: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/18.jpg)
Branching Structures
?
Task A Task B
YES NO
![Page 19: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/19.jpg)
IF Statement: Structure
if (condition)
{
//statements
}
STATEMENTS ARE ONLY EXECUTED IF THE CONDITION IS TRUE
![Page 20: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/20.jpg)
Conditions
Conditions evaluate a state or compare two or more numbersWhile the code is runningIf x is greater than yIf the remainder is equal to 0While x + y is less than 7
![Page 21: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/21.jpg)
Conditional OperatorsLess than (<)Greater than (>)Equal to (==)Less than or equal to (<=)Greater than or equal to (>=)Not equal to (!=)
![Page 22: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/22.jpg)
English to Codex is greater than y
x > yx plus y is less than 7
x + y < 7x is an even number
x % 2 == 0the number does not equal our target
number != target
![Page 23: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/23.jpg)
Using Conditions with IF Statements
If the temperature is below 55, output that the user needs a jacket
if (temp < 55)
{
System.out.println(“Bring a jacket!”);
}
![Page 24: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/24.jpg)
Warm-up: Thursday, Mar 27
Write an IF statement for the following:
Given two numbers num1 and num2, output the sum of the numbers ONLY IF the sum is less than 10.
![Page 25: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/25.jpg)
Warm-up: Thursday, Mar 27
Write an IF statement for the following:
Given two numbers num1 and num2, output the sum of the numbers ONLY IF the sum is less than 10.
double sum = a+b;
if (sum < 10) {
System.out.println(sum);
}
![Page 26: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/26.jpg)
Review: IF StatementsUsed to add branching structure to codesMake decisionsAnswer questions
if (condition) {
//statements;
}
![Page 27: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/27.jpg)
Review: Conditional Operators
Less than (<)Greater than (>)Equal to (==)Less than or equal to (<=)Greater than or equal to (>=)Not equal to (!=)
![Page 28: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/28.jpg)
IF-ELSE StatementsUsed for situations with two possible
outcomesNoted by the keyword “otherwise”
ExamplesIf it’s sunny, wear shorts; otherwise, wear
jeansIf it’s a weekday, set the alarm for 7:00,
otherwise, turn off the alarm
![Page 29: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/29.jpg)
IF-ELSE Statement: Structure
if (condition) {
//statements;
}
else {
//statements;
}
*NOTE: There is NO condition after else
![Page 30: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/30.jpg)
Example: IF-ELSEif (weather==“sunny”) {
System.out.println(“Wear shorts”);
}
else {
System.out.println(“Wear jeans”);
}
![Page 31: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/31.jpg)
EVEN MORE BRANCHING!Sometimes, you need EVEN MORE than 2
outcomesFor these situations, you use IF – ELSE-IF –
ELSE statements
ExamplesIf you like dogs, buy a dog, but if you like
cats, but a cat; otherwise, get a different pet.
If your number is divisible by 5, output 1, but if your number is divisible by 7, output 2; otherwise, output 0.
![Page 32: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/32.jpg)
IF – ELSE-IF – ELSE Structure
if (condition1) {
//statements;
}
else if (condition2) {
//statements;
}
else {
//statements;
}
![Page 33: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/33.jpg)
Example IF – ELSE-IF – ELSE
if (num % 5 == 0) {
System.out.println(1);
}
else if (num % 7 == 0) {
System.out.println(2);
}
else {
System.out.println(0);
}
![Page 34: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/34.jpg)
ELSE-IF’s
You can have as many ELSE-IF’s in an IF statement as you want, but you can only have
ONE IF and ONE ELSE
![Page 35: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/35.jpg)
OKAYif ( …) {
}
else if (…) {
}
else if (…) {
}
else {
}
![Page 36: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/36.jpg)
NOT OKAYif ( …) {
}
else if (…) {
}
else {
}
else {
}
![Page 37: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/37.jpg)
OKAY…but it doesn’t mean the same thing…
if ( …) {
}
if (…) {
}
else if (…) {
}
else {
}
![Page 38: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/38.jpg)
OKAY…but it doesn’t mean the same thing…
if ( …) {
}
if (…) {
}
else if (…) {
}
else {
}
![Page 39: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/39.jpg)
Warm-Up: Friday, Mar 28
Write an IF statement for the following situation:
Given two numbers, a and b, output the sum; unless the sum is greater than 20, then output the number 20.
![Page 40: Warm-Up: Monday, March 24](https://reader035.vdocuments.mx/reader035/viewer/2022062810/56815e9a550346895dcd2cfe/html5/thumbnails/40.jpg)
Warm-Up: Thursday, Mar 26
Given two numbers, a and b, output the sum; unless the sum is greater than 20, then output the number 20.
sum = a+b;
if (sum > 20) {
System.out.println(20);
}
else {
System.out.println(sum);
}