JF 4 3 Practice
JF 4 3 Practice
Lesson Objectives:
• Use primitive data types in Java code
• Specify literals for the primitive types and for Strings
• Demonstrate how to initialize variables
• Describe the scope rules of a method
• Recognize when an expression requires a type conversion
• Apply casting in Java code
• Use arithmetic operators
• Use the assignment operator
• Use a method from the Math class
• Access a Math class method from the Java API
Vocabulary:
Identify the vocabulary word for each definition below.
Named primitive or object storage mechanisms defined in a program. The assigned value
may or may not (constants) change.
The smallest java primitive type (1 byte) that can hold an integer value.
This Java primitive data type (4 bytes) can hold integer values.
This Java primitive data type (8 bytes) is the largest primitive that can hold a decimal
value.
This Java primitive data type (4 bytes) can be initialized with a decimal number preceding
letter f. Example: float x = 3.5f;
Can be any number, text, or other information that represents a value; used to initialize a
primitive type.
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
A Java statement when a variable is defined but not necessarily assigned a value.
Example: int x;
This word describes the mathematical precedence that a variable has in a Java program.
A java primitive data type (2 bytes) that can hold single character values. Example: “a”,
“#”, or “X”
Used to describe the block of code where a variable exists in a program. A block of code is
denoted by {}.
The process of explicitly modifying one data type to become a different data type.
The equals sign “=” used in a Java statement to assign a value to a variable.
The process of modifying one data type to become a different data type, this may be
implicit or explicit.
A Java primitive data type (2 bytes) that holds integer numbers within a shorter range than
an int.
A one-bit java primitive type that can hold the value true or false.
2. Write the following math formulas in Java. You will need to use methods from the Math class as well as nesting of methods
and parentheses to force the order of operations to correctly calculate the answer. Assume that all the variables in the
formulas have already been declared and initialized.
√𝑥𝑥 5 −6
a. 𝑎𝑎 =
4
b. 𝑏𝑏 = 𝑥𝑥 𝑦𝑦 − 6 𝑥𝑥
𝑧𝑧
c. 𝑐𝑐 = 4𝑐𝑐𝑐𝑐𝑐𝑐( ) − 𝑠𝑠𝑠𝑠𝑠𝑠𝑥𝑥 2
5
d. 𝑑𝑑 = 𝑥𝑥 4 − �6𝑥𝑥 − 𝑦𝑦 3
1
e. 𝑒𝑒 = 1
𝑦𝑦−
𝑥𝑥−2𝑦𝑦
3. A bus holds 45 people. The school will only use a bus if they can fill it completely. The rest of the people will ride in vans.
Write a program that will take in the number of people that are signed up to go on a field trip. Have the program print the
number of busses necessary and then total number of people that will need to ride in vans.
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. 2
4. Write true or false on the blanks in the program below to show the value of the boolean variable true_false as the program
executes.
int i=5;
int j=6;
boolean true_false;
true_false=(j<5); ________
true_false=(j>3); ________
true_false=(j<i); ________
true_false=(i<5); ________
true_false=(j<=5); ________
true_false=(6<6); ________
true_false=(i!=j); ________
true_false=(i==j || i<50); ________
true_false=(i==j && i<50); ________
true_false=(i>j || true_false && j>=4); ________
5. Explain why each of the declarations in the second list are wrong.
6. Explain why each of the declarations in the second list do not follow conventions for variable names.
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. 3