《JAVA语言程序设计》第九章一星题

T1

import java.util.Date;

public class T1 {
    public static void main(String[] args) {
        long l=10000;
        for(int i=0;i<8;i++) {
            Date d = new Date();
            d.setTime(l);
            System.out.println(d);
            l*=10;
        }
    }
}

T2

import java.util.Random;

public class T2 {
    public static void main(String[] args) {
        Random rand = new Random(1000);
        for(int i=0;i<50;i++)
            System.out.println(rand.nextInt(100));
    }
}

T3

import java.util.GregorianCalendar;

public class T3 {
    public static void main(String[] args) {
        java.util.GregorianCalendar date=new java.util.GregorianCalendar();
        System.out.println(date.get(GregorianCalendar.YEAR)+"年"+
                (1+date.get(GregorianCalendar.MONTH))+"月"+
                date.get(GregorianCalendar.DAY_OF_MONTH)+"日"
                );
        java.util.GregorianCalendar d = new java.util.GregorianCalendar();
        long a = 1234567898765L;
        d.setTimeInMillis(a);
        System.out.println(d.get(GregorianCalendar.YEAR)+"年"+
                        (1+d.get(GregorianCalendar.MONTH))+"月"+
                        d.get(GregorianCalendar.DAY_OF_MONTH)+"日"
                );

    }
}

T4

public class T4 {
    public static void main(String[] args) {
        StopWatch m = new StopWatch();
        m.start();
        for (int i = 0; i < 9999999; i++) {
            for (int j = 0; j < 9999999; j++) {
            }
        }
        m.stop();
        System.out.println(m.getElapsedTime());
    }
}
    class StopWatch {
        private long startTime;
        private long endTime;

        public long getStartTime() {
            return startTime;
        }

        public long getEndTime() {
            return endTime;
        }

        // 构造方法与类同名
        public StopWatch() {
            this.startTime = System.currentTimeMillis();
        }

        public void start() {
            this.startTime = System.currentTimeMillis();
        }

        public void stop() {
            this.endTime = System.currentTimeMillis();
        }

        public long getElapsedTime() {
            return this.endTime - this.startTime;
        }
    }

T5

import java.util.Scanner;

public class T5 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int a=input.nextInt();
        int b=input.nextInt();
        int c=input.nextInt();
        QuadraticEquation p = new QuadraticEquation(a,b,c);
        int q=p.getDiscriminant();
        System.out.print("judge:");
        System.out.println(q);
        if(q>0){
            System.out.print("root1:");
            System.out.println(p.getRoot1());
            System.out.print("root2:");
            System.out.println(p.getRoot2());
        }
        else if(q==0){
            System.out.print("root:");
            System.out.println(p.getRoot1());
        }
        else{
            System.out.println("The equation has no roots.");
        }
    }
}
class QuadraticEquation{
    private int a;
    private int b;
    private int c;
    public int getA(){
        return a;
    }
    public int getB(){
        return b;
    }
    public int getC(){
        return c;
    }
    public QuadraticEquation(int a,int b,int c){
        this.a=a;
        this.b=b;
        this.c=c;
    }
    public int getDiscriminant(){
        if(b*b-4*a*c>0)
            return 1;
        else if(b*b-4*a*c==0)
            return 0;
        else
            return -1;
    }
    public double getRoot1(){
        return (-b+Math.sqrt(b*b-4*a*c))/(2*a);
    }
    public double getRoot2(){
        return (-b-Math.sqrt(b*b-4*a*c))/(2*a);
    }
}

T6

在这里插入代码片import java.util.Scanner;

public class T6 {
    public static void main(String[] args) {
        Scanner input =new Scanner(System.in);
        double a=input.nextDouble();
        double b=input.nextDouble();
        double c=input.nextDouble();
        double d=input.nextDouble();
        double e=input.nextDouble();
        double f=input.nextDouble();
        LinearEquation p = new LinearEquation(a,b,c,d,e,f);
        if(p.isSolvable()==true){
            System.out.print("x:");
            System.out.println(p.getX());
            System.out.print("y:");
            System.out.println(p.getY());
        }
        else{
            System.out.println("The equation has no solution.");
        }
    }
}
class LinearEquation{
    private double a;
    private double b;
    private double c;
    private double d;
    private double e;
    private double f;
    public LinearEquation(double a,double b,double c,double d,double e,double f){
        this.a=a;
        this.b=b;
        this.c=c;
        this.d=d;
        this.e=e;
        this.f=f;
    }
    public double getA(){
        return a;
    }
    public double getB(){
        return b;
    }
    public double getC(){
        return c;
    }
    public double getD(){
        return d;
    }
    public double getE(){
        return e;
    }
    public double getF(){
        return f;
    }
    public boolean isSolvable(){
        if(a*d-b*c!=0){
            return true;
        }
        else
            return false;
    }
    public double getX(){
        return (e*d-b*f)/(a*d-b*c);
    }
    public double getY(){
        return(a*f-e*c)/(a*d-b*c);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值