1.分数类
<span style="font-size:14px;">public class Rational {
private int num; // 分子
private int den; // 分母
public Rational(int num, int den) {// 构造器(分子,分母)
this.num = num;
this.den = den;
normalize();
}
public Rational(String str) {// 构造器:从一个字符串中取出分子和分母
String s1 = str.split("/")[0]; // 分子的字符串
String s2 = str.split("/")[1]; // 分母的字符串
this.num = Integer.parseInt(s1);
this.den = Integer.parseInt(s2);
normalize();
}
public Rational(double x) {// 构造器:将小数转化为分数
this.num = (int) (x * 10000);
this.den = 10000;
simplify();
normalize();
}
public Rational add(Rational other) {
return new Rational(this.num * other.den + this.den * other.num,
this.den * other.den).normalize();
}
public Rational sub(Rational other) {
return new Rational(this.num * other.den - this.den * other.num,
this.den * other.den).normalize();
}
public Rational mul(Rational other) {
return new Rational(this.num * other.num, this.den * other.den)
.normalize();
}
public Rational div(Rational other) {
return new Rational(this.num * other.den, this.den * other.num)
.normalize();
}
public Rational simplify() {// 化简
if (num != 0) {
int divisor = gcd(Math.abs(num), Math.abs(den));
num /= divisor;
den /= divisor;
}
return this;
}
public Rational normalize() {// 正规化
if (this.num == 0) {
this.den = 1;
} else if (this.den < 0) {
this.den = -this.den;
this.num = -this.num;
}
return this;
}
public String toString() {
return num + (den != 1 ? ("/" + den) : "");
}
private int gcd(int x, int y) {// 求分子和分母的最大公约数
if (x > y) {
int temp = x;
x = y;
y = temp;
}
for (int i = x; i > 1; i--) {
if (x % i == 0 && y % i == 0) {
return i;
}
}
return 1;
}
}
</span>
2.求分数
<span style="font-size:14px;">public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("r1 = ");
String str1 = sc.next();
Rational r2 = new Rational(0.3);
Rational r1 = new Rational(str1);
System.out.printf("%s + %s = %s\n", r1, r2, r1.add(r2).simplify());
System.out.printf("%s - %s = %s\n", r1, r2, r1.sub(r2).simplify());
System.out.printf("%s * %s = %s\n", r1, r2, r1.mul(r2).simplify());
System.out.printf("%s / %s = %s\n", r1, r2, r1.div(r2).simplify());
sc.close();
}
}</span>
3.时钟类
<span style="font-size:14px;">public class Clock {
private int hour; // 时
private int minute; // 分
private int second; // 秒
public Clock() {
Calendar cal = Calendar.getInstance(); // 构造器(获取系统时间)
this.hour = cal.get(Calendar.HOUR_OF_DAY);
this.minute = cal.get(Calendar.MINUTE);
this.second = cal.get(Calendar.SECOND);
}
public Clock(int hour, int minute, int second) { // 构造器(获得指定的时间)
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void go() {// 走字方法
++second;
if (second == 60) {
second = 0;
++minute;
if (minute == 60) {
minute = 0;
++hour;
if (hour == 24) {
hour = 0;
}
}
}
}
public boolean countDown() {// 倒计时方法
if (second > 0) {
--second;
} else {
if (minute > 0) {
--minute;
second = 59;
} else {
if (hour > 0) {
--hour;
minute = 59;
second = 59;
}
}
}
return hour == 0 && minute == 0 && second == 0;
}
public String toString() { // 获得时间对象的字符串表示形式
DecimalFormat df = new DecimalFormat("00"); // 数字格式化器
return df.format(hour) + ":" + df.format(minute) + ":"
+ df.format(second);
}
}</span>
3.在框体中显示时间和倒计时
<span style="font-size:14px;">private static Timer timer = null;
public static void main(String[] args) {
final Clock c = new Clock(0, 0, 4);
JFrame f = new JFrame();
f.setTitle("时钟");
f.setSize(400, 200);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel lbl = new JLabel("时间", JLabel.CENTER);
Font font = new Font("微软雅黑", Font.PLAIN, 60);
lbl.setFont(font);
lbl.setText(c.toString());
f.add(lbl);
f.setVisible(true);
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isOver = c.countDown();
lbl.setText(c.toString());
if(isOver) {
timer.stop(); // 停止计时器
JOptionPane.showMessageDialog(null, "时间到!!!");
}
}
}); // 创建一个计时器对象
timer.start(); // 启动计时器
}
}</span>