Results 1 to 10 of 10
  1. #1
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Java Calendar Help

    I have having a few problems with my code. (everything does compile without errors)
    First and foremost, my calendar is not starting on the correct day meaning depending on the "Year you were born" it dictates when it will start and that should just be a constant.
    Number two, is there an easy way to do a "word wrap" I am trying to implement instructions for the user to follow but they are too long and run off the applet.

    Thanks in advance for any help/guidance.
    -AJB5876

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package birthdayapplet;
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    
    /**
     *
     * @author AB
     */
    public class BirthdayApplet extends Applet implements ActionListener
    {
        public static int WIDTH=500, HEIGHT=500;
        private Button displayButton;
        private TextField monthField, dayField, yearField;
        private int inputMonth, inputDay, inputYear = 1, dayPrinter = 1;
        private int firstDayofYear = 2, firstDayOfMonth = 0, firstDayofWeek = 2;
        private String year = " ", days = " ";
     
        // Creates arrays to store days in month and names
        int[] daysInMonth = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String[] monthNames = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        int[] daysInWeek = {0, 1, 2, 3, 4, 5, 6};
        String[] daysOFweek = {"Sunday", "Monday", "Tuesday", "Wednsday", "Thursday", "Friday", "Saturday"};
      
        public void init()
        {
            setSize (WIDTH, HEIGHT);
        
            displayButton = new Button ("Display Your Birthday");  // Creates buttons, textfields, labels
            displayButton.addActionListener(this);
            monthField = new TextField (2);
            dayField = new TextField (2);
            yearField = new TextField (4);
            Label monthLabel = new Label ("Month");
            Label dayLabel = new Label ("Day");
            Label yearLabel = new Label ("Year");
                
            add(monthLabel); // Adds the buttons, labels, and textfields to applet
            add(monthField);
            add(dayLabel);
            add(dayField);
            add(yearLabel);
            add(yearField);
            add(displayButton);
        }
        
        private static boolean isLeapYear(int year) // Determines if input year is leap year
        {
            if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
            
        private int getDay(int month, int year)
        {
            int jday = 0;
            for (int i = 1; i < inputMonth; i++)
            {
                jday = jday + daysInMonth[i];
            }
            jday = jday + inputDay;
            return jday;
        }
     
        private int getDayOfWeek(int jday) // Finds day of the week birthday falls on
        {
            return (jday + firstDayofWeek - 1)%7;
        }
    
        public void paint (Graphics g) // Prints calendar/leap year(if applicable)
        {      
            g.drawString("INSTRUCTIONS: Please enter in your birthday with numerical values. This Applet will then calculate what day it will fall on for 2013 and whether or not you were born on a leap year.",20,50); 
                    
            
            if (isLeapYear(inputYear) == true)
            {
                g.drawString("The year you were born was a leap year!", 140, 300);
            }
            if (inputMonth > 3 && inputMonth < 8)
            {
                g.drawString(" " + monthNames[inputMonth] + " " + year, 205, 100);
            }
            else
                g.drawString(monthNames[inputMonth] + " " + year, 205, 100);
            
    	g.drawString(days, 170, 125);
            
            int x = 0, y = 0;
            for (int weeks = 1; weeks < 7; weeks++)
            {
    	
    	    y += 15;x = 0;
    	    for (int day = 0; day < 7; day++)
                {
    		
                    if (weeks == 1 && day < firstDayOfMonth - 1)
                    {
                        g.drawString(" ", 170, 150);
                    }
                    else 
                    {
                    
                        if (dayPrinter <= daysInMonth[inputMonth])
                        {
    			
                        	g.drawString(" " + dayPrinter, 170 + x, 150 + y);
                        	dayPrinter++;
                        }
                    }
    		x += 25;
    		
                }
    	    
            }
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == displayButton)
            {
                          
                inputMonth = Integer.parseInt(monthField.getText());  // Converts inputs from strings to integer values.
                inputDay = Integer.parseInt(dayField.getText());
                inputYear = Integer.parseInt(yearField.getText());
                year = "2013";
                days = "S     " + "M     " + "T     " + "W     " + "T     " + "F     " + "S";
    		
    		GregorianCalendar now = new GregorianCalendar(inputYear, inputMonth - 1, inputDay); //Gregorian Calendar
    		firstDayOfMonth = now.get(Calendar.DAY_OF_WEEK);
     
                repaint();
            }
        }
        
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Calendar Help

    First and foremost, my calendar is not starting on the correct day meaning depending on the "Year you were born" it dictates when it will start and that should just be a constant.
    I'm not really sure what you mean by this.

    But your applet is doing something a bit odd (or at least the user will find it confusing!). In the paint() method() you say

    Java Code:
    g.drawString(monthNames[inputMonth] + " " + year, 205, 100);
    which labels the calendar using year ie with the current year (2013) as set in the actionPerformed() method.

    Then when you actually display the calendar underneath you use the firstDayOfMonth value. This is also set in actionPerformed():

    Java Code:
    GregorianCalendar now = new GregorianCalendar(inputYear, inputMonth - 1, inputDay); //Gregorian Calendar
    firstDayOfMonth = now.get(Calendar.DAY_OF_WEEK);
    But notice that in figuring out the first day of the month you are calculating what is was back in inputYear. Consequently the calendar part relates to the actual birth year, but it is labelled "<month>, 2013". Which should it be?

    ---

    There are few "tidy up" things you could do. Since they assist with debugging (and avoiding) errors they are better not left until the code is finished. (Which everyone does: those saints here on the forum who claim not to should not be believed).

    * Get rid of unused imports - java.io.* and friends
    * Get rid of unused methods and variables - getDay(), firstDayOfYear etc. Or, if you think they should be being used, figure out why they aren't.
    * Consider using components in place of drawString(). drawString() is an unruly child who will only break your heart. Of course there may be some assignment condition that forces you to use it but, if not, components properly aid out will behave better when the applet needs to be repainted. [Edit] Using a component would fix the fact that drawString() doesn't wrap.
    * Consider using Swing (JApplet instead of Applet): the Swing classes are just easier.
    * Try and cut down on the number of class wide variables you have.
    Last edited by pbrockway2; 03-29-2013 at 12:06 AM.

  3. #3
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Java Calendar Help

    Thanks pbrockway2.
    Ive tried messing around with it but I am still unable to display the calendar correctly at the bottom. It should only use the users input for year to calculate whether or not they were born on a leap year. The 2013 should display the current calendar and show what day they were born on.

    -AJB

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Calendar Help

    It's hard to say anything much without seeing the code you're using now...

    Your original leap year calculation looks OK to me. And gives the expected output (message below the calendar) when I try a bunch of numbers.

    Did you understand why you were getting the calendar as it looked in the user's birth year rather than 2013? And what to change so that 2013's calendar is printed?

  5. #5
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Java Calendar Help

    Yes I have figured out why 2013 is not printing but It still always starts on the same day no matter what the input year is... any thoughts?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Calendar Help

    It's fine when I run it - If I enter 28 June 1959 I see June starting on a Sunday, if I enter 28 June 2013 I see that June will start on a Friday. Or do you mean something else by "it always starts on the same day"? (That's using your original code, and the calendar is erroneously labeled "2013").

    ---

    Note that June 28th is a Saturday this year, but that's another problem...

  7. #7
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Java Calendar Help

    All of my days are starting with sunday, for some reason is what I mean.

    -AJB

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Calendar Help

    In actionPerformed() you say

    Java Code:
    year = "2013";
    days = "S     " + "M     " + "T     " + "W     " + "T     " + "F     " + "S";
             
    GregorianCalendar now = new GregorianCalendar(inputYear, inputMonth - 1, inputDay); //Gregorian Calendar
    firstDayOfMonth = now.get(Calendar.DAY_OF_WEEK);
    so the months will all start on a Sunday because that's what you set days to. What you are calling firstDayOfMonth is being set to the day of the week on which the user was born, which doesn't seem right. In paint() you start the numbering from that day, which will be inaccurate 6/7 of the time for either 2013 or the user's birth year.

    It would help if you posted the code you are using now and gave an example of what *exactly* you expect to see for some given date(s).

  9. #9
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Java Calendar Help

    Java Code:
    package birthdayapplet;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    /**
     *
     * @author Austin J Benner
     */
    public class BirthdayApplet extends Applet implements ActionListener
    {
        public static int WIDTH=550, HEIGHT=550;
        private Button displayButton;
        private TextField monthTextField, dayTextField, yearTextField;
        private int inputMonth, inputDay, inputYear = 1, calPrinter = 1;
        private int firstDayofYear = 2, firstDayofMonth = 0, firstDayofWeek = 2;
        private String year = " ", days = " ";
     
        // Creates arrays to store days in month and names
        int[] daysInMonth = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String[] monthNames = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        int[] daysInWeek = {0, 1, 2, 3, 4, 5, 6};
        String[] daysOFweek = {"Sunday", "Monday", "Tuesday", "Wednsday", "Thursday", "Friday", "Saturday"};
      
        public void init()
        {
            setSize (WIDTH, HEIGHT);
        
            displayButton = new Button ("Display Your Birthday");  // Creates buttons, textfields, labels
            displayButton.addActionListener(this);
            monthTextField = new TextField (2);
            dayTextField = new TextField (2);
            yearTextField = new TextField (4);
            Label monthLabel = new Label ("Month");
            Label dayLabel = new Label ("Day");
            Label yearLabel = new Label ("Year");
                
            add(monthLabel); // Adds the buttons, labels, and textfields to applet
            add(monthTextField);
            add(dayLabel);
            add(dayTextField);
            add(yearLabel);
            add(yearTextField);
            add(displayButton);
        }
        
        private static boolean isLeapYear(int year) // Determines if input year is leap year
        {
            if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
            
        private int getDay(int month, int year)
        {
            int jday = 0;
            for (int i = 1; i < inputMonth; i++)
            {
                jday = jday + daysInMonth[i];
            }
            jday = jday + inputDay;
            return jday;
        }
     
        private int getDayOfWeek(int jday) // Finds day of the week birthday falls on
        {
            return (jday + firstDayofWeek - 1)%7;
        }
    
        public void paint (Graphics g) // Prints calendar/leap year(if applicable)
        {      
            g.drawString("INSTRUCTIONS: Please enter in your birthday with numerical values. ",20,50); 
            g.drawString("This Applet will then calculate what day it will fall on for 2013 and",45,65);        
            g.drawString("whether or not you were born on a leap year.",45,80);
            
            if (isLeapYear(inputYear) == true)
            {
                g.drawString("The year you were born was a leap year!", 140, 300);
            }
            if (inputMonth > 3 && inputMonth < 8)
            {
                g.drawString(" " + monthNames[inputMonth] + " " + year, 205, 100);
            }
            else
                g.drawString(monthNames[inputMonth] + " " + year, 205, 100);
            
    	g.drawString(days, 170, 125);
            
            int x = 0, y = 0;
            for (int weeks = 1; weeks < 7; weeks++)
            {
    	
    	    y += 15;x = 0;
    	    for (int day = 0; day < 7; day++)
                {
    		
                    if (weeks == 1 && day < firstDayofMonth - 1)
                    {
                        g.drawString(" ", 170, 150);
                    }
                    else 
                    {
                    
                        if (calPrinter <= daysInMonth[inputMonth])
                        {
    			
                        	g.drawString(" " + calPrinter, 170 + x, 150 + y);
                        	calPrinter++;
                        }
                    }
    		x += 25;
    		
                }
    	    
            }
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == displayButton)
            {
                          
                inputMonth = Integer.parseInt(monthTextField.getText());  // Converts inputs from strings to integer values.
                inputDay = Integer.parseInt(dayTextField.getText());
                inputYear = Integer.parseInt(yearTextField.getText());
                year = " 2013 ";
                days = "S     " + "M     " + "T     " + "W     " + "T     " + "F     " + "S";
    		
    		GregorianCalendar now = new GregorianCalendar(inputYear, inputMonth - 1, inputDay); //Gregorian Calendar
    		inputYear = now.get(Calendar.DAY_OF_WEEK);
     
                repaint();
            }
        }
        
    }
    Here is what needs to be done. The program needs tells what day of the week someone’s birthday falls on this year. I need to write a method named getDayOfWeek that takes an int parameter, representing what day of the year it is, and returns the numeric value for “Monday”. For example, for 2013, the first day of the year was on Tuesday. The thirty-second day of the year (February 1, 2013) was a Friday, so getDayOfWeek(1) should return the numeric value for “Tuesday” and getDayOfWeek(32) should return the numeric value for “Friday”.

    In addition, my program will need to let the user know if the year in which they were born was a leap year and it does that just fine. As for the calendar, the month name and current year should be displayed in the first row, the weekdays should be labeled in the second row, and the respective calendar for that month displayed beneath. The actual birthday must be “highlighted” in some fashion (i.e. bold, colored, circled, etc.).


    If the user enters in June 16, 2000 the Calendar should display.

    Java Calendar Help-cal.jpg


    -AJB

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Calendar Help

    Take those three things in order and tackle them one at a time:

    (1) "a method named getDayOfWeek that takes an int parameter, representing what day of the year it is, and returns the numeric value for “Monday”
    (2) "calendar for that month displayed"
    (3) "The actual birthday must be “highlighted” in some fashion"

    It turns out that doing (1) will help get (2) done. That's because at the moment, in the paint() method, you use firstDayofMonth to figure out when to start writing numbers. But firstDayofMonth is initialised to zero and is never changed. That's why the numbers start at Sunday. To get the correct value of firstDayofMonth, use the method you write for (1).

    (Are you sure you need to implement a getDayOfWeek() method? I ask because it would be easy to get the birth day-of-the-week directly in actionPerformed() from the Calander instance.)

    ---

    Declare the getDayOfWeek() method and make some attempt to implement it. Almost certainly it will involve the facts that 2013 started on day-of-week number 2 and that weeks all have 7 days in them. Do you know about the % operator? Consider using it here.

Similar Threads

  1. I Need help: how to make a Calendar using Java !
    By Mustafa Shady in forum New To Java
    Replies: 7
    Last Post: 12-25-2011, 11:41 PM
  2. Replies: 4
    Last Post: 09-21-2011, 04:39 PM
  3. Help creating a calendar in Java
    By bars225 in forum New To Java
    Replies: 11
    Last Post: 06-18-2011, 09:16 PM
  4. Java Calendar AM PM problem
    By ss2010 in forum New To Java
    Replies: 7
    Last Post: 01-08-2011, 12:07 AM
  5. Java Calendar Class
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:51 AM

Posting Permissions

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