In outputting what does n do In outputting what is the difference between the Systemoutprint command and the Serialoutprintln command Warmup Monday February 24 In outputting what does n do ID: 257946
Download Presentation The PPT/PDF document "Warm-up: Monday, February 24" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Warm-up: Monday, February 24
In outputting, what does “\n” do?
In outputting, what is the difference between the
System.out.print
( ) command and the
Serial.out.println
( ) command? Slide2
Warm-up: Monday, February 24
In outputting, what does “\n” do?
Goes to the next line; like hitting “enter”
In outputting, what is the difference between the
System.out.print
( ) command and the
Serial.out.println
( ) command?
p
rint( ) stays on the same line
p
rintln
( ) will go to the next lineSlide3
Arithmetic Operators
PAP Computer Science, Cycle 5Slide4
8 Mathematical Operators
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
Exponent (^)
Increment (++)
Decrement (--)Slide5
Addition, Subtraction, Multiplication
These operators work identically in programming as in traditional math
5 + 8
13
10 – 4 6
4 * 3 12Slide6
Division
Works differently for integers (whole numbers) and decimals
If operands are integers, the answer will be an integer
If operands are decimals, the answer will be a decimal
11 / 2
5
11.0 / 2.0 5.5Slide7
Modulus (%)
Modulus returns the remainder from division
Example: 26 % 5
1 26 / 5 5Slide8
Incrementing (++)
Increases the value of a variable by 1
Example
int
x = 5;
x++;
System.out.print
(x)
6Slide9
Decrementing (--)
Decreases the value of a variable by 1
Example
int
x = 5;
x--;
System.out.print
(x)
4Slide10
Pre- and Post-Incrementing
When incrementing/decrementing, you can put the operator before or after the variable
Example
int
x = 5;
int
y = 5;
x++;
++y;
x
6 y 6Slide11
Pre- and Post-Incrementing
Example:
int
x = 5;
int
y = x++;
x
6 y 5
Example:
int
x = 5;
int
y = ++x;
x
6 y 6Slide12
Order of OperationsSlide13
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6Slide14
OOO Example
3 * 7
– 6 + 2 * 5 / 4 + 6Slide15
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 + 2 * 5 / 4 + 6Slide16
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 +
2 * 5
/ 4 + 6Slide17
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 + 2 * 5 / 4 + 6
21 – 6 + 10 / 4 + 6Slide18
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 + 2 * 5 / 4 + 6
21 – 6 +
10 / 4
+ 6Slide19
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 + 2 * 5 / 4 + 6
21 – 6 + 10 / 4 + 6
21 – 6 + 2 + 6Slide20
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 + 2 * 5 / 4 + 6
21 – 6 + 10 / 4 + 6
21 – 6
+
2 + 6Slide21
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 + 2 * 5 / 4 + 6
21 – 6 + 10 / 4 + 6
21 – 6 + 2 + 6
15 + 8Slide22
OOO Example
3 * 7 – 6 + 2 * 5 / 4 + 6
21 – 6 + 2 * 5 / 4 + 6
21 – 6 + 10 / 4 + 6
21 – 6 + 2 + 6
15 + 8
23Slide23
Warm-up: Tuesday, Feb 25
What does the % operator return?
int
x = 6;
x++;
What does ‘x’ equal now? Slide24
Creating Variables
PAP Computer Science, Cycle 5Slide25
Variables
Variables are place-holders for values
Storage locations
They reference a single number, character, or String, and can be called by their name.
Example:
X = 5
Y = X + 5;Slide26
Declaring a Variable
Variables must be declared with two things:
Data type
Name
Initializing a variable (giving it a value) is NOT required when declaring a variableSlide27
Declaring/Initializing a Variable
Example 1
int
x;
x = 5;
Example 2
int
x = 5;Slide28
Initializing a Variable
Variable name ALWAYS goes on the left
x
= 5 NOT 5 = x
y = x
Sets y to whatever is stored in x
Slide29
Naming a Variable
Names in Java must follow certain rules
Naming conventions hold true for variables, methods, classes,
etc
Cannot be a reserved word
int
, float, double, char, void, public, static, return
Can only consist of letters, numbers, _, $
Cannot begin with a number
Case sensitive
X != x VAR !=
var
Test != test Slide30
Legal Identifiers
Variable
s
tr
n
um_of_books
$Amount
integer3
convert2fahrSlide31
Illegal Identifiers (Names)
employee salary
Spaces are not allowed
Hello!
! is not allowed
o
ne+two
+ is not allowed
2nd
Cannot begin an identifier with a numberSlide32
Data Types
Data types refer to the type of data that will be stored in the variable
Lets the computer know how much memory it needs to set aside for the variable
Data Types
Int
, Short, or Long (whole numbers)
Float or Double (decimals)
Byte (binary byte)
Char (single character)
Boolean (true/false)Slide33
Int, Short, Long
Used for whole numbers
Short (16 bits of memory)
-32,768 to 32,767
Int
(32 bits of memory)
-2,147,483,648 to 2,147,483,647
Long (64 bits of memory)
-2
63
to 2
63Slide34
Float and Double
Used for decimal point numbers
Float (32 bits)
-3.4 x 10
38
to 3.4 x 10
38
6 to 7 digits of precision (decimal places)
Double (64 bits)
-1.7 x 10
308
to 1.7 x 10
308
15 digits of precision (decimal places)Slide35
Byte, Char, Boolean
Byte (8 bits)
Used for binary bytes (B11001101)
Char (16 bits)
Used for characters (‘A’)
Boolean (1 bit)
True or falseSlide36
Using Variables in Code
public class Variables
{
public static void main(String[ ]
args
)
{
int
test1 = 90;
float test2 = 86.34;
int
test3 = 23;
float sum = test1+test2+test3;
System.out.println
(“Sum = “ + sum);
}
}Slide37
Warm-up: Wednesday, Feb 26
Write the code to declare a
variable named “number” that
contains the number
45.46Slide38
Strings
PAP-Computer Science, Cycle 5Slide39
Strings
List of multiple characters
Also known as
Character array
Character string
String literal
String constant
In Java, String is a special classSlide40
String Class
As a class, a String has certain properties and methods
Properties
Length – how many letters in the String?
Letter position
Methods
compareTo
startsWith
endWithSlide41
Declaring Strings
Works much the same way as declaring any other variable
Needs data type and a name
Value is optionalSlide42
Declaring a String
String name = “Ms. Alexander”;
String message = “Good luck on your test!”;
String weather = “It is sunny outside.”;Slide43
Example Code - Strings
public class Example
{
public static void main(String[]
args
)
{
int
value = 56;
String
str
= “Your value is “;
System.out.println
(
str
+ value);
}
}