5初级 - Java程序的基本结构-练习

133 阅读3分钟

3-1

package com.github.hcsp;

public class Main {
    // Create a static member "message" here to store the string "Hello"
    // 在这里创建一个名为"message"的静态成员变量,并存储字符串"Hello"

    public static String message = "hello";

    public static void main(String[] args) {
        System.out.println(message);
    }
}

3-2

package com.github.hcsp;

public class Main {
    public static String message = "Hello";

    public static void main(String[] args) {
        System.out.println(message);
        updateMessage();
        System.out.println(message);
    }

    public static void updateMessage() {
        Main.message = "hi";
        // Modify the static variable "message" to "Hi"
        // 将静态成员变量"message"的值改成"Hi"
    }
}

4-1

package com.github.hcsp;

import com.github.hcsp.pet.Cat;

public class Home {
    private static Cat cat;

    public static void main(String[] args) {
        System.out.println(cat);
        cat = newCat();
        System.out.println(cat);
    }

    public static Cat newCat() {
        // Create a new Cat instance and return it here
        // 在这里创建一个新的Cat实例并返回之
        return new Cat();
    }
}

4-2

package com.github.hcsp;

import com.github.hcsp.pet.Cat;

public class Home {
    public static void main(String[] args) {
        System.out.println(newCat("White"));
    }

    public static Cat newCat(String name) {
        // Create a new Cat instance, assign the name to its "name" member variable,
        // and return it here
        // 在这里创建一个新的Cat实例,将其成员变量"name"设为参数所指定的名字,并返回之
        Cat cat = new Cat();
        cat.name = name;
        return cat;
    }
}

4-3

package com.github.hcsp;

import com.github.hcsp.pet.Cat;

public class Home {
    public static Cat black;
    public static Cat white;

    public static void main(String[] args) {
        createTwoCats();
        System.out.println(black);
        System.out.println(white);
    }

    public static void createTwoCats() {
        // Create two new Cats named "Black" and "White"
        // then assign them to "black" and "white" static variable respectively
        // 在这里创建两个Cat实例,分别赋值给"black"和"white"静态变量,然后将它们的"name"成员变量分别设置为"black"和"White"
        black = new Cat();
        black.name = "Black";
        white = new Cat();
        white.name = "White";
    }
}

4-4

  • 创建一个实例方法
package com.github.hcsp.pet;

public class Cat {
    public String name;

    public void sayName() {
        // Print itself's name to standard output (System.out) here
        // 在这里将自己的名字打印到标准输出(System.out)
        System.out.println(this.name);
    }
}

4-5

  • cat.java
package com.github.hcsp.pet;

public class Cat {
    public String name;

    /** Return the name length of this cat. Return -1 if it's anonymous. 返回这只猫名字的长度。如果没有名字则返回-1。 */
    public int getNameLength() {
        // Fix the NullPointerException thrown in this method
        // 在本方法中,修复抛出的空指针异常(NullPointerException)
        if (name != null) {
            return name.length();
        } else {
            return -1;
        }

    }
}

4-6

package com.github.hcsp.pet;

public class Cat {
    private String name;

    // Create a constructor here
    // 在这里创建一个构造器
    public Cat(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat(" + name + ")";
    }
}

4-7

package com.github.hcsp.pet;

public class Cat {
    public String name;

    // Create two constructor here: Cat() and Cat(String name)
    // 在这里创建两个构造器:Cat()和Cat(String name)

    public Cat() {

    }

    public Cat(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat(" + name + ")";
    }
}

4-8

4-9

package com.github.hcsp;

import com.github.hcsp.pet.Cat;

public class Home {
    public static Cat cat1;
    public static Cat cat2;

    public static void main(String[] args) {
        createTwoCats();
        // We want to create two cats, not one cat with two references
        // Fix createTwoCats() method to make the problem output "1: Cat(Tom), 2: Cat(Tom),
        // cat1==cat2: false"
        // 我们希望创建两只猫,而不是一只猫的两个引用
        // 请修改createTwoCats()方法,使得程序输出"1: Cat(Tom), 2: Cat(Tom), cat1==cat2: false"
        System.out.println("1: " + cat1 + ", 2:" + cat2 + ", cat1==cat2: " + (cat1 == cat2));
    }

    public static void createTwoCats() {
        cat1 = new Cat("Tom");
        cat2 = new Cat("Tom");
    }
}

4-10

package com.github.hcsp;

import com.github.hcsp.pet.Cat;

public class Main {
    public static void main(String[] args) {
        Home home = new Home(new Cat("Tom"));
        Home deepCopy = deepCopy(home);

        // Complete deepCopy() method to make the program output "false"
        // 补全deepCopy()方法,使得程序输出"false"
        System.out.println(home.cat == deepCopy.cat);
    }
    // Return the deep copy of a Home instance
    // 将传入的Home实例进行深拷贝,返回深拷贝后的实例
    public static Home deepCopy(Home home) {
        Cat cat = new Cat("Tom");
        Home home1 = new Home(cat);
//        home.cat = cat1;
        return home1;
    }
}

5-1

package com.github.hcsp;

public class Main {
    public static void main(String[] args) {
        System.out.println(relation(1, 2));
        System.out.println(relation(2, 1));
        System.out.println(relation(2, 2));
    }

    // Fix the compilation error
    // Return "a<b" if a < b, "a>b" if a > b, "a=b" otherwise
    // 修复编译错误
    // 在a<b时返回字符串"a<b",在a>b时返回字符串"a>b",否则返回"a=b"
    public static String relation(int a, int b) {
        String sign;
        if (a > b) {
            sign = ">";
        } else if (a < b) {
            sign = "<";
        } else {
            sign = "=";
        }

        return "" + a + sign + b;
    }
}