Question
A module encapsulates three attributes: title of module, name of lecturer and the number of hours for delivery. An outline listing of the class and
A module encapsulates three attributes: title of module, name of lecturer and the number of hours for delivery. An outline listing of the class and its attributes are given in the accompanying file. An equals method, a toString method and a hashCode are given. You are required to write a compareTo method that implements the Comparable
https://www.journaldev.com/780/comparable-and-comparator-in-java-example.
The file Assignment8.java contains a short list of module instances. Write code to sort and print the list using both the natural ordering and the lecturer comparator.
import java.util.*; class Module implements Comparable{ private String title; private String lecturer; private int hours; Module(String t, String l, int h){ title = t; lecturer = l; hours = h; } public String title(){ return title; } String lecturer(){ return lecturer; } int hours(){ return hours; } public boolean equals(Object ob){ if(!(ob instanceof Module)) return false; Module m = (Module)ob; return title.equals(m.title); } public int hashCode(){ return title.hashCode(); } public String toString(){ return title+" "+lecturer+" "+hours; } public int compareTo(Module m){ } public static Comparator lectCompare(){ }
import java.util.*; public class Assignment08 { public static void main(String[] args) { //Q1 Test ===================================== //Test array Module[] mlst = { new Module("Programming","Donal",36), new Module("Networks","Faheem",36), new Module("Graphics","Barry",36), new Module("Web","Ruairi",36), new Module("Hardware","Paddy",36), new Module("Algorithms","Eoin",36), new Module("OOP","Tony",36), new Module("Maths","Paddy",36), new Module("BigData","Osama",36) }; // Q1 end ====================================== // Q2 Test ====================================== // Q2 end ======================================= // Q3 Test ====================================== } }
language java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started