Lab Sheet 3: Variables and Data Types


Task 1: Variable Declaration and Initialization
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 oftrue
.A char variable named
grade
with a value of 'A'.
Print the values of all the variables.
Task 2: Primitive Data Types
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 valuefalse
.char
: Assign the value 'Z'.
Print the values of all the variables.
Task 3: Variable Naming Conventions
Identify which of the following variable names are legal and which are illegal. Correct the illegal ones:
123age
$salary
_value
my-variable
double
firstName
Declare and initialize variables using the corrected names.
Task 4: Type Conversion
Perform the following implicit (widening) conversions:
Convert a
byte
variable to ashort
.Convert an
int
variable to along
.Convert a
float
variable to adouble
.
Perform the following explicit (narrowing) conversions:
Convert a
double
variable to anint
.Convert a
long
variable to ashort
.Convert a
float
variable to abyte
.
Print the values before and after each conversion.
Additional Practical Tasks
Task 5: Simple Calculator
Write a program to perform basic arithmetic operations (addition, subtraction, multiplication, and division) on two numbers.
Declare two variables
num1
andnum2
and assign them values.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
Write a program to swap the values of two variables.
Declare two variables
a
andb
and assign them values.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
Write a program to check if a number is even or odd.
Declare a variable
number
and assign it a value.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
Write a program to convert a temperature from Celsius to Fahrenheit.
Declare a variable
celsius
and assign it a value.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");
Subscribe to my newsletter
Read articles from Arzath Areeff directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Arzath Areeff
Arzath Areeff
I co-founded digizen.lk to promote online safety and critical thinking. Currently, I’m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.