码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象时钟类(倒计时),分数计算类

时间:2014-10-23 20:41:04      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   java   for   sp   div   on   

/**
 * 时钟类
 * 
 * @author Administrator
 * 
 */
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 min, int sec) {
		this.hour = hour;
		this.minute = min;
		this.second = sec;
	}


	public void goon() {
		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--;
			if(second<=0){
				second=59;
				minute--;
				if(minute < 0){
					minute = 59;
					hour--;
					if(hour < 0){
						hour = 0;
					}
				}
			}
		}
		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);
	}
}



public class ClockGUI {
	
	private static Timer timer = null;
	
	public static void main(String[] args) throws Exception {

		final Clock sj = new Clock(1, 1, 2);

		JFrame w = new JFrame();
		w.setVisible(true);
		w.setSize(800, 600);
		w.setTitle("时钟");
		w.setResizable(false);
		w.setLocationRelativeTo(null);
		w.setLayout(null);
		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel lbl1 = new JLabel("剩余时间:");
		Font font = new Font("楷体", 1, 56);
		lbl1.setFont(font);
		lbl1.setBounds(50, 200, 700, 120);
		w.add(lbl1);

		final JLabel lbl2 = new JLabel("");
		Font font1 = new Font("楷体", 1, 56);
		lbl2.setFont(font1);
		lbl2.setBounds(320, 200, 700, 120);
		lbl2.setText(sj.toString());

		w.add(lbl2);

		timer = new Timer(1000, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(sj.countDown()) {
					timer.stop();
					JOptionPane.showMessageDialog(null,"时间结束!");
				}
				lbl2.setText(sj.toString());
			}
		});
		timer.start();
	}
}

  

/**
 * 分数类
 * 
 * @author Administrator
 * 
 */
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.num = Integer.parseInt(s2);
		normalize();
	}
	
//	public Rational()
	

	/**
	 * 分数加法
	 * 
	 * @param other
	 * @return
	 */
	public Rational add(Rational other) {
		return new Rational(this.num * other.den + this.den * other.num,
				this.den * other.num).normalize();
	}

	/**
	 * 分数减法
	 * 
	 * @param other
	 * @return
	 */
	public Rational sub(Rational other) {
		return new Rational(this.num * other.den - this.den * other.num,
				this.den * other.num).normalize();
	}

	/**
	 * 分数乘法
	 * 
	 * @param other
	 * @return
	 */
	public Rational mul(Rational other) {
		return new Rational(this.num * other.num, this.den * other.den).normalize();
	}

	/**
	 * 分数除法
	 * 
	 * @param other
	 * @return
	 */
	public Rational div(Rational other) {
		return new Rational(this.num * other.den, this.den * other.num).normalize();
	}

	/**
	 * 化简
	 * 
	 * @return 化简后的分数对象
	 */
	public Rational simplify() {
		int divisor = gcd(Math.abs(num), Math.abs(den));
		num /= divisor;
		den /= divisor;
		return this;
	}

	/**
	 * 正规化
	 * @return 正规化以后的分数对象
	 */
	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) : "");
	}

	/**
	 * 找分子和分母的最大公约数
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	private int gcd(int x, int y) {
		if (x > y) {
			return gcd(y, x);
		}
		for (int i = x; i > 1; i--) {
			if (x % i == 0 && y % i == 0) {
				return i;
			}
		}
		return 1;
	}
}

  

面向对象时钟类(倒计时),分数计算类

标签:blog   io   os   ar   java   for   sp   div   on   

原文地址:http://www.cnblogs.com/superyueyue/p/4046632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!