forked from CodeX-SIT/community-25-java-task1-Java-Task1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq3.java
More file actions
87 lines (63 loc) · 3.23 KB
/
q3.java
File metadata and controls
87 lines (63 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// q3.java - Literals, Increment/Decrement Operators, and Expressions Demo
// Complete the fill-in-the-blanks to demonstrate various Java concepts
public class q3 {
public static void main(String[] args) {
System.out.println("=== Java Literals and Operators Demo ===\n");
// Part 1: Different types of literals
System.out.println("=== Part 1: Literals Demo ===");
int decimal = 42;
int binary = 0b101010;
int octal = 052;
int hex = 0x2A;
System.out.println("Decimal literal: " + decimal);
System.out.println("Binary literal: " + binary);
System.out.println("Octal literal: " + octal);
System.out.println("Hexadecimal literal: " + hex);
float floatNum = 3.14f;
double doubleNum = 2.718281828;
System.out.println("Float literal: " + floatNum);
System.out.println("Double literal: " + doubleNum);
char letter = 'A';
char unicodeChar = '\u0041';
System.out.println("Character literal: " + letter);
System.out.println("Unicode character: " + unicodeChar);
boolean isTrue = true;
String message = "Hello, Java!";
System.out.println("Boolean literal: " + isTrue);
System.out.println("String literal: " + message);
System.out.println("\n=== Part 2: Increment/Decrement Operators ===");
int counter = 10;
System.out.println("Initial counter value: " + counter);
System.out.println("Post-increment (counter++): " + counter++);
System.out.println("Counter after post-increment: " + counter);
System.out.println("Pre-increment (++counter): " + ++counter);
System.out.println("Counter after pre-increment: " + counter);
System.out.println("Post-decrement (counter--): " + counter--);
System.out.println("Counter after post-decrement: " + counter);
System.out.println("Pre-decrement (--counter): " + --counter);
System.out.println("Counter after pre-decrement: " + counter);
System.out.println("\n=== Part 3: Data Type of Expressions ===");
int intVar = 5;
double doubleVar = 2.5;
float floatVar = 1.5f;
char charVar = 'B';
int result1 = intVar + 3;
double result2 = intVar + doubleVar;
double result3 = floatVar + doubleVar;
int result4 = charVar + intVar;
System.out.println("int + int = " + result1 + " (Type: int)");
System.out.println("int + double = " + result2 + " (Type: double)");
System.out.println("float + double = " + result3 + " (Type: double)");
System.out.println("char + int = " + result4 + " (Type: int, 'B' has ASCII value 66)");
System.out.println("\n=== Part 4: Operator Associativity ===");
int a = 20, b = 10, c = 5;
int result5 = a - b - c;
System.out.println("Left-to-right: " + a + " - " + b + " - " + c + " = " + result5);
System.out
.println("Evaluation: (" + a + " - " + b + ") - " + c + " = " + (a - b) + " - " + c + " = " + result5);
int x, y, z;
x = y = z = 15;
System.out.println("Right-to-left assignment: x = y = z = 15");
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
}
}