Skip to main content

Command Palette

Search for a command to run...

Lab Sheet 3: Variables and Data Types

Published
3 min read
Lab Sheet 3: Variables and Data Types
A
I co-founded🫆digizen (fighting internet chaos 🧠), building an AI app to tackle misinformation, and running ideaGeek to turn “I have an idea” into real startups. I also share tech + travel on YouTube (TechNomad & Rz Omar). I touch grass too 🌱… but mostly to debug life 💻

Task 1: Variable Declaration and Initialization

  1. Declare and initialize the following variables:

    • An integer variable named age with a value of 25.

    • A double variable named salary with a value of 50000.75.

    • A boolean variable named isEmployed with a value of true.

    • A char variable named grade with a value of 'A'.

  2. Print the values of all the variables.


Task 2: Primitive Data Types

  1. Declare variables of the following primitive data types and assign them the maximum or minimum values as specified:

    • byte: Assign the maximum value.

    • short: Assign the minimum value.

    • int: Assign the maximum value.

    • long: Assign the minimum value.

    • float: Assign the value 123.456.

    • double: Assign the value 123.456789.

    • boolean: Assign the value false.

    • char: Assign the value 'Z'.

  2. Print the values of all the variables.


Task 3: Variable Naming Conventions

  1. Identify which of the following variable names are legal and which are illegal. Correct the illegal ones:

    • 123age

    • $salary

    • _value

    • my-variable

    • double

    • firstName

  2. Declare and initialize variables using the corrected names.


Task 4: Type Conversion

  1. Perform the following implicit (widening) conversions:

    • Convert a byte variable to a short.

    • Convert an int variable to a long.

    • Convert a float variable to a double.

  2. Perform the following explicit (narrowing) conversions:

    • Convert a double variable to an int.

    • Convert a long variable to a short.

    • Convert a float variable to a byte.

  3. Print the values before and after each conversion.


Additional Practical Tasks

Task 5: Simple Calculator

  1. Write a program to perform basic arithmetic operations (addition, subtraction, multiplication, and division) on two numbers.

  2. Declare two variables num1 and num2 and assign them values.

  3. Print the results of each operation.

Example Code:

int num1 = 10;  
int num2 = 5;  

System.out.println("Addition: " + (num1 + num2));  
System.out.println("Subtraction: " + (num1 - num2));  
System.out.println("Multiplication: " + (num1 * num2));  
System.out.println("Division: " + (num1 / num2));

Task 6: Swap Two Numbers

  1. Write a program to swap the values of two variables.

  2. Declare two variables a and b and assign them values.

  3. Swap their values without using a third variable.

Example Code:

int a = 10;  
int b = 20;  

System.out.println("Before Swap: a = " + a + ", b = " + b);  

a = a + b;  
b = a - b;  
a = a - b;  

System.out.println("After Swap: a = " + a + ", b = " + b);

Task 7: Check Even or Odd

  1. Write a program to check if a number is even or odd.

  2. Declare a variable number and assign it a value.

  3. Use the modulus operator (%) to determine if the number is even or odd.

Example Code:

int number = 7;  

if (number % 2 == 0) {  
    System.out.println(number + " is even.");  
} else {  
    System.out.println(number + " is odd.");  
}

Task 8: Convert Celsius to Fahrenheit

  1. Write a program to convert a temperature from Celsius to Fahrenheit.

  2. Declare a variable celsius and assign it a value.

  3. Use the formula:
    [ \text{Fahrenheit} = (\text{Celsius} \times 1.8) + 32 ]

Example Code:

double celsius = 25.0;  
double fahrenheit = (celsius * 1.8) + 32;  

System.out.println(celsius + "°C is equal to " + fahrenheit + "°F");

More from this blog

T

TechNomad

46 posts

TechNomad is mostly coding exercises, not long boring explanations 😌. I share what I teach and learn through hands-on problems—mainly for my students, but anyone can jump in. If you learn by doing (and breaking things), you’ll fit right in. Less theory, more “try this and see what happens.”