Thinking In Java习题作业集

本文档记录了作者学习Java过程中的重点内容,包括从基础知识到高级特性,如类的创建与使用、继承与多态、异常处理等,并通过实例展示了Java语言的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要学习Thinking in Java

后面的习题以及例子

第七章 Access Control



第六章 Initialization & Cleanup

Integer[] a = new Integer[rand.nextInt(20)];
it's only an array of references, and the initialization is not complete until the reference itself itself is initialized by creating a new Integer object.

public class ChapterSix {
    public static void main(String[] args) {

        // exercise 15
        // Cats cats = new Cats();

        /* exercise 16
        String[] str = {new String("wang"), new String("rl")};
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
        }
        */

        // exercise 17 & 18
        // Str[] arr = {new Str("wang"), new Str("rl")};

        // exercise 19
        // vararg("wang", "rl");
        // vararg(new String[]{new String("wangrl"), new String("rl")});

        // exercise 20
        // main(1, 2, 3);

        /* exercise 21
        for (Currency c : Currency.values()) {
            System.out.println(c + ", ordinal " + c.ordinal());
        }
        */

        /* exercise 22
        Currency one = Currency.ONE;
        Currency two = Currency.TWO;
        Currency three = Currency.THREE;

        output(one);
        output(two);
        output(three);
        */

    }

    public static void output(Currency c) {
        switch (c) {
            case ONE: {
                System.out.println("one " + c.ordinal());
                break;
            }
            case TWO: {
                System.out.println("two " + c.ordinal());
            } case THREE: {
                System.out.println("three " + c.ordinal());
            }

        }
    }


    public enum Currency{
        ONE, TWO, THREE, FOUR, FIVE, SIX
    }

    public static void main(Object... args) {
        for (Object a : args) {
            System.out.print(" " + a);
        }
    }


    public static void vararg(String... args) {
        for(String str : args) {
            System.out.print(" " + str);
        }
    }
}

class Str {
    String str;
    public Str(String str) {
        System.out.println("constructor str = " + str);
    }
}

class Cat {
    public Cat(int maker) {
        System.out.println("Cat maker " + maker);
    }
}

class Cats {
    Cat cat1;
    Cat cat2;
    {
        cat1 = new Cat(1);
        cat2 = new Cat(2);
        System.out.println("cat1 & cat2 initialized.");
    }
    public Cats() {
        System.out.println("Cats initialized");
    }
}

/**
 * output
 * exercise 22
 * one 0
 * two 1
 * three 1
 * three 2
 *
 * exercise 21
 * ONE, ordinal 0
 * TWO, ordinal 1
 * THREE, ordinal 2
 * FOUR, ordinal 3
 * FIVE, ordinal 4
 * SIX, ordinal 5
 *
 *
 * exercise 20
 * 1 2 3
 *
 * exercise 19
 *  wang rl wangrl rl
 *
 * exercise 17 & 18
 * constructor str = wang
 * constructor str = rl
 *
 * exercise 16
 * wang
 * rl
 *
 * exercise 15
 * Cat maker 1
 * Cat maker 2
 * cat1 & cat2 initialized.
 * Cats initialized
 */

/**
 * Created by wang on 17-8-20.
 */
public class ChapterSix {
    public static void main(String[] args) {
        /* exercise 3 & 4
        Cat cat = new Cat();
        Cat cat1 = new Cat("cry");
        */

        /* exercise 5 & 6
        Dog dog = new Dog();
        dog.bark(3);
        dog.bark("bark");
        dog.bark("bark", 4);
        dog.bark(4, "bark");
        */

        // exercise 7
        // D d = new D();

        // exercise 8
        // D d1 = new D();
        // d1.first();

        /* exercise 9
        Two two = new Two(1, 2);
        */

        /* exercise 10 & 11
        new Book();
        System.gc();

        Book book = new Book();
       */
        /* exercise 12
        Tank tank = new Tank();
        new Tank();

        // ensure gc collect
        System.gc();
        */

        /* exercise 13
        System.out.println("Inside main()");
        // Cups.cup1.f(99);
        */

        /* exercise 14
        Str str1 = new Str();
        Str.printStatic();
        */
    }
    // static Cups cup1 = new Cups();
    // static Cups cup2 = new Cups();
}
class Str {
    static String str1 = "string definition";
    static String str2;
    static {
        str2 = "string block";
    }
    public static void printStatic() {

        System.out.println("printStatic " + str1 + " " + str2 + " ");
    }
}


class Cup {
    Cup(int maker) {
        System.out.println("Cup(" + maker + ")");
    }
    void f(int marker) {
        System.out.println("f(" + marker + ")");
    }
}

class Cups {
    static Cup cup1;
    static Cup cup2;
    static {
        cup1 = new Cup(1);
        cup2 = new Cup(2);
    }
    Cups() {
        System.out.println("Cups()");
    }
}



class Tank {
    boolean isEnpty = false;
    public Tank() {

    }
    protected void finalize() {
        if(!isEnpty) {
            isEnpty = true;
            System.out.print("true");
        }
    }


}

class Book {
    protected void finalize() {
        System.out.println("book finalize");
    }
}

class Two {
    public Two(int i) {
        System.out.println(" " + i);
    }
    public Two(int i, int j) {
        this(i);
        System.out.println(" " + i + " " + j);
    }
}

class D {
    public void first() {
        second();
        this.second();
    }

    private void second() {
        System.out.println("second");
    }
}


class Dog {
    public void bark(int i) {
        System.out.println("Dog bark " + i + " times.");
    }
    public void bark(String str) {
        System.out.println("Dog " + str);
    }
    public void bark(String str, int i) {
        System.out.println("Dog " + i + " " + str);
    }
    public void bark(int i, String str) {
        System.out.println("Dog " + i + " " + str);
    }
}


class Cat {
    public Cat() {
        System.out.println("Cat constructor");
    }
    public Cat(String str) {
        System.out.println("Cat constructor " + str);
    }
}

/**
 * output
 * exercise 14
 * printStatic string definition string block
 *
 * exercise 13
 * Cup(1)
 * Cup(2)
 * Cups()
 * Inside main()
 *
 * output
 * exercise 12
 *
 *
 * exercise 10 & 11
 * book finalize
 *
 * exercise 9
 *  1
 *  1 2
 *
 * exercise 8
 * second
 * second
 *
 * exercise 6
 * Dog 4 bark
 * Dog 4 bark
 *
 * exercise 5
 * Dog bark 3 times.
 * Dog bark
 *
 * exercise 4
 * Cat constructor
 * Cat constructor cry
 *
 * exercise 3
 * Cat constructor
 */
/**
 * Created by wang on 17-8-19.
 */


public class ChapterSix {

    public static void main(String[] args) {
        /* exercise 1
        Init i = new Init();
        System.out.println(i.s);
        */

        /* exercise 2
        Init i = new Init();
        i.s = "wan";

        Init s = new Init("wan");
        System.out.println("i = " + i.s + ", s = " + s.s);
        */

        /* exercise 3 & 4
        Init i = new Init();
        Init j = new Init("s");
        */


    }
}

class Init {
    String s;
    public Init(String s) {
        System.out.println("second constructor");
        this.s = s;
    }

    public Init() {
        System.out.println("default constructor");
    }
}

/**
 * output
 * exercise 4
 * default constructor
 * second constructor
 *
 * exercise 3
 * default constructor
 *
 * exercise 2
 * i = wan, s = wan
 *
 * exercise 1
 * null
 *
 */

第五章 Controlling Execution

import java.util.Random;

/**
 * Created by wang on 17-8-19.
 */
public class ChapterFour {
    public static void main(String[] args) {
        /* exercise 1
        for (int i = 1; i <= 100; i++) {
            System.out.print("" + i + " ");
        }
        */
        /** exercise 2 & 3
         * infinite loop
         * while(true)
         *
        Random rand = new Random(47);
        for (int i = 0; i < 25; i++) {
            int k = 0;
            if (i == 2) {
                k = rand.nextInt(50);
            } else {
                int n = rand.nextInt(50);
                if (n > k) {
                    System.out.print("" + n + " ");
                }
            }
        }
         */

        /* exercise 4
        for (int i = 2; i < 100; i++) {
            boolean isPrime = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0)
                     isPrime = false;
            }
            if (isPrime)
                System.out.print("" + i + " ");
        }
        */

        /* exercise 5
        int i1 = 0x40;
        int i2 = 0x41;
        */

        /* exercise 6
        System.out.println("" + IfElse.test(10, 5, 0, 20));
        */

        /* exercise 7
        for (int i = 1; i <= 100; i++) {
            if (i == 99) break;
            System.out.print("" + i + " ");
        }
        */

        /* exercise 8
        for (int i = 'a'; i <= 'c'; i++) {
            switch (i) {
                case 'a':
                    System.out.println(i);
                    break;
                case 'b':
                    System.out.println(i);
                default:
                    System.out.println(i);
            }
        }
        */

        /* exercise 9
        System.out.print(1 + ", " + 1 + ", ");
        int a = 1;
        int b = 1;
        for (int i = 3; i <= 5; i++) {
            int c = a+b;
            System.out.print(c + ", ");
            a = b;
            b = c;
        }
        */

        for (int i = 1000; i < 10000; i++) {
            int a = i / 1000;
            int b = i % 1000;
            int c = i % 100;
            int d = i % 10;
        }
    }
}

class IfElse {
    static int test(int testval, int target, int begin, int end) {
        if (testval > end) return 0;
        if (testval < begin) return 0;
        if (testval > target) {
            return 1;
        } else if (testval < target){
            return -1;
        } else {
            return 0;
        }
    }
}

/**
 * output
 * exercise 9
 * 1, 1, 2, 3, 5,
 *
 * exercise 8
 * 97
 * 98
 * 98
 * 99
 *
 * exercise 7
 * 1 ... 99
 *
 * exercise 6
 * 1
 *
 * exercise 5
 *
 * exercise 4
 * 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
 *
 * exercise 2 & 3
 * 8 5 11 11 29 18 22 7 38 28 1 39 9 28 48 11 20 8 16 40 11 22 4
 *
 * exercise 1
 * 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 */

第四章 Everything Is an Object

1. 基本类型

Boolean Character Byte Short Integer Long Float Double Void

Character ch = new Character('x');

2. 创建对象

class ATypeName { /* Class body goes here */}
ATypeName a = new ATypeName();
import java.util.Date;
import java.util.Random;

import static util.Print.print;


/**
 * Created by wang on 17-8-19.
 */
public class ChapterTwo {
    /* exercise 1
    public static void main(String[] args) {
        print("Hello, it's: ");
        print(new Date());
    }
    */
    /* exercise 2
    public static void main(String[] args) {
        Tank t1 = new Tank();
        Tank t2 = new Tank();
        t1.level = 10;
        t2.level = 20;
        t1 = t2;
        System.out.println("t1 = " + t1.level);
        System.out.println("t2 = " + t2.level);
    }
    */

    /*
    static void f(Tank y) {
        y.level = 10;
    }
    public static void main(String[] args) {
        Tank t = new Tank();
        t.level =20;
        System.out.println("t = " + t.level);
        f(t);
        System.out.println("t = " + t.level);
    }
    */

    /*
    public static void main(String[] args) {
        double d = 100;
        double t = 10;
        double v = d/t;
        System.out.println("v = " + v);
    }
    */

    /* exercise 5 & exercise 6
    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.name = "spot";
        d1.says = "Ruff!";
        Dog d2 = new Dog();
        d2.name = "scruffy";
        d2.says = "Wurf!";
        System.out.println("d1 name = " + d1.name + ", says = " + d1.says);
        System.out.println("d2 name = " + d2.name + ", says = " + d2.says);
        Dog d3 = new Dog();
        d3.name = "snna";
        d3.says = "Wow!";
        d1 = d3;
        System.out.print(d1.equals(d3));
    }
    */

    /* exercise 7
    public static void main(String[] args) {
        Random rand = new Random(47);
        int number = rand.nextInt();
        if (number % 2 == 0) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }
    }
    */

    /* exercise 8
    public static void main(String[] args) {
        long octal = 01234;
        long hex = 0x1234;
        System.out.println("octal = " + Long.toBinaryString(octal) +
                ", hex = " + Long.toBinaryString(hex));
    }
    */

    /* exercise 9
    public static void main(String[] args) {
        System.out.println("smallest float: " + Float.MIN_VALUE);
        System.out.println("smallest double: " + Double.MIN_VALUE);
        System.out.println("largest float: " + Float.MIN_VALUE);
        System.out.println("largest double: " + Double.MAX_VALUE);
    }
    */

    /* exercise 10
    public static void main(String[] args) {
        int i1 = 0x40;
        int i2 = 0x41;
        i1 |= i2;
        System.out.println("i1 = " + i1);
    }
    */

    /* exercise 11 & 12 & 13
    public static void main(String[] args) {
        int i1 = 0x10;
        i1 = i1 >> 1;
        System.out.println("signed right-shift: " + Integer.toBinaryString(i1));

        int i2 = 0xff;
        i2 = i2 >>> 1;
        System.out.println("unsigned right-shift: " + Integer.toBinaryString(i2));

        char c = 'c';
        System.out.println(Integer.toBinaryString(c));
    }
    */

    public static void main(String[] args) {
        String s1 = "wang";
        String s2 = "rl";
        System.out.println("s1 == s2: " + s1 == s2);
        System.out.println("s1 != s2: " + s1 != s2);
        System.out.println("s1.equal(s2): " + s1.equals(s2));
    }
}

class Dog {
    String name;
    String says;
}

class Tank {
    int level;
}

/**
 * exercise 14
 * false
 * true
 * s1.equal(s2): false
 *
 * exercise 12 & 13
 * signed right-shift: 1000
 * unsigned right-shift: 1111111
 * 1100011
 *
 * exercise 11
 * signed right-shift: 1000
 *
 * exercise 10
 * i1 = 65
 *
 * exercise 9
 * smallest float: 1.4E-45
 * smallest double: 4.9E-324
 * largest float: 1.4E-45
 * largest double: 1.7976931348623157E308
 *
 * exercise 8
 * octal = 668, hex = 4660
 * octal = 1010011100, hex = 1001000110100
 *
 * exercise 7
 * false
 *
 * exercise 6
 * true
 *
 * exercise 5
 * d1 name = spot, says = Ruff!
 * d2 name = scruffy, says = Wurf!
 *
 * exercise 4
 * v = 10.0
 *
 * exercise 3
 * t = 20
 * t = 10
 *
 * exercise 2
 * t1 = 20
 * t2 = 20
 *
 * output
 * exercise 1
 * Hello, it's:
 * Sat Aug 19 14:23:28 CST 2017
 */

第一章 Introduction

1. 预备知识程序的基本语法

2. 源码


第二章 Introduction to Objects

1. The progress of abstraction

2. An object has an interface

Creating abstract data types(class) is a fundamental concept in object-oriented programming.

The entity is the object, and each object belongs to a particular class that defines its characteristic and behaviors.

The requests you can make of an object are defined by its interface, and the type is what determines the interface.

3. 创建对象 ,接口调用

Light light = new Light();

light.on();

4. Inheritance

class Square extends Shape

5. polymorphism

void doSomething(Shape shape) {
    shape.draw();
    shape.erase();
}
6. 容器和泛型容器

ArrayList<Shape> shapes = new ArrayList<>();

7. 对象创建以及回收机制

8. 错误处理

9. Concurrent

10. Client/Server Programming

11. javadoc是个非常实用和重要的工具,生成帮助文档,可以通过浏览器阅读。


第三章 Everything Is an Object

import java.util.Date;

/**
 * Created by wang on 17-8-18.
 */
public class ChapterOne {
    /* exercise 1
    private int i;
    private char c;
    public static void main(String[] args) {
        ChapterOne object = new ChapterOne();
        System.out.println(object.i);
        System.out.println(object.c);
    }
    */

    /* exercise 2
    public static void printHello() {
        System.out.println("hello, world");
    }
    public static void main(String[] args) {
        printHello();
    }
    */

    /*
    public static void main(String[] args) {
        ATypeName a = new ATypeName();
    }
    */

    /* exercise 4 & exercise 5
    public static void main(String[] args) {
        DataOnly data = new DataOnly();
        data.i = 47;
        data.d = 1.1;
        data.b = false;
        if (!data.b) {
            System.out.println("" + (data.i + data.d));
        }
    }
    */

    /*
    static int storage(String s) {
        return s.length() * 2;
    }
    public static void main(String[] args) {
        System.out.println("" + storage("wangrl"));
    }
    */
    /* exercise 7
    public static void main(String[] args) {
        Incrementable.increment();
        System.out.println("" + StaticTest.i);
    }
    */

    /* exercise 8
    public static void main(String[] args) {
        StaticTest s1 = new StaticTest();
        StaticTest s2 = new StaticTest();
        s1.i++;
        System.out.println("s1.i = " + s1.i);
        System.out.println("s2.i = " + s2.i);
    }
    */

    /* exercise 9
    public static void main(String[] args) {
        Character ch = 'x';
        char c = ch;
        Integer in = 4;
        int i = in;
        System.out.println("Character = " + ch + ", char = " + c);
        System.out.println("Integer = " + in + ", int = " + i);
    }
    */

    /* exercise 10
    public static void main(String[] args) {
        String s1 = args[0];
        String s2 = args[1];
        String s3 = args[2];
        System.out.println(s1 + s2 + s3);
    }
    */

    /* exercise 11
    public static void main(String[] args) {
        AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
        a.changeTheHueOfTheColor(40);
        System.out.print("" + a.anIntegerRepresentingColors);
    }
    */

    /* exercise 12
        javadoc
     */

    /**
     * exercise 13
     * Use html elements.
     */

    /**
     * exercise 14
     * Use html.
     */

    /**
     * static method to generate a sentence.
     */
    /* exercise 15
    public static void printHello() {
        System.out.println("hello, world");
    }
    public static void main(String[] args) {
        printHello();
    }
    */

    /**
     * exercise 16
     * generate a html javadoc file.
     */



}


/** The first Thinking in Java example program.
 * Displays a string and today's date.
 * @author Bruce Eckel
 * @author www.MindView.net
 * @version 4.0
 */
class HelloDate {
    /** Entry point to class & application
     * @param args array of string arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello, it's: ");
        System.out.println(new Date());
    }
}



class AllTheColorsOfTheRainbow {
    int anIntegerRepresentingColors;
    void changeTheHueOfTheColor(int newHue) {
        anIntegerRepresentingColors = newHue;
    }
}

class Incrementable {
    static void increment() {
        StaticTest.i++;
    }
}

class StaticTest {
    static int i = 47;
}

class DataOnly {
    int i;
    double d;
    boolean b;
}

class ATypeName {

}

/**
 * exercise 11
 * 40
 *
 * exercise 10
 * hello,wangrl
 *
 * exercise 9
 * Character = x, char = x
 * Integer = 4, int = 4
 *
 * exercise 8
 * s1.i = 48
 * s2.i = 48
 *
 * exercise 7
 * 48
 *
 * exercise 6
 * 12
 *
 * exercise 4 & 5
 * 48.1
 *
 * exercise 2
 * hello, world
 *
 * exercise 1
 * 0
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值