跳至主要內容

Practice Final exam

AI悦创原创Python 一对一教学uicUTSPython 一对一教学uicUTS大约 9 分钟...约 2656 字

Question 1

EN

Given this method header, what is the data type returned by the method?

public String getName(int ID)

Question 2

EN

Write down the correct way to declare and create an object using the following Constructor method header: Book(String title, double cost, boolean inStock)

Question 3

Analyse this array and write down the value of table3D[0][1][0].

 int[][][] table3D = {
     {{5,7,4},{19,20,6}},
     {{15,32,9},{7,8,3}}, 
     {{79,13,11},{11,25,23}}
 };

这个三维数组 table3D 的索引是从 0 开始的,所以 table3D[0][1][0] 将代表数组的第 1 个元素的第 2 个元素的第 1 个元素。

下面是这个数组的更详细的表示,为了更清楚地表示索引:

table3D[0] = {{5,7,4},{19,20,6}}
table3D[1] = {{15,32,9},{7,8,3}}
table3D[2] = {{79,13,11},{11,25,23}}

table3D[0] 中:

table3D[0][0] = {5,7,4}
table3D[0][1] = {19,20,6}

table3D[0][1] 中:

table3D[0][1][0] = 19

所以,table3D[0][1][0] 的值是 19。

Question 4

Write the Java code to declare and initialise a constant called STATUS to hold the value true. You must choose the most appropriate data type and assign it the value true

public class Main {
    public static void main(String[] args) {
        final boolean STATUS = true;
        System.out.println(STATUS);
    }
}

我创建了一个boolean类型的常量STATUS并将其初始化为true。注意,因为它是final,所以我们不能在后面改变它的值。

final()

在Java中,final关键字可以用在许多不同的上下文中,包括类、方法和变量。下面是final关键字在不同情况下的行为:

  1. final 变量:一旦初始化,就不能更改其值。对于基本数据类型,这意味着变量的值不可更改,而对于对象引用,这意味着不能更改引用到的对象(但可以更改对象的状态,即其成员变量)。例如:

    final int num = 10; // num 不能被重新赋值
    final List<String> list = new ArrayList<>(); // list 引用不能被重新赋值,但我们可以改变 list 中的元素
    
  2. final 方法:无法在子类中被重写。这意味着当我们希望方法的行为在所有的子类中都保持一致时,我们可以将其声明为final。例如:

    public class Parent {
        public final void show() {
            System.out.println("This is a final method.");
        }
    }
    
    public class Child extends Parent {
        // 下面的代码会产生编译错误,因为我们试图重写 final 方法
        /*
        @Override
        public void show() {
            System.out.println("Overriding a final method.");
        }
        */
    }
    
  3. final:不能被继承。这意味着我们无法创建一个继承自final类的类。例如:

    public final class FinalClass {
        // some code
    }
    
    // 下面的代码会产生编译错误,因为我们试图继承 final 类
    /*
    public class SubClass extends FinalClass {
        // some code
    }
    */
    

总结起来,final关键字提供了一种方式,允许我们限制代码的某些方面不被修改,这有助于我们保持代码的稳定性和一致性。

Question 5

Write a for loop that adds up the numbers between 5 and 30 inclusive.

Question 6

Write a class Student that has: Attributes for name, ID and number of subjects

A Student constructor with three parameters used to initialise all attributes in the class.

Accessor and mutator for the student ID

A toString() method to return the value of all attributes combined in one String

A method that returns true or false depending on whether the Student is full time (this is true of the Student is studying 4 subjects or more)

YOU MUST WRITE THE ENTIRE CLASS

1
public class Student {
    private String name;
    private String ID;
    private int numberOfSubjects;

    public Student(String name, String ID, int numberOfSubjects) {
        this.name = name;
        this.ID = ID;
        this.numberOfSubjects = numberOfSubjects;
    }

    // Accessor for the student ID
    public String getID() {
        return this.ID;
    }

    // Mutator for the student ID
    public void setID(String ID) {
        this.ID = ID;
    }

    // toString() method
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", ID='" + ID + '\'' +
                ", numberOfSubjects=" + numberOfSubjects +
                '}';
    }

    // Method to check if the student is full time
    public boolean isFullTime() {
        return this.numberOfSubjects >= 4;
    }
}

这个Student 类包括了以下属性:

  • name 代表学生的名字
  • ID 代表学生的ID
  • numberOfSubjects 代表学生的学科数量

构造器Student接收三个参数来初始化这些属性。

访问器 getID 和修改器 setID 用于获取和设置学生的ID。

toString 方法返回所有属性值组成的字符串。

isFullTime 方法检查学生是否是全日制学生。如果学生的科目数量大于或等于4,那么这个学生就是全日制学生。

tip
  1. 具有三个属性:名字(name),ID和科目数量(number of subjects)。

  2. 具有一个构造器(constructor),它接受三个参数来初始化类的所有属性。

  3. 具有学生ID的访问器(accessor)和修改器(mutator)。访问器用于获取属性的值,而修改器用于更改属性的值。

  4. 具有一个toString()方法,这个方法返回所有属性值组成的一个字符串。

  5. 具有一个方法,该方法根据学生是否为全日制(如果学生学习的科目数量为4或更多则为真)返回true或false。

上述的Java代码正是根据这些要求创建的"Student"类。其中:

  • Student(String name, String ID, int numberOfSubjects)这是一个构造器,用于初始化类的所有属性。

  • getID()setID(String ID)分别是学生ID的访问器和修改器。

  • toString()方法用于返回所有属性值组成的字符串。

  • isFullTime()方法用于检查学生是否是全日制的。

上次编辑于:
贡献者: AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度