#PTAOOP711. 7-11 学生类设计

7-11 学生类设计

本站提交说明

此题属于 面向对象程序设计练习。请在练习页填写原题的填空或函数片段并提交;本题独立提交页接收拼接后的完整 Java 程序。

评测使用题面样例与本站补充测试,包含编译、输出及部分成员 API 检查。通过本站测试不代表通过 PTA 隐藏测试。

设计一个类Student,并在Main类中生成Student类对象进行测试
1.对于Student类,设计私有属性name和age,并为每一个成员变量name和age设计其setXXX()和getXXX()方法,并对于setAge方法,如果age被赋值为<=6,则age值为7,其他为参数值。
2.对于Student类,设计无参构造方法,在构造方法中为age赋值为7,name赋值为“Unknown" 并添加语句System.out.println("No-argument constructor");
3.设计方法public void display(),方法中显示该学生的姓名和年龄,显示格式为name:无名,age:8

Main类的main方法中,创建1个学生对象,然后调用display方法显示学生信息,接着通过键盘输入1个学生的姓名和年龄,调用display方法去显示学生信息。

Design a class named Student and create an object of the Student class in the Main class for testing

For the Student class, design private attributes name and age, and implement setXXX() and getXXX() methods for each member variable name and age. For the setAge method, if the age is assigned a value <=6, the age should be set to 7; otherwise, it should retain the parameter value.

  1. For the Student class, design a no-argument constructor that sets the age to 7 and the name to "Unknown," and add the statement System.out.println("No-argument constructor");

  2. Design method public void display(), which displays the student's name and age in the format: name: Anonymous, age: 8

In the main method of the Main class, create a student object, then call the display method to show the student information. Next, input the student's name and age via keyboard, and call the display method again to display the student information.

输入格式:

在一行内输入学生的姓名和年龄 Enter the student's name and age in a single line

输出格式:

对每一组输入,输出学生的姓名和年龄。 For each input group, output the student's name and age.

输入样例:

在这里给出一组输入。例如:

tom 6
jerry 8

输出样例:

在这里给出相应的输出。例如:

No-argument constructor
name:Unknown,age:7
name:tom,age:7
No-argument constructor
name:Unknown,age:7
name:jerry,age:8