Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Trouble printing out the date for a method

    This is the class that's not main

    Java Code:
     /**
     * A class to give students experience using loops. This class creates and
     * manipulates objects of Greg's Date class.
     */
    public class SpeedDating
    {
        // Note: this class has no instance variables!
     
        /**
         * Creates an empty SpeedDating object so that you can call the methods (a
         * constructor that takes no parameters is known as a "default" constructor)
         */
        public SpeedDating()
        {
        }   // Constructor has empty body
     
        /**
         * Prints the day of the week (e.g. "Thursday") on which Halloween will fall
         * for 10 consecutive years.
         *
         * @param startYear the first of the 10 consecutive years
         */
        public void printHalloweens(int startYear)
                
       {
            int lastYear = startYear + 10;
            for (int i = startYear; i < lastYear; i++) {
                Date Halloween = new Date(10, 31, i);
                System.out.print(Halloween.getYear() + ", ");
                System.out.println(Halloween.getDayOfWeek() + ", ");
            }
     
            // TO DO: write body of this method here
        }
     
            // TO DO: write body of this method here
        
     
        /**
         * Computes and returns the Date on which Thanksgiving will fall in a given
         * year.
         *
         * NOTE: By law, Thanksgiving is the 4th Thursday in November
         *
         * @param year the year for which to compute the date of Thanksgiving
         * @return the Date of Thanksgiving for the specified year
         */
        public Date getThanksgiving(int year)
        {
           
            Date Thanksgiving = new Date(11, 1, year);
            int count = 1;
            int weekCount = 0;
            for ( ; count <= 30; count++) {
                if (Thanksgiving.getDayOfWeek().equals("Thursday")) {
                    weekCount++;
                }
                if (weekCount == 4) {
                    break;
                }
                Thanksgiving.next();
            }
            return Thanksgiving;
     
     
     
        }
        // TO DO: write body of this method here
     
        /**
         * Computes and returns the number of days between two dates, counting the
         * end date but not the start date. E.g., the number of days between
         * 11/1/2012 and 11/5/2012 is 4, not 5.
         *
         * Precondition: The start date must occur on or before the end date.
         *
         * @param start the earlier of the two dates
         * @param end the later of the two dates
         *
         * @return the number of days elapsed between the start date and the end
         * date
         */
        public int countingTheDays(Date start, Date end)
        {
     
            do{
                start.next();
            }while(start.getMonth()>end.getMonth() && start.getDay() < end.getDay());
               
     
            return start.getDay();
     
            // TO DO: write body of this method here
        }
    }
    This is my tester class

    Java Code:
    import javax.swing.JOptionPane;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author 
     */
    public class SpeedDatingTester {
       
        public static void main (String args[])
    	{
           
       SpeedDating date=new SpeedDating(); 
          
                  
                  String input = JOptionPane.showInputDialog
    			           ("Enter the halloween year ") ;
           
           int startYear = Integer.parseInt(input) ;
          
    
    date.printHalloweens(startYear);
    
    
    
    
    input = JOptionPane.showInputDialog
    			           ("Enter the year for november ") ;
           
           int novyear = Integer.parseInt(input) ;
         
            date.getThanksgiving(novyear) ; 
            
            Date Thanksgiving =  date.getThanksgiving(novyear) ;
            
            System.out.println(Thanksgiving) ;
    The problem is that I can't seem to print out the date. I get something like this Date@f449b8. What's going on? How can I fix it?
    Last edited by Michael305rodri; 11-04-2012 at 04:11 AM.

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Trouble printing out the date for a method

    See SimpleDateFormat (Java Platform SE 7 ) to format and print your Date object.

    ex.

    Java Code:
    		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
    		System.out.print(sdf.format(new Date()));
    Last edited by SJF; 11-04-2012 at 03:50 AM. Reason: example

  3. #3
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Trouble printing out the date for a method

    I'm still lost :(
    I now changed the code to this

    SpeedDating thanksgiving = new SpeedDating() ;


    input = JOptionPane.showInputDialog
    ("Enter the year for november ") ;

    int year = Integer.parseInt(input) ;


    System.out.println(thanksgiving.getThanksgiving(ye ar)) ;

    }
    }

    But it printed out Date@1e91a4d

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Trouble printing out the date for a method

    You need to put the Date() object that is being returned from thanksgiving.getThanksgiving(year) into SimpleDateFormat.
    What is currently being printed out is the default Object.toString() because Date() does not have an override for .toString() Java is telling you that it is a Date object at memory reference 1e91a4d.... Not very useful unless you know how to read memory at 1e91a4d and turn the bits there into a human readable time.... oh! wait! SimpleDateFormat can do that for you!

  5. #5
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Trouble printing out the date for a method

    Thank you. I now understand what's going on. The memory reference and all that you said is very similar to what my professor said. I'm still having trouble writing the code. So I import java.text.SimpleDateFormat; ? And SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy"); is kind of throwing me off. The parameters would be the day, month, and year? So I put the month and days as ints and for year i just put year because that's what the user has to input. I'm sorry... Is that I'm very new to programming :(

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Trouble printing out the date for a method

    So you'd create the SimpleDateFormat object with a format string in "EEE, d MMM yyyy" is 'Day name in week ', 'Day' 'Mon' 'Year' but look at the link above for other formats... like "dd/MM/yyyy"
    Then give the Date object that getThanksgiving(year) is providing to the SimpleDateFormat object

    Java Code:
    String tdayStr = sdf.format(thanksgiving.getThanksgiving(year));  // This is the string representation of the date
    System.out.println(tdayStr);

  7. #7
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Trouble printing out the date for a method

    I went to the site but I don't think they have that format :( The closest one is "EEE, MMM d, ''yy" thank you though I feel like I'm getting really close!
    I tried this:

    Java Code:
    String thanksdate = SpeedDating.format(thanksgiving.getThanksgiving(year));
        System.out.println(thanksdate);
    However it's giving me an error message. Cannot find symbol Symbol: method format(Date) location : class SpeedDating

  8. #8
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Trouble printing out the date for a method

    Please someone help. My assignment is due soon and I haven't been able to print the thanksgiving method correctly yet :(

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Trouble printing out the date for a method

    You're supposed to be using a SimpleDateFormat object.
    SJF even gave you the code above.
    Instead of 'new Date()' you would be passing in the Date object returned by your 'getThansgiving()' method.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Replies: 18
    Last Post: 08-09-2011, 08:08 AM
  2. Replies: 2
    Last Post: 05-12-2011, 04:52 PM
  3. Having trouble with printing a blank line.
    By Meta in forum New To Java
    Replies: 4
    Last Post: 05-11-2010, 10:54 PM
  4. Having trouble looping & printing output
    By carrotcake in forum New To Java
    Replies: 1
    Last Post: 04-04-2010, 05:37 AM
  5. Having printing trouble
    By random0munky in forum New To Java
    Replies: 5
    Last Post: 12-07-2009, 09:27 PM

Posting Permissions

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