1.关于Calenddar类的小程序
package Exercise; import java.io.ObjectInputStream.GetField; import java.time.DayOfWeek; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Random; public class Stock { public static void main(String[] args) { Calendar i = new GregorianCalendar(); display(i); i.add(Calendar.DAY_OF_YEAR, 100); display(i); } public static void display(Calendar k){ String[] mon ={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; String[] we ={"星期一","星期二","星期三","星期四","星期五","星期六","星期天"}; System.out.print(k.get(GregorianCalendar.YEAR)+"年"); System.out.print(mon[k.get(GregorianCalendar.MONTH)]); System.out.print(k.get(GregorianCalendar.DAY_OF_MONTH)+"日"); System.out.print(we[k.get(GregorianCalendar.DAY_OF_WEEK)]+" "); System.out.print(k.get(GregorianCalendar.HOUR)+":"); System.out.print(k.get(GregorianCalendar.MINUTE)+":"); System.out.print(k.get(GregorianCalendar.SECOND)+" "); } }
2exercise 9.6
1 import java.util.Date; 2 import java.util.Random; 3 4 public class StopWatch { 5 private double starttime ; 6 private double endtime ; 7 public StopWatch() { 8 starttime =System.currentTimeMillis(); 9 } 10 public void start(){ 11 starttime =System.currentTimeMillis(); 12 } 13 public void stop(){ 14 endtime = System.currentTimeMillis(); 15 } 16 public double gettime(){ 17 return endtime-starttime; 18 } 19 20 public static void main(String[] args) { 21 // TODO Auto-generated method stub 22 int[] a = new int[1000]; 23 Random t = new Random(); 24 StopWatch sw = new StopWatch(); 25 sw.start(); 26 for(int k:a) 27 {k=t.nextInt(10000);} 28 int index=0; 29 for(int b=0;b<a.length-1;b++) 30 { 31 int ret=a[b]; 32 for(int c=b+1;c<a.length;c++) 33 { 34 if(a[c]<ret) 35 index=c; 36 ret =a[c]; 37 } 38 if(ret!=a[b]) 39 {int temp = a[b]; 40 a[b] = a[index]; 41 a[index] = temp;} 42 } 43 sw.stop(); 44 System.out.println(sw.gettime()); 45 } 46 47 }
注意选择排序的写法。要熟练。
3选择排序,非常简答的程序
1 public static void selectSort(int[]a) 2 { 3 int minIndex=0; 4 int temp=0; 5 if((a==null)||(a.length==0)) 6 return; 7 for(int i=0;i<a.length-1;i++) 8 { 9 minIndex=i;//无序区的最小数据数组下标 10 for(intj=i+1;j<a.length;j++) 11 { 12 //在无序区中找到最小数据并保存其数组下标 13 if(a[j]<a[minIndex]) 14 { 15 minIndex=j; 16 } 17 } 18 if(minIndex!=i) 19 { 20 //如果不是无序区的最小值位置不是默认的第一个数据,则交换之。 21 temp=a[i]; 22 a[i]=a[minIndex]; 23 a[minIndex]=temp; 24 } 25 } 26 }
4.P306 Exercise9.7
1 import java.util.Date; 2 3 public class Account { 4 private int id; 5 private double balance; 6 private static double aIR; 7 private Date date=new Date(); 8 public Account(){ 9 10 } 11 public Account(int id,double balance){ 12 this.id = id; 13 this.balance = balance; 14 } 15 public int getId() 16 {return id;} 17 public double getBalance() 18 {return balance;} 19 public double getAIR() 20 {return aIR;} 21 public void setId(int i) 22 {id=i;} 23 public void setBalance(double b) 24 {balance=b;} 25 public void setaIR(double k) 26 {aIR=k;} 27 public String getdate() 28 {return date.toString();} 29 public double getMIR() 30 {return aIR/12;} 31 public void withdraw(double wd){ 32 balance-=wd; 33 } 34 public void deposit(double d ){ 35 balance-=d; 36 } 37 38 public static void main(String[] args) { 39 // TODO Auto-generated method stub 40 Account a = new Account(24, 15000); 41 a.setaIR(0.15); 42 System.out.println("the id of the acount is "+a.getId()+" "+"the balance is "+a.getBalance() 43 +" "+"the aIR is "+a.getAIR()+" theMTR is "+a.getMIR()+" the date is "+a.getdate()); 44 System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 45 a.withdraw(7500); 46 System.out.println("the id of the acount is "+a.getId()+" "+"the balance is "+a.getBalance() 47 +" "+"the aIR is "+a.getAIR()+" theMTR is "+a.getMIR()+" the date is "+a.getdate()); 48 } 49 50 51 }