裁判测试程序样例中展示的是一段定义基类people、派生类student以及测试两个类的相关java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。
时间: 2023-05-31 21:17:53 浏览: 543
### 回答1:
```
class People {
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Student extends People {
private String studentId;
public Student(String name, int age, String studentId) {
super(name, age);
this.studentId = studentId;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
}
public class Test {
public static void main(String[] args) {
People p = new People("Tom", 20);
System.out.println(p.getName());
System.out.println(p.getAge());
Student s = new Student("Jerry", 18, "S001");
System.out.println(s.getName());
System.out.println(s.getAge());
System.out.println(s.getStudentId());
}
}
```
补充完整后的代码如上,缺失的部分包括了People类和Student类的构造函数,以及Student类中新增的studentId属性和相应的get/set方法。此外还有测试程序中输出studentId的部分。
### 回答2:
这段测试程序样例展示了一个基类People和其派生类Student的相关Java代码。这份代码有一些缺失,在补充完整之后,才能保证测试程序正常运行。
首先,需要定义基类People和派生类Student的完整代码。People类可能包含以下属性和方法:
```java
public class People {
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void introduce() {
System.out.println("My name is " + this.name + ", I am " + this.age + " years old.");
}
}
```
Student类继承自People类,可以添加一些特殊属性和方法:
```java
public class Student extends People {
private String school;
private String major;
public Student(String name, int age, String school, String major) {
super(name, age);
this.school = school;
this.major = major;
}
public String getSchool() {
return this.school;
}
public String getMajor() {
return this.major;
}
public void introduce() {
super.introduce();
System.out.println("I am a student in " + this.school + ", majoring in " + this.major + ".");
}
}
```
接下来就是测试程序的代码。首先是People类和Student类的相关测试:
```java
People p = new People("Xiao Ming", 20);
p.introduce();
Student s = new Student("Xiao Hong", 21, "Peking University", "Computer Science");
s.introduce();
```
这段代码创建了一个People对象和一个Student对象,并分别调用它们的introduce方法,输出了它们的信息。
最后,测试程序还需要包含两个必要的类:Main类和Test类。Main类是程序的入口,Test类用于执行测试。
Main类代码如下:
```java
public class Main {
public static void main(String[] args) {
Test test = new Test();
test.testPeople();
test.testStudent();
}
}
```
这段代码创建了一个Test对象,并依次调用它的testPeople和testStudent方法,执行测试。
Test类代码如下:
```java
public class Test {
public void testPeople() {
People p = new People("Xiao Ming", 20);
assertEquals(p.getName(), "Xiao Ming");
assertEquals(p.getAge(), 20);
}
public void testStudent() {
Student s = new Student("Xiao Hong", 21, "Peking University", "Computer Science");
assertEquals(s.getName(), "Xiao Hong");
assertEquals(s.getAge(), 21);
assertEquals(s.getSchool(), "Peking University");
assertEquals(s.getMajor(), "Computer Science");
}
private void assertEquals(Object actual, Object expected) {
if (actual == null && expected == null) {
return;
}
if (actual != null && actual.equals(expected)) {
return;
}
throw new RuntimeException("Test failed: expected=" + expected + ", actual=" + actual);
}
}
```
这里的Test类包含testPeople和testStudent两个方法,分别对People对象和Student对象执行测试。assertEquals方法用于判断测试是否通过,如果测试结果与期望值不符,就会抛出异常。
以上是补充完整的测试程序代码,可以正常执行并验证类的正确性。
### 回答3:
在裁判测试程序样例中,定义了一个基类People以及它的派生类Student,并展示了测试两个类的相关Java代码。但是这段代码中缺失了一些重要的部分,需要我们补充完整,以保证测试程序的正常运行。
首先,我们需要补充People类的构造方法和属性。在这个示例中,我们可以定义People类具有name和age两个属性,并且提供set和get方法来设置和获取这些属性值。
```java
public class People {
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
接下来,我们需要补充Student类的构造方法和属性。在这个示例中,我们可以定义Student类有一个grade属性,并且提供了set和get方法来设置和获取这个属性值。
```java
public class Student extends People {
private int grade;
public Student(String name, int age, int grade) {
super(name, age);
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
```
最后,我们需要编写测试代码,测试刚才新定义的People和Student类是否能够正常工作。
```java
public class Test {
public static void main(String[] args) {
People p = new People("张三", 18);
assert p.getName().equals("张三");
assert p.getAge() == 18;
Student s = new Student("李四", 20, 85);
assert s.getName().equals("李四");
assert s.getAge() == 20;
assert s.getGrade() == 85;
System.out.println("测试通过");
}
}
```
通过补充缺失的代码,我们可以成功地测试出People类和Student类的方法是否能够正常工作,以此确保代码的正确性。
阅读全文