forked from javakurssi/Tuntimateriaalit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson1.java
More file actions
160 lines (126 loc) · 4.12 KB
/
Copy pathLesson1.java
File metadata and controls
160 lines (126 loc) · 4.12 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
package lesson1;
import java.util.ArrayList;
import java.util.List;
public class Lesson1 {
public static void main(String[] args) {
// Examples of variables
int number = 42;
double pi = 3.14;
String message = "Hello, world!";
boolean isJavaFun = true;
// Examples of control structures
if (isJavaFun) {
System.out.println("Java is fun!");
} else if (number == 43) {
System.out.println("Java is also fun!");
} else {
System.out.println("Java is not fun?");
}
int dayOfWeek = 3;
switch (dayOfWeek) {
case 1:
System.out.println("It's Monday");
break;
case 2:
System.out.println("It's Tuesday");
break;
// ...
default:
System.out.println("It's some other day");
}
int x = 5;
// Example of ternary operator
int y = (x > 0) ? 1 : -1;
// Examples of loops
int[] numbersArray = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbersArray.length; i++) {
System.out.print(numbersArray[i] + " ");
}
for (int i = numbersArray.length - 1; i >= 0; i--) {
System.out.print(numbersArray[i] + " ");
}
for (int num : numbersArray) {
System.out.print(num + " ");
}
int i = 0;
while (i < 5) {
System.out.print(i + " ");
i++;
}
// Examples of methods
int sum = add(3, 5);
System.out.println("Sum: " + sum);
// Examples of arrays and lists
int[] array = { 1, 2, 3, 4, 5 };
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.remove(0);
for (String n : list) {
System.out.println(n);
}
// Examples of object oriented programming
Animal animal = new Animal("Elephant", 25);
System.out.println("Name: " + animal.getName() + ", Age: " + animal.getWeight());
// Example of exception handling
try {
int result = divide(10, 0);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.err.println("Error: " + e.getMessage());
}
// Examples of string handling
String str1 = "Hello";
String str2 = "World";
String str3 = str1 + ", " + str2;
System.out.println(str3);
String text = "This is a sample text for demonstration.";
String substring = "is";
int index = text.indexOf(substring);
System.out.println("Found in: " + index);
if (index >= 0) {
System.out.println("Found it!");
}
System.out.println("Index of \"" + substring + "\" in the text: " + index);
String str = "Hello, World!";
int length = str.length();
System.out.println("Length of the string \"" + str + "\": " + length);
// StringBuilder example
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Java ");
stringBuilder.append("StringBuilder ");
stringBuilder.append("example");
System.out.println(stringBuilder.toString());
// Same as this
String withoutStringBuilder = "Java " + "StrinBuilder " + "example";
}
// Examples of methods
public static int add(int a, int b) {
return a + b;
}
public static int divide(int a, int b) {
return a / b;
}
// Example of a class
static class Animal {
private String name;
private int weight;
public Animal(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public int getWeight() {
return weight;
}
public void setName(String name) {
this.name = name;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
}