《Java语言程序设计与数据结构》编程练习答案(第十章)

《Java语言程序设计与数据结构》编程练习答案(第十章)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

10.1

 public class book {
    public static void main(String[] args)
            throws Exception
    {
        Time t1 = new Time();
        Time t2 = new Time(555550000);
        System.out.printf("%d %d %d\n",t1.getHour(),t1.getMinute(),t1.getSecond());
        System.out.printf("%d %d %d\n",t2.getHour(),t2.getMinute(),t2.getSecond());
    }
}

class Time{
    private int hour;
    private int minute;
    private int second;
    public Time(){
        long ass = System.currentTimeMillis();
        long seconds = ass/1000;
        second = (int)seconds%60;
        seconds/=60;
        minute = (int)seconds%60;
        seconds/=60;
        hour = (int)seconds%24;
    }
    public Time(int h,int m,int s){
        hour = h;
        minute = m;
        second = s;
    }
    public Time(long ass){
        long seconds = ass/1000;
        second = (int)seconds%60;
        seconds/=60;
        minute = (int)seconds%60;
        seconds/=60;
        hour = (int)seconds%24;
    }
    int getHour(){
        return hour;
    }
    int getMinute(){
        return minute;
    }
    int getSecond(){
        return second;
    }
    public void setTime(long elapseTime){
        long seconds = elapseTime/1000;
        second = (int)seconds%60;
        seconds/=60;
        minute = (int)seconds%60;
        seconds/=60;
        hour = (int)seconds%24;
    }
}

10.10

public class book {
    public static void main(String[] args)
            throws Exception
    {
        Queue test = new Queue();
        for(int i=1;i<21;i++)
            test.enqueue(i);
        while(!test.empty())
            System.out.printf("%d ",test.dequeue());
    }
}

class Queue{
    private int[] element;
    private int size;
    public Queue(){
        element = new int[8];
        size = 0;
    }
    public void enqueue(int v){
        size++;
        if(size>element.length){
            int[] tmp = new int[2*size-1];
            for(int i=0;i<size-1;i++)
                tmp[i] = element[i];
            element = tmp;
        }
        element[size-1]=v;
    }
    public int dequeue(){
        int res = element[0];
        size--;
        for(int i=0;i<size;i++)
            element[i]=element[i+1];
        return res;
    }
    public boolean empty(){
        return size==0;
    }
    public int getSize(){
        return size;
    }
}

10.27

class MyStringBuilder1{
    private char[] element;
    private int length;
    public MyStringBuilder1(String s){
        length = s.length();
        element = new char[s.length()];
        for(int i=0;i<s.length();i++)
            element[i] = s.charAt(i);
    }
    public MyStringBuilder1 append(MyStringBuilder1 s){
        return new MyStringBuilder1(this.toString()+s.toString());
    }
    public MyStringBuilder1 append(int i){
        return append(new MyStringBuilder1(i+""));
    }
    public int length(){
        return length;
    }
    public char charAt(int index){
        if(index<length)
            return element[index];
        else {
            System.out.print("Damn you fool!");
            return '@';
        }
    }
    public MyStringBuilder1 toLowerCase(){
        return new MyStringBuilder1(this.toString().toLowerCase());
    }
    public MyStringBuilder1 substring(int begin,int end){
        return new MyStringBuilder1(this.toString().substring(begin,end));
    }
    public String toString(){
        return new String(element);
    }
}

10.28

class MyStringBuilder2{
    private String kernel;
    public MyStringBuilder2(){
        kernel = new String();
    }
    public MyStringBuilder2(char[] chars){
        kernel = new String(chars);
    }
    public MyStringBuilder2(String s){
        kernel = "";
        for(int i=0;i<s.length();i++)
            kernel+=s.charAt(i);
    }
    public MyStringBuilder2 insert(int offset,MyStringBuilder2 s){
        MyStringBuilder2 res = new MyStringBuilder2(this.toString().substring(0,offset)+s.toString()+this.toString().substring(offset));
        return res;
    }
    public MyStringBuilder2 reverse(){
        String tmp = "";
        int len = kernel.length();
        for(int i=0;i<this.kernel.length();i++)
            tmp+=kernel.charAt(len-1-i);
        return new MyStringBuilder2(tmp);
    }
    public MyStringBuilder2 substring(int begin){
        return new MyStringBuilder2(this.toString().substring(begin));
    }
    public MyStringBuilder2 toUpperCase(){
        return new MyStringBuilder2(this.toString().toUpperCase());
    }
    public String toString(){
        return kernel;
    }
}

你可能感兴趣的:(Java,Android)