230 likes | 353 Vues
This guide covers the essentials of declaring and initializing variables, including integral and floating-point types. You'll learn how to declare constants and use the Math library to print values. We will discuss type compatibility across different variable types, ensuring that you understand valid assignments. Additionally, the guide explores expressions and their types, along with practical examples. Lastly, we dive into generating random numbers using the Random class, allowing you to create variables that assign random integers and doubles within specified ranges.
E N D
CSC 204 Drill and Practice Expressions, Types, Variables, Random Numbers
Declare a variable • Declare a variable named temperature capable of holding an integral value.
Declare and initialize a variable • Declare a variable named temperature capable of holding an integral value. • Give it the initial value 32.
Declare a variable • Declare a variable named bodyTempcapable of holding a floating point value.
Declare and initialize a variable • Declare a variable named bodyTempcapable of holding a floating point value. • Give it the initial value 98.6.
Declare and initialize a constant • Declare a constant named NORMAL_BODY_TEMP and give it the initial value 98.6.
Use the Math Library • Print the value of Pi.
Use the Math Library • Given the variable radius that represents the radius of a circle, create variables named area and circumference and assign them the area and circumference of the circle.
Type Compatibility • If you have the variables of the types defined below: • byte b; double d; • float f; inti; • long l; short s; • Which of the assignments are valid? • i = b; • i = d; • i = f; • i = l; • i = s;
Type Compatibility • If you have the variables of the types defined below: • byte b; double d; • float f; inti; • long l; short s; • Which of the assignments are valid? • l = b; • l = d; • l = f; • l = i; • l = s;
Type Compatibility • If you have the variables of the types defined below: • byte b; double d; • float f; inti; • long l; short s; • Which of the assignments are valid? • d = b; • d = l; • d = f; • d = i; • d = s;
Expressions • Give the type and values of each of the following expressions. • 2 * 3 • 2 % 3 • 2 / 3 • 2 + 3 • 2.0 * 3 • 0 % 3 • 2.0 / 3 • 24 % 3
Expressions • Give the type and values of each of the following expressions. • 4 + 2 * 3 • 4 - 3 - 2 • 4 * 2 % 3 • 4 % 3 * 2 • 10 / 3 / 2.0 • 2.0 + 4 / 6 • 2.0 * 4 / 6 • 23 % 8 % 2
Random Numbers • Create a variable named r that can generate random numbers.
Random Numbers • Using the Random variable r, assign a random integer between 0 and 10 inclusive to the integer variable val.
Random Numbers • Using the Random variable r, assign a random integer between 1 and 10 inclusive to the integer variable val.
Random Numbers • Using the Random variable r, assign a random even integer between 1 and 10 inclusive to the integer variable val.
Random Numbers • Using the Random variable r, assign a random integer between min and max, where min ≤ max , the integer variable val.
Random Numbers • Using the Random variable r, assign a random double between 0 and 1.0 to the double variable val.
Random Numbers • Using the Random variable r, assign a random double between 0 and 10.0 to the double variable val.
Random Numbers • Using the Random variable r, assign a random double between 100.0 and 150.0 to the double variable val.