Lab Sheet 3: Variables and Data Types

Task 1: Variable Declaration and Initialization
Declare and initialize the following variables:
An integer variable named
agewith a value of 25.A double variable named
salarywith a value of 50000.75.A boolean variable named
isEmployedwith a value oftrue.A char variable named
gradewith 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_valuemy-variabledoublefirstName
Declare and initialize variables using the corrected names.
Task 4: Type Conversion
Perform the following implicit (widening) conversions:
Convert a
bytevariable to ashort.Convert an
intvariable to along.Convert a
floatvariable to adouble.
Perform the following explicit (narrowing) conversions:
Convert a
doublevariable to anint.Convert a
longvariable to ashort.Convert a
floatvariable 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
num1andnum2and 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
aandband 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
numberand 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
celsiusand 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");




