Chapter 1 Data type and variables
1. Write a Java Program that declares and initializes variables of different data types (int, double, boolean, String)
and prints their values. Ensure that the program demonstrates the use of appropriate data types.
Code-
class VariableDemo {
public static void main(String[] args) {
// Declare and initialize variables of different data types
int integerVariable = 40;
double doubleVariable = 3.14;
boolean booleanVariable = false;
String stringVariable ="Hello, World!";
// Print the values of the variables
System.out.println("Integer Variable:" + integerVariable);
System.out.println("Double Variable:" + doubleVariable);
System.out.println("Boolean Variable:" + booleanVariable);
System.out.println("String Variable:" + stringVariable);
}
}
Output-
Integer Variable: 40
Double Variable: 3.14
Boolean Variable: false
String Variable: Hello, World!
2. Create a Java Program that converts a given double value into an integer by performing explicit type casting.
Print both the original double value and the converted integer value.
Code-
public class DoubleToIntConversion {
public static void main(String[] args) {
// Given double value
double doubleValue = 7.76;
// Convert the double value to an integer using explicit casting
int intValue = (int) doubleValue;
// Print the original double value and the converted integer value
System.out.println("Original Double Value:" + doubleValue);
System.out.println("Converted Integer Value:" + intValue);
}
}
Output-
Original Double Value: 7.76
Converted Integer Value: 7
3. Define a constant named"PI" with a value of 3.14159 in