JAVA初学——week7 第九章 对象和类 1星练习题

*9.3 (使用日期类 Date)

import java.util.Date;
public class UseTheDateClassDate_9_3 {
    public static void main(String[] args) {
        long l = 10000;
        Date day = new Date(l);
        System.out.println(l+":"+day.toString());
        for(int i = 0;i < 7;++ i){
            l *= 10;
            day.setTime(l);
            System.out.println(l+":"+day.toString());
        }
    }
}

*9.4 (使用随机类 Random)

import java.util.Random;
public class UseTheRandomClass_9_4 {
    public static void main(String[] args) {
        Random randpos = new Random(1000);
        System.out.println(String.format("%.0f",50 * randpos.nextDouble()));
    }
}

*9.5 (使用公历类 GregorianCalendar )

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.GregorianCalendar;
public class UseGregorianCalendarClass_9_5 {
    public static void main(String[] args) {
        GregorianCalendar day = new GregorianCalendar();
        int a = day.get(GregorianCalendar.YEAR);
        int b = day.get(GregorianCalendar.MONTH);
        int c = day.get(GregorianCalendar.DAY_OF_MONTH);
        System.out.println("修改前"+ a + " 年 " + b + "月" + c + "日");
        long l = 1234567898765L;
        day.setTimeInMillis(l);
        a = day.get(GregorianCalendar.YEAR);
        b = day.get(GregorianCalendar.MONTH);
        c = day.get(GregorianCalendar.DAY_OF_MONTH);
        System.out.println("修改后" + a + " 年 " + b + "月" + c + "日");
    }
}

*9.6 (秒表)

import java.util.Date;
import java.util.Random;
import java.util.Timer;
public class Stopwatch_9_6 {
    public static void main(String[] args) {
        Random randpos = new Random(100001);
        double n[] = new double[100000];
        for(int i = 0; i< 100000;++ i) n[i] = randpos.nextDouble();
        StopWatch a = new StopWatch();//开始计时
        //选择排序
        for(int i = 0;i < 100000 - 1;++ i){
            int smallest = i;
            for(int j = i + 1;j < 100000;++ j){
                if(n[j] < n[smallest]){
                    smallest = j;
                }
            }
            if(i != smallest){
                double tool = n[i];
                n[i] = n[smallest];
                n[smallest] = tool;
            }
        }
        a.stop();//结束计时
        System.out.println(a.getElapsedTime()+"ms");
    }
}
class StopWatch{
    private Date startTime;
    private Date endTime;
    public StopWatch(){
        startTime = new Date();
    }
    public void start(){
        startTime = new Date();
    }
    public void stop(){
        endTime = new Date();
    }
    public long getElapsedTime(){
        return endTime.getTime() - startTime.getTime();
    }
}

*9.10 (代数:二次方程式)

import java.util.Scanner;
public class AlgebraQuadraticEquation_9_10 {
    public static void main(String[] args) {
        System.out.print("对于二次方程 ax^2+bx+c=0 ,请输入a,b,c:");
        Scanner input = new Scanner(System.in);
        QuadraticEquation r = new QuadraticEquation();
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        if(r.getDiscriminant(a,b,c) > 0){
            System.out.println("r1 = " + r.getRoot1(a,b,c) + ", r2 = " + r.getRoot2(a,b,c));
        }else if(r.getDiscriminant(a,b,c) == 0){
            System.out.println("r1 = r2 = " + r.getRoot1(a,b,c));
        }else{
            System.out.println("The equation has no roots.");
        }
    }
}
class QuadraticEquation{
    private int a,b,c;
    public int getA(int a){//返回a
        return a;
    }
    public int getB(int b){//返回b
        return b;
    }
    public int getC(int c){//返回c
        return c;
    }
    public int getDiscriminant(int a,int b,int c){//返回判别式
        return (b * b - 4 * a * c);
    }
    public double getRoot1(int a,int b,int c){
        if(getDiscriminant(a,b,c) < 0){
            return 0;
        }
        return ((-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a));
    }
    public double getRoot2(int a,int b,int c){
        if(getDiscriminant(a,b,c) < 0){
            return 0;
        }
        return ((-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a));
    }
}

*9.11 (代数:2 x 2 的线性方程)

import java.util.Scanner;
public class linearEquation_9_11 {
    public static void main(String[] args) {
        System.out.print("Enter a,b,c,d,e,f:");
        LinearEquation q = new LinearEquation();
        Scanner input = new Scanner(System.in);
        q.setA(input.nextDouble());
        q.setB(input.nextDouble());
        q.setC(input.nextDouble());
        q.setD(input.nextDouble());
        q.setE(input.nextDouble());
        q.setF(input.nextDouble());

        if(!q.isSolvable()) System.out.println("The equation has no solution");
        else {
            System.out.println("x = " + q.getX() + " , y = " + q.getY());
        }
    }
}
class LinearEquation{
    private double a,b,c,d,e,f;
    //构造a,b,c,d,e,f
    public void setA(double x) { a = x; }
    public void setB(double x) { b = x; }
    public void setC(double x) { c = x; }
    public void setD(double x) { d = x; }
    public void setE(double x) { e = x; }
    public void setF(double x) { f = x; }

    //获取a,b,c,d,e,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;}

    //判断分母是否为0
    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、付费专栏及课程。

余额充值