在IDEA用Java设计一个教材类,一个课程类,及一个为某门课程指定参考教材的类。一门课程可以有多本参考教材,再设计一个测试类。写出实验内容及步骤(包括主要流程和说明)。(源码含注释)
时间: 2023-06-14 18:06:57 浏览: 213
JAVA课程设计看看吧
教材类(Material.java):
```java
public class Material {
private String name; //教材名称
private String author; //教材作者
public Material(String name, String author) {
this.name = name;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
```
课程类(Course.java):
```java
import java.util.ArrayList;
public class Course {
private String name; //课程名称
private int credit; //学分
private ArrayList<Material> materials; //参考教材
public Course(String name, int credit) {
this.name = name;
this.credit = credit;
this.materials = new ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
public ArrayList<Material> getMaterials() {
return materials;
}
public void setMaterials(ArrayList<Material> materials) {
this.materials = materials;
}
public void addMaterial(Material material) {
materials.add(material);
}
}
```
为某门课程指定参考教材的类(CourseMaterial.java):
```java
public class CourseMaterial {
private Course course; //课程
private Material material; //参考教材
public CourseMaterial(Course course, Material material) {
this.course = course;
this.material = material;
course.addMaterial(material); //将该教材加入到课程的参考教材列表中
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Material getMaterial() {
return material;
}
public void setMaterial(Material material) {
this.material = material;
}
}
```
测试类(Test.java):
```java
public class Test {
public static void main(String[] args) {
//创建教材
Material material1 = new Material("Java编程思想", "Bruce Eckel");
Material material2 = new Material("Effective Java", "Joshua Bloch");
Material material3 = new Material("Head First Java", "Kathy Sierra, Bert Bates");
//创建课程
Course course1 = new Course("Java程序设计", 4);
Course course2 = new Course("Java高级编程", 3);
//为课程1指定参考教材
CourseMaterial courseMaterial1 = new CourseMaterial(course1, material1);
CourseMaterial courseMaterial2 = new CourseMaterial(course1, material3);
//为课程2指定参考教材
CourseMaterial courseMaterial3 = new CourseMaterial(course2, material1);
CourseMaterial courseMaterial4 = new CourseMaterial(course2, material2);
//输出课程信息及参考教材
System.out.println(course1.getName() + " 学分:" + course1.getCredit());
System.out.println("参考教材:");
for (Material material : course1.getMaterials()) {
System.out.println(material.getName() + " 作者:" + material.getAuthor());
}
System.out.println();
System.out.println(course2.getName() + " 学分:" + course2.getCredit());
System.out.println("参考教材:");
for (Material material : course2.getMaterials()) {
System.out.println(material.getName() + " 作者:" + material.getAuthor());
}
}
}
```
实验内容及步骤:
1. 定义教材类(Material.java),包括教材名称和作者。
2. 定义课程类(Course.java),包括课程名称、学分和参考教材。参考教材使用ArrayList存储。
3. 定义为某门课程指定参考教材的类(CourseMaterial.java),包括课程和参考教材。在构造函数中将该教材加入到课程的参考教材列表中。
4. 定义测试类(Test.java),在其中创建教材、课程和指定参考教材,然后输出每门课程的信息及参考教材。
5. 运行测试类,检查输出结果。
注:以上源码需要在同一个包中。
阅读全文