Java--面向对象试题

来源:互联网 发布:同花顺软件使用步骤 编辑:程序博客网 时间:2024/06/01 22:13
package com.lovo;import java.text.DecimalFormat;import java.util.Calendar;/** * 时钟类 * @author 张小龙 * */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);}/** * 构造器(获得指定的时间) * @param hour 时 * @param minute 分 * @param 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;}}}}/** * 倒计时 * @return 倒计时结束返回true 否则返回false */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);}}
<pre name="code" class="java">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;/** *  分数的引用 *    张小龙 */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
原创粉丝点击