java语言的学习及练习

第一次作业题目:
设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。

完成练习中学习的知识点:
1.java基本输入输出小结:
http://blog.csdn.net/zhangpengju999/article/details/9704681
scanner用法:

public static void main(String[] args) throws FileNotFoundException { 
                InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java")); 
                Scanner s = new Scanner(in); 
                while(s.hasNextLine()){ 
                        System.out.println(s.nextLine()); 
                } 
        }

代码:

package fraction;
import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        Fraction a = new Fraction(in.nextInt(),in.nextInt());
        Fraction b = new Fraction(in.nextInt(),in.nextInt());
        a.print();
        b.print();
        a.plus(b).print();
        a.multiply(b).plus(new Fraction(5,6)).print();
        a.print();
        b.print();
        in.close();
    }
}
class Fraction {
    private int fz;
    private int fm;

    public Fraction(int a, int b){
        fz = a;
        fm = b;
    }
    public double toDouble(){
            return (double)fz/fm;
    }

    public Fraction plus(Fraction r){
        int newfz = fz*r.fm + r.fz*fm;
        int newfm = fm * r.fm;
        simplify();
        return new Fraction(newfz,newfm);
    }

    public Fraction multiply(Fraction r){
        int newfz = fz * r.fz;
        int newfm = fm * r.fm;
        simplify();
        return new Fraction(newfz, newfm);
    }

    public void print(){
        simplify();
        if(fz % fm == 0){
            System.out.println(fz/fm);
        }else{
            System.out.printf("%d/%d\n", fz,fm);
        }
    }

    public int gcd(int a, int b){
        if(a % b == 0){
            return b;
        }else{
            return gcd(b,a%b);
        }
    }

    public void simplify(){
        int a = gcd(fz,fm);
        fz = fz / a;
        fm = fm / a;
    }
}

第二次题目:
实现一个有秒计时的数字时钟。

知识点:
string.format:java的格式化输入。

实现代码:

package Clock;
import java.util.Scanner;

class display {
    private int value = 0;
    private int limit;

    public display(int a,int b){
        value = a;
        limit = b;
    }
    public boolean increase(){
        value ++;
        if(value == limit){
            value = 0;
            return false;
        }
        return true;
    }

    public int getValue(){
        return value;
    }

    public int getLimit(){
        return limit;
    }
}

public class Clock {
    private display s ;
    private display min ;
    private display h;

    public Clock(int hour, int minute, int second){
        h = new display(hour,24);
        min = new display(minute,60);
        s = new display(second,60);
    }

    public void tick(){
        if(!s.increase()){
            if(!min.increase()){
                h.increase();
            }
        }   
    }

    public String toString(){
        return String.format("%02d:%02d:%02d", h.getValue(),min.getValue(),s.getValue());
    }

    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        Clock clock = new Clock(in.nextInt(),in.nextInt(),in.nextInt());
        clock.tick();
        System.out.println(clock);
        in.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值