这是用户在 2024-3-21 11:06 为 https://app.immersivetranslate.com/pdf-pro/25091a32-fc83-489f-bf29-fdfbe115131c 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
2024_03_21_0bfcb3140eaa8d9e48edg

Lab 2
Inheritance and Polymorphism
实验2 继承和多态

Exercise 1 (n)  练习1

  1. Downloads shapes.zip 下载 shapes.zip
  2. Import project Shapes 导入项目Shapes
  • Select File > Import
    选择文件 > 导入
  • Select "Existing Project into Workspace"
    选择“将现有项目导入工作区”
  • Select archive file and browse for shapes.zip
    选择存档文件并浏览shapes.zip。

Import Projects 导入项目

Select a directory to search for existing Eclipse projects.
选择一个目录来搜索现有的Eclipse项目。
  • Click Finish 点击完成
Select root directory: 选择根目录
  • Select archive file: 选择存档文件
Browse.. 浏览..
Projects: 项目:

Exercise 1 练习1

3) Inspect the code.
检查代码。

  • It will have the
    它将会有
same structure as 相同的结构
the class diagram 类图
below: 下面:
<>
C Circle
comp41670.lab2
a radius: double
circle(double)
draw():void
getRadius():double
calculateArea():void
calculatePerimeter():voic

Exercise 1 (not graded)
练习1(不计分)

  1. Create and run a new class (e.g., ShapesDemo) with a main method that instantiates and draws a Square, a Rectangle and a Circle.
    创建并运行一个新的类(例如,ShapesDemo),其中包含一个main方法,该方法实例化并绘制一个正方形、一个矩形和一个圆形。

Exercise 2 (graded) (1/3)
练习2(评分)(1/3)

You can submit a zip file of your Java project on Brightspace at Assessment >> Assignments >> Lab 2 - Software Development Exercise
您可以在Brightspace的评估>>作业>>实验2-软件开发练习中提交您的Java项目的zip文件
Create a Java Project (e.g., StudentsRegistration) that manages the registration of students to modules in a university. This project should include the following classes (but it can include more if necessary to support code reuse):
创建一个Java项目(例如,StudentsRegistration),管理学生在大学中注册模块的过程。这个项目应该包括以下类(如果需要支持代码复用,可以包括更多类):
Student: has a name, an address, a date of birth, a year of study, and a boolean variable (isEU) that identifies whether the student is from the EU or not and another boolean variable (feesPaid) that identifies whether the student paid his/her fees. Also the student has a double variable fees indicating the fees to be paid. Initially no student has paid his/her fees. The Student class implements the following methods:
学生:有一个名字,一个地址,一个出生日期,一个学年,一个布尔变量(isEU),用于标识学生是否来自欧盟,以及另一个布尔变量(feesPaid),用于标识学生是否已经支付了学费。此外,学生还有一个双精度变量(fees),表示需要支付的学费。最初,没有学生支付了学费。学生类实现了以下方法:
  • protected void calculateFees(): this method assigns the instance variable fees a value of 3000 for EU students and 6000 for non-EU students.
    protected void calculateFees():此方法将实例变量费用分配给欧盟学生为3000,非欧盟学生为6000。
  • public String toString(): this method returns a string containing the class name and the value of its instance variables.
    public String toString(): 此方法返回一个包含类名和其实例变量值的字符串。
  • public void payFees(): first it calculates the student fees and then it sets feesPaid to true.
    public void payFees(): 首先计算学生的费用,然后将feesPaid设置为true。
WorkingStudent: has the same instance variables as a Student but additionally it has a salary. The WorkingStudent class implements the following methods:
工作学生:具有与学生相同的实例变量,但还有一个工资。WorkingStudent类实现以下方法:
  • public void calculateFees(): this method assigns the instance variable fees a value of 3000 for EU students and 6000 for non-EU students. Additionally, if the salary of the working student exceeds it adds 2000 to the fees, while if the salary of the student is comprised between and , it adds 1000 to the fees.
    public void calculateFees(): 此方法将实例变量fees的值分配为3000(欧盟学生)和6000(非欧盟学生)。此外,如果工作学生的工资超过 ,则将2000添加到费用中,而如果学生的工资介于 之间,则将1000添加到费用中。
  • public String toString(): this method returns a string containing the class name and the value of its instance variables.
    public String toString(): 此方法返回一个包含类名和其实例变量值的字符串。
Lecturer: has a name, an address, a date of birth and also the name of the school s/he is based on.
讲师:有一个名字,一个地址,一个出生日期,还有他/她所在学校的名称。

Exercise 2 (graded) (2/3)
练习2(评分)(2/3)

Module: has a name, a code, a semester (AUT, SPR, SUM), and a year during which the module is taught. It implements the following methods:
模块:具有名称、代码、学期(AUT、SPR、SUM)和教授该模块的年份。它实现以下方法:
  • public void setModuleCoordinator(Lecturer moduleCoordinator) to assign a module coordinator to the module.
    将一个讲师分配为模块的模块协调员。
  • public void registerStudent(Student student): this method registers at most 50 students in the module. A student can be registered if paid her fees and the module is in the same year of study of the student.
    public void registerStudent(Student student): 此方法最多注册50名学生到该模块。只有 已缴纳学费且模块与学生的学年相同的情况下,学生才能注册。
  • public String toString(): returns a string containing the class name and the value of its instance variables except for the registered students.
    public String toString(): 返回一个包含类名和其实例变量值的字符串,但不包括已注册的学生。
  • public void printRegisteredStudents() : print the information of the students registered in the module.
    打印已注册模块的学生信息。

Exercise 2 (graded) (3/3)
练习2(评分)(3/3)

Create a class (e.g., StudentsRegistrationDemo) which implements the main method that should do the following:
创建一个类(例如,StudentsRegistrationDemo),该类实现一个主方法,该方法应该执行以下操作:
  • Create 2 students, 2 working students and 2 modules and 2 lecturers.
    创建2个学生,2个在职学生,2个模块和2个讲师。
  • Assign to each module a different lecturer.
    为每个模块分配不同的讲师。
  • Pay the fees for all the students and working students.
    为所有学生和在职学生支付费用。
  • Register all the students and the working students to the modules.
    将所有学生和在职学生注册到模块中。
  • For each module, it prints the module information (using the method toString) and the information of the students registered in the modules (invoking the method printRegisteredStudents).
    对于每个模块,它打印模块信息(使用toString方法)和注册在模块中的学生的信息(调用printRegisteredStudents方法)。

Suggestions: 建议:

  • Try to exploit inheritance in order to reuse code as much as possible and avoid repetitions. To this aim you can create other classes (not mentioned in the previous slide). Those classes can put together the attributes shared by more than one class.
    尽量利用继承来尽可能地重用代码并避免重复。为此,您可以创建其他类(不在前一张幻灯片中提到的类)。这些类可以将多个类共享的属性组合在一起。
  • It is not necessary to use interfaces for this assignment.
    这个任务不需要使用接口。
  • To represent dates of birth use the Java class LocalDate and its static method of (see https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html)
    使用Java类LocalDate及其静态方法of来表示出生日期(参见https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html)。
  • Try to separate the classes in different packages and assign them a meaningful name
    尝试将类分别放置在不同的包中,并为它们分配一个有意义的名称

GOOD LUCK! 好运!