-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleJavaDemo.java
More file actions
326 lines (273 loc) · 12.1 KB
/
SimpleJavaDemo.java
File metadata and controls
326 lines (273 loc) · 12.1 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Java Features Analysis - Course Project
* Analysis and Design of Languages
* Student: Shirin Shoghli (ID: 9625512123)
* Professor: Ezzat Doost
* University: Islamic Azad University South Tehran Branch
* Year: 2021
*
* This program demonstrates Java's core features as presented in the course project.
* Based on the presentation: "Java Features Analysis"
*/
public class SimpleJavaDemo {
public static void main(String[] args) {
System.out.println("=== Java Features Analysis ===");
System.out.println("Course: Analysis and Design of Languages");
System.out.println("Student: Shirin Shoghli (9625512123)");
System.out.println("Professor: Ezzat Doost");
System.out.println("University: Islamic Azad University South Tehran Branch");
System.out.println("Year: 2021\n");
// Demonstrate core Java features from presentation
demonstrateJavaHistory();
demonstrateStaticTyping();
demonstrateOOP();
demonstrateCollections();
demonstrateExceptionHandling();
demonstrateMultithreading();
demonstrateNetworking();
demonstrateLanguageComparison();
demonstrateJavaAdvantages();
System.out.println("\n=== Analysis Complete ===");
System.out.println("Java: Write Once, Run Anywhere (WORA)");
System.out.println("One of the top programming languages worldwide (TIOBE Index)");
}
// Java History and Introduction
private static void demonstrateJavaHistory() {
System.out.println("1. JAVA INTRODUCTION:");
System.out.println(" - Designed by James Gosling at Sun Microsystems");
System.out.println(" - Released: May 23, 1995");
System.out.println(" - Top 2 programming language since 2001 (TIOBE Index)");
System.out.println(" - Improved alternative to C++ (portable, beginner-friendly)");
System.out.println(" - Simple, object-oriented, network-friendly, interpreted");
System.out.println(" - Robust, secure, architecture-neutral, portable");
System.out.println(" - High-performance, multithreaded, dynamic\n");
}
// Static Typing - Types must be declared
private static void demonstrateStaticTyping() {
System.out.println("2. STATIC TYPING:");
// Java requires type declaration
String language = "Java";
int releaseYear = 1995;
double currentVersion = 21.0;
boolean isTopLanguage = true;
System.out.println(" Language: " + language);
System.out.println(" Release Year: " + releaseYear);
System.out.println(" Current Version: " + currentVersion);
System.out.println(" Top Language: " + isTopLanguage);
// This would cause compilation error:
// language = 42; // Type mismatch!
System.out.println(" ✓ Types checked at compile time");
System.out.println(" ✓ Errors caught before runtime");
System.out.println(" ✓ Unlike Python/JavaScript (dynamic typing)\n");
}
// Object-Oriented Programming
private static void demonstrateOOP() {
System.out.println("3. OBJECT-ORIENTED PROGRAMMING:");
// Create objects
Student student = new Student("Shirin Shoghli", "Computer Science", "9625512123");
Course course = new Course("Analysis and Design of Languages", "Ezzat Doost");
University university = new University("Islamic Azad University South Tehran Branch");
// Use objects
System.out.println(" Student: " + student.getName());
System.out.println(" Student ID: " + student.getStudentId());
System.out.println(" Course: " + course.getName());
System.out.println(" Professor: " + course.getProfessor());
System.out.println(" University: " + university.getName());
// Polymorphism demonstration
Person person1 = new Student("Ali", "Engineering", "123456");
Person person2 = new Professor("Dr. Ezzat Doost", "Computer Science");
System.out.println(" " + person1.introduce());
System.out.println(" " + person2.introduce());
System.out.println(" ✓ Encapsulation: Private fields with public accessors");
System.out.println(" ✓ Inheritance: Person → Student/Professor");
System.out.println(" ✓ Polymorphism: Method overriding\n");
}
// Collections and Generics
private static void demonstrateCollections() {
System.out.println("4. COLLECTIONS & GENERICS:");
// ArrayList with generics
java.util.List<String> programmingLanguages = new java.util.ArrayList<>();
programmingLanguages.add("Java");
programmingLanguages.add("Python");
programmingLanguages.add("C++");
programmingLanguages.add("JavaScript");
programmingLanguages.add("C#");
System.out.println(" Programming Languages: " + programmingLanguages);
// HashMap with rankings
java.util.Map<String, Integer> languageRankings = new java.util.HashMap<>();
languageRankings.put("Java", 1);
languageRankings.put("Python", 2);
languageRankings.put("C++", 3);
languageRankings.put("JavaScript", 4);
languageRankings.put("C#", 5);
System.out.println(" Java Ranking: " + languageRankings.get("Java"));
System.out.println(" ✓ Type-safe collections");
System.out.println(" ✓ Generic data structures\n");
}
// Exception Handling (Robustness)
private static void demonstrateExceptionHandling() {
System.out.println("5. EXCEPTION HANDLING (ROBUSTNESS):");
// Array bounds checking
int[] numbers = {1, 2, 3, 4, 5};
try {
System.out.println(" Valid access: " + numbers[2]);
System.out.println(" Invalid access attempt...");
int invalid = numbers[10]; // This will throw exception
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(" ✓ Caught: " + e.getMessage());
}
// Type safety demonstration
try {
String text = "Hello World";
System.out.println(" String length: " + text.length());
// text = 42; // Compilation error - static typing!
System.out.println(" ✓ No runtime type errors");
System.out.println(" ✓ Array bounds checking");
System.out.println(" ✓ No pointers (safer than C++)");
} catch (Exception e) {
System.out.println(" Error: " + e.getMessage());
}
System.out.println();
}
// Multithreading
private static void demonstrateMultithreading() {
System.out.println("6. MULTITHREADING:");
// Create threads
Thread thread1 = new Thread(() -> {
for (int i = 1; i <= 3; i++) {
System.out.println(" Thread 1: Task " + i);
try { Thread.sleep(100); } catch (InterruptedException e) {}
}
});
Thread thread2 = new Thread(() -> {
for (int i = 1; i <= 3; i++) {
System.out.println(" Thread 2: Task " + i);
try { Thread.sleep(100); } catch (InterruptedException e) {}
}
});
// Start threads
thread1.start();
thread2.start();
// Wait for completion
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {}
System.out.println(" ✓ Concurrent execution");
System.out.println(" ✓ GUI + network handling simultaneously");
System.out.println(" ✓ Built-in thread support\n");
}
// Network Programming
private static void demonstrateNetworking() {
System.out.println("7. NETWORK PROGRAMMING:");
try {
// URL handling
java.net.URL url = new java.net.URL("https://www.google.com");
System.out.println(" Protocol: " + url.getProtocol());
System.out.println(" Host: " + url.getHost());
System.out.println(" ✓ Built-in networking support");
System.out.println(" ✓ TCP/IP, HTTP, FTP protocols");
System.out.println(" ✓ Easy URL-based access");
System.out.println(" ✓ Network-friendly language\n");
} catch (java.net.MalformedURLException e) {
System.out.println(" Error: " + e.getMessage());
}
}
// Language Comparison
private static void demonstrateLanguageComparison() {
System.out.println("8. LANGUAGE COMPARISON:");
System.out.println(" Java vs Python:");
System.out.println(" Java: String name = \"Java\"; // Static typing");
System.out.println(" Python: name = \"Python\" # Dynamic typing");
System.out.println(" Java vs JavaScript:");
System.out.println(" Java: String message = \"Hello\";");
System.out.println(" JavaScript: let message = \"Hello\";");
System.out.println(" ✓ Different languages, similar syntax");
System.out.println(" ✓ JavaScript was originally LiveScript");
System.out.println(" Java vs C++:");
System.out.println(" Java: String text = \"Hello\"; // Automatic GC");
System.out.println(" C++: char* text = new char[6]; // Manual memory");
System.out.println(" ✓ Java: No pointers, safer");
System.out.println(" ✓ C++: More control, potential errors\n");
}
// Java Advantages
private static void demonstrateJavaAdvantages() {
System.out.println("9. JAVA ADVANTAGES:");
System.out.println(" ✓ Platform Independence (WORA)");
System.out.println(" ✓ Object-Oriented Programming");
System.out.println(" ✓ High-Level Language");
System.out.println(" ✓ Enterprise Standard");
System.out.println(" ✓ Security (no pointers, sandbox)");
System.out.println(" ✓ Distributed Computing Support");
System.out.println(" ✓ Automatic Memory Management");
System.out.println(" ✓ Large Community & Ecosystem");
System.out.println(" ✓ Career Opportunities");
System.out.println(" ✓ Used in Android, Enterprise, Big Data\n");
}
}
// Base class for inheritance
abstract class Person {
protected String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract String introduce();
}
// Student class - inheritance
class Student extends Person {
private String major;
private String studentId;
public Student(String name, String major, String studentId) {
super(name);
this.major = major;
this.studentId = studentId;
}
public String getStudentId() {
return studentId;
}
@Override
public String introduce() {
return "I'm " + name + ", studying " + major + " (ID: " + studentId + ")";
}
}
// Professor class - inheritance
class Professor extends Person {
private String department;
public Professor(String name, String department) {
super(name);
this.department = department;
}
@Override
public String introduce() {
return "I'm Professor " + name + " from " + department + " department";
}
}
// Course class - encapsulation
class Course {
private String name;
private String professor;
public Course(String name, String professor) {
this.name = name;
this.professor = professor;
}
// Getters (encapsulation)
public String getName() {
return name;
}
public String getProfessor() {
return professor;
}
}
// University class
class University {
private String name;
public University(String name) {
this.name = name;
}
public String getName() {
return name;
}
}