JAVA作业求助。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA作业求助。相关的知识,希望对你有一定的参考价值。

作业1:根据实例讲解,设想一个依赖关系的场景,并试用代码实现
包括两个类的设计,主类中对象创建及关联。
作业2:根据实例讲解,设想一个关联关系的场景,并试用代码实现
包括两个类的设计,主类中对象创建及关联。

在Java中,依赖关系通常表示为一个类依赖于另一个类,因为它需要调用另一个类的方法或构造函数来完成它的工作。例如,假设我们有一个名为Car的类,它拥有一个名为startEngine的方法,用于启动汽车的发动机。我们还有一个名为Driver的类,它有一个名为drive的方法,用于驾驶汽车。那么Driver类就依赖于Car类,因为Driver类需要调用Car类的startEngine方法来启动汽车。
下面是一个示例代码,实现了一个依赖关系的场景:
=================
public class Car
public void startEngine()
System.out.println("Engine started.");


public class Driver
private Car car;
public Driver(Car car)
this.car = car;

public void drive()
car.startEngine();
System.out.println("Car is being driven.");


public class Main
public static void main(String[] args)
Car car = new Car();
Driver driver = new Driver(car);
driver.drive();


=================
在这段代码中,Driver类的构造函数接收一个Car类的实例作为参数,并将它保存在私有字段car中。在Driver类的drive方法中,我们首先调用了car实例的startEngine方法,然后打印一条消息,表示汽车正在被驾驶。
关联关系通常表示为两个类之间的关系,它们之间的关系可能是一对一,一对多或多对多。例如,假设我们有一个名为Student的类,它有一个名为enroll的方法,用于选课。我们还有一个名为Course的类,它有一个名为addStudent的方法,用于添加学生。那么Student类和Course类之间就存在一对多的关联关系,因为一个学生可以选择多个课程,而一个课程也可以被多个学生选择。
下面是一个示例代码,实现了一个关联关系的场景:
=================

public class Student
private List<Course> courses = new ArrayList<>();
public void enroll(Course course)
courses.add(course);
course.addStudent(this);


public class Course
private List<Student> students = new ArrayList<>();
public void addStudent(Student student)
students.add(student);


public class Main
public static void main(String[] args)
Student student = new Student();
Course course1 = new Course();
Course course2 = new Course();
student.enroll(course1);
student.enroll(course2);


=================
在这段代码中,Student类拥有一个私有字段courses,表示学生选择的课程。Student类的enroll方法用于选课,它接收一个Course类的实例作为参数,并将它添加到courses列表中。同时,enroll方法还会调用课程实例的addStudent方法,将学生添加到课程的学生列表中。
在Main类的main方法中,我们创建了一个学生实例和两个课程实例。我们调用学生实例的enroll方法,将两个课程实例添加到学生的选课列表中,同时将学生添加到两个课程的学生列表中。这就实现了一对多的关联关系。
总之,依赖关系和关联关系都是表示两个类之间的关系,但是它们之间的关系不同。依赖关系表示一个类依赖于另一个类,因为它需要调用另一个类的方法或构造函数来完成它的工作;关联关系表示两个类之间的关系,它们之间可能是一对一,一对多或多对多。
参考技术A 1、依赖关系,uml图示为虚线加箭头 A------>B, 是一种使用的关系, 即一个类的实现需要另一个类的协助。比用常常写的工具类就是可以被 其他类使用的
代码示例: 为订单生成订单号码的服务类,使用Math类产生随机数
这个时候 OrderService类依赖Math类
OrderService ------> Math
public class OrderService
public String getOrderNum()
return Math.random()*1000+"";


2、关联关系,分为聚合、组合、及一般关联 。
组合关系 A◆———>B 部分和整体之间具有相同的生命周期,当整体消亡后,部分也将消亡。举例 人体结构中 人体和大脑之间是一种组合关系, 人体亡了大脑也会死 例子有点沉重哈,但是好理解
代码示例
Body ◆———>Brain
public class Body
private Brain brain;
public Body()
this.brain = new Brain();

public void think()
brain.think();


聚合关系 A◇———>B 部分与整体之间并没有相同的生命周期,整体消亡后部分可以独立存在。 比如汽车和轮胎, 汽车销毁了 轮胎可以作为配件 运用到其他汽车上,所以是一种聚合关系。
代码示例:
Car ◆———>Tyre
public class Car
private Tyre tyre;
public Car(Tyre tyre)
this.tyre= tyre;

public void run()
tyre.run();


一般关联:是一种结构化的关系,指一种对象和另一种对象有联系,给定关联的两个类,可以从其中的一个类的对象访问到另一个类的相关对象。
解释上很拗口 不如组合 聚合来的清晰明了。可以和依赖关系作个对比,依赖实际上就是 主类依赖其他类的方法 完成部分逻辑,比如工具类的调用等, 但是两个类之间没有任何联系。 而关联关系 是存在某种联系的 比如学校和老师,老师和学生,学生和课本 等等, 两个对象之间是存在某种联系的。
java示例:
Student ————> Teacher
public class Student
private Teacher englishTeacher;
public String myEnglishTeacherName()
return englishTeacher.getName();


上面的关联关系,只是描述了 对象和对象之间的联系,并没有指明关联的数量。 比如一个学生是关联多个老师的(语文老师、数学老师 etc) ,那么这种关联是一种1对多的关联 等等,先理解基本的然后再深入比较好
从类的代码表达来看, 关联关系(组合、聚合、一般关联关系) 都是一个类作为另一个类的成员变量来表示, 依赖关系大部分是 一个类作为另一个类的方法参数,或者直接调用被依赖类的静态方法, 或者直接实例化被依赖类然后调用其方法 等。
理解好了 依赖和关联 包括集成 实现 等关系及 UML图示后, 对于 代码的设计将会有很大帮助
参考技术B 例如,我们可以设想一个学生类和课程类的场景。
学生类:
class Student
private String name;
private int age;
public Student(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 Course
private String name;
private int credit;
private Student student;
public Course(String name, int credit)
this.name = name;
this.credit = credit;

public String getName()
return name;

public void setName(String name)
this.name = name;

例如,我们也可以设想一个员工类和部门类的场景。
员工类:
class Employee
private String name;
private int age;
private Department department;
public Employee(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;

public Department getDepartment()
return department;

public void setDepartment(Department department)
this.department = department;


部门类:
class Department
private String name;
private List<Employee> employees;
public Department(String name)
this.name = name;
this.employees = new ArrayList<>();

public String getName()
return name;

public void setName(String name)
this.name = name;

public List<Employee> getEmployees()
return employees;

public void setEmployees(List<Employee> employees)
this.employees = employees;

public void addEmployee(Employee employee)
employees.add(employee);
employee.setDepartment(this);


在主类中,我们可以使用如下代码来创建员工对象和部门对象,并进行关联:
Employee employee1 = new Employee("John", 25);
Employee employee2 = new Employee("Jane", 30);
Department
参考技术C 作业1:依赖关系的场景
场景:有一个图书馆,图书馆有图书,图书有作者。
图书馆类:
public class Library
private List<Book> books;

public Library()
this.books = new ArrayList<Book>();


public void addBook(Book book)
this.books.add(book);


public void printAuthorOfAllBooks()
for (Book book : this.books)
System.out.println("Author of book " + book.getName() + " is " + book.getAuthor().getName());




图书类:
public class Book
private String name;
private Author author;

public Book(String name, Author author)
this.name = name;
this.author = author;


public String getName()
return this.name;


public Author getAuthor()
return this.author;



作者类:
public class Author
private String name;

public Author(String name)
this.name = name;


public String getName()
return this.name;



主类:
public class Main
public static void main(String[] args)
Library library = new Library();
Author author1 = new Author("Author 1");
Author author2 = new Author("Author 2");
Book book1 = new Book("Book 1", author1);
Book book2 = new Book("Book 2", author2);
library.addBook(book1);
library.addBook(book2);
library.printAuthorOfAllBooks();



作业2:关联关系的场景
场景:有一个学生和一个课程,学生可以选择课程。
学生类:
public class Student
private String name;
private Course course;

public Student(String name)
this.name = name;


public String getName()
return this.name;


public void setCourse(Course course)
this.course = course;


public void printCourse()
if (this.course == null)
System.out.println(this.name + " has not chosen any course yet.");
else
System.out.println(this.name + " has chosen the course " + this.course.getName());




课程类:
public class Course
private String name;

public Course(String name)
this.name = name;


public String getName()
return this.name;



主类:
public class Main
public static void main(String[] args)
Student student1 = new Student("Student 1");
Student student2 = new Student("Student 2");
Course course1 = new Course("Course 1");
Course course2 = new Course("Course 2");
student1.setCourse(course1);
student2.setCourse(course2);
student1.printCourse();
student2.printCourse();

参考技术D 作业1:
例如,有一个学校类School,它有两个子类Teacher和Student。其依赖关系是每个老师会带多个学生,学生也只能有一位老师。用代码表示如下:
class School
private List<Teacher> teachers;
private List<Student> students;
public void setTeachers(List<Teacher> teachers)
this.teachers = teachers;

public void setStudents(List<Student> students)
this.students = students;


// 其他方法

class Teacher
private String name;
private List<Student> students;
public void setName(String name)
this.name = name;

public void setStudents(List<Student> students)
this.students = students;

// 其他方法

class Student
private String name;
private Teacher teacher;
public void setName(String name)
this.name = name;

public void setTeacher(Teacher teacher)
this.teacher = teacher;

// 其他方法

-----------------------------------------------
作业2 ,
例如,有一个书店类Bookstore,它有两个子类Book和Customer。其关联关系是每个书籍可以有多个购买者,每个顾客也可以买很多书籍。用代码表示如下:
class Bookstore
private List<Book> books;
private List<Customer> customers;
public void setBooks(List<Book> books)
this.books = books;

public void setCustomers(List<Customer> customers)
this.customers = customers;


// 其他方法

class Book
private String name;
private List<Customer> customers;
public void setName(String name)
this.name = name;

public void setCustomers(List<Customer> customers)
this.customers = customers;

// 其他方法

class Customer
private String name;
private List<Book> books;
public void setName(String name)
this.name = name;

public void setBooks(List<Book> books)
this.books = books;

// 其他方法

以上是关于JAVA作业求助。的主要内容,如果未能解决你的问题,请参考以下文章

C++,,,课堂作业求助,,,编写一个程序,,计算自己的生日距离今天多少天,,是星期几

Python作业求助

c语言作业,求助!

hadoop2.4版本中yarn的web管理界面不能查看作业状态!!!求助

求助,aix中,能否用shell命令,每次执行的时候,自动将crontab作业前天出错的日志保存到另一个文本中

oo第二次博客作业