- Practice declaring and initializing variables in Java.
- Practice using different operators.
In this lab, you are going to practice declaring and initializing variables while also practicing using various operators that you have learned in this section.
Fork and clone this repository. When you do, you will see a Lab.java
file.
This is where you will put all of your code. Note the comments in the file as
well. This should provide a basic layout of where to put certain parts of your
code.
Each of the comments in the Lab.java
file will represent a section from these
instructions.
During each section, feel free to run the program as much as you want to ensure the output is what you expect. You may also run the program with the debugger to see the variables in the Java Visualizer. You could also use the browser based visualizer here as well.
In this section, declare and initialize the variables you will work with in this program.
- Declare a
boolean
variable calledresult
. - Declare an
int
variable calledx
. - Declare and initialize an
int
variable calledy
to the value 2. - Declare and initialize an
int
variable calledz
to the value 8. - Declare and initialize a
String
variable calledthanks
to the value "Thank you for visiting, ". - Declare and initialize a
String
variable calledname
to the value "Eric".
Run your program after you have finished declaring and initializing all variables in this section. This section produces no output as there are no print statements, but you should still make sure it runs and compiles without error.
In this section, you will practice using the +
operator to concatenate
multiple String
values.
- Declare and initialize a
String
variable calledthanksName
to combine the stringsthanks
andname
together. - Print out the variable
thanksName
to the console usingSystem.out.println()
. - The program should print
Thank you for visiting, Eric
when it executes the print line. - Here is a visualization of memory and how everything should look come this section:
In this section, you will practice using various arithmetic operators on int
data types.
- Initialize
x
to the sum ofy
andz
.- Remember, at this point,
x
has been declared but not initialized.
- Remember, at this point,
- Print out the variable
x
to the console usingSystem.out.println()
. - Assign
x
to the quotient ofz
divided byy
. - Print out the variable
x
to the console. - Assign
x
to the following expression:- Write the expression where
z
is multiplied with itself. - Take the product of the previous expression and mod it with
y
.- Hint: product %
y
.
- Hint: product %
- Write the expression where
- Print out the variable
x
to the console. - In this section alone, the program should print:
10
4
0
Here is a visualization of memory and how everything should look come this section:
In this section, you will practice using relational operators with int
data
types. Remember, the result of using relational operators is a boolean
.
- Initialize
result
to check ifx
is less than or equal toy
. - Print out the variable
result
to the console usingSystem.out.println()
. - Assign
result
to check ify
is not equal toz
. - Print out the variable
result
to the console. - Assign
result
to check ifz
is less thanx
- Print out the variable
result
to the console. - In this section alone, the program should print:
true
true
false
Here is a visualization of memory and how everything should look come this section:
In this section, you will practice using some compound assignment operators with
int
data types.
- Re-assign
x
to the value of 4. - Use the addition compound assignment operator (
+=
) to re-assignx
with the value ofy
added tox
. - Print out the variable
x
to the console usingSystem.out.println()
. - Use the decrement operator (
--
) to re-assignx
to decrease its value by 1. - Print out the variable
x
to the console. - Use the multiplication compound assignment operator (
*=
) to re-assignx
with the value ofx
timesz
. - Print out the variable
x
to the console. - In this section alone, the program should print:
6
5
40
Here is a visualization of memory and how everything should look come this section:
The expected output of the program should be:
Thank you for visiting, Eric
10
4
0
true
true
false
6
5
40