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.
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 success, and
false if this Planner was already full;
AbstractEvent eventAt( int pos ); returns the event at the
specified position in
this Planner. This operation must not change this state of this
Planner. The first event of the Planner is at position 0;
AbstractEvent remove( int pos ); removes the event at the
specified position of
this Planner. Shifts any subsequent items to the left (start of the
array). Returns the event that was removed from this Planner;
void display( Date date ); the method display prints all the events that have a recurrence on the specified date;
void sort( Comparator< AbstractEvent > c ), the method passes the array of events and the comparator object to the method java.util.Arrays.sort;
The class overrides the method String toString(). An example of the expected format is given below.
4) Notifications:
Create a class called notifications that is sensitive to changes in the planner. Every time an item shows up in planner, the notifications class will display some kind of notification automatically on the screen. You need to use the observer design pattern.
These are the abstract classes:
// AbstractEvent.java : Java class to represent the
AbstractEvent
import java.util.Date;
public abstract class AbstractEvent {
// data fields
private String description;
private Date start_time;
private Date end_time;
// method to set the description of the
AbstractEvent
public void setDescription(String description)
{
this.description =
description;
}
// method to set the start date for the
AbstractEvent
public void setStartTime(Date start)
{
this.start_time = start;
}
// method to set the end date for the
AbstractEvent
public void setEndTime(Date end)
{
this.end_time = end;
}
// method to return the description of the
AbstractEvent
public String getDescription()
{
return description;
}
// method to return the start date of
AbstractEvent
public Date getStartTime()
{
return start_time;
}
// method to return the end date of the
AbstractEvent
public Date getEndTime()
{
return end_time;
}
// The below 3 methods must be define by the concrete
subclass of AbstractEvent
// abstract method hasMoreOccurrences() whose
implementation depends on the kind of event
public abstract boolean hasMoreOccurrences();
// abstract method nextOccurrence() whose
implementation depends on the kind of event
public abstract Date nextOccurrence();
// abstract method init() whose implementation depends
on the kind of event
public abstract void init();
}
//end of AbstractEvent.java
1.2
//DailyEvent.java
import java.util.Calendar;
import java.util.Date;
public class DailyEvent extends AbstractEvent{
// data field to store the number of recurrences
private int num_consecutive_days;
// helper field to return the next occurrence
private int days_occurred = 0;
// method to set the number of recurrences
public void setNumberOfConsecutiveDays(int
num_days)
{
this.num_consecutive_days =
num_days;
}
// method to get the number of recurrences
public int getNumberOfConsecutiveDays()
{
return num_consecutive_days;
}
// method to return if the event has more occurrences
or not
@Override
public boolean hasMoreOccurrences() {
// if days occurred < number of
consecutive days , then next occurrence is valid
if(days_occurred <
num_consecutive_days)
return
true;
return false;
}
// method to return the date of next
occurrence
@Override
public Date nextOccurrence() {
// if days_occurred >= number of
recurrences, return null (as it exceeds number of
recurrences)
if(days_occurred >=
num_consecutive_days)
return
null;
// return the next occurrence
date
Calendar cal =
Calendar.getInstance();
cal.setTime(getStartTime());
cal.add(Calendar.DATE,
days_occurred);
days_occurred++; // increment the
number of days by a day
return cal.getTime();
}
// method to re-initialize the state of the object
so that a call to the method nextOccurrence() returns the date of
the first occurrence of this event
@Override
public void init() {
days_occurred = 0;
}
public String toString()
{
return("Start Date:
"+getStartTime()+" End Date : "+getEndTime()+" Consecutive days of
occurrence : "+num_consecutive_days);
}
}
//end of DailyEvent.java
1.3
//WeeklyEvent.java
import java.util.Calendar;
import java.util.Date;
public class WeeklyEvent extends AbstractEvent{
// data field to store the limit date
private Date limit;
// helper field to return the next occurrence
private int num_days = 0;
// method to set the limit date
public void setLimit(Date limit)
{
this.limit = limit;
}
// method to return the limit date
public Date getLimit()
{
return limit;
}
// method to return if the event has more occurrences
or not
@Override
public boolean hasMoreOccurrences() {
// check if the call to
nextOccurrence will return a date within the limit
Calendar cal =
Calendar.getInstance();
cal.setTime(getStartTime());
cal.add(Calendar.DATE,
num_days);
if(cal.getTime().compareTo(limit)
< 0)
return
true;
return false;
}
// method to return the next occurrence date
@Override
public Date nextOccurrence() {
Calendar cal =
Calendar.getInstance();
cal.setTime(getStartTime());
cal.add(Calendar.DATE,
num_days);
// if next occurrence date >
limit , return null, else return the next occurrence date
if(cal.getTime().compareTo(limit)
>= 0)
return
null;
num_days += 7; // increment the
number of days by 1 week
return cal.getTime();
}
// method to re-initialize the state of the object
so that a call to the method nextOccurrence() returns the date of
the first occurrence of this event
@Override
public void init() {
num_days = 0;
}
public String toString()
{
return("Start Date:
"+getStartTime()+" End Date : "+getEndTime()+" Limit Date :
"+limit);
}
}
//end of WeeklyEvent.java
Code:
// AbstractEvent.java : Java class to represent the
AbstractEvent
import java.util.Date;
public abstract class AbstractEvent {
// data fields
private String description;
private Date start_time;
private Date end_time;
// method to set the description of the
AbstractEvent
public void setDescription(String description) {
this.description =
description;
}
// method to set the start date for the
AbstractEvent
public void setStartTime(Date start) {
this.start_time = start;
}
// method to set the end date for the
AbstractEvent
public void setEndTime(Date end) {
this.end_time = end;
}
// method to return the description of the
AbstractEvent
public String getDescription() {
return description;
}
// method to return the start date of
AbstractEvent
public Date getStartTime() {
return start_time;
}
// method to return the end date of the
AbstractEvent
public Date getEndTime() {
return end_time;
}
// The below 3 methods must be define by the
concrete subclass of AbstractEvent
// abstract method hasMoreOccurrences() whose
implementation depends on the kind
// of event
public abstract boolean hasMoreOccurrences();
// abstract method nextOccurrence() whose
implementation depends on the kind of
// event
public abstract Date nextOccurrence();
// abstract method init() whose implementation
depends on the kind of event
public abstract void init();
}
//end of AbstractEvent.java
//DescriptionComparator.java
import java.util.Comparator;
public class DescriptionComparator implements Comparator<AbstractEvent> {
/*
* compares descriptions of events
*
* return 1 (o1.getDescription() >
o2.getDescription() lexicographically)
* return 0 (o1.getDescription() == o2.getDescription()
lexicographically)
* return -1 (o1.getDescription() <
o2.getDescription() lexicographically)
*/
@Override
public int compare(AbstractEvent o1, AbstractEvent o2)
{
return
o1.getDescription().compareTo(o2.getDescription());
}
}//end DescriptionComparator.java
//FirstOccComparator.java
import java.util.Comparator;
public class FirstOccComparator implements Comparator<AbstractEvent> {
/*
* compares start time of events
*
* return 1 (o1.getStartTime() > o2.getStartTime()
Date occurrence wise)
* return 0 (o1.getStartTime() == o2.getStartTime()
Date occurrence wise)
* return -1 (o1.getStartTime() < o2.getStartTime()
Date occurrence wise)
*/
@Override
public int compare(AbstractEvent o1, AbstractEvent o2)
{
return
o1.getStartTime().compareTo(o2.getStartTime());
}
}//end FirstOccComparator.java
//LastOccComparator.java
import java.util.Comparator;
public class LastOccComparator implements Comparator<AbstractEvent> {
/*
* compares end time of events
*
* return 1 (o1.getEndTime() > o2.getEndTime() Date
occurrence wise)
* return 0 (o1.getEndTime() == o2.getEndTime() Date
occurrence wise)
* return -1 (o1.getEndTime() < o2.getEndTime() Date
occurrence wise)
*/
@Override
public int compare(AbstractEvent o1, AbstractEvent o2)
{
return
o1.getEndTime().compareTo(o2.getEndTime());
}
}//end LastOccComparator.java
//Notifications.java
import java.util.Observable;
import java.util.Observer;
//Observes the changes in planning
public class Notifications implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out
.println("Notification :\n" + arg.toString() +
"\nNow Planner has " + ((Planner) o).size() + " events");
System.out.println("*********************************************************************************");
}
}//end Notifications.java
//Planner.java represents event planner
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Observable;
public class Planner extends Observable {
//holds all the events
private AbstractEvent[] events;
//hold the count of events in array
private int noOfEvents;
public Planner(int maxCapacity) {
//initialize the events array with
maxCapacity
events = new
AbstractEvent[maxCapacity];
}
//gives current count of events
public int size() {
//returns the no of elements
return noOfEvents;
}
//add events
public boolean addEvent(AbstractEvent event) {
//check that arrays is empty or
event passed is null
if (event ==null || noOfEvents
>= events.length) {
return
false;
}
//add event and increment number
Of events
events[noOfEvents++] = event;
//confirm that change has
happened.
setChanged();
//notify the Notification object
with details
notifyObservers("Event with details
below \n" + event.toString() + " added");
//return true if the event added
was null or not
return events[noOfEvents - 1] !=
null;
}
//returns the event at given index
public AbstractEvent eventAt(int pos) {
return events[pos];
}
//removes events
public AbstractEvent remove(int pos) {
//copy the event to be
deleted
AbstractEvent deletedEvent =
events[pos];
//back shift all the
events
for (int x = pos + 1; x <
events.length; x++) {
events[x - 1] =
events[x];
}
//event count decreased
noOfEvents--;
//marks event has changed for
notification
setChanged();
//notify the observers with
details
notifyObservers("Event with details
below \n" + deletedEvent.toString() + " deleted");
return deletedEvent;
}
//displays if there is occurrence of event is on
the given date
public void display(Date date) {
for (AbstractEvent event :
events) {
if
(event.nextOccurrence() != null) {
//checks if both date are same or not
if (event.nextOccurrence().compareTo(date) == 0)
{
System.out.println(event.toString());
}
}
}
}
//sorts event list
public void sort(Comparator<AbstractEvent> c)
{
//sorts the arrays as per
comparator
Arrays.sort(events, c);
}
@Override
public String toString() {
return "Event List " +
Arrays.toString(events) + "Max Capacity " + events.length;
}
} //end Planner.java
//WeeklyEvent.java
import java.util.Calendar;
import java.util.Date;
public class WeeklyEvent extends AbstractEvent {
// data field to store the limit date
private Date limit;
// helper field to return the next occurrence
private int num_days = 0;
// method to set the limit date
public void setLimit(Date limit) {
this.limit = limit;
}
// method to return the limit date
public Date getLimit() {
return limit;
}
// method to return if the event has more
occurrences or not
@Override
public boolean hasMoreOccurrences() {
// check if the call to
nextOccurrence will return a date within the limit
Calendar cal =
Calendar.getInstance();
cal.setTime(getStartTime());
cal.add(Calendar.DATE,
num_days);
if
(cal.getTime().compareTo(limit) < 0)
return true;
return false;
}
// method to return the next occurrence date
@Override
public Date nextOccurrence() {
Calendar cal =
Calendar.getInstance();
cal.setTime(getStartTime());
cal.add(Calendar.DATE,
num_days);
// if next occurrence date >
limit , return null, else return the next
// occurrence date
if (cal.getTime().compareTo(limit)
>= 0)
return
null;
num_days += 7; // increment the
number of days by 1 week
return cal.getTime();
}
// method to re-initialize the state of the object
so that a call to the method
// nextOccurrence() returns the date of the first
occurrence of this event
@Override
public void init() {
num_days = 0;
}
public String toString() {
return ("Start Date: " +
getStartTime() + " End Date : " + getEndTime() + " Limit Date : " +
limit);
}
}
//end of WeeklyEvent.java
//DailyEvent.java
import java.util.Calendar;
import java.util.Date;
public class DailyEvent extends AbstractEvent {
// data field to store the number of
recurrences
private int num_consecutive_days;
// helper field to return the next occurrence
private int days_occurred = 0;
// method to set the number of recurrences
public void setNumberOfConsecutiveDays(int num_days)
{
this.num_consecutive_days =
num_days;
}
// method to get the number of recurrences
public int getNumberOfConsecutiveDays() {
return num_consecutive_days;
}
// method to return if the event has more
occurrences or not
@Override
public boolean hasMoreOccurrences() {
// if days occurred < number of
consecutive days , then next occurrence is valid
if (days_occurred <
num_consecutive_days)
return
true;
return false;
}
// method to return the date of next
occurrence
@Override
public Date nextOccurrence() {
// if days_occurred >= number
of recurrences, return null (as it exceeds number
// of recurrences)
if (days_occurred >=
num_consecutive_days)
return null;
// return the next occurrence
date
Calendar cal =
Calendar.getInstance();
cal.setTime(getStartTime());
cal.add(Calendar.DATE,
days_occurred);
days_occurred++; // increment the
number of days by a day
return cal.getTime();
}
// method to re-initialize the state of the object
so that a call to the method
// nextOccurrence() returns the date of the first
occurrence of this event
@Override
public void init() {
days_occurred = 0;
}
public String toString() {
return ("Start Date: " +
getStartTime() + " End Date : " + getEndTime() + " Consecutive days
of occurrence : "
+ num_consecutive_days);
}
}
//end of DailyEvent.java
//TestEvent.java
import java.util.Calendar;
import java.util.Date;
//Tests the planner
public class TestEvent {
public static void main(String[] args) {
Planner planner = new Planner(3);
//add observer to planner
planner.addObserver(new
Notifications());
//initialize daily events
DailyEvent dailyEvent = new
DailyEvent();
dailyEvent.setDescription("Daily
Event");
dailyEvent.setStartTime(new
Date());
dailyEvent.setNumberOfConsecutiveDays(1);
Calendar cal =
Calendar.getInstance();
cal.setTime(dailyEvent.getStartTime());
cal.add(Calendar.DATE, 1);
dailyEvent.setEndTime(cal.getTime());
//add daily events through
planner
planner.addEvent(dailyEvent);
//initialize daily events
WeeklyEvent weeklyEvent = new
WeeklyEvent();
weeklyEvent.setDescription("Weekly Event");
weeklyEvent.setStartTime(new
Date());
cal =
Calendar.getInstance();
cal.setTime(dailyEvent.getStartTime());
cal.add(Calendar.DAY_OF_WEEK,
1);
weeklyEvent.setEndTime(cal.getTime());
weeklyEvent.setLimit(cal.getTime());
//add weekly events through
planner
planner.addEvent(weeklyEvent);
//remove weekly events through
planner
planner.remove(1);
}
}//end TestEvent
Registry Java Programming 2) Interface Comparator: The method for comparing two objects is written outside of...
In java please: TotalCostForTicket interface Create interface that includes one variable taxRate which is .09. It also includes an abstract method calculateTotalPrice which has no parameters and returns a value of type double. public abstract class Ticket { //There is a public static instance variable of type double for the basePrice. public static double basePrice; // private int theaterNumber; private int seatNumber; private boolean isTicketSold; private boolean isTicketReserved; public Ticket(int theaterNumber, int seatNumber) { this.theaterNumber = theaterNumber; this.seatNumber = seatNumber;...
Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LibraryCard { private String id; private String cardholderName; ...
Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add(Event evt) Add an event to the calendar Event get(int i) Fetch the ith Event added to the calendar Event get(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...
Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...
Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...
java
Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...
this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...
The following is for java programming. the classes
money date and array list are so I are are pre made to help with
the coding so you can resuse them where applicable
Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...
I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public class ItemToPurchase { // instance variables private String itemName; private String itemDescription; private int itemPrice; private int itemQuantity; // default constructor public ItemToPurchase() { this.itemName = "none"; this.itemDescription = "none"; this.itemPrice = 0; this.itemQuantity = 0; } public ItemToPurchase(String itemName, int itemPrice, int itemQuantity,String itemDescription) { ...
java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date { private String month; private String day; private String year; public Date(String month, String day, String year) { this.month = month; this.day = day; this.year = year; } public String getMonth() { return month; } public String getDay() { return day; } public String...