10-1 Time类
public class Time {
private long hour;
private long minute;
private long second;
public Time(){
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds/1000;
long currentSeconds = totalSeconds%60;
this.second = currentSeconds;
long totalMinutes = totalSeconds/60;
long currentMinutes = totalMinutes%60;
this.minute = currentMinutes;
long totalHours = totalMinutes/60;
long currentHours = totalHours%24;
this.hour = currentHours;
}
public Time(long elapseTime){
long totalSeconds = elapseTime/1000;
long currentSeconds = totalSeconds%60;
this.second = currentSeconds;
long totalMinutes = totalSeconds/60;
long currentMinutes = totalMinutes%60;
this.minute = currentMinutes;
long totalHours = totalMinutes/60;
long currentHours = totalHours%24;
this.hour = currentHours;
}
public Time(long hour, long minute, long second){
this.hour = hour;
this.minute = minute;
this.second = second;
}
public long getHour(){
return hour;
}
public long getMinute(){
return minute;
}
public long getSecond(){
return second;
}
public void setTime(long elapseTime){
long totalSeconds = elapseTime/1000;
long currentSeconds = totalSeconds%60;
this.second = currentSeconds;
long totalMinutes = totalSeconds/60;
long currentMinutes = totalMinutes%60;
this.minute = currentMinutes;
long totalHours = totalMinutes/60;
long currentHours = totalHours%24;
this.hour = currentHours;
}
}
10.1测试
package chaper_10;
public class TestForTime {
public static void main(String[] args) {
Time object1=new Time();
Time object2=new Time(555550000);
System.out.println("object1's Current time is "
+object1.getHour()+":"+object1.getMinute()+":"+object1.getSecond()+" GMT");
System.out.println("object2's Current time is "
+object2.getHour()+":"+object2.getMinute()+":"+object2.getSecond()+" GMT");
}
}
10.3MyInteger类
package chaper_10;
public class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
public boolean isEven() {
if (getValue() % 2 == 0)
return true;
else
return false;
}
public boolean isOdd() {
if (getValue() % 2 != 0) {
return true;
} else
return false;
}
public boolean isPrime() {
for (int detect = 2; detect <= getValue() / 2; detect++) {
if (getValue() % detect == 0) {
return false;
}
}
return true;
}
public static boolean isEvn(int value) {
if (value % 2 == 0) {
return true;
} else
return false;
}
public static boolean isOdd(int value) {
if (value % 2 != 0) {
return true;
} else
return false;
}
public static boolean isPrime(int value) {
for (int detect = 2; detect <= value / 2; detect++) {
if (value % detect == 0) {
return false;
}
}
return true;
}
public boolean isEven(MyInteger myInt) {
if (myInt.value % 2 == 0)
return true;
else
return false;
}
public boolean isOdd(MyInteger myInt) {
if (myInt.value % 2 % 2 != 0) {
return true;
} else
return false;
}
public boolean isPrime(MyInteger myInt) {
for (int detect = 2; detect <= getValue() / 2; detect++) {
if (myInt.value % 2 % detect == 0) {
return false;
}
}
return true;
}
public boolean equals(int mode) {
if (mode == this.value)
return true;
else
return false;
}
public boolean equals(MyInteger mode){
if(mode.value == this.value)
return true;
else
return false;
}
public static int parseInt(char[] ch){
return Integer.valueOf(new String(ch));
}
public static int parseInt(String str){
return Integer.valueOf(str);
}
}
10.3测试
package chaper_10;
public class TestForMyInteger {
public static void main(String[] args){
MyInteger myInteger = new MyInteger(17);
MyInteger myInteger1 = new MyInteger(18);
System.out.println("myInteger is "+myInteger.getValue());
System.out.println("isEven? " + myInteger.isEven());
System.out.println("isOdd? " + myInteger.isOdd());
System.out.println("isPrime? " + myInteger.isPrime());
System.out.println("isEven? " + myInteger.isEven(myInteger));
System.out.println("isOdd? " + myInteger.isOdd(myInteger));
System.out.println("isPrime? " + myInteger.isPrime(myInteger));
//注意静态方法的调用方式
System.out.println("isEven? " + MyInteger.isEvn(8));
System.out.println("change " + MyInteger.parseInt("18"));
System.out.println("equals? " + myInteger.equals(18));
System.out.println("equals? " + myInteger.equals(myInteger1));
}
}