*10.1(时间类Time)
GMT时间(格林尼治标准时间)一般指世界时间, 即0时区的区时,比北京时间(东8区)晚8小时;所以,
GMT时间+8小时所得结果就是北京时间。
package p10;
public class Time {
private long hour;
private long minute;
private long second;
//无参构造方法
public Time() {
//获取当前时间,可以用 System.currentTimeMillis()方法获取当前总的毫秒数
long totalMilliseconds=System.currentTimeMillis();
// 总的毫秒数/1000=总的秒数
long totalSeconds=totalMilliseconds/1000;
//总的秒数%60=当前秒数
long currentSeconds=totalSeconds%60;
this.second=currentSeconds;
// 总的秒数/60=总的分钟数
long totalMinutes=totalSeconds/60;
//总的分钟数%60=当前分钟数
long currentMinutes=totalMinutes%60;
this.minute=currentMinutes;
// 总的分钟数/60=总的小时数
long totalHours=totalMinutes/60;
//总小时数%24=当前小时数
long currentHours=totalHours%24;
this.hour =currentHours;
}
public Time(long elapseTime) {
// 总的毫秒数/1000=总的秒数
long totalSeconds=elapseTime/1000;
//总的秒数%60=当前秒数
long currentSeconds=totalSeconds%60;
this.second=currentSeconds;
// 总的秒数/60=总的分钟数
long totalMinutes=totalSeconds/60;
//总的分钟数%60=当前分钟数
long currentMinutes=totalMinutes%60;
this.minute=currentMinutes;
// 总的分钟数/60=总的小时数
long totalHours=totalMinutes/60;
//总小时数%24=当前小时数
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) {
// 总的毫秒数/1000=总的秒数
long totalSeconds=elapseTime/1000;
//总的秒数%60=当前秒数
long currentSeconds=totalSeconds%60;
this.second=currentSeconds;
// 总的秒数/60=总的分钟数
long totalMinutes=totalSeconds/60;
//总的分钟数%60=当前分钟数
long currentMinutes=totalMinutes%60;
this.minute=currentMinutes;
// 总的分钟数/60=总的小时数
long totalHours=totalMinutes/60;
//总小时数%24=当前小时数
long currentHours=totalHours%24;
this.hour =currentHours;
}
}
package p10;
public class Test1 {
public static void main(String[] args) {
Time object1=new Time();
Time object2=new Time(555550000);
//显示object1的时间
System.out.println("object1's Current time is "
+object1.getHour()+":"+object1.getMinute()+":"+object1.getSecond()+" GMT");
//显示object2的时间
System.out.println("object2's Current time is "
+object2.getHour()+":"+object2.getMinute()+":"+object2.getSecond()+" GMT");
}
}
10.2(BMI类)
10.3(MyInteger类)
package p10;
public class MyInteger {
//存储这个对象表示的int值
public int value;
public MyInteger(int value) {
this.value=value;
}
public int getInt() {
return this.value;
}
//判断是否是偶数,偶数:能被2整除的数,包括正偶数、负偶数和零
public boolean isEven() {
if(getInt() % 2 == 0)
return true;
else
return false;
}
//判断是否是奇数,奇数:不能被2整除的数
public boolean isOdd() {
if(getInt() % 2 != 0)
return true;
else
return false;
}
//素数:在大于1的整数中,只能被1和这个数本身整除的数,如2,3,5,7,11
public boolean isPrime() {
for(int divisor=2; divisor<=getInt()/2; divisor++) {
if(getInt()%divisor==0) {
return false;
}
}
return true;
}
public static boolean isEven(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 divisor=2; divisor<=value/2; divisor++) {
if(value%divisor==0) {
return false;
}
}
return true;
}
public static boolean isEven(MyInteger myInteger) {
if(myInteger.value % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(MyInteger myInteger) {
if(myInteger.value % 2 != 0)
return true;
else
return false;
}
public static boolean isPrime(MyInteger myInteger) {
for(int divisor=2; divisor<=myInteger.value/2; divisor++) {
if(myInteger.value % divisor == 0) {
return false;
}
}
return true;
}
public boolean equals(int count) {
if(this.value==count)
return true;
else
return false;
}
public boolean equals(MyInteger myInteger) {
if(myInteger.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 s) {
return Integer.valueOf(s);
}
}
10.4(MyPoint类)
package p10;
public class MyPoint {
public double x;
public double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public MyPoint() {
this.x=0;
this.y=0;
}
public MyPoint(double x, double y) {
this.x=x;
this.y=y;
}
public double distance(MyPoint myPoint) {
return Math.sqrt(Math.pow((this.x-myPoint.x),2) + Math.pow((this.y-myPoint.y),2));
}
public double distance(double x, double y) {
return Math.sqrt(Math.pow((this.x-x),2) + Math.pow((this.y-y),2));
}
}
package p10;
public class Test4 {
public static void main(String[] args) {
MyPoint a=new MyPoint();
MyPoint b=new MyPoint(10,30.5);
System.out.printf("两点间的距离为:%.2f", a.distance(b));
}
}
*10.5(显示素数因子)
书上写有这个类,在本书第320页,类的实现也有:
package p10;
public class StackOfIntegers {
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY=16;
public StackOfIntegers() {
this(DEFAULT_CAPACITY);
}
public StackOfIntegers(int capacity) {
elements=new int[capacity];
}
public void push(int value) {
if(size>=elements.length) {
int[] temp=new int[elements.length*2];
System.arraycopy(elements, 0, temp, 0, elements.length);
elements=temp;
}
elements[size++]=value;
}
public int pop() {
return elements[--size];
}
public int peek() {
return elements[size-1];
}
public boolean empty() {
return size==0;
}
public int getSize() {
return size;
}
}
package p10;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入一个正整数:");
int number=input.nextInt();
input.close();
//创建一个栈类
StackOfIntegers stack=new StackOfIntegers();
int i=2;
while(number/i!=0) {
if(number%i==0) {
number=number/i;
stack.push(i);
//System.out.println(i);显示最小因子
}
else if(number%i!=0){
i++;
}
}
//先进先出后进后出
while(!stack.empty()) {
System.out.print(stack.pop()+" ");
}
}
}
*10.5(显示素数)
package p10;
public class Test6 {
public static void main(String[] args) {
//创建一个栈类
StackOfIntegers stack=new StackOfIntegers();
PrintPrimeNumbers(50);
//先进先出后进后出
while(!stack.empty()) {
System.out.print(stack.pop()+" ");
}
}
//书上183页代码
private static void PrintPrimeNumbers(int i) {
final int NUMBER_OF_PRIMES_PER_LINE=10;
int count=0;
int number=2;
while(count
**10.7(游戏:ATM机)
package p10;
import java.util.Scanner;
import p9.Account;//账户类
public class Test7 {
public static void main(String[] args) {
//首先创建一个对象数组:创建一个有10个账户的数组
Account[] accountArray=new Account[100];
//初始化对象数组
for(int i=0; i
package p9;
import java.util.Date;
public class Account {
public int id=0;//用户名
public double balance=0;//余额
public double annualInteresRate=0;//当前利率
public java.util.Date dateCreated;//存储开户时间
//无参构造方法
public Account(){
}
//有参构造方法
public Account(int id, double balance){
this.id=id;
this.balance=balance;
}
public int getId(){
return id;
}
public double getBalance() {
return balance;
}
public double getAnualInterestRate() {
return annualInteresRate;
}
public void setId(int id){
this.id=id;
}
public void setBalance(double balance) {
this.balance=balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInteresRate=annualInterestRate;
}
public void setDateCreated(Date dateCreated){
System.out.println(dateCreated.toString());
}
//月利息
public double getMonthlyInterestRate() {
return annualInteresRate/12;
}
//取钱
public double withDraw(double withdraw) {
return this.balance=this.balance-withdraw;
}
//存钱
public double deposit(double deposit) {
return this.balance=this.balance+deposit;
}
}
***10.8(财务:税款类Tax)
*10.14(MyDate 类)
package p10;
import java.util.GregorianCalendar;
public class MyDate {
private int year;
private int month;//月份是从0开始的,0代表一月份
private int day;
private long eclapseTime;//从格林威治时间1970年1月1日午夜开始流逝的毫秒数
//无参构造方法,该方法 创建当前日期的 MyDate对象
public MyDate() {
//获取当前日期,在这里我直接设置自己当前时间(你们也可以随机设置自己当前日期)
GregorianCalendar object1=new GregorianCalendar();
this.year=object1.get(GregorianCalendar.YEAR);
this.month=object1.get(GregorianCalendar.MONTH);
this.day=object1.get(GregorianCalendar.DATE);
}
//有参构造方法,创建以从1970年1月1日午夜开始流逝的毫秒数为时间的 MyDate对象
public MyDate(long eclapseTime) {
GregorianCalendar object2=new GregorianCalendar();
//设置从格林威治时间算起的一个特定时间
object2.setTimeInMillis(eclapseTime);
this.year=object2.get(GregorianCalendar.YEAR);
this.month=object2.get(GregorianCalendar.MONTH);
this.day=object2.get(GregorianCalendar.DATE);
}
//有参构造方法,创建带指定 (年,月,日)的 MyDate对象
public MyDate(int year, int month, int day) {
this.year=year;
this.month=month;
this.day=day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
//设置一个从格林威治时间1970年1月1日午夜开始流逝的毫秒数
public void setDate(long eclapseTime) {
this.eclapseTime=eclapseTime;
}
}
package p10;
import java.util.Date;
public class Test14 {
public static void main(String[] args) {
Date d1=new Date();
Date d2=new Date(34355555133101L);
System.out.println("对象1的时间为:"+d1.getHours()+"小时"+d1.getMinutes()+"分钟"+d1.getSeconds()+"秒");
System.out.println("对象2的时间为:"+d2.getHours()+"小时"+d2.getMinutes()+"分钟"+d2.getSeconds()+"秒");
}
}