Java Programming Labs

Write real Java code, solve coding challenges, and build your programming skills through hands-on practice.

Java Fundamentals - Module 1

Write actual code to solve each challenge. Your code is validated for correctness.

Lab 1: Variables & Output
Beginner
Coding Challenge
Your Task: Create a complete Java program that stores and displays personal profile information using different data types.

Detailed Requirements:
1. String variable "name" - Store a person's full name (e.g., "John Smith"). Strings are reference types used for text and must be enclosed in double quotes.
2. int variable "age" - Store the person's age as a whole number (e.g., 25). The int primitive type stores integers without decimals, ranging from -2,147,483,648 to 2,147,483,647.
3. double variable "height" - Store height in meters with decimal precision (e.g., 1.75). The double primitive type stores 64-bit floating-point numbers for decimal values.
4. boolean variable "isStudent" - Store true or false to indicate student status. The boolean primitive only holds two possible values: true or false.

Output Requirements:
Use System.out.println() to print each variable with a descriptive label. String concatenation with + operator combines text and variables. Your output should look like:
Name: John Smith Age: 25 Height: 1.75 Is Student: true

Requirements Checklist

Declare a String variable named "name" with any value
Declare an int variable named "age"
Declare a double variable named "height"
Declare a boolean variable named "isStudent"
Print all variables using System.out.println()
Code compiles without errors
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
� String declaration: String name = "John";
� Integer declaration: int age = 25;
� Double declaration: double height = 1.75;
� Boolean declaration: boolean isStudent = true;
� Print with label: System.out.println("Age: " + age);
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 2: Conditionals & Logic
Beginner
Coding Challenge
Your Task: Build a grade calculator that converts numeric scores to letter grades using conditional statements (if-else).

Detailed Requirements:
1. Declare an int variable "score" - Initialize with a test value between 0-100. Try different values (95, 82, 71, 65, 45) to test all grade paths.
2. Declare a char variable "grade" - This will store the letter grade ('A', 'B', 'C', 'D', or 'F'). Note: char uses single quotes, not double quotes.
3. Implement grading logic using if-else chain:
   � if (score >= 90) ? Grade A (Excellent - top performers)
   � else if (score >= 80) ? Grade B (Good - above average)
   � else if (score >= 70) ? Grade C (Average - meets expectations)
   � else if (score >= 60) ? Grade D (Below Average - needs improvement)
   � else ? Grade F (Failing - below 60)

4. Add pass/fail status: Any score of 60 or above is passing. Use a separate if-else to print "PASSED" or "FAILED".

Key Concept: The order of conditions matters! Check from highest to lowest. Once a condition is true, remaining else-if blocks are skipped.

Expected Output:
Score: 85 Grade: B Status: PASSED

Requirements Checklist

Declare an int variable "score" with a test value
Use if statement to check for grade A (score >= 90)
Include else-if for grades B, C, D
Include else block for grade F
Print pass/fail status using a conditional
Code compiles and runs correctly
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Start with highest grade: if (score >= 90) { grade = 'A'; }
� Chain conditions: else if (score >= 80) { grade = 'B'; }
� Comparison operators: >= > < <= == !=
� Char uses single quotes: 'A' not "A"
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 3: Loops & Iteration
Intermediate
Coding Challenge
Your Task: Implement the famous FizzBuzz challenge - one of the most common programming interview questions that tests your understanding of loops, conditionals, and the modulo operator.

Detailed Requirements:
1. Create a for loop that iterates from 1 to 20 (inclusive).
   Syntax: for (int i = 1; i <= 20; i++) { ... }
   � int i = 1 - initialization (start at 1)
   � i <= 20 - condition (continue while i is 20 or less)
   � i++ - increment (add 1 to i after each iteration)

2. For each number, apply these rules IN THIS EXACT ORDER:
   � If divisible by BOTH 3 AND 5 ? print "FizzBuzz"
   � Else if divisible by 3 only ? print "Fizz"
   � Else if divisible by 5 only ? print "Buzz"
   � Else ? print the number itself

Key Concept - Modulo Operator (%):
The % operator returns the remainder after division. If i % 3 == 0, then i divides evenly by 3 (no remainder), meaning i IS divisible by 3.
Examples: 9 % 3 = 0 (divisible), 10 % 3 = 1 (not divisible), 15 % 5 = 0 (divisible)

?? Critical: You MUST check FizzBuzz (both 3 AND 5) FIRST! Why? Numbers like 15 are divisible by both 3 and 5. If you check "Fizz" first, you'll print "Fizz" instead of "FizzBuzz" because the Fizz condition will match before reaching FizzBuzz.

Expected Output (first 15 numbers):
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

Requirements Checklist

Create a for loop from 1 to 20
Check FizzBuzz first (divisible by both 3 AND 5)
Check for Fizz (divisible by 3)
Check for Buzz (divisible by 5)
Print the number if none of the above
Output matches expected FizzBuzz pattern
Output
// Expected: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz
Hints & Tips
� For loop: for (int i = 1; i <= 20; i++) { }
� Check both: if (i % 3 == 0 && i % 5 == 0)
� Modulo example: 15 % 3 == 0 is true (15 � 3 = 5, no remainder)
� Print number: System.out.println(i);
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below