java语言程序设计基础篇第十章编程练习题

懒的我不要不要的,真不知道怎么写的这点代码

1

package yongheng;
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    public static void main(String agrs[]){
        Time t1 = new Time();
        System.out.println(t1.getHour() + " " + t1.getMinute() + " " + t1.getSecond());
        Time t2 = new Time(555550000);
        System.out.println(t2.getHour() + " " + t2.getMinute() + " " + t2.getSecond());
    }
}  

class Time{
    private long second;
    private long minute;
    private long hour;

    public Time(){
        long t = System.currentTimeMillis()/1000;
        second = t % 60;
        t /= 60;
        minute = t % 60;
        t /= 60;
        hour = t % 24;
    }

    public Time(long t){
        t /= 1000;
        second = t % 60;
        t /= 60;
        minute = t % 60;
        t /= 60;
        hour = t;
    }

    public Time(long hour, long minute, long second){
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    public long getSecond(){
        return second;
    }

    public long getMinute(){
        return minute;
    }

    public long getHour(){
        return hour;
    }

    public void setTime(long elapseTime){
        elapseTime /= 1000;
        this.second = elapseTime%60;
        elapseTime /= 60;
        this.minute = elapseTime%60;
        elapseTime /= 60;
        this.hour = elapseTime;
    }
}

2
。。。。

3

package yongheng;
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    public static void main(String agrs[]){
        MyInteger n = new MyInteger(10);
        System.out.println(n.isPrime());
    }
}  

class MyInteger{
    private int value;

    public MyInteger(int value){
        this.value = value;
    }

    public int getMyInteger(){
        return value;
    }

    public boolean isEven(){
        return value % 2 == 0 ? true : false;
    }

    public boolean isOdd(){
        return value % 2 == 0 ? false : true;
    }

    public boolean isPrime(){
        if(value == 1) return false;

        for(int i = 2; i*i <= value; ++i){
            if(value%i == 0)
                return false;
        }
        return true;
    }

    public static boolean isEven(int num){
        return num%2 == 1 ? false : true;
    }

    public static boolean isOdd(int num){
        return num%2 == 1 ? true : false;
    }

    public static boolean isPrime(int num){
        for(int i = 2; i*i <= num; ++i)
            if(num%i == 0)
                return false;
        return num == 1 ? false : true;
    }

    public boolean equals(int num){
        return this.value == num;
    }

    public boolean equals(MyInteger num){
        return this.value == num.getMyInteger();
    }

    public static int parseInt(char[] str){
        return Integer.parseInt(str.toString());
    }

    public static int parseIne(String str){
        return Integer.parseInt(str);
    }
}


4

package yongheng;
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    public static void main(String agrs[]){
        MyPoint p1 = new MyPoint();
        MyPoint p2 = new MyPoint(10,30.5);
        System.out.println(p1.distance(p2));
    }
}  

class MyPoint{
    private double x,y;

    public MyPoint(){
        x = 0;
        y = 0;
    }

    public MyPoint(double x, double y){
        this.x = x;
        this.y = y;
    }

    public double getX(){
        return x;
    }

    public double getY(){
        return y;
    }

    public double distance(MyPoint p){
        return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
    }

    public double diatance(double x, double y){
        return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
    }
}

5 6 7
。。。。。。

8

class Tax{
    private int filingStatus;
    private int[][] brackets;
    private double[] rates;
    double taxableIncome;
    public static final int SINGLE_FILER = 0;
    public static final int MARRIED_JOINTLY_OR_QUALIFYING_WIDOW = 1;
    public static final int MARRIED_SEPARATELY = 2;
    public static final int HEAD_OF_HOUSEHOLD = 3;

    public Tax(){
        filingStatus = 0;
        brackets = new int[4][5];
        //这里可能理解错了,我猜纳税等级应该就后边表里的那些钱的区间吧
        for(int i = 0; i < 4; ++i)
            for(int j = 0; j < 5; ++j)
                brackets[i][j] = j;
        rates = new double[5];
        rates[0] = 0.15;
        rates[1] = 0.275;
        rates[2] = 0.305;
        rates[3] = 0.355;
        rates[4] = 0.391;
        taxableIncome = 0;
    }

    public Tax(int filinfStatus, int[][] brackets, double[] rates, double taxableIncome){
        this.filingStatus = filinfStatus;
        this.brackets = brackets;
        this.rates = rates;
        this.taxableIncome = taxableIncome;
    }

    public int getFilingStatus(){
        return filingStatus;
    }

    public void setFilingStatus(int filingStatus){
        this.filingStatus = filingStatus;
    }

    public double getTax(){
        return taxableIncome;
    }

    public int[][] getBrackets(){
        return brackets;
    }

    public void setBrackets(int[][] brackets){
        this.brackets = brackets;
    }

    public double[] getRates(){
        return rates;
    }

    public void setRates(double[] rates){
        this.rates = rates;
    }
}

9

class Course{
    private String courseName;
    private String[] students = new String[100];
    private int numberOfStudents;

    public Course(String courseName){
        this.courseName = courseName;
    }

    public void addStudent(String student){
        if(students.length == numberOfStudents){
            String[] temp = new String[numberOfStudents << 1];
            System.arraycopy(students, 0, temp, 0, students.length);
            students = temp;
        }
        students[numberOfStudents++] = student;
    }

    public String[] getStudents(){
        return students;
    }

    public int getNumberOfStudents(){
        return numberOfStudents;
    }

    public String getCourseName(){
        return courseName;
    }

    public void dropStudent(String student){
        for(int i = 0; i < numberOfStudents; ++i){
            if(students[i] == student){
                for(int j = i+1; j < numberOfStudents; ++j,++i){
                    students[i] = students[j];
                }
                break;
            }
        }
        --numberOfStudents;
    }

    public void clear(){
        students = null;
        numberOfStudents = 0;
    }
}

10

package yongheng;
import java.util.Scanner;

public class Main {
    public static void main(String agrs[]){
        Scanner cin = new Scanner(System.in);
        Queue que = new Queue();
        for(int i = 0; i < 20; ++i){
            que.enqueue(i);
        }

        int time = que.getSize();
        int index = 0;
        for(int i = 0; i < time; ++i){
            System.out.print(que.dequeue(index) + " ");
        }
        System.out.println("\n" + que.empty());
    }
}  

class Queue{
    private int[] element = null;
    private int size = 0;
    private int numberOfElement = 0;

    public Queue(){
        element = new int[8];
        numberOfElement = 8;
        size = 0;
    }

    public void enqueue(int v){
        if(numberOfElement == size){
            numberOfElement = element.length << 1;
            int[] temp = new int[numberOfElement];
            System.arraycopy(element, 0, temp, 0, element.length);
            element = temp;
        }
        element[size++] = v;
    }

    public int dequeue(int index){
        int res = element[index];
        for(int i = index, j = index+1; j < size; ++i,++j){
            element[i] = element[j];
        }
        --size;
        return res;
    }

    public boolean empty(){
        return size == 0 ? true : false;
    }

    public int getSize(){
        return size;
    }
}

11

class Circle2D{
    private double x;
    private double y;
    private double radius;

    public Circle2D(){
        x = y = 0;
        radius = 1;
    }

    public Circle2D(double x, double y, double radius){
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    public double getArea(){
        return Math.PI*radius*radius;
    }

    public double getPerimeter(){
        return Math.PI*2.0*radius;
    }

    public boolean contains(double x, double y){
        double dis = Math.sqrt((x-this.x)*(x-this.x) + (y-this.y)*(y-this.y));
        if(dis < radius) return true;
        return false;
    }

    public boolean contains(Circle2D circle){
        double tx = circle.getX();
        double ty = circle.getY();
        double tradius = circle.getRadius();
        double dis = Math.sqrt((tx-this.x)*(tx-this.x) + (ty-this.y)*(ty-this.y));
        if((this.radius-tradius) > dis) return true;
        return false;
    }

    public boolean overlaps(Circle2D circle){
        double tx = circle.getX();
        double ty = circle.getY();
        double tradius = circle.getRadius();
        double maxRadius,minRadius;
        double dis = Math.sqrt((tx-this.x)*(tx-this.x) + (ty-this.y)*(ty-this.y));
        if((this.radius+tradius) < dis) return false;
        if(tradius > this.radius){
            maxRadius = tradius;
            minRadius = this.radius;
        }else{
            maxRadius = this.radius;
            minRadius = tradius;
        }
        if((maxRadius-minRadius) > dis) return false;
        return true;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double getRadius() {
        return radius;
    }

}

12


import java.util.Scanner;
public class Main {

    public static void main(String[] args){
    }
}

class Triangle2D{
    private MyPoint p1,p2,p3;

    public Triangle2D(){
        p1 = new MyPoint(0,0);
        p2 = new MyPoint(1,1);
        p3 = new MyPoint(2,5);
    }

    public Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3){
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public double getArea(){
        double a = p1.distance(p2);
        double b = p1.distance(p3);
        double c = p2.distance(p3);
        double p = (a+b+c)/2.0;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }

    public double getPerimeter(){
        return (p1.distance(p2) + p1.distance(p3) +p2.distance(p3));
    }

    public boolean contains(MyPoint p){
        //编程练习题3.3
        //三角形一点与p连线,与另外两点构成的直线求交点
        //求得交点后判断交点是否在另外两点构成的线段上
        //每个顶点都这样求一次
        //只要有一次求出的交点在另外两点构成的线段上
        //则 不包含此点
        return false;
    }

    public boolean contains(Triangle2D t){
        if(this.contains(t.getP1()) && this.contains(t.getP2()) && this.contains(t.getP3()))
            return true;
        return false;
    }

    public boolean overlaps(Triangle2D t){
        //枚举两个三角形的每条边
        //如果两三角形各有一条或一条以上的边相交
        //则重叠
        return false;
    }

    public MyPoint getP1() {
        return p1;
    }

    public void setP1(MyPoint p1) {
        this.p1 = p1;
    }

    public MyPoint getP2() {
        return p2;
    }

    public void setP2(MyPoint p2) {
        this.p2 = p2;
    }

    public MyPoint getP3() {
        return p3;
    }

    public void setP3(MyPoint p3) {
        this.p3 = p3;
    }

}

class MyPoint{
    private double x,y;

    public MyPoint(){
        x = 0;
        y = 0;
    }

    public MyPoint(double x, double y){
        this.x = x;
        this.y = y;
    }

    public double getX(){
        return x;
    }

    public double getY(){
        return y;
    }

    public double distance(MyPoint p){
        return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
    }

    public double diatance(double x, double y){
        return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
    }
}
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值