Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-17-2010, 05:09 PM
Member
 
Join Date: Mar 2010
Posts: 5
Rep Power: 0
DavidEvans is on a distinguished road
Default Calender Program Help Needed!!
Hey folks,

I'm hoping someone can help me with a programming assignment I have for my Computer Science class. We are supposed to create a calender program in java that prints out a calender in table form for a given month (the actual dates do not have to reflect the real dates so long as there are the correct number of days in each month).

I have got almost all the whole program working as it should using two classes;
  • DavidEvansHW3 - contains the main and user input methods
  • DavidEvansCalender - contains all the methods for generating and outputting the calender

However there is one feature that I haven't been able to get to work; - Making the program output a random starting day each time the program is run. At the moment the starting day is always Sunday but I am trying to get it to start on a random day and print a white space in place of each day before the starting date so for example if the month started on Tuesday the first two days Sunday and Monday would appear blank.

I have pasted the code for both classes below. The code for random start day is at line 77 and would be extremely grateful to anyone who can suggest a way in which to get the program to work correctly.

Thanks in advance


Code:
import java.util.Scanner;

public class DavidEvansHW3 {

   public static void main(String arhs[])
   {
   	
   	//Variable
   	Scanner get=new Scanner(System.in);
   	DavidEvansCalender c= new DavidEvansCalender();
   	
   	//get values and set them
   	System.out.printf("Please enter the year: ");
   	c.setYear(get.nextInt());
   	
   	System.out.printf("Please enter the month: ");
   	c.setMonth(get.next());
   	
   	c.Display();
   }
    
}
Code:
import java.util.Random;
public class DavidEvansCalender {

    private int year;
    private String name; //first character of month name is Capital letter and the others small case letters
    
    
    //Constructors
    public DavidEvansCalender() {
    }
    
    public DavidEvansCalender(int y, String m)
    {
    	year=y;
    	name=m;
    }
    
    //Set methods
    public void setYear(int y)
    {
    	year=y;
    }
    public void setMonth(String n )
    {
    	name=n;	
    }
    
    //Get methods
    public int getYear(){return year;}
    public String getMonth(){return name;}
    
    //LeapYear method
    public boolean LeapYear()
    {
    	if ((year %4==0 && year %100!=0) && (year % 400==0))
    		return true;
    	else
    		return false;
    }
    
    public void Display()
    {
    	//vaiables
    	Random ran=new Random(); //to choose random starting day for a month
    	int i;   // for loop;
    	int dayOfmonth;
    	int startDay;
    	boolean leap;
    	
    	//determine if year is leap or not
    	leap=LeapYear(); // returns if year is a leap year or not 	
    	
    	//determine number of day in the month
    	if(leap && name.equals("February"))
    		dayOfmonth=29;
    	else if(name.equals("February"))
    		dayOfmonth=28;
    	else if(name.equals("April") || name.equals("June") || name.equals("September")||name.equals("November"))
    		dayOfmonth=30;
    	else
    		dayOfmonth=31;
    	
    	//print Calender title
    	System.out.printf("\n\n%30s - %d \n\n",name, year);  
    	
    	//print the Calender
    	
    	//choose random Start Day
    	startDay=ran.nextInt(6)+1;
    	System.out.printf("%-12s %-12s %-12s %-12s %-12s %-12s %-12s\n","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    	//skip first Start Day
    	for(i=1;i<=dayOfmonth;i++)
    	{
    		if(i%8==0)
    			System.out.println();
    		else
    		    System.out.printf("%-13d",i);
    	}
    	
    	
    }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-17-2010, 06:37 PM
Senior Member
 
Join Date: Mar 2010
Posts: 269
Rep Power: 1
iluxa is on a distinguished road
Default
A couple of notes.

1. Always name your methods with first lower-case letter: "display", "leapYear", etc.
2. Give more descriptive names to your methods: "isLeapYear()" as opposed to "leapYear()"
3. isLeapYear () if statement seems incorrect. The way it's written now, only returns true if the year is divisible by 400 (&& vs. ||)
4. You're picking out the random start day quite nicely, but not using it here's what you should do:
First, note that if the month has 30 days, and your start day is 4, you should iterate through a total of 34 days: first 4 will get skipped, and 30 will get printed out. So your for-loop should look like this:

Code:
    	for(i=1;i<=dayOfmonth+startDay;i++)
    	{
                if (i <= startDay)
                        System.out.print ("  ");        // or whatever better way to print a blank.
    		else if(i%8==0)
    			System.out.println();
    		else
    		    System.out.printf("%-13d",(i-startDay));
    	}
Now, why are you printing the new line every 8 days (and not every 7 days)?

Cheers!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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
Help needed with 'shop' program jkhamler New To Java 1 12-06-2009 05:35 PM
Calender tiger100plus New To Java 1 12-26-2008 07:20 PM
date and calender not getting the right results valoyivd New To Java 4 04-14-2008 11:51 AM
Datepicker Calender - month change event - jquery rmpatel101 Advanced Java 0 04-02-2008 06:42 PM
Help needed writing a program... Francis New To Java 2 11-22-2007 02:03 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:59 AM.



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