Unit 1 · Lab 1

Variables, Output, Errors & Division

Variables · Output · Errors · Division · Scanner

🐞 Task 1: Spot the Issue

Write the code, then try to compile

Your Goal

1. Write the code shown below in your editor.

2. Compile the code and observe what happens.

3. Fix any issues, then identify the error type.

Example Code

💥 Task 2: Compile & Run

Write the code, then compile and run it

Your Goal

1. Write the code shown below in your editor.

2. Compile the code, then run the program.

3. Observe what happens, identify the error type, and fix it.

Example Code

🧠 Task 3: Logic Error

It compiles and runs — but is the answer right?

Your Goal

1. Write code that calculates the average of 10 and 3.

2. Use only integer variables and observe the resulting value.

3. Decide whether the result is correct even though the code compiles and runs.

4. Identify the error type, then fix it so the decimal portion is preserved.

🖨️ Task 4: Print vs Println

Two ways to output text

Your Goal

1. Print Hello World Java on one line.

2. Print end on the next line.

Example Output
Hello World Java
end

💬 Task 5: Direct vs Quoted

When do you need double quotes?

Your Goal

1. Print the boolean value false.

2. Print the integer 5.

3. Print the String "5".

4. Explain why false and 5 do not need double quotes.

🏷️ Task 6: Naming Rules

Which names are valid?

Your Goal

1. Identify whether each item is a valid variable name:
myVar 2ndPlace class _temp my-var MAX_SIZE my var count2

2. State which rule each invalid name breaks.

✨ Task 7: Escape Sequences

Printing special characters

Your Goal

1. Print He said "Hello".

2. Print the file path C:\Users\Name with the backslashes visible.

3. Use one output statement to print two lines:
Line1
Line2

Example Output
He said "Hello"
C:\Users\Name
Line1
Line2

🗃️ Task 8: Uninitialized Variable

Can you use a variable without giving it a value?

Your Goal

1. Declare double b; without assigning a value.

2. Print b using System.out.println(b).

3. Compile the code and observe what happens.

4. Identify the error type, then fix it.

➗ Task 9: Integer Division

int vs double — truncation and promotion

Your Goal

1. Declare int c = 8 / 7, print c, and explain the value.

2. Declare double d = 8 / 7, print d, and explain why the result is not 1.142....

3. Declare double e = 8.0 / 7, print e, and explain why the result is different.

4. Explain integer truncation and automatic promotion in your own words.

🧮 Task 10: Mixed Arithmetic

What happens when int meets double?

Your Goal

1. Declare one int variable and one double variable with any values.

2. Compute intVar + doubleVar * 2 and print the result.

3. Store that result in an int variable and observe the compiler response.

4. Explain which type wins when int and double mix.

⌨️ Task 11: Constants & Input

Combining user input with constants

Your Goal

1. Write a program that asks the user to enter an integer amount.

2. Use the constant final double RATE = 0.15 for the rate.

3. Calculate a fee based on the amount and rate, then print it.

4. Explain the result type when an int multiplies a double.

🔒 Task 12: Final Constants

Can you change a final variable?

Your Goal

1. Declare the constant final int MAX_SCORE = 100.

2. Reassign MAX_SCORE to a different value.

3. Compile the code and observe what happens.

4. Explain what final means and why the error occurs.

01 / 13