Question

In: Computer Science

Registry Java Programming 2) Interface Comparator: The method for comparing two objects is written outside of...

Registry Java Programming

2) Interface Comparator:

The method for comparing two objects is written outside of the class of the objects to be sorted. Several methods can be written for comparing the objects according to different criteria. Specifically, write three classes, DescriptionComparator, FirstOccComparator,
and LastOccComparator that implement the interface java.util.Comparator.

  • DescriptionComparator implements the interface Comparator. The
    method compare compares the description of two objects. It returns a negative value if the description of the first object comes before the description of the second object in the lexicographic order, 0 if both descriptions are logically the same, and a positive number if the description of the first object comes after the description of the second object in the lexicographic order;

  • FirstOccComparator implements the interface Comparator. It compares the start time of two events. It returns a negative value if the start time of the first event is before that of the second event, 0 if both start times are logically the same, and a positive number if the start time of the first event is after the start time of the second event;

LastOccComparator implements the interface Comparator. It compares the dates of the last recurrences of two events. It returns a negative value if the date of the last recurrence of the first event is before that of the last recurrence of the second event, 0 if the date of the last recurrence of both events is logically the same, and a positive number if the date the last recurrence of the first event is after the date of the last recurrence of the second event.

Solutions

Expert Solution

DescriptionComparator class

package comaprator;

import java.util.Comparator;

//comparator with description comparison

public class DescriptionComparator implements Comparator<MyObject>{

@Override

public int compare(MyObject o1, MyObject o2) {

return o1.description.compareTo(o2.description); //return -1,0 ,1 if string lexicographically

//less equal, and greater respectively

}

}

FirstOccComparator class

package comaprator;

import java.util.Comparator;

//comparator with event start time comparison

public class FirstOccComparator implements Comparator<MyObject>{

@Override

public int compare(MyObject o1, MyObject o2) {

if(o1.startTime.before(o1.startTime))

return -1;

if(o1.startTime.after(o2.startTime))

return 1;

return 0;

}

}

LastOccComparator class

package comaprator;

import java.util.Comparator;

//comparator with date of last occurrence

public class LastOccComparator implements Comparator<MyObject>{

@Override

public int compare(MyObject o1, MyObject o2) {

if(o1.lastOccurence.before(o2.lastOccurence))

return -1;

if(o1.lastOccurence.after(o2.lastOccurence))

return 1;

return 0;

}

}

MyObject class

package comaprator;

//object outside of the comparison class to compare

import java.sql.Timestamp;

import java.util.Date;

public class MyObject {

public MyObject(String description, Timestamp startTime, Date lastOccurence) {

super();

this.description = description;

this.startTime = startTime;

this.lastOccurence = lastOccurence;

}

String description;

Timestamp startTime;

Date lastOccurence;

@Override

public String toString() {

return "MyObject [description=" + description + ", startTime=" + startTime + ", lastOccurence=" + lastOccurence

+ "]\n";

}

}

Tester class

package comaprator;

//driver program to test

import java.sql.Timestamp;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Date;

public class Tester {

public static void main(String[] args) {

MyObject m1=new MyObject("hello",new Timestamp(765432),new Date("13/06/2016"));

MyObject m2=new MyObject("wow",new Timestamp(60002),new Date("03/8/2020"));

MyObject m3=new MyObject("hi",new Timestamp(765432),new Date("13/06/1998"));

ArrayList<MyObject> l=new ArrayList<>();

l.add(m1);

l.add(m2);

l.add(m3);

System.out.println(l);

Collections.sort(l,new DescriptionComparator());

System.out.println(l);

l.clear(); //cleae list to have clean data

l.add(m1);

l.add(m2);

l.add(m3);

Collections.sort(l,new FirstOccComparator());

System.out.println(l);

l.clear();

l.add(m1);

l.add(m2);

l.add(m3);

Collections.sort(l,new LastOccComparator());

System.out.println(l);

}

}

output

[MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]

, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]

, MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]

]

[MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]

, MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]

, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]

]

[MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]

, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]

, MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]

]

[MyObject [description=hi, startTime=1970-01-01 05:42:45.432, lastOccurence=Wed Jan 06 00:00:00 IST 1999]

, MyObject [description=hello, startTime=1970-01-01 05:42:45.432, lastOccurence=Fri Jan 06 00:00:00 IST 2017]

, MyObject [description=wow, startTime=1970-01-01 05:31:00.002, lastOccurence=Sun Mar 08 00:00:00 IST 2020]

]


Related Solutions

Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
(Generic PriorityQueue using Comparator) Revise MyPriorityQueue in Listing 24.8, using a generic parameter for comparing objects....
(Generic PriorityQueue using Comparator) Revise MyPriorityQueue in Listing 24.8, using a generic parameter for comparing objects. Define a new constructor with a Comparator as its argument as follows: PriorityQueue(Comparator comparator).
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface...
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface Filter { boolean accept(Object x); } Modify the implementation of the Data class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Solve Exercise •• P10.6, using a lambda expression for the filter....
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing. Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create...
Registry Java Programming 3) Planner Create a class called Planner. A Planner is a data structure...
Registry Java Programming 3) Planner Create a class called Planner. A Planner is a data structure for storing events. The maximum capacity of the planner (physical size) is specified by the formal parameter of the constructor of the class. Instance methods int size(); returns the number of events that are currently stored in this Planner (logical size); boolean addEvent( AbstractEvent event ); adds an event to the last position of this Planner. It returns true if the insertion was a...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Learning Objectives: ● review implementing the interface Comparable<T> ● programming against interfaces not objects. ● review...
Learning Objectives: ● review implementing the interface Comparable<T> ● programming against interfaces not objects. ● review reading in data from a file ● use the internet to learn about the interface Comparator<T> ● use a Comparator to provide an alternative way to order elements in a collection Description: Turn in: Turn in the assignment via Canvas Create a package called booksthat includes 3 files: Book.java, BookApp.java, and books.csv (provided). Class Book represents Pulitzer prize winning books that have a title,...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2....
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2. Create a class Car. Implements Vehicle. It's paint() method prints "Car Painted". It's drive( ) method prints "Car Driven". 3. Create a class Bus. Implements Vehicle. It's paint() method prints "Bus Painted" . It's drive( ) method prints "Bus Driven".   4. Create a method AutoWork which accepts an object of type Vehicle. The method makes a call to the vehicle's paint( ) and drive()...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT