Java 1023

来源:互联网 发布:希尔伯特曲线算法 编辑:程序博客网 时间:2024/05/29 12:09
package com.lovo;/** * 时钟类 */import java.text.DecimalFormat;import java.util.Calendar;public class Clock {private int hour;private int min;private int sec;public Clock(int hour, int min, int sec) {this.hour = hour;this.min = min;this.sec = sec;}/** * 读取本地时间; */public Clock(){Calendar cal = Calendar.getInstance();hour = cal.get(11);  //Calendar.HOUR_OF_DAY;min  = cal.get(12);  //Calendar.MINUTE;sec  = cal.get(13);  //Calendar.SECOND;}/** * 时间顺时针走; */public void go(){sec++;if(sec == 60){sec = 0;min++;if(min == 60){min = 0;hour++;if(hour == 24){hour = 0;}}}}/** * 时间逆时针走; */public boolean back(){        if(sec > 0){        --sec;        }else{        if(min > 0){        --min;        sec = 59;        }else{        if(hour > 0){        --hour;        min = 59;        sec = 59;        }        }        }        return hour ==0 && min == 0 && sec == 0;}/** * 格式化,调用了DecimalFormat方法。 * @return */public String showTime(){   DecimalFormat df = new DecimalFormat("00");   return df.format(hour) + ":" + df.format(min) + ":" + df.format(sec);}}


package com.lovo;/** * 时钟 * @author 周博 */import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Timer;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;public class Test02 {private static Timer timer = null;public static void main(String[] args) {final Clock c = new Clock(1,0,5);JFrame f = new JFrame();f.setTitle("时钟");f.setVisible(true);f.setSize(400,400);f.setResizable(false);f.setLocationRelativeTo(null);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JLabel l = new JLabel("时间",JLabel.CENTER);    l.setText(c.showTime());f.add(l);    Font font = new Font("微软雅黑",1,50);l.setFont(font); timer = new Timer(1000,new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//c.go();                            //顺时针boolean isover = c.back();         //逆时针if(isover){timer.stop();JOptionPane.showMessageDialog(null, "时间到!");}l.setText(c.showTime());}});  //daley(延迟多少),listener(需要new ActionListener监听器)    timer.start();    //计时器}}

package com.lovo;/** * 分数类 * @author 周博 * */public class Rational {private int num;// 分子private int den;// 分母/** * 构造器 * @param num 分子 * @param den 分母 */public Rational(int num, int den) {this.num = num;this.den = den;normalize();}/** * 构造器 * @param str 代表一个分数的字符串 */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();}/** * 构造器 * @param x 一个小数 */public Rational(double x) {this.num = (int) (x * 10000);this.den = 10000;simplify();normalize();}/** * public   Rational add(Rational other) { ... } * 访问修饰符  返回类型     方法名(参数列表)       { ... } * 加法 * @param other 另一个分数 * @return 两个分数相加的结果 */public Rational add(Rational other) {return new Rational(this.num * other.den + this.den * other.num, this.den * other.den).normalize();}/** * 减法 * @param other 另一个分数 * @return 两个分数相减的结果 */public Rational sub(Rational other) {return new Rational(this.num * other.den - this.den * other.num, this.den * other.den).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() {if(num != 0) {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) : "");}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;}}

package com.lovo;/** * @author 周博 */import java.util.Scanner;public class Test01 {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();}}


0 0
原创粉丝点击