Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with assignment please

    Hello i need help with an assignment i have to do. using five classes, i have to create a planner that uses an array and can store an "activity" for a specific date and time. here's two classes done already:

    OurDate
    OurTime

    The three other classes are as follows (and what they need to contain):

    Event class:

    Data fields:
    date: OurDate - the day/month/year of the event
    time: OurTime - the hour/minute that event starts at
    description: String - a description of event
    Methods:
    default constructor
    initial constructors
    getDate () : OurDate - returns date of event
    getTime() : OurTime - returns time of event
    getDescription () : String - returns description of event
    setDate (OurDate) - sets date to parameter value
    setTime (OurTime) - sets time to parameter value
    setDescription (String) - sets description to parameter value
    toString() : String - displays event to a String
    inputFromKeyboard () - prompts input from keyboard for all
    data fields

    Planner class:

    Data fields:
    activities : Event[] - array of Event
    numEvents : int - how many events are stored in the activities
    array
    Methods:
    default constructor
    inputActivity () - adds an Event to the activities array if there
    is room and if there is no other activity for that date and time
    displayOneDay(OurDate) - looks through array and displays
    all events for the parameter date
    displaySevenDays (OurDate) - displays events for the seven
    days starting at parameter date
    deleteEvent (OurDate, OurTime) - looks through array for an
    event at parameter date and time, and deletes it (if found)

    and finally Assignment class, which only has method main and loops everything. note i cant use any librarys and stuff because we havent done anything with them yet.

    i cant figure this out for the life of my. what goes in the setDate getDate setTime getTime fields? how do i tell teh array to check if a date and time is already entered?

    help is greatly, greatly appreciated.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The get* methods don't accept any parameters. They simply return the result of the datatype specified. So the getDate would return a type OurDate which will be what is stored within the property date (which I don't like the property name of, but that's not my call). Setters are reverse, so the set* methods accept the datatype provided within the brackets and return void. Those would set the associated property.

    Since you are just using an array, you simply iterate it and check each item until you reach the end. To check them, you simply iterate the activities and compare each existing activity. You can simply check each field, or if you are far enough to do it, implement the Comparator interface and write the compareTo. A hint to do that with just comparisons of each property:
    PHP Code:
    boolean bConflict false;
    for (
    int i 0this.activities.length && !bConflict; ++i)
    {
        
    Event e this.activities[i];
        if (
    e.getDate().isEqual(myNewEvent.getDate()))
        {
            if (
    e.getTime().isEqual(myNewEvent.getTime()))
            {
                
    // this condition is no good.
                
    bConflict true;
            }
        }
    }

    if (!
    bConflict)
    {
        
    // this is a.o.k.
    }
    else
    {
        
    // this is no good.

    I'm not sure why you've written the isEqual method. Java does have a built in equals method that can be overridden, the only difference is you have to check the argument instanceof type to verify that its the proper datatype for the equals check. If anything, I'd suggest using the equals, or if isEqual is a must I'd override the equals and than chain the isEqual to it.

    Also, the array will potentially give you problems. Since you cannot resize the array by simply appending to it, you'll need to keep track of the size or use try/catch exceptions to see if it goes out of bounds (currently you do have a variable for the size of the array, which I assume is the existing items currently, and can be compared to the length). Copying an array is a simple matter of constructing a new array that is larger, than iterating each item to add to it, then assigning that back to the member. Or you can use the Arrays class which has a copyOf method that should do the trick if you need to resize. If you weren't stuck on Event[] datatype in the instructions, I'd actually suggest a priority queue based on the Date and Time provided. Add an event, and it automatically inserts it based on date and time, so when you iterate the list it would be sequential in order of date and time.

    You'll need to look over the date handling as well. Its not quite as cut and dry as you have here; if you change the month, the number of days will change which may alter the currently assigned day. Same goes with the year if it were february 29 for example.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    blahblahblah (01-29-2013)

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you very much for the hep Fou-Lu. I was able to get my program fully working. The only small problem I have left is when I delete an event, the most recent activity is deleted rather than what i specified. This is the code I am using:

    Code:
    public void deleteEvent() {
    		Scanner input = new Scanner(System.in);
    		OurDate date = new OurDate();
    		OurTime time = new OurTime();
    		date.inputFromKeyboard();
    		time.inputFromKeyboard();
    		for (int i = 1; i <= numEvents; i++) {
    		    Event e = this.activities[i];
    		    if (e.getDate().isEqual(activities[numEvents].getDate()) && e.getTime().isEqual(activities[numEvents].getTime())) {
    		    	activities[i] = null;
    		    	numEvents--;
    		        activities[i] = new Event();
    		    }
    		}
    	}
    I have to do this without an array list as well.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You're comparing the last event to the first event for if it should be set to null. The signature you have is also incorrect, as the original post indicates this takes the date and time.
    It's simpler than this, even without the use of Arrays or collections:
    PHP Code:
    for (int i 0this.activities.length; ++i// don't start at 1.  numEvents also won't really be usable here.
    {
        
    Event e this.activities[i];
        if (
    != null)
        {
            if (
    e.getDate().isEqual(date) && e.getTime().isEqual(time))
            {
                
    this.activities[i] = null;
                --
    this.numEvents;
                
    // not sure if you want to instantiate a new event.
            
    }
        }

    Now, you need to decide if you want to leave a spacer within the array or not. If memory is a non-issue, than I say just leave it, BUT you will need to modify any code in which you iterate to verify it's not null prior to accessing it. So if you hit the array[4] and it has a null on it, you cannot check the value of this since it doesn't exist. Personally, I would either leave the null over adding a default event with no data within it, or I would shift the array back.

    Edit:
    I should mention, this works okay in my head, but it is untested. Pretty sure its as easy as that in Java :P

    Edit:
    Hmm, I'm wondering as well if a [do/]while loop would be better here. You'll need to add a terminating condition too, there's no reason continuing through the array if you've already verified that the item was removed (you can only have one slot of date and time).


    Last edited by Fou-Lu; 01-29-2013 at 03:28 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    blahblahblah (01-29-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •