Unit 1 · Lab 3

Methods, Math & String Practice

signatures · Math calls · random ranges · String methods

🧾 Task 1: Read a Signature

Pull the method apart before you ever try to call it.

Your Goal

1. Identify the method name in each signature.

2. Count how many parameters each signature has.

3. Describe the parameter types and the return type for each one.

Example Code

🧩 Task 2: Write a Matching Call

Move from reading signatures to building a valid call yourself.

Your Goal

1. Write one valid call for each signature.

2. Identify which two signatures are overloaded versions of the same method.

3. Store the returned value when a method gives one back.

4. Call the void method as a statement by itself.

5. Write your four finished calls in code comments.

Example Code

📐 Task 3: Math Formula

Use several Math calls together to build real expressions.

Your Goal

1. Create variables named distance, penalty, and score from the formulas below.

2. Translate each mathematical operation into the correct Math method call.

3. Print all three results after the formulas are complete.

distance=rise2+run2\text{distance} = \sqrt{\text{rise}^{2} + \text{run}^{2}}
penalty=∣−4.5∣⋅level\text{penalty} = \left|-4.5\right| \cdot \text{level}
score=∣rise−run∣2level\text{score} = \dfrac{\left|\text{rise} - \text{run}\right|^{2}}{\text{level}}
Example Code

🔺 Task 4: Heron's Formula

Use a real geometry formula that depends on several steps.

Your Goal

1. Create three variables for the side lengths 13, 14, and 15.

2. Create a variable named s from the semiperimeter formula below.

3. Translate Heron's formula into Java to compute the triangle area.

4. Print the final area.

Semiperimeter
s=a+b+c2s = \dfrac{a + b + c}{2}
Area
A=s(s−a)(s−b)(s−c)A = \sqrt{s(s-a)(s-b)(s-c)}

🔧 Task 5: Fix the Return Types

Some method calls fit these variables, and some do not.

Your Goal

1. Inspect every line and decide which variable types are already correct.

2. Repair only the lines whose variable types do not match the returned value.

3. Keep every method call exactly the same.

4. Compile only after you finish all the fixes.

Example Code

🎯 Task 6: Fix a Random Range

Small casting mistakes can break an otherwise correct range.

Your Goal

1. Inspect each expression and decide whether it really builds the intended integer range.

2. Target die from 1 to 6, code from 100 to 999, and lane from 3 to 6.

3. Fix the broken lines without changing the target ranges.

4. Keep any line that is already correct.

5. Explain why casting before multiplication makes the broken expressions fail.

Example Code

🎲 Task 7: Build Random Ranges

Turn one random double into the exact integer range you need.

Your Goal

1. Create die so it can be any integer from 1 to 6.

2. Create ticket so it can be any integer from 20 to 29.

3. Create room so it can be any integer from 300 to 399.

Example Code

🔎 Task 8: Read One String Carefully

Use one String object in several different ways.

Your Goal

1. Print the length of word.

2. Print the letters from index 2 through index 4.

3. Print everything from index 4 to the end.

4. Print the first location of the letter "o".

5. Print the first location of "z" and explain what the returned value means.

6. Decide whether word has the same text as "robotics".

Example Code

⚖️ Task 9: Compare Words

Use string comparisons to decide sameness and alphabetical order.

Your Goal

1. Create a variable named sameText to test whether first and third have the same text.

2. Create a variable named order to compare first and second alphabetically.

3. Print both variables.

4. Decide which word should appear first in sorted order.

Example Code

🧱 Task 10: Build HIDE

Use substring pieces to build a target word in the correct order.

Your Goal

1. Create a new String result using only pieces of original.

2. Make the final printed word become HIDE.

3. Print the finished value of result.

Example Code

🏷️ Task 11: Decode a Label

Pull several useful pieces out of one string.

Your Goal

1. Create a variable named state for the first two letters.

2. Create a String number for the middle three digits.

3. Create a variable named section for the last letter.

4. Print all three extracted pieces.

Example Code

🔤 Task 12: Pull One Character

Use a one-character substring when a full slice would be too wide.

Your Goal

1. Create a variable named row for the first character.

2. Create a variable named gate for the last character by using length() to locate its index.

3. Keep both results as String values, not chars or numbers.

Example Code

🚧 Task 13: Fix a Slice

Find the invalid boundary and repair only the ending index.

Your Goal

1. Predict what happens when the code runs.

2. Identify why the ending index is invalid for label.

3. Fix only the second argument of substring.

4. Run the repaired code and confirm that it prints CSA.

Example Code

🧊 Task 14: Immutable Check

Compare the two lines and figure out what actually changed.

Your Goal

1. Run the program exactly as written.

2. Compare the first printed line and the second printed line.

3. Explain why the original string stayed the same until the new result was saved.

Example Code

🪪 Task 15: Escaped Status Card

Mix string concatenation with escape characters to match a target display.

Your Goal

1. Create variables for a student name, a club name, and a rank number.

2. Build a String status one line at a time using +=.

3. Use escaped quotes and line breaks so the text matches the target.

4. Print status with one println statement.

Example Output
"Ava"
Robotics Club
Rank #3

🚫 Task 16: Missing or Empty?

A blank string and a missing string do not behave the same way.

Your Goal

1. Predict what each line will do before you run the program.

2. Identify which line is still safe even when the variable does not point to a real string.

3. Assign a real String to missingName before the final method call.

4. Run the repaired code and confirm that the program finishes.

Example Code
01 / 17