2020/11/26 EECS2030 Lab 07 https://www.eecs.yorku.ca/course_archive/2020-21/F/2030/labs/lab7/EECS2030Lab07.html 1/2 EECS2030 Lab 07 Due: Wednesday Dec 02, 2020 before 11:59PM Introduction The main goals of this lab are to: get better understanding of Inheritance and obligatory methods. learn to read JUnit tests during implementations heads-up on Collection in Java, including linear, hash-based and comparsion-basd collections. heads-up on Interfaces and Generics in Java. Specification When we define a Java class, we automatically inherit some methods from java.lang.Object class. There are 3 of them that implementers are usually 'obliged' to override, so that the methods beheave in a more meaningful way. toString() This method is used when we print the object (e.g., println() methods output a single value after converting the value using the appropriate toString() method). Object class toString() returns a string representation of memory address. Usually we should override this method so that the object's state (attribute values) is represented in a meaningful way. equals() This method is used in array and collections to search for element (e.g., contains(), remove()) or check the equality over the whole collection (e.g., Arrays.equals(arr1,arr2)). Object class used memory addresses to determine if two objects are equal, and two objects are equal only when have the same address. Usually we should override this method so as to support logical equality: objects of same states are equal. Implementing equals() is suprisingly harder than it appears to be. There is an amazing amount of disagreement in the Java community regarding correct implementation of equals(). In this course (and this lab) we follow these rules: 1. an instance is equal to itself 2. an instance is never equal to null 3. only instances of the exact same type can be equal 4. same type instances with the same state are equal According to rule 2 and 3, when the argument to equals() is null or is of a different type of 'this', the method should return false and should not throw exceptions. To check the state of object (rule4), some or all fields are examined. The recipe for checking equality of fields is that for primitive types we check their vlaues, for reference fields we rely on the equals() of that type (but beware that one or both fields might be null). hashCode() This method is used when determining the position of element in hash-based data structures such as HashSet, HashMap in Java collection framework. Object class uses address to determine the hash code, so two different objects of the same state will have different hash codes. The rule here is that if you override equals() you must also override hashCode(), otherwise, the hash-based containers won't work properly. Specifically, for two objects x and y, if x.equals(y) is true, then x.hashCode() == y.hashCode() (It would be nice if two distinct (i.e., unequal) objects have two distinct hash codes but this is not required; That is, two different objects can have the same hash code.) compareTo() This method is used to establish a 'nature ordering' of objects. This method is used in searching and sorting algorithms in Java arrays and collections, and in comparsion-bases data structures. So if you want to sort an array or a collection the object or want to store objects in comparsion-based data structures such as TreeSet, TreeMap, you are obliged to implement this method in the class. Different from toString(), equals(), and hashcode() discussed above, compareTo() does not exist in Object class, so a class does not automatically inherit this method. This method is declared in java.lang.Comparable Interface. So in order to establish natural ordering, the class implements the Comparable interface and implements the compareTo() method decleared in the interface. Comparators Implementing Comparable interface allows implementers to define 'main' natural ordering once. This requires the modification of the orignal class and cannot define other natural orderings. So what if you cannot modify the original class, or, want to define multiple versions of ordering and thus sorting objects in different ways? You can define different 'comparator' classes, each of which implements java.util.Comparator interface and implements the compare(arg1, arg2) method declared in the interface. By giving sorting or searching algorithms different comparators, objects can be sorted or searched in different ways. Implementation Download the shell project from the following zip file. DO NOT DOUBLE CLICK TO OPEN THIS ZIP FILE! You should import the project from the archive into your workspace as outlined in Lab 1. This project maintains a student system. Given a JUnit test class (which relies on certain API of methods) and its expected outputs, complete the classes such that all tests pass. Specifically, Implement necessary obligatory methods in class Prof, Course and Book, as specified in the classes, and hinted in the test cases. You should follow the specifications given in the classes, and when applicable, you should also follow the rules describled above. 2020/11/26 EECS2030 Lab 07 https://www.eecs.yorku.ca/course_archive/2020-21/F/2030/labs/lab7/EECS2030Lab07.html 2/2 Complete the Comparator class. This class allows the Profs to be compared based on years of employment, in ascending order. Also implement one more comparator class so that the Profs can be compared based on names in lexicographical order, as hinted in the test cases. You may want to implement Prof calss before implementing Book class and the Comparator classes. Slides on Comparators are provided along with the lab. A local copy of the Java API can be found here. Submit You should submit files Prof.java, Course.java, Book.java, as well as two comparator classes. To submit your solution via the terminal (assuming you have used remotelab to build your code and complete the lab), first navigate to the src directory for your project, and then issue the submit command into the terminal window to submit fiels one by one: submit 2030 lab7 Prof.java submit 2030 lab7 Book.java ... or, submit some or all files together, for example, submit 2030 lab7 Prof.java Book.java ... submit 2030 lab7 *.java At any time, you can issue submit -l 2030 lab7 into the terminal to check the files that you have submitted for lab6. Alternatively, if you are using Eclipse on your own machine, you can upload your files through web submit . Make sure you choose 2030 for the course, and lab7 for the assignment. Upload java files only, DO NOT SUBMIT the ZIP file of your project! Hui Wang | Template design by Andreas Viklund
欢迎咨询51作业君