Lab 02: Character Stats
Declaring variables of every type and printing formatted output
After this lab, you will be able to:
- Declare and initialize variables of type
int,double,boolean,char, andString - Use
finalto declare constants that cannot be reassigned - Perform arithmetic that mixes
intanddouble(requiring a cast) - Print variables with labels using string concatenation
What You’re Building
You will create a character stat sheet — like a tabletop RPG character card — that declares variables for every primitive type plus String, uses constants for fixed values, and computes a derived stat (HP percentage) that requires casting. The output is a formatted stat block printed to the console.
Concepts and Misconceptions
| Concept | Common Mistake | What the Test Catches |
|---|---|---|
| Variable types | Declaring double hp = 85 instead of 85.0 (works, but masks understanding) |
Type-specific tests verify declared types via source inspection |
final constants |
Forgetting final, or naming constants in camelCase instead of UPPER_SNAKE |
Source scan for final keyword and naming convention |
| Casting int to double | Writing currentHp / maxHp (integer division yields 0) |
Expected percentage is 0.0 instead of the correct value |
| String concatenation with numbers | Expecting "HP: " + 85 + "/" + 100 to do math |
Prints correctly but students sometimes add parens in wrong places |
Checkpoints
Checkpoint 1: Declare One of Each Type and Print
What to do: Declare at least one variable of each type: int, double, boolean, char, String. Print each on its own line with a label (e.g., "Level: 5").
What the test checks: Output contains one line per type with the correct label and value format.
Debugging tip: If the test expects "Active: true" and you print "Active: True", that is a failure. Java’s boolean prints as lowercase true/false. Do not capitalize.
Checkpoint 2: Use final Constants
What to do: Declare at least two final constants (e.g., MAX_HP, MAX_LEVEL) using UPPER_SNAKE_CASE. Use them in your output instead of raw numbers.
What the test checks: The source file contains final declarations. The test also verifies that reassigning a constant causes a compile error (this is structural — just make sure you used final).
Debugging tip: If you get “cannot assign a value to final variable,” that means final is working correctly. The error is telling you the constant is protected. Use a different variable if you need a mutable value.
Checkpoint 3: Compute HP Percentage with Casting
What to do: Given int currentHp and int maxHp, compute the HP percentage as a double. You must cast to avoid integer division: (double) currentHp / maxHp * 100.
What the test checks: The printed percentage matches the expected value (e.g., "HP: 85.0%"). Integer division would produce 0.0% or a wrong value.
Debugging tip: If your percentage prints 0.0, you are doing integer division. The cast must go on the numerator before the division: (double) currentHp / maxHp. Casting the result after division is too late — the truncation already happened.
How to Debug
-
Check your types explicitly. If a test expects
85.0and you print85, the variable is anintwhen it should be adouble. Add.0to the literal or cast the expression. -
Print intermediate values. If the percentage is wrong, print
currentHp,maxHp, and the raw division result on separate lines. This tells you exactly where the math goes wrong. -
Read the compiler error top-down. Java compiler errors point to the exact line and column. The first error is usually the real problem; errors after it are often cascading failures from the first.
Scoring
| Component | Points | Criteria |
|---|---|---|
| Checkpoints | 3 | 1 pt each. Binary: the checkpoint test passes or it does not. |
| Autograder | 5 | Correctness across all test cases. Partial credit by proportion of tests passed. |
| Timeliness | 2 | Full credit if submitted by the due date. 0 if late. |
| Total | 10 |