文章目录:
  1. Question 1
  2. Question 2
  3. Question 3
  4. Question 4
  5. Question 5
  6. Question 6
  7. Question 7
  8. Question 8
  9. Question 9
  10. Question 10
  11. Question 11
  12. Question 12
  13. Question 13
类我已经建好了,但是老师我为什么没有对象啊()

Question 1

代码:

package cc.lixu.oo01;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("24373099 李旭");
        Scanner s = new Scanner(System.in);
        while(!s.nextLine().equals("QUIT")){
            System.out.println("Hello, World!");
            s = new Scanner(System.in);
        }
        System.out.println("----- Good Bye! -----");
        System.exit(0);
    }
}

运行截图:

image-20250918192147960

Question 2

代码:

package cc.lixu.oo01;

public class Test {
    static byte a;static short b;static int c;static long d;static boolean e;static char f;static float g;static double h;
    public static void main(String[] args) {

        System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f+" "+g+" "+h);
    }
}

运行结果:

0 0 0 0 false   0.0 0.0

表格:

基本类型默认值基本类型默认值
byte(byte)0booleanfalse
short(short)0char'\u0000'
int0float0.0f
long0Ldouble0.0d

Question 3

代码:

package cc.lixu.oo01;

public class FindMinMax {
    public static void main(String[] args) {
        double temp, max, min;
        double d1 = 1, d2 = -9.9, d3 = 96.9;
        max = d1>d2 ? d1 : d2;
        max = max>d3 ? max : d3;
        min = d1<d2 ? d1 : d2;
        min = min<d3 ? min : d3;
        System.out.println("max = " + max);
        System.out.println("min = " + min);
    }
}

运行截图:

image-20250918193503451

Question 4

运行截图:

image-20250918193730716

运行流程:

执行main函数{
    运行text1(0){
        运行print("1"){
            输出字符串"1"
        }
        返回(0<1)的结果 1
    }
    运行text2(2){
        运行print("2"){
            输出字符串"2"
        }
        返回(2<2)的结果 2
    }
    由于&&式收到0,逻辑运算符test1(0) && test2(2) && test3(2)结束运算,返回flase
    boolean变量 b = false;
    "b is " + b = "b is flase"
    运行print("b is flase"){
        输出字符串"b is flase"
    }
}

Question 5

(1) Student.java

(2) Student.classExam.class

(3) 运行结果如下:

运行截图:

image-20250918195029102

Question 6

运行截图:

image-20250918200339418

代码:

package cc.lixu.oo01;

class TwoDimensionArray {
    public static void main(String[] args) {
        int[][] b = { { 11 }, { 21, 22 }, { 31, 32, 33 } };
        int sum = 0;
        b[0][0] = 1000;
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++)
                sum += b[i][j];
        }
        System.out.println("b[0][0] = " + b[0][0] + '\n' +
                "sum = " + sum + '\n' +
                "b.lengh = " + b.length);

        int[][] arr1 = new int[3][4];
        int[][] arr2 = new int[3][];
        int[][] arr3 = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
        int i, j, k = 0;

        // 为arr1[][]赋值,从上到下,从左到右,0-11
        for (i = 0; i < 3; i++) {
            // arr1为3行4列
            for (j = 0; j < 4; j++) {
                arr1[i][j] = i*4 + j + 1;
            }
        }

        // 为arr2[][]开辟内存空间,arr2每一行是变长的,元素个数为3、4、5
        for (i = 0; i < 3; i++) {
            // 参考开辟内存空间:new int[2],开辟2个int的空间
            arr2[i] = new int[i+3];
        }

        // arr2[][]赋值,从上到下,从左到右,从0开始递增
        int t = 0;
        for (i = 0; i < 3; i++) {
            for (j = 0; j < arr2[i].length; j++) {
                arr2[i][j] = t;
                t++;
            }
        }

        // 输出arr1
        System.out.println(" arr1:");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 4; j++) {
                System.out.print(arr1[i][j] + " ");
                //参考:System.out.print(" " + arr1[i][j]);
            }
        }
        System.out.println();

        // 输出arr2
        System.out.println(" arr2:");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < arr2[i].length; j++) {
                System.out.print(arr2[i][j] + " ");
            }
        }
        System.out.println();

        // 输出arr3
        System.out.println(" arr3:");
        for (i = 0; i < 3; i++) {
            // arr3为3行3列
            for (j = 0; j < 3; j++) {
                System.out.print(arr3[i][j]+" ");
            }
        }
    }
}

Question 7

输出结果:Jeep好好

解释原因:

for循环从1到4,

结束循环。

Question 8

代码:

package cc.lixu.oo01;

import java.util.Scanner;

public class shuzifangzhen {
    public static void main(String[] args) {
        Scanner x = new Scanner(System.in);
        int n = x.nextInt();
        for (int i = 1; i <= n*n; i++){
            System.out.print(i + " ");
            if(i%n == 0) System.out.println();
        }
    }
}

运行截图:

image-20250918201916721

image-20250918202051878

Question 9

我的代码:

public static int[] qsort(int[] arr){
    if(arr == null) return null;
    if(arr.length == 0) return arr;
    quicksort(arr, 0, arr.length - 1);
    return arr;
}

private static void quicksort(int[] arr, int l, int r){
    if(l >= r) return;
    int x = arr[l], i = l - 1, j = r + 1;
    while(i < j){
        do i++; while(arr[i] < x);
        do j--; while(arr[j] > x);
        if(i < j) swap(arr, i, j);
    }
    quicksort(arr, l, j);
    quicksort(arr, j+1, r);
}

private  static void swap(int[] arr, int i, int j){
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

Question 10

(1)

p1 的 x, y 坐标: 1111, 2222
p2 的 x, y 坐标: -100, -200
p1 的 x, y 坐标: 0, 0
p2 的 x, y 坐标: 0, 0

(2)

浅拷贝:浅拷贝指复制对象时,只复制对象本身的字段值,如果字段是基本类型,则复制数值;如果字段是引用类型,则复制引用地址(即指向同一内存),不会复制引用对象本身。两个对象共享引用类型字段指向的对象,修改其中一个会影响另一个。

深拷贝:深拷贝会复制对象本身及它所引用的所有对象,即递归地复制所有引用对象,最终创建一个完全独立的新对象,两个对象之间不存在共享引用。因此修改一个对象不会影响另一个。

(3)

为了避免浅拷贝,需要在copy()方法中:新建一个当前类的实例;将所有的基本类型字段直接复制;把所有的引用类型字段也调用其copy()方法生成新的实例,而不是直接复制引用,保证复制的子对象也是深拷贝的。

(4)

Question 11

代码:

public static String strscat(String... args){
    if(args==null) return null;
    String a = "";
    for(String s: args){
        a += s;
    }
    return a;
}

测试截图:

image-20250918223101200

image-20250918223147528

image-20250918223205412

(1)我认为Java 可变参数本质上是一个数组,当定义方法如 void method(String... args) 时,编译器会在底层把它转换成 void method(String[] args)

​ 调用时如果传入多个参数,Java 编译器会自动将它们打包成一个数组,或者也可以直接传入一个数组。这使得调用者方便传入任意数量的参数,而方法内部按数组处理,且应该确保可变参数只能定义在参数列表的最后一个位置,且只能有一个可变参数。

(2)能通过编译,因为就像上面说的,strscat(String... args) 本质为 strscat(String[] args),直接传入new String[]{"a", "b"}就相当于传入一个String类型的数组,完全可以编译。

(3)不能,因为本身两个都是传String[] args的参,并不是重载。

image-20250918223919399

image-20250918223937337

(4)不能通过编译。因为传入了多个独立的字符串,Java 不会自动把多个独立参数拼成数组,所以参数不匹配报错。

image-20250918224218623

Question 12

import java.util.Scanner;

public class BigNumberAddition {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 读取两个大数
        String a = scanner.nextLine();
        String b = scanner.nextLine();
        // 计算它们的和
        int pos = Math.max(a.length(), b.length());
        int i = a.length() - 1;
        int j = b.length() - 1;
        int jin = 0, p = pos;
        int[] ans = new int[pos+10];
        for(;i >= 0||j >= 0; i--, j--){
            p--;
            int d = jin;
            if(i>=0) d += a.charAt(i) - '0';
            if(j>=0) d += b.charAt(j) - '0';
            jin = 1;
            if(d>=10) d-=10;
            else jin = 0;
            ans[p] = d;
        }

        // 输出结果
        if(jin==1) System.out.print("1");
        for(int t = 0; t < pos; t++){
            System.out.print(ans[t]);
        }
    }
}

运行截图:

image-20250919233343615

Question 13

抽象的类:

People类:

public class People {
    private String name;
    private int weight;

    public People(String name, int weight){
        this.name = name;
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public void reduceWeight(int weight){
        if(weight<0){
            System.out.println("哥们少吃点吧");
            return;
        }else if(weight==0){
            System.out.println("没减也是减");
            return;
        }
        this.weight -= weight;
        System.out.println("恭喜您成功减重" + weight + "KG!");
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", weight=" + weight +
                '}';
    }
}

测试类:

package cc.lixu.oo01;

public class TestPeople {
    public static void main(String[] args) {
        People wang = new People("小王", 70);
        System.out.println("你的体重是:" + wang.getWeight());
        wang.reduceWeight(25);
        System.out.println("你的体重是:" + wang.getWeight());
    }
}

image-20250920004631485