Before we start
Why this lab
Every Java program you write this semester — bank systems, threads, GUIs — is built from the same few pieces. Which ones?
Variables hold data, types say what kind of data, decisions choose a path and loops repeat work. This lab puts all of them in one program, Lab1Demo.
Lab 1 of 10
Lab at a glance
After this lab you can
- Set up and verify a JDK (11+) with NetBeans, Eclipse or IntelliJ IDEA
- Explain how source code becomes bytecode that the JVM runs
- Declare variables of the eight primitive types and
String - Apply widening and narrowing casts
- Use
if / else if / elseandswitch - Repeat work with
for,whileanddo-while; control loops withbreakandcontinue
Before you start
- Create folders and save files
- Basic idea of variables, expressions, input and output
- JDK 11+ and an IDE installed
Plan for the session
- Theory recap
- Walk through the program, section by section, and run it
- In-lab tasks, shown to the instructor
- Report exercises at home
Setting up · 1 of 4
Which Java do you need?
- A JDK (Java Development Kit): it contains the compiler
javacand the runtimejava. A JRE alone cannot compile. - Use the current LTS release, JDK 25, from the official Java site
oracle.com/java. The manual needs JDK 11 or later. - Install the JDK first, then the IDE: this course uses Apache NetBeans.
- On the next slides, pick your operating system: each slide has direct download links, the steps and a YouTube video link.
Setting up · 2 of 4
Install the JDK
Setting up · 3 of 4
Install Apache NetBeans
Setting up · 4 of 4
Your first project in NetBeans
Theory 1 of 6
From .java to a running program
- You write
Lab1Demo.java;javaccompiles it to bytecode inLab1Demo.class. - The JVM loads the bytecode and starts at
main: “write once, run anywhere”. - A
publicclass must live in a file with the same name.
Theory 2 of 6
Structure of a Java program
public class Lab1Demo {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}- Every statement lives inside a class; execution begins in
main. System.out.println()prints a line to the console.- Check your setup: compile and run this before starting the tasks.
Theory 3 of 6
Variables and the eight primitive types
| Type | Holds | Example |
|---|---|---|
| byte / short | small integers | byte b = 100; |
| int | common integers | int age = 21; |
| long | large integers (suffix L) | long pop = 9876543210L; |
| float | single-precision (suffix f) | float gpa = 3.75f; |
| double | double-precision decimals | double h = 5.91; |
| char | one character | char grade = 'A'; |
| boolean | true / false | boolean ok = true; |
- Java is statically typed: every variable has a declared type.
Stringis a reference type for text.
Theory 4 of 6
Type casting
- Widening (small → large) is automatic and safe.
- Narrowing needs a cast and may lose data: the fraction is cut off, not rounded.
Type casting
double d = 25; // widening: automatic -> 25.0 int price = (int) 99.99; // narrowing: explicit -> 99
Theory 5 of 6
Decisions: if-else and switch
- Use
if / else if / elsefor ranges and conditions. - Use
switchto compare one value against fixed cases; forgetbreakand execution falls through.
Decisions: if-else and switch
switch (score / 10) {
case 10: case 9: grade = 'A'; break;
case 8: grade = 'B'; break;
default: grade = 'F';
}
Theory 6 of 6
Loops, break and continue
Theory 6 of 6
Loops, break and continue (cont.)
for: number of iterations known in advance (summation).while: repeat while a condition holds (reverse digits).do-while: body runs at least once (Collatz sequence).breakleaves the loop;continueskips to the next iteration.
Implementation design
Execution flow
- Declare and print the eight primitive variables and a String
- Widen an int to double, narrow a double to int, convert char ↔ int
- Classify a number (positive/zero/negative) and a BMI with if-else
- Assign a letter grade with switch
- Sum 1..10 with for; print even numbers using continue and break
- Reverse digits with while
- Print the Collatz sequence of 27 with do-while
Single class, everything inside main: the focus is syntax, not objects.
Code walkthrough 1 of 10 · Lab1Demo.java
Variables of every primitive type
// 1. Primitive Data Types and String byte byteVal = 100; short shortVal = 30000; int age = 21; long bigNum = 9876543210L; float gpa = 3.75f; double height = 5.91; char initial = 'A'; boolean isStudent = true; String name = "Mozdaher Abdul Quader";
- One variable per primitive type, plus a String.
- Note the suffixes:
Lfor long,ffor float.
Code walkthrough 2 of 10 · Lab1Demo.java
Printing with labels
System.out.println("=== Primitive Data Types and String ===");
System.out.println("byte : " + byteVal);
System.out.println("short : " + shortVal);
System.out.println("int : " + age);
System.out.println("long : " + bigNum);
System.out.println("float : " + gpa);
System.out.println("double : " + height);
System.out.println("char : " + initial + " (Unicode = " + (int) initial + ")");
System.out.println("boolean : " + isStudent);
System.out.println("String : " + name);+joins text and values into one String.(int) initialshows the Unicode number of a char.
Code walkthrough 3 of 10 · Lab1Demo.java
Widening and narrowing casts
// 2. Type Casting
System.out.println("\n=== Type Casting ===");
double ageAsDouble = age;
System.out.println("int " + age + " widened to double: " + ageAsDouble);
double pi = 3.14159;
int piTruncated = (int) pi;
System.out.println("double " + pi + " narrowed to int: " + piTruncated);
Code walkthrough 3 of 10 · Lab1Demo.java
Widening and narrowing casts (cont.)
char letter = 'Z';
int code = letter;
char nextChar = (char) (code + 1);
System.out.println("char 'Z' as int: " + code + " | next char: " + nextChar);- int → double happens automatically (widening).
- double → int needs
(int)and drops the fraction. - A char is a number: 90 + 1 is the char after Z.
Code walkthrough 4 of 10 · Lab1Demo.java
if / else if / else
// 3. Conditional Statement
System.out.println("\n=== Conditional Statement ===");
int n = 42;
if (n > 0) {
System.out.println(n + " is Positive");
} else if (n == 0) {
System.out.println(n + " is Zero");
} else {
System.out.println(n + " is Negative");
}- Only the first true branch runs.
Code walkthrough 5 of 10 · Lab1Demo.java
Ranges with an if-else chain
double weightKg = 70.0;
double heightM = 1.75;
double bmi = weightKg / (heightM * heightM);
System.out.printf("BMI = %.2f => ", bmi);
if (bmi < 18.5) {
System.out.println("Underweight");
} else if (bmi < 25.0) {
System.out.println("Normal weight");
} else if (bmi < 30.0) {
System.out.println("Overweight");
} else {
System.out.println("Obese");
}- Each test only needs the upper bound: earlier branches already ruled out smaller values.
printf("%.2f")prints two decimals.
Code walkthrough 6 of 10 · Lab1Demo.java
switch with fall-through
int score = 85;
char grade;
switch (score / 10) {
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'F';
}score / 10is integer division: 85 → 8.case 10:has no break, so it falls through to case 9.defaultcatches everything else.
Code walkthrough 7 of 10 · Lab1Demo.java
for loop: summation
// 5. For Loop
System.out.println("\n=== For Loop: Summation 1 to n ===");
int upperBound = 10;
int sum = 0;
for (int i = 1; i <= upperBound; i++) {
sum = sum + i;
}
System.out.println("Sum of 1 to " + upperBound + " = " + sum);- Initialise, test, update — all in the loop header.
Code walkthrough 8 of 10 · Lab1Demo.java
continue and break
System.out.print("Even numbers from 1 to 20, skipping multiples of 6: ");
for (int i = 1; i <= 20; i++) {
if (i % 6 == 0) {
continue;
}
if (i > 18) {
break;
}
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
System.out.println();continueskips multiples of 6.breakstops the loop once i passes 18.
Code walkthrough 9 of 10 · Lab1Demo.java
while loop: reverse the digits
// 6. While Loop
System.out.println("\n=== While Loop: Reverse Digits ===");
int number = 12345;
int temp = number;
int reversed = 0;
while (temp != 0) {
int digit = temp % 10;
reversed = reversed * 10 + digit;
temp = temp / 10;
}
System.out.println("Original number: " + number);
System.out.println("Reversed number: " + reversed);% 10takes the last digit;/ 10removes it.- The loop runs until no digits are left.
Code walkthrough 10 of 10 · Lab1Demo.java
do-while: the Collatz sequence
// 7. Do-While Loop
System.out.println("\n=== Do-While Loop: Collatz Sequence ===");
long collatz = 27;
int steps = 0;
System.out.print("Sequence: ");
do {
System.out.print(collatz + " ");
if (collatz % 2 == 0) {
collatz = collatz / 2;
} else {
Code walkthrough 10 of 10 · Lab1Demo.java
do-while: the Collatz sequence (cont.)
collatz = 3 * collatz + 1;
}
steps++;
} while (collatz != 1);
System.out.println("1");
System.out.println("Steps to reach 1: " + steps);- The body runs before the test, so 27 is printed first.
- Even → halve; odd → 3n + 1; stop at 1.
Step through the code · 1 of 3
Trace: widening and narrowing
Watch each variable appear in main’s stack frame and see exactly what a cast keeps.
Step through the code · 1 of 3
Trace: widening and narrowing (cont.)
Step through the code · 2 of 3
Trace: reversing the digits
Follow temp, digit and reversed through every pass of the while loop.
Step through the code · 2 of 3
Trace: reversing the digits (cont.)
Step through the code · 3 of 3
Trace: switch, case by case
Pick a score. See which case the switch jumps to, how 100 falls through into case 9, and what break does.
Step through the code · 3 of 3
Trace: switch, case by case (cont.)
For reference · 1 file
The complete program
public class Lab1Demo {
public static void main(String[] args) {
// 1. Primitive Data Types and String
byte byteVal = 100;
short shortVal = 30000;
int age = 21;
long bigNum = 9876543210L;
float gpa = 3.75f;
double height = 5.91;
char initial = 'A';
boolean isStudent = true;
String name = "Mozdaher Abdul Quader";
System.out.println("=== Primitive Data Types and String ===");
System.out.println("byte : " + byteVal);
System.out.println("short : " + shortVal);
System.out.println("int : " + age);
System.out.println("long : " + bigNum);
System.out.println("float : " + gpa);
System.out.println("double : " + height);
System.out.println("char : " + initial + " (Unicode = " + (int) initial + ")");
System.out.println("boolean : " + isStudent);
System.out.println("String : " + name);
// 2. Type Casting
System.out.println("\n=== Type Casting ===");
double ageAsDouble = age;
System.out.println("int " + age + " widened to double: " + ageAsDouble);
double pi = 3.14159;
int piTruncated = (int) pi;
System.out.println("double " + pi + " narrowed to int: " + piTruncated);
char letter = 'Z';
int code = letter;
char nextChar = (char) (code + 1);
System.out.println("char 'Z' as int: " + code + " | next char: " + nextChar);
// 3. Conditional Statement
System.out.println("\n=== Conditional Statement ===");
int n = 42;
if (n > 0) {
System.out.println(n + " is Positive");
} else if (n == 0) {
System.out.println(n + " is Zero");
} else {
System.out.println(n + " is Negative");
}
double weightKg = 70.0;
double heightM = 1.75;
double bmi = weightKg / (heightM * heightM);
System.out.printf("BMI = %.2f => ", bmi);
if (bmi < 18.5) {
System.out.println("Underweight");
} else if (bmi < 25.0) {
System.out.println("Normal weight");
} else if (bmi < 30.0) {
System.out.println("Overweight");
} else {
System.out.println("Obese");
}
// 4. Switch Statement
System.out.println("\n=== Switch Statement: Grade Classifier ===");
int score = 85;
char grade;
switch (score / 10) {
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'F';
}
System.out.println("Score: " + score + " => Grade: " + grade);
// 5. For Loop
System.out.println("\n=== For Loop: Summation 1 to n ===");
int upperBound = 10;
int sum = 0;
for (int i = 1; i <= upperBound; i++) {
sum = sum + i;
}
System.out.println("Sum of 1 to " + upperBound + " = " + sum);
System.out.print("Even numbers from 1 to 20, skipping multiples of 6: ");
for (int i = 1; i <= 20; i++) {
if (i % 6 == 0) {
continue;
}
if (i > 18) {
break;
}
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
System.out.println();
// 6. While Loop
System.out.println("\n=== While Loop: Reverse Digits ===");
int number = 12345;
int temp = number;
int reversed = 0;
while (temp != 0) {
int digit = temp % 10;
reversed = reversed * 10 + digit;
temp = temp / 10;
}
System.out.println("Original number: " + number);
System.out.println("Reversed number: " + reversed);
// 7. Do-While Loop
System.out.println("\n=== Do-While Loop: Collatz Sequence ===");
long collatz = 27;
int steps = 0;
System.out.print("Sequence: ");
do {
System.out.print(collatz + " ");
if (collatz % 2 == 0) {
collatz = collatz / 2;
} else {
collatz = 3 * collatz + 1;
}
steps++;
} while (collatz != 1);
System.out.println("1");
System.out.println("Steps to reach 1: " + steps);
}
}Copy or download each file, save it under the same name, then compile and run: javac *.java → java Lab1Demo
Implementation
Expected output
=== Primitive Data Types and String === byte : 100 short : 30000 int : 21 long : 9876543210 float : 3.75 double : 5.91 char : A (Unicode = 65) boolean : true String : Mozdaher Abdul Quader === Type Casting === int 21 widened to double: 21.0 double 3.14159 narrowed to int: 3 char 'Z' as int: 90 | next char: [ === Conditional Statement === 42 is Positive BMI = 22.86 => Normal weight === Switch Statement: Grade Classifier === Score: 85 => Grade: B === For Loop: Summation 1 to n === Sum of 1 to 10 = 55 Even numbers from 1 to 20, skipping multiples of 6: 2 4 8 10 14 16 18 === While Loop: Reverse Digits === Original number: 12345 Reversed number: 54321 === Do-While Loop: Collatz Sequence === Sequence: 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 Steps to reach 1: 111
Why is the next char after Z a “[”? Characters are numbers: 'Z' is 90, and 91 is '['.
Interactive 1 of 5
Try widening and narrowing
Interactive 2 of 5
Evaluate operators step by step
Interactive 3 of 5
Follow an if-else ladder
Interactive 4 of 5
Watch switch and fall-through
Interactive 5 of 5
Compare the three loops
In-lab task 1 of 4
Task 1: Temperature converter
Show a hint
Scanner sc = new Scanner(System.in); double c = sc.nextDouble(); and compute F first, because R depends on it.In-lab task 2 of 4
Task 2: FizzBuzz extended
Show a hint
i % 15 == 0 or i % 3 == 0 && i % 5 == 0 first.In-lab task 3 of 4
Task 3: Number system classifier
Show a hint
case 1: case 3: can share one body. Careful with negative numbers: -3 % 4 is -3 in Java.In-lab task 4 of 4
Task 4: Digit frequency counter
Show a hint
freq[n % 10]++; n /= 10;. Use long if you want very large inputs.In-lab tasks
Show your work during the lab
For each task, show the instructor:
- the source code
- a successful compilation
- the running output
- a short explanation of the logic you used
Lab report · exercise 1 of 2
Student grade management system
Lab report · exercise 2 of 2
Pattern & sequence explorer
Lab report
Writing the lab report
- Problem statement and your design (classes, fields, methods)
- Complete source code, formatted and commented
- Sample output or screenshots for normal and edge cases
- A short discussion: what each OOP concept did in your program
Academic integrity
Copying from the internet, classmates, seniors or any other unauthorized source is strictly prohibited. Full marks may be deducted if plagiarism, copied work or academic dishonesty is detected. Complete the tasks, implementation, output analysis and report yourself.
Discussion and analysis
Viva questions (1 of 2)
Show the answer
(int) 99.99 give, and why must the cast be written?Show the answer
Discussion and analysis
Viva questions (2 of 2)
do-while over while?Show the answer
break from a switch case?Show the answer
Check yourself
Quiz
Summary
Summary
- javac turns .java source into .class bytecode; the JVM runs it starting from main.
- Eight primitive types plus String; Java checks types at compile time.
- Widening is automatic; narrowing needs a cast and can lose data.
- if-else for conditions and ranges, switch for fixed values (remember break).
- for when the count is known, while for conditions, do-while to run at least once.