Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 11:21 PM
Member
 
Join Date: Mar 2008
Posts: 14
wco5002 is on a distinguished road
Creating Calendar in Applet
Hey,

I'm writing a program as follows:

Suppose you are writing a program that tells what day of the week someone’s birthday falls on this year. 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 1999, the first day of the year was on Friday. The thirty-second day of the year (February 1, 1999) was a Monday, so getDayOfWeek(1) should return the numeric value for “Friday” and getDayOfWeek(32) should return the numeric value for “Monday”. (Hint: if you divide the day of the year by 7, the remainder will always be a numeric value between 0 and 6, which can be made to correspond to the days of the week).

As part of this program, you’ll also want a method that takes the month and day as parameters and re-turns what day of the year it is. For example, getDay(1,1) should return 1; getDay (2,1) should return 32; and getDay (12,31) should return 366. (Hint: If the month is 3, and the day is 5, you have to add the number of days in January plus the number of days in February to 5 to get the result: 31 + 28 + 5 = 64.)

Design and develop a Java Applet, named BirthdayApplet, that lets the user input their birth date (month, day and 4-digit year) and displays which day of the week it falls on for the year 2007 along with an indication of whether the year they were born was a leap year or not. Use the getDayOfWeek, getDay, and isLeapYear methods described above.

The applet should contain three separate TextField components for inputting the month, day and year along with Labels indicating their purpose and expected format (i.e. year = xxxx). Once the user has entered values into the text fields, they may select the “Display Birthday” Button to display their birthday for THIS year (2008). Your program should also let the user know if the year they were born was a leap year or not. The format for displaying the birthday follows (i.e. User entered November 24, 2000):

Code:
November 2008 S M T W T F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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.). You may use any text-related GUI component(s) to display the birthday and associated month calendar. You may select the style and size of the text you would like to use for displaying the month calendar, just ensure that the requested birthday is “highlighted“ (identifiable from the other printed values) when complete.



I'm getting a little lost and confused...here's what I have so far, and I hope someone can give me a few pointers to get this rolling and get on the right path. Thanks.

Code:
import java.applet.*; import java.awt.*; import java.io.*; import javax.swing.*; import java.awt.event.*; public class BirthdayApplet extends Applet implements ActionListener { public static int WIDTH=500, HEIGHT=500; private Button displayButton; private TextField monthField, dayField, yearField; private AudioClip myAudioClip; private int inputMonth, inputDay, inputYear; private int firstDayofYear = 2, firstDayofMonth = -1; // Creates arrays to store the days in the month and their 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[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satruday"}; public void init() { setSize (WIDTH, HEIGHT); // Plays 'Happy Birthday' Song myAudioClip = getAudioClip(getCodeBase(), "../../audio.mid"); myAudioClip.loop(); // Creates the textfields, labels, and buttons. displayButton = new Button ("Display Birthday"); 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"); // Adds the buttons, labels, and textfields to applet. add(monthLabel); add(monthField); add(dayLabel); add(dayField); add(yearLabel); add(yearField); add(displayButton); } public void paint (Graphics g) { g.drawString(monthNames[inputMonth] + " 2008", 200, 100); g.drawString("S" + " " + "M" + " " + "T" + " " + "W" + " " + "R" + " " + "F" + " " + "S", 165, 125); } public void actionPerformed(ActionEvent e) { if (e.getSource() == displayButton) { // Converts inputs from strings to integer values. inputMonth = Integer.parseInt(monthField.getText()); inputDay = Integer.parseInt(dayField.getText()); inputYear = Integer.parseInt(yearField.getText()); repaint(); } } }

Last edited by wco5002 : 04-08-2008 at 11:54 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-09-2008, 04:40 AM
Member
 
Join Date: Mar 2008
Posts: 14
wco5002 is on a distinguished road
Alright, I've been working on the prog a little, but I'm still unsure about how to draw the actual calendar days itself. Here's the code:

Code:
import java.applet.*; import java.awt.*; import java.io.*; import javax.swing.*; import java.awt.event.*; public class BirthdayApplet extends Applet implements ActionListener { public static int WIDTH=500, HEIGHT=500; private Button displayButton; private TextField monthField, dayField, yearField; private AudioClip myAudioClip; private int inputMonth, inputDay, inputYear = 1, dayPrinter = 1; private int firstDayofYear = 2, firstDayofMonth = -1, firstDayofWeek = 2; private String year = "", days = ""; // Creates arrays to store the days in the month and their 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", "Wednesday", "Thursday", "Friday", "Saturday"}; // Initializes the applet public void init() { setSize (WIDTH, HEIGHT); // Plays 'Happy Birthday' Song myAudioClip = getAudioClip(getCodeBase(), "../../audio.mid"); myAudioClip.loop(); // Creates the textfields, labels, and buttons. displayButton = new Button ("Display Birthday"); 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"); // Adds the buttons, labels, and textfields to applet. add(monthLabel); add(monthField); add(dayLabel); add(dayField); add(yearLabel); add(yearField); add(displayButton); } // Determines if the input year is a leap year. private static boolean isLeapYear(int year) { if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } else { return false; } } // Finds the Julian Day equivalency of inputs. 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; } // Finds which day of the week birthday falls. private int getDayOfWeek(int jday) { return (jday + firstDayofWeek - 1)%7; } // Paints calendar and leap year (if applicable). public void paint (Graphics g) { if (isLeapYear(inputYear) == true) { g.drawString("The year you were born was a leap year!", 140, 400); } if (inputMonth > 3 && inputMonth < 8) { g.drawString(" " + monthNames[inputMonth] + " " + year, 205, 200); } else g.drawString(monthNames[inputMonth] + " " + year, 205, 200); g.drawString(days, 170, 225); } public void actionPerformed(ActionEvent e) { if (e.getSource() == displayButton) { // Converts inputs from strings to integer values. inputMonth = Integer.parseInt(monthField.getText()); inputDay = Integer.parseInt(dayField.getText()); inputYear = Integer.parseInt(yearField.getText()); year = "2008"; days = "S " + "M " + "T " + "W " + "T " + "F " + "S"; repaint(); } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 05:21 AM
Member
 
Join Date: Mar 2008
Posts: 14
wco5002 is on a distinguished road
Alright I've gotten everything working except for the actual print out of the dates in the month. I'll post the full code again, as I have it completed thus far. Any pointers are GREATLY appreciated.

Code:
import java.applet.*; import java.awt.*; import java.io.*; import javax.swing.*; import java.awt.event.*; public class BirthdayApplet extends Applet implements ActionListener { public static int WIDTH=500, HEIGHT=500; private Button displayButton; private TextField monthField, dayField, yearField; private AudioClip myAudioClip; private int inputMonth, inputDay, inputYear = 1, dayPrinter = 1; private int firstDayofYear = 2, firstDayOfMonth = -1, firstDayofWeek = 2; private String year = "", days = ""; // Creates arrays to store the days in the month and their 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", "Wednesday", "Thursday", "Friday", "Saturday"}; // Initializes the applet public void init() { setSize (WIDTH, HEIGHT); // Plays 'Happy Birthday' Song myAudioClip = getAudioClip(getCodeBase(), "../../audio.mid"); myAudioClip.loop(); // Creates the textfields, labels, and buttons. displayButton = new Button ("Display Birthday"); 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"); // Adds the buttons, labels, and textfields to applet. add(monthLabel); add(monthField); add(dayLabel); add(dayField); add(yearLabel); add(yearField); add(displayButton); } // Determines if the input year is a leap year. private static boolean isLeapYear(int year) { if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } else { return false; } } // Finds the Julian Day equivalency of inputs. 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; } // Finds which day of the week birthday falls. private int getDayOfWeek(int jday) { return (jday + firstDayofWeek - 1)%7; } // Paints calendar and leap year (if applicable). public void paint (Graphics g) { 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); for (int weeks = 1; weeks < 7; weeks++) { for (int day = 0; day < 7; day++) { if (weeks == 1 && day < firstDayOfMonth) { g.drawString(" ", 170, 150); } else { if (dayPrinter < 31) { String dayPrinter2 = String.valueOf(dayPrinter); g.drawString(" " + dayPrinter2 + " ", 170, 150); dayPrinter = Integer.parseInt(dayPrinter2.trim()); dayPrinter++; } } } } } public void actionPerformed(ActionEvent e) { if (e.getSource() == displayButton) { // Converts inputs from strings to integer values. inputMonth = Integer.parseInt(monthField.getText()); inputDay = Integer.parseInt(dayField.getText()); inputYear = Integer.parseInt(yearField.getText()); year = "2008"; days = "S " + "M " + "T " + "W " + "T " + "F " + "S"; repaint(); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
calendar John SWT / JFace 10 06-28-2008 05:07 AM
Calendar.DATE mew New To Java 1 01-04-2008 08:51 PM
Problem with calendar Felissa Advanced Java 2 07-01-2007 09:39 PM
Web calendar Daniel Enterprise JavaBeans 1 06-27-2007 06:36 PM
Applet, To center text and To open I engage in a dialog in an Applet Marcus Java Applets 4 06-08-2007 07:15 AM


All times are GMT +3. The time now is 12:49 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org