Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Homwork question again

  • 17-07-2013 6:55pm
    #1
    Registered Users, Registered Users 2 Posts: 34,460 ✭✭✭✭


    I have been having real difficulty trying to solve this.

    Its a past exam paper in which I have been at it since Monday and getting anything but progress

    here is the question and my answer is below. Could anyone help me check if first 3 parts are right and help with d & e.

    Thanks in advance

    Write the code for the following methods in the newDate class
    a
    public boolean validDate (int day, int month, int year)
    The month can only be in the range 1-12 and a month must have the correct number of days eg 30.2.1997 is invalid because February can only have 28 days, or 29 days if the year is a leap year.
    This method must return true if the date is valid and false if the date is
    invalid.
    b
    public boolean leap Year (int year)
    If the year can be evenly divided by 4, but not 100, it is a leap year.
    However, years that are evenly divisible by 400 are also leap years.
    This method must return true if the year is a leap year and false if the year
    is not a leap year.
    c
    public int julianDate (int day, int month, int year)
    The Julian calendar numbers each day of the year according to its ordinal
    value, from 1 to 365 (or, in a leap year, from 1 to 366). Therefore,February1 has a Julian date of 32.This method must return
    the value of the Julian date (ie the number of
    days).
    d
    public int daysElapsed (int day1, int month1, int year1, int day2, int month2, int year2)
    This method returns the actual number of days between any 2 dates.
    e
    public int ageDate (int dayBirth, int monthBirth, int yearBirth)
    This method must return a person’s age in years as at today’s date.
    package oop;

    public class NewDate2Assignment {

    //31 12 2004 //true
    //30 2 2005 //false
    //29 2 2008 //true
    //29 2 2002 //false
    //78 22 2012 //false
    public boolean validDate(int day, int month, int year){

    if(true){
    return true;
    }else{
    return false;
    }

    }


    public boolean leapYear(int year){

    if((year % 4 == 0 && year % 100 != 0)||year % 400 == 0){
    return true;
    }else{
    return false;
    }

    }

    public int julianDate(int day, int month, int year){

    if ((month == 9 || month ==4 || month == 11)&& day> 30 {
    return false;
    }else{
    return true;

    }

    public int daysElapsed(int day1, int month1, int year1, int day2, int month2, int year2){

    }

    public int ageDate (int dayBirth, int monthBirth, int yearBirth){

    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    Questions D and E aren't quite as simple as the earlier questions as I'm sure you surmised. I might mention that dealing with dates in programming can have a lot of gotchas and Java libraries are already built to help with these but I don't imagine you would be allowed use them. In terms of gotchas what you have to handle are months with varying number of days and years with varying number of days (leap years).

    So for Question D a good approach might be to convert the months into days. So for example 7th of February would actually be day 38 of that year. In this manner we have managed to remove the complexity of the varying lengths of months. It does mean that we have to calculate, in our code, the number of days in the months that have been and gone since the start of the year.

    Our second gotcha, the leap years, need to be handled. To do this we might want to determine how many leap years occur between our earliest date Date1 and our later date Date2. So every year between the year of Date1 and Date2 has to be checked if it's a leap year (we have a method for doing this already!). After checking them all we'll have nearly ended up with the number of leap years that occured. There is another gotcha that we see now. This is what if Date1 occurs before Feb 29 of the current year and Date2 occurs after Feb 29 of the current year? We'll have to check for this and add between 0 and 2 more leap years (depending on when Date1 and Date2 fall) to the number of leap years we currently have. This final number is now one of the figures to allow us to find the days between Date1 and Date2.

    After all that we have now managed to handle both of the gotchas I mentioned!

    So our code might look something like this:
    public int daysElapsed(int day1, int month1, int year1, int day2, int month2, int year2){
    
        int dayOfYear1;
    
        // we implement code to find the value of dayOfYear1, e.g. convert day and months into a single number of days
    
        int dayOfYear2;
    
        // we do the same for dayOfYear2
    
        int leapYearCount;
    
        // we calculate the number of extra days due to leap years we need to factor for 
    
    }
    
    

    We're well on our way at this point. Now we have to figure out how to handle the difference in years and how to come up with the overall days between the dates. We can just count the number of years the dates are seperated by by substracting the small year from the larger year. If we multiply this numer by 365 and add on our leap year count we come to the last important figure we need for our final result. This figure is the total number of day between the Jan 1st of year1 and Jan 1st of year 2 which we will might call yearGapInDays. If you think about it all we need to do to get the final figure is to subtract dayOfYear1 from yearGapInDays and add dayOfYear2 on to yearGapInDays.

    We now have a working solution.

    I've given you all the information you need to complete Question D but it should still be challenging enough to complete. When you do complete it you should try to think up a similar procedure to complete Question E. I might just say you might just want to "hard code" the current date for E. This means creating an int for the day, month and year and just giving them a value. Alternatively you will have to use the Date class which is described HERE but that might just be a level of complexity you're not ready to handle.


  • Registered Users, Registered Users 2 Posts: 2,021 ✭✭✭ChRoMe


    Aswerty wrote: »
    Questions D and E aren't quite as simple as the earlier questions as I'm sure you surmised. I might mention that dealing with dates in programming can have a lot of gotchas and Java libraries are already built to help with these but I don't imagine you would be allowed use them. In terms of gotchas what you have to handle are months with varying number of days and years with varying number of days (leap years).

    So for Question D a good approach might be to convert the months into days. So for example 7th of February would actually be day 38 of that year. In this manner we have managed to remove the complexity of the varying lengths of months. It does mean that we have to calculate, in our code, the number of days in the months that have been and gone since the start of the year.

    Our second gotcha, the leap years, need to be handled. To do this we might want to determine how many leap years occur between our earliest date Date1 and our later date Date2. So every year between the year of Date1 and Date2 has to be checked if it's a leap year (we have a method for doing this already!). After checking them all we'll have nearly ended up with the number of leap years that occured. There is another gotcha that we see now. This is what if Date1 occurs before Feb 29 of the current year and Date2 occurs after Feb 29 of the current year? We'll have to check for this and add between 0 and 2 more leap years (depending on when Date1 and Date2 fall) to the number of leap years we currently have. This final number is now one of the figures to allow us to find the days between Date1 and Date2.

    After all that we have now managed to handle both of the gotchas I mentioned!

    So our code might look something like this:
    public int daysElapsed(int day1, int month1, int year1, int day2, int month2, int year2){
    
        int dayOfYear1;
    
        // we implement code to find the value of dayOfYear1, e.g. convert day and months into a single number of days
    
        int dayOfYear2;
    
        // we do the same for dayOfYear2
    
        int leapYearCount;
    
        // we calculate the number of extra days due to leap years we need to factor for 
    
    }
    
    

    We're well on our way at this point. Now we have to figure out how to handle the difference in years and how to come up with the overall days between the dates. We can just count the number of years the dates are seperated by by substracting the small year from the larger year. If we multiply this numer by 365 and add on our leap year count we come to the last important figure we need for our final result. This figure is the total number of day between the Jan 1st of year1 and Jan 1st of year 2 which we will might call yearGapInDays. If you think about it all we need to do to get the final figure is to subtract dayOfYear1 from yearGapInDays and add dayOfYear2 on to yearGapInDays.

    We now have a working solution.

    I've given you all the information you need to complete Question D but it should still be challenging enough to complete. When you do complete it you should try to think up a similar procedure to complete Question E. I might just say you might just want to "hard code" the current date for E. This means creating an int for the day, month and year and just giving them a value. Alternatively you will have to use the Date class which is described HERE but that might just be a level of complexity you're not ready to handle.

    Great post, however I'd just use the Java/Joda time library. If this is a java exam the JDK should be allowed.


  • Registered Users, Registered Users 2 Posts: 34,460 ✭✭✭✭The_Kew_Tour


    public int daysElapsed (int day1, int month1, int year1, int day2, int month2, int year2)
    This method returns the actual number of days between any 2 dates.


    could anybody answer this for me? I have everything else, but struggling on this.

    Been at it for past 6 hours and not getting anywhere(yes im stupid:pac:)

    thanks million for all help by way. Greatly appreciated. I want to work things out myself, but sometimes you just have no prayer.


  • Registered Users, Registered Users 2 Posts: 2,021 ✭✭✭ChRoMe


    public int daysElapsed (int day1, int month1, int year1, int day2, int month2, int year2)
    This method returns the actual number of days between any 2 dates.


    could anybody answer this for me? I have everything else, but struggling on this.

    Been at it for past 6 hours and not getting anywhere(yes im stupid:pac:)

    thanks million for all help by way. Greatly appreciated. I want to work things out myself, but sometimes you just have no prayer.

    Stackoverflow.com is your friend

    http://stackoverflow.com/questions/7103064/java-calculate-the-number-of-days-between-two-dates


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    Hmm, if use of the JDK is allowed then implementing question D would be quite trivial. I'd have hoped that at this level of learning they're being shown how to implement basic algorithms instead of implementing the prepackaged methods that already exists. Can you shed any light on this The_Kew_Tour, can you use the prepacked date and time libraries?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,021 ✭✭✭ChRoMe


    Aswerty wrote: »
    Hmm, if use of the JDK is allowed then implementing question D would be quite trivial. I'd have hoped that at this level of learning they're being shown how to implement basic algorithms instead of implementing the prepackaged methods that already exists. Can you shed any light on this The_Kew_Tour, can you use the prepacked date and time libraries?

    Yeah the gut feeling I had was for them to do it by hand, however if its not explicitly stated by the brief, then I'd consider it fair game.


  • Registered Users, Registered Users 2 Posts: 34,460 ✭✭✭✭The_Kew_Tour


    Aswerty wrote: »
    Hmm, if use of the JDK is allowed then implementing question D would be quite trivial. I'd have hoped that at this level of learning they're being shown how to implement basic algorithms instead of implementing the prepackaged methods that already exists. Can you shed any light on this The_Kew_Tour, can you use the prepacked date and time libraries?

    Hi

    We are using Eclipse.

    Im not sure what you mean by prepacked date and libaries. I really am a noob when it comes to computers. Not your fault that is mine.

    Have done 5 modules so far and have passed them both theory and practical but when comes to Java im really stuck

    thanks for your and other poster help btw. Really appreciate it.


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    I currently do .Net development so am so used to calling everything libraries. What I mean are Java packages which are provided as part of the Java Development Kit that contain functionality common across many different types of applications. We can import a package that gives us access to Date and Time functionality at the top of our Java files. So for example adding the following to the top of your file would give you access to the Date and Calendar classes which could aid you in completing your homework (the JDK Date class is pretty poor and that's why Chrome mentioned using the Joda Time library/package).
    package java.util;
    What me and Chrome were discussing was that chances are your lecturer did not intend for you to use them since you're not going to learn much that way.

    Also I don't expect anyone is going to give you the answer since you or others in your class might end up just submitting it as their own work. Not that I'm pointing fingers at you. Your lecturer if they are any use should give you an example solution if you ask once the homework is handed in.


  • Registered Users, Registered Users 2 Posts: 34,460 ✭✭✭✭The_Kew_Tour


    No Worries


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    A, B and C are building blocks for D.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    I never actually read C. It is of course what I ended up describing in the first part of my solution.


Advertisement