Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to write a class with specific parameters (in comment blocks) and this is

ID: 3653718 • Letter: I

Question

I have to write a class with specific parameters (in comment blocks) and this is what I have so far, but I am stuck... [code] /* * this type supports date/time timestamp objects for 2012 * * Your task is to complete the DateTime methods (constructors, mutators, accessors, etc.) * The comments in each method describe what is necessary in that method. * Some methods (display methods and "math" methods) are already done for you. * * Note: This class will not compile correctly as is -- you must complete the required methods */ import java.util.Scanner; public class DateTime { private double dtStamp; // days since 12/31/2011 at midnight (1 - 366) // fractional part is the time of day // used by validation loops static Scanner in = new Scanner(System.in); // array dtDays used for converting between days in year and month/day - don't mess with it! public static final int[] dtDays = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; //// Constructor overloads //// public DateTime() { // sets dtStamp to 1 & announces "Happy New Year 2012!" } public DateTime( double dt ) { // sets dtStamp to dt } public DateTime( int month, int day ) { // call calcDTime method overload to initialize dtStamp } public DateTime( int month, int day, int hour, char amPm ) { // call calcDTime method overload to initialize dtStamp // (amPm is expected to be 'a' or 'p') } //// set... mutators for date & hour //// public void setDate(int month, int day) { // call calcDTime method overload to initialize dtStamp } public void setTime( int hour, char amPm ) { // convert hour to 24 hour time and make it a fractional day // Use it to adjust the value of dtStamp } //// get... accessors for 2012 day number & hour //// public int getDay() { // return integer part of dtStamp } public int getTime() { // return hour of dtStamp in 24 hour format // ( = fractional part of dtStamp * 24 ) } //// display methods (mainly for demo purposes) //// public String displayDate() { // returns dtStamp as a string: weekday, mm/dd/2012 int yrDays = (int)(dtStamp); int month; for(month = 0; month < 12; month++) if(yrDays < dtDays[month] + 1) break; int day = yrDays - dtDays[month-1]; String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; String weekday = dayNames[(yrDays-1)%7]; return weekday + ", " + month + '/' + day + "/2012 "; } public String displayDateTime() { // returns dtStamp as a string: mm/dd/2012 hh(+am or pm) double time = dtStamp - (int)(dtStamp); int hour = (int)(time * 24 + 0.5); String amPm = "pm"; if(hour > 12) hour -= 12; else if(hour < 12) amPm = "am"; String date = displayDate(); return date + hour + amPm; } public String elapsed( DateTime last ) { // calculate the elapsed time between last and this // and returns the difference in days as a string int days = last.getDay() - (int)(dtStamp); return days < 0 ? -days + " days ago" : days + " days from now"; } //// returns a DateTime object that is days after (or before) this one //// public DateTime nextDTime(int days) { double newDT = dtStamp + days; if(newDT < 0 || newDT > 366) { System.out.println("New date is not in 2012 - your program may crash"); return null; } return new DateTime(newDT); } //// interal calculation methods -- hands off! //// private double calcDTime( int month, int day ) { /* * loops are used to validate & correct (if necessary): * - month (1-12) * - day (1-daysInMonth) */ while(month < 1 || month > 12) { System.out.println("Invalid month. Enter a month 1-12"); month = in.nextInt(); } // given valid month, validate day (1 - daysInMonth) int daysInMonth = 31; switch(month) { case 4: case 6: case 9: case 11: daysInMonth = 30; break; case 2: daysInMonth = 29; break; } while(day < 1 || day > daysInMonth) { System.out.println("Invalid day. Enter a day 1-" + daysInMonth); day = in.nextInt(); } /* * calculate date day number in 2012 * for example, 2/1 is day number 32 * (uses an array for conversion - chapter 8) */ return dtDays[month-1] + day; } private double calcDTime(int month, int day, int hour, char amPm) { /* * call calcDTime overload to do date conversion, then, validate * hour (0-23) - adjusted for amPm = 'p' */ double date = calcDTime(month, day); while(hour < 1 || hour > 12) { System.out.println("Invalid hour. Enter hour 1-11 am/pm or 12 (pm)"); hour = in.nextInt(); } if(hour < 12 && amPm == 'p') hour += 12; date += hour/24.0; return date; } } [code]

Explanation / Answer

Please rate...

Completed program DateTime.java

======================================================

import java.util.Scanner;
public class DateTime
{
    private double dtStamp;
    // days since 12/31/2011 at midnight (1 - 366)
    // fractional part is the time of day
    // used by validation loops
    static Scanner in = new Scanner(System.in);
    // array dtDays used for converting between days in
    // year and month/day - don't mess with it!
    public static final int[] dtDays = {0, 31, 60, 91, 121, 152, 182,
            213, 244, 274, 305, 335};
    // Constructor overloads
    public DateTime()
    {
            // sets dtStamp to 1 & announces "Happy New Year 2012!"
        dtStamp=1;
        System.out.println("Happy New Year 2012!");
    }
    public DateTime( double dt )
    {
     // sets dtStamp to dt
        dtStamp=dt;
    }
    public DateTime( int month, int day )
    {
     // call calcDTime method overload to initialize dtStamp
        dtStamp=calcDTime(month,day);
    }
    public DateTime( int month, int day, int hour, char amPm )
    {
     // call calcDTime method overload to initialize dtStamp
     // (amPm is expected to be 'a' or 'p')
        dtStamp=calcDTime(month,day,hour,amPm);
    }
     // set... mutators for date & hour
    public void setDate(int month, int day)
    {
      // call calcDTime method overload to initialize dtStamp
        dtStamp=calcDTime(month,day);
    }
    public void setTime( int hour, char amPm )
    {
     // convert hour to 24 hour time and make it a fractional day
     // Use it to adjust the value of dtStamp
        while(hour < 1 || hour > 12)
        {
            System.out.println("Invalid hour. Enter hour 1-11 am/pm or 12 (pm)");
            hour = in.nextInt();
        }
        if(hour < 12 && amPm == 'p')
            hour += 12;
        dtStamp += hour/24.0;
    }
     // get... accessors for 2012 day number & hour
    public int getDay()
    {
     // return integer part of dtStamp
        int t=(int)dtStamp;
        return t;
    }
    public int getTime()
    {
     // return hour of dtStamp in 24 hour format //
     // ( = fractional part of dtStamp * 24 )
        int t=(int)dtStamp;
        double fp=dtStamp-t;
        return (int)(fp*24);
    }
     // display methods (mainly for demo purposes) //
    public String displayDate()
    {
     // returns dtStamp as a string: weekday, mm/dd/2012
        int yrDays = (int)(dtStamp);
        int month;
        for(month = 0; month < 12; month++)
            if(yrDays < dtDays[month] + 1) break;
        int day = yrDays - dtDays[month-1];
        String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        String weekday = dayNames[(yrDays-1)%7];
        return weekday + ", " + month + '/' + day + "/2012 ";
    }
    public String displayDateTime()
    {
     // returns dtStamp as a string: mm/dd/2012 hh(+am or pm)
        double time = dtStamp - (int)(dtStamp);
        int hour = (int)(time * 24 + 0.5);
        String amPm = "pm";
        if(hour > 12) hour -= 12;
        else if(hour < 12) amPm = "am"; String date = displayDate();
        return date + hour + amPm;
    }
    public String elapsed( DateTime last )
    { // calculate the elapsed time between last and this
      // and returns the difference in days as a string
      int days = last.getDay() - (int)(dtStamp);
        return days < 0 ? -days + " days ago" : days + " days from now";
    }
     // returns a DateTime object that is days after (or before) this one
    public DateTime nextDTime(int days)
    {
        double newDT = dtStamp + days;
        if(newDT < 0 || newDT > 366)
         {
             System.out.println("New date is not in 2012 - your program may crash");
             return null;
         }
        return new DateTime(newDT);
    }
     // interal calculation methods -- hands off!
     private double calcDTime( int month, int day )
     {
      /* * loops are used to validate &
      correct (if necessary): * - month (1-12) * - day (1-daysInMonth) */
         while(month < 1 || month > 12)
         {
             System.out.println("Invalid month. Enter a month 1-12");
             month = in.nextInt();
         }
          // given valid month, validate day (1 - daysInMonth)
          int daysInMonth = 31;
         switch(month)
         {
             case 4: case 6: case 9: case 11: daysInMonth = 30; break;
             case 2: daysInMonth = 29; break;
         }
         while(day < 1 || day > daysInMonth)
         {
             System.out.println("Invalid day. Enter a day 1-" + daysInMonth);
             day = in.nextInt();
         }
          /* * calculate date day number in 2012 * for example, 2/1 is
          day number 32 * (uses an array for conversion - chapter 8) */
         return dtDays[month-1] + day;
     }
    private double calcDTime(int month, int day, int hour, char amPm)
    {
     /* * call calcDTime overload to do date
     conversion, then, validate * hour (0-23) - adjusted for amPm = 'p' */
        double date = calcDTime(month, day);
        while(hour < 1 || hour > 12)
        {
            System.out.println("Invalid hour. Enter hour 1-11 am/pm or 12 (pm)");
            hour = in.nextInt();
        }
        if(hour < 12 && amPm == 'p')
            hour += 12; date += hour/24.0; return date;
    }
}