Skip to content

Commit 608db28

Browse files
realDuYuanChaogithub-actions
andauthored
update basics (#105)
* static example * date example * for in * add range function * return example * constructor example * update this keyword * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 53288a0 commit 608db28

File tree

9 files changed

+207
-0
lines changed

9 files changed

+207
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.examplehub.basics;
2+
3+
import java.util.Calendar;
4+
5+
public class CalendarExample {
6+
public static void main(String[] args) {
7+
Calendar calendar = Calendar.getInstance();
8+
System.out.println(calendar);
9+
}
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.examplehub.basics;
2+
3+
public class ConstructorExample {
4+
public static void main(String[] args) {
5+
ConstructorTest test = new ConstructorTest();
6+
System.out.println(test); /* ConstructorTest{username='admin', password='112233'} */
7+
8+
test = new ConstructorTest("root", "root");
9+
System.out.println(test); /* ConstructorTest{username='root', password='root'} */
10+
}
11+
}
12+
13+
class ConstructorTest {
14+
public String username;
15+
public String password;
16+
17+
public ConstructorTest() {
18+
this("admin", "112233");
19+
}
20+
21+
public ConstructorTest(String username, String password) {
22+
this.username = username;
23+
this.password = password;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "ConstructorTest{"
29+
+ "username='"
30+
+ username
31+
+ '\''
32+
+ ", password='"
33+
+ password
34+
+ '\''
35+
+ '}';
36+
}
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.examplehub.basics;
2+
3+
import java.util.Date;
4+
5+
public class DateExample {
6+
public static void main(String[] args) {
7+
System.out.println(new Date());
8+
}
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.examplehub.basics;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ForInExample {
7+
public static void main(String[] args) {
8+
int[] numbers = {5, 55, 555, 5555, 55555};
9+
/*
10+
* 5
11+
* 55
12+
* 555
13+
* 5555
14+
* 55555
15+
*/
16+
for (int number : numbers) {
17+
System.out.println(number);
18+
}
19+
20+
/*
21+
* E
22+
* H
23+
*/
24+
for (char ch : "ExampleHub".toCharArray()) {
25+
if (Character.isUpperCase(ch)) {
26+
System.out.println(ch);
27+
}
28+
}
29+
30+
/*
31+
* Java
32+
* Python
33+
* C
34+
*/
35+
List<String> books =
36+
new ArrayList<String>() {
37+
{
38+
add("Java");
39+
add("Python");
40+
add("C");
41+
}
42+
};
43+
for (String book : books) {
44+
System.out.println(book);
45+
}
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.examplehub.basics;
2+
3+
public class ReturnExample {
4+
public static void main(String[] args) {
5+
System.out.println(test(15, 10)); /* 1 */
6+
System.out.println(test(10, 15)); /* -1 */
7+
System.out.println(test(10, 10)); /* 0 */
8+
}
9+
10+
private static int test(int test, int target) {
11+
if (test > target) {
12+
return 1;
13+
}
14+
if (test < target) {
15+
return -1;
16+
}
17+
return 0;
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.examplehub.basics;
2+
3+
public class StaticExample {
4+
public static void main(String[] args) {
5+
StaticTest staticTest = new StaticTest();
6+
StaticTest.increment();
7+
System.out.println(StaticTest.count); /* 1 */
8+
staticTest.method(); /* normal method */
9+
System.out.println(StaticTest.count); /* 2 */
10+
}
11+
}
12+
13+
class StaticTest {
14+
public static int count = 0;
15+
16+
public static void increment() {
17+
count++;
18+
}
19+
20+
public void method() {
21+
increment();
22+
System.out.println("normal method");
23+
}
24+
}

src/main/java/com/examplehub/basics/ThisKeywordExample.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public static void main(String[] args) {
1313

1414
rectangle = new Rectangle(1, 1, 3, 4);
1515
System.out.println(rectangle); /* Rectangle{x=1, y=1, width=3, height=4} */
16+
17+
Counter counter = new Counter();
18+
System.out.println(counter.increment().increment().increment().getCount()); /* 3 */
1619
}
1720
}
1821

@@ -52,3 +55,16 @@ public String toString() {
5255
return "Rectangle{" + "x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + '}';
5356
}
5457
}
58+
59+
class Counter {
60+
private int count = 0;
61+
62+
public Counter increment() {
63+
count++;
64+
return this;
65+
}
66+
67+
public int getCount() {
68+
return count;
69+
}
70+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.examplehub.maths;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Range {
7+
public static int[] range(int n) {
8+
List<Integer> numbers = new ArrayList<>();
9+
for (int i = 0; i < n; ++i) {
10+
numbers.add(i);
11+
}
12+
return numbers.stream().mapToInt(i -> i).toArray();
13+
}
14+
15+
public static int[] range(int start, int end) {
16+
List<Integer> numbers = new ArrayList<>();
17+
for (int i = start; i < end; ++i) {
18+
numbers.add(i);
19+
}
20+
return numbers.stream().mapToInt(i -> i).toArray();
21+
}
22+
23+
public static int[] range(int start, int end, int step) {
24+
List<Integer> numbers = new ArrayList<>();
25+
for (int i = start; i < end; i += step) {
26+
numbers.add(i);
27+
}
28+
return numbers.stream().mapToInt(i -> i).toArray();
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Arrays;
6+
import org.junit.jupiter.api.Test;
7+
8+
class RangeTest {
9+
@Test
10+
void testRange() {
11+
assertTrue(Arrays.equals(new int[] {0, 1, 2, 3, 4}, Range.range(5)));
12+
assertTrue(Arrays.equals(new int[] {10, 11, 12, 13, 14, 15}, Range.range(10, 16)));
13+
assertTrue(Arrays.equals(new int[] {1, 3, 5, 7, 9}, Range.range(1, 10, 2)));
14+
}
15+
}

0 commit comments

Comments
 (0)