Skip to content

Commit ae75e47

Browse files
committed
更新
1 parent d511ecc commit ae75e47

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed

Part4/CalendarTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.text.DateFormatSymbols;
2+
import java.util.*;
3+
4+
public class CalendarTest {
5+
public static void main(String[] args) {
6+
GregorianCalendar d = new GregorianCalendar();
7+
8+
int today = d.get(Calendar.DAY_OF_MONTH);
9+
int month = d.get(Calendar.MONTH);
10+
11+
d.set(Calendar.DAY_OF_MONTH, 1);
12+
13+
int weekday = d.get(Calendar.DAY_OF_WEEK);
14+
15+
int firstDayOfWeek = d.getFirstDayOfWeek();
16+
17+
int indent = 0;
18+
while (weekday != firstDayOfWeek) {
19+
indent++;
20+
d.add(Calendar.DAY_OF_MONTH, -1);
21+
weekday = d.get(Calendar.DAY_OF_WEEK);
22+
}
23+
24+
String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
25+
do {
26+
System.out.printf("%4s", weekdayNames[weekday]);
27+
d.add(Calendar.DAY_OF_MONTH, 1);
28+
weekday = d.get(Calendar.DAY_OF_WEEK);
29+
} while (weekday != firstDayOfWeek);
30+
System.out.println();
31+
for (int i = 1; i <= indent; i++) {
32+
System.out.print(" ");
33+
}
34+
d.set(Calendar.DAY_OF_MONTH, 1);
35+
do {
36+
int day = d.get(Calendar.DAY_OF_MONTH);
37+
System.out.printf("%3d", day);
38+
if (day == today) {
39+
System.out.print("*");
40+
} else {
41+
System.out.print(" ");
42+
}
43+
d.add(Calendar.DAY_OF_MONTH, 1);
44+
weekday = d.get(Calendar.DAY_OF_WEEK);
45+
if (weekday == firstDayOfWeek) {
46+
System.out.println();
47+
}
48+
} while (d.get(Calendar.MONTH) == month);
49+
if (weekday != firstDayOfWeek) {
50+
System.out.println();
51+
}
52+
}
53+
}

Part4/ConstructorTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.*;
2+
3+
public class ConstructorTest {
4+
public static void main(String[] args) {
5+
Emplogee[] staff = new Emplogee[3];
6+
staff[0] = new Emplogee("Harry", 40000);
7+
staff[1] = new Emplogee(60000);
8+
staff[2] = new Emplogee();
9+
10+
for (Emplogee e : staff) {
11+
System.out.println(
12+
"name = " + e.getName() +
13+
", id = " + e.getId() +
14+
", salary = " + e.getSalary()
15+
);
16+
}
17+
}
18+
}
19+
20+
class Emplogee {
21+
private static int nextId;
22+
23+
private int id;
24+
private String name = "";
25+
private double salary;
26+
27+
static {
28+
Random generator = new Random();
29+
nextId = generator.nextInt(10000);
30+
}
31+
32+
{
33+
id = nextId;
34+
nextId++;
35+
}
36+
37+
public Emplogee(String n, double s) {
38+
name = n;
39+
salary = s;
40+
}
41+
42+
public Emplogee(double s) {
43+
this("Emplogee #" + nextId, s);
44+
}
45+
46+
public Emplogee() {
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
53+
public double getSalary() {
54+
return salary;
55+
}
56+
57+
public int getId() {
58+
return id;
59+
}
60+
}
61+

Part4/EmployeeTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.*;
2+
3+
public class EmployeeTest {
4+
public static void main(String[] args) {
5+
Employee[] staff = new Employee[3];
6+
staff[0] = new Employee("Calrl Cracker", 75000, 1987, 12, 15);
7+
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
8+
staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
9+
10+
for (Employee e : staff) {
11+
e.raiseSalary(5);
12+
}
13+
14+
for (Employee e : staff) {
15+
System.out.println(
16+
"name = " + e.getName() +
17+
", salary = " + e.getSalary() +
18+
", hireDay = " + e.getHireDay()
19+
);
20+
}
21+
}
22+
}
23+
24+
class Employee {
25+
private String name;
26+
private double salary;
27+
private Date hireDay;
28+
29+
public Employee(String n, double s, int year, int month, int day) {
30+
name = n;
31+
salary = s;
32+
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
33+
hireDay = calendar.getTime();
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public double getSalary() {
41+
return salary;
42+
}
43+
44+
public Date getHireDay() {
45+
return hireDay;
46+
}
47+
48+
public void raiseSalary(double byPercent) {
49+
double raise = salary * byPercent / 100;
50+
salary += raise;
51+
}
52+
}
53+

Part4/ParamTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
public class ParamTest {
2+
public static void main(String[] args) {
3+
System.out.println("Testing tripleValue:");
4+
double percent = 10;
5+
System.out.println("Before: percent = " + percent);
6+
tripleValue(percent);
7+
System.out.println("After: percent = " + percent);
8+
9+
System.out.println("\nTesting tripleSalary:");
10+
Employee harry = new Employee("Harrt", 50000);
11+
System.out.println("Before: salary = " + harry.getSalary());
12+
tripleSalary(harry);
13+
System.out.println("After: salary = " + harry.getSalary());
14+
15+
System.out.println("\nTesting swap:");
16+
Employee a = new Employee("Alice", 70000);
17+
Employee b = new Employee("Bob", 60000);
18+
System.out.println("Before: a = " + a.getName());
19+
System.out.println("Before: b = " + b.getName());
20+
swap(a, b);
21+
System.out.println("After: a = " + a.getName());
22+
System.out.println("After: b = " + b.getName());
23+
}
24+
25+
public static void tripleValue(double x) {
26+
x = 3 * x;
27+
System.out.println("End of method: x = " + x);
28+
}
29+
30+
public static void tripleSalary(Employee x) {
31+
x.raiseSalary(200);
32+
System.out.println("End of method: salary = " + x.getSalary());
33+
}
34+
35+
public static void swap(Employee x, Employee y) {
36+
Employee temp = x;
37+
x = y;
38+
y = temp;
39+
System.out.println("End of method: x = " + x.getName());
40+
System.out.println("End of method: y = " + y.getName());
41+
}
42+
}
43+
44+
class Employee {
45+
private String name;
46+
private double salary;
47+
48+
public Employee(String n, double s) {
49+
name = n;
50+
salary = s;
51+
}
52+
53+
public String getName() {
54+
return name;
55+
}
56+
57+
public double getSalary() {
58+
return salary;
59+
}
60+
61+
public void raiseSalary(double byPercent) {
62+
double raise = salary * byPercent / 100;
63+
salary += raise;
64+
}
65+
}
66+

Part4/StaticTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
public class StaticTest {
2+
public static void main(String[] args) {
3+
Employee[] staff = new Employee[3];
4+
5+
staff[0] = new Employee("Tom", 40000);
6+
staff[1] = new Employee("Dick", 60000);
7+
staff[2] = new Employee("Harry", 65000);
8+
9+
for (Employee e : staff) {
10+
e.setId();
11+
System.out.println(
12+
"name = " + e.getName() +
13+
", id = " + e.getID() +
14+
", salary = " + e.getSalary()
15+
);
16+
}
17+
int n = Employee.getNextId();
18+
System.out.println("Next available id = " + n);
19+
}
20+
}
21+
22+
class Employee {
23+
private static int nextId = 1;
24+
25+
26+
private String name;
27+
private double salary;
28+
private int id;
29+
30+
public Employee(String n, double s) {
31+
name = n;
32+
salary = s;
33+
id = 0;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public double getSalary() {
41+
return salary;
42+
}
43+
44+
public int getID() {
45+
return id;
46+
}
47+
48+
public void setId() {
49+
id = nextId;
50+
nextId++;
51+
}
52+
53+
public static int getNextId() {
54+
return nextId;
55+
}
56+
57+
public static void main(String[] args) {
58+
Employee e = new Employee("Harry", 50000);
59+
System.out.println(e.getName() + " " + e.getSalary());
60+
}
61+
}

0 commit comments

Comments
 (0)