Question

Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

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> sortByDate()

A sorted list of all events, ordered by event date.

java.util.ArrayList<Event> sortByName()

A sorted list of all events, ordered by the event names.

java.lang.String toString()

I have everything working except for the sortByDate() and the sortByName() methods.

public java.util.ArrayList<Event> sortByName()

A sorted list of all events, ordered by the event names.

This is an Accessor. It must not affect the order that the events were added to the calendar

Returns:

a list of events ordered by the alphabetical order of the event names

*********************************************************

public java.util.ArrayList<Event> sortByDate()

A sorted list of all events, ordered by event date.

This is an Accessor. It must not affect the order that the events were added to the calendar

Returns:

a list of events ordered by event date

Here is what I have written so far.

import java.util.ArrayList;

public class MyCalendar {

private ArrayList<Event> calendar;

public TRAPSCalendar() {

calendar = new ArrayList<Event>();

}

public boolean add​(Event evt) {

return calendar.add(evt);

}

public Event get​(int i) {

if (i < 0 || i > calendar.size()) {

return null;

}

return calendar.get(i);

}

public Event get​(java.lang.String name) {

for(int i = 0; i <calendar.size(); i++) {

if(calendar.get(i).getEventName().equals(name)) {

return calendar.get(i);

}

}

return null;

}

public int size() {

return calendar.size();

}

public java.util.ArrayList<Event> list() {

//return calendar;

return new ArrayList<Event>(calendar);

}

public java.util.ArrayList<Event> sortByName() {

}

public java.util.ArrayList<Event> sortByDate() {

}

public java.lang.String toString() {

String data = "There are a total of " + calendar.size()

+ " Events on the Calendar ";

if(calendar.size() > 0) {

data += "and their names are: ";

}

for(int i = 0; i < calendar.size(); i++) {

data += "\n" + (i+1) + ". " + calendar.get(i).getEventName();

}

return data;

}

}

For the sort methods we have to follow these guidelines

You are to modify the book’s selection sort code to sort Events by date and name. You may add these methods to your MyCalendar class directly. Alternatively, you can put them in a different class and have the MyCalendar class call them as needed.

Note that the sort methods must not change the order that the MyCalendar stores its events. You should create a copy of the Event ArrayList, and sort the copy. Be sure to test that, after sorting, “get(0)” returns the first Event added to the MyCalendar.

Here is the SelectionSorty class

public class SelectionSort {

/** The method for sorting the numbers */

public static void selectionSort(double[] list) {

for (int i = 0; i < list.length - 1; i++) {

// Find the minimum in the list[i..list.length-1]

double currentMin = list[i];

int currentMinIndex = i;

for (int j = i + 1; j < list.length; j++) {

if (currentMin > list[j]) {

currentMin = list[j];

currentMinIndex = j;

}

}

// Swap list[i] with list[currentMinIndex] if necessary;

if (currentMinIndex != i) {

list[currentMinIndex] = list[i];

list[i] = currentMin;

}

}

}

}

Here is the Event Class- it works fine, but you might need it to understand my problem

public class Event {

private String name;

private String venue;

private String eventDate;

private int ticketsSold;

private int ticketPrice;

private int overhead;

public Event() {

}

public Event(String eventName, String eventVenue) {

this.name = eventName;

this.venue = eventVenue;

}

public Event(String eventName,

String eventVenue,

String date,

int ticketsSold,

int ticketPrice,

int overhead) {

this.name = eventName;

this.venue = eventVenue;

this.ticketsSold = ticketsSold;

this.ticketPrice = ticketPrice;

this.overhead = overhead;

this.eventDate = date;

}

public int compareDate(Event other) {

String x = other.getDate();

String[] arrX = x.split("-");

String y = this.getDate();

String[] arrY = y.split("-");

int o = Integer.parseInt(arrX[0]); //other event year

int o1 = Integer.parseInt(arrX[1]);// other event month

int o2 = Integer.parseInt(arrX[2]);// other event day

int t = Integer.parseInt(arrY[0]);// this even year

int t1 = Integer.parseInt(arrY[1]);// this even month

int t2 = Integer.parseInt(arrX[2]);//this even day

if(x.equals(y)) {

return 0;

}

else if(o > t){

return -1;

}

else if(o < t) {

return 1;

}

else if(o == t && o1 > t1 ) {

return -1;

}

else if (o == t && o1 == t1 && o2 > t2 ) {

return -1;

}

else {

return 1;

//if(o == t && o1 == t1 && o2 > t2 ) {

//return -1;

//} else {

//return 1;

}

}

public int compareName(Event other) {

String tN = this.getEventName();

String oN = other.getEventName();

int x = tN.compareToIgnoreCase(oN);

if(x == 0) {

return 0;

}else if (x > 0) {

return 1;

}else {

return -1;

}

}

public int compareProfit(Event other) {

int tP = this.getProfit();

int oP = other.getProfit();

if(tP == oP) {

return 0;

} else if(tP > oP) {

return 1;

} else {

return -1;

}

}

public int getBreakEvenPoint() {

int x = (int) Math.ceil(overhead/ticketPrice);

return x;

}

public String getDate() {

return eventDate;

}

public String getEventName() {

return name;

}

public String getEventVenue() {

return venue;

}

public int getOverhead() {

return overhead;

}

public int getProfit() {

return (ticketPrice * ticketsSold) - overhead;

}

public int getTicketPrice() {

return ticketPrice;

}

public int getTicketsSold() {

return ticketsSold;

}

public boolean sellTickets​(int numberOfTickets) {

this.ticketsSold = ticketsSold + numberOfTickets;

if(numberOfTickets <= 0) {

return false;

}

if(this.ticketsSold <= ticketsSold) {

return false;

} else {

return true;

}

}

public void setDate​(String date) {

this.eventDate = date;

}

public void setEventName​(String eventName) {

this.name = eventName;

}

public void setEventVenue​(String eventVenue) {

this.venue = eventVenue;

}

public void setOverhead​(int overhead) {

this.overhead = overhead;

}

public void setTicketPrice​(int ticketPrice) {

this.ticketPrice = ticketPrice;

}

public void setTicketsSold​(int ticketsSold) {

if(ticketsSold < 0) {

ticketsSold = 0;

} else {

this.ticketsSold = ticketsSold;

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below are the modified files. It assumes that your Event class's methods compareDate() and compareName() are implemented correctly.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

MyCalendar.java
----------
import java.util.ArrayList;

public class MyCalendar {

private ArrayList<Event> calendar;

public MyCalendar() {

calendar = new ArrayList<Event>();

}

public boolean add (Event evt) {

return calendar.add(evt);

}

public Event get (int i) {

if (i < 0 || i > calendar.size()) {

return null;

}

return calendar.get(i);

}

public Event get (java.lang.String name) {

for(int i = 0; i <calendar.size(); i++) {

if(calendar.get(i).getEventName().equals(name)) {

return calendar.get(i);

}

}

return null;

}

public int size() {

return calendar.size();

}

public java.util.ArrayList<Event> list() {

//return calendar;

return new ArrayList<Event>(calendar);

}

public java.util.ArrayList<Event> sortByName() {
ArrayList<Event> copy = new ArrayList<Event>(calendar);
SelectionSort.sortByName(copy);
return copy;
}

public java.util.ArrayList<Event> sortByDate() {
ArrayList<Event> copy = new ArrayList<Event>(calendar);
SelectionSort.sortByDate(copy);
return copy;

}

public java.lang.String toString() {

String data = "There are a total of " + calendar.size()

+ " Events on the Calendar ";

if(calendar.size() > 0) {

data += "and their names are: ";

}

for(int i = 0; i < calendar.size(); i++) {

data += "\n" + (i+1) + ". " + calendar.get(i).getEventName();

}

return data;

}

}

SelectionSort.java
--------
import java.util.ArrayList;

public class SelectionSort {

/** The method for sorting the numbers */

public static void selectionSort(double[] list) {

for (int i = 0; i < list.length - 1; i++) {

// Find the minimum in the list[i..list.length-1]

double currentMin = list[i];

int currentMinIndex = i;

for (int j = i + 1; j < list.length; j++) {

if (currentMin > list[j]) {

currentMin = list[j];

currentMinIndex = j;

}

}

// Swap list[i] with list[currentMinIndex] if necessary;

if (currentMinIndex != i) {

list[currentMinIndex] = list[i];

list[i] = currentMin;

}

}

}

public static void sortByDate(ArrayList<Event> list) {

for (int i = 0; i < list.size() - 1; i++) {

// Find the minimum in the list[i..list.length-1]

Event currentMin = list.get(i);

int currentMinIndex = i;

for (int j = i + 1; j < list.size(); j++) {

if (currentMin.compareDate(list.get(j)) > 0) {

currentMin = list.get(j);

currentMinIndex = j;

}

}

// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {

list.set(currentMinIndex, list.get(i));
list.set(i, currentMin);
}
}

}

public static void sortByName(ArrayList<Event> list) {

for (int i = 0; i < list.size() - 1; i++) {

// Find the minimum in the list[i..list.length-1]

Event currentMin = list.get(i);

int currentMinIndex = i;

for (int j = i + 1; j < list.size(); j++) {

if (currentMin.compareName(list.get(j)) > 0) {

currentMin = list.get(j);

currentMinIndex = j;

}

}

// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {

list.set(currentMinIndex, list.get(i));
list.set(i, currentMin);
}
}

}

}

Add a comment
Know the answer?
Add Answer to:
Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble with the daysSince() Method in bold at the end of the program public...

    I'm having trouble with the daysSince() Method in bold at the end of the program public class Date { static final int [] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private int day, month, year; public Date() { day = 1; month = 1; year = 2000; } public Date(int d, int m, int y) { day = d; month = m; year = y; } public Date (String s) { setDate(s);...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • I am having a problem with my code. Here are the relevant parts. The method priceNow...

    I am having a problem with my code. Here are the relevant parts. The method priceNow is supposed to take as an input a string in the form "Q1'20" (for example) and calculate the new price knowing that the price of the CPU decreases by 2% every quarter. Here is my class definition code: public class CPUv2 {    private String launchDate;    public CPUv2() {        this.CPUGeneration = 1;        this.CPUSeries = "i3";        this.suggestedPrice =...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • I need help with the last method listed in the problem: Implement a class Grid that...

    I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • please help with program,these are he requirements: I am also getting a "missing return statement" in...

    please help with program,these are he requirements: I am also getting a "missing return statement" in my milestone class Create a new class Milestone which contains at least an event description, a planned completion date, and an actual completion date (which will be unset when the milestone is created). There should be a method to changed the planned completion date as well as a method to designate the Milestone as achieved. Add a method that returns whether the milestone has...

  • 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT