首页 > 代码库 > 时钟,泳池

时钟,泳池

 1 package com.fj; 2  3 import java.util.Calendar; 4  5 /** 6  * 时钟类 7  * @author 俊霖 8  * 9  */10 public class Clock {11     private int hour;12     private int minute;13     private int second;14     15     public Clock() {16         Calendar calendar = Calendar.getInstance();17         hour = calendar.get(Calendar.HOUR_OF_DAY);18         minute = calendar.get(Calendar.MINUTE);19         second = calendar.get(Calendar.SECOND);20     }21 22     public void go() {23         second++;24         if (second >= 59) {25             second = 0;26             minute++;27         }28         if (minute >= 59) {29             minute = 0;30             hour++;31         }32         if (hour >= 23) {33             hour = 0;34         }35     }36     37     public StringBuffer show() {38         StringBuffer time = new StringBuffer();    39         if (hour < 10) {40             time.append("0");41         }42         time.append(hour + ":");43         if (minute < 10) {44             time.append("0");45         }46         time.append(minute + ":");47         if (second < 10) {48             time.append("0");49         }50         time.append(second);51         return time;52     }53     54     public int getHour() {55         return hour;56     }57     public void setHour(int hour) {58         this.hour = hour;59     }60     public int getMinute() {61         return minute;62     }63     public void setMinute(int minute) {64         this.minute = minute;65     }66     public int getSecond() {67         return second;68     }69     public void setSecond(int second) {70         this.second = second;71     }72     73 }
 1 Clock clock = new Clock(); 2         for (int i = 0;i < 10; i++) { 3             clock.go(); 4             System.out.println(clock.show()); 5             try { 6                 Thread.sleep(1000); 7             } catch (InterruptedException e) { 8                 e.printStackTrace(); 9             }10         }

泳池

 1 package com.fj; 2 /** 3  * 泳池 4  * @author 俊霖 5  * 6  */ 7 public class Circle { 8     private double radius; 9     10     public double getArea() {11         double area = Math.PI * radius * radius;12         return area;13     }14     15     public double getCircumference() {16         double circumference = 2 * Math.PI * radius;17         return circumference;18     }19     20     public double getRadius() {21         return radius;22     }23 24     public void setRadius(double radius) {25         this.radius = radius;26     }27     28     29 }
1 Circle circle1 = new Circle();2         Circle circle2 = new Circle();3         double radius = Integer.parseInt(JOptionPane.showInputDialog("请输入泳池半径:"));4         circle1.setRadius(radius);5         circle2.setRadius(radius + 3);6         System.out.printf("装修过道需要%.2f元!\n",(circle2.getArea()-circle1.getArea()) * 10);7         System.out.printf("修围墙需要%.2f元!",circle2.getCircumference() * 8);

 

时钟,泳池