Results 1 to 2 of 2
  1. #1
    DavidEvans is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    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


    Java 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();
       }
        
    }
    Java 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);
        	}
        	
        	
        }
    }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    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:

    Java 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!

Similar Threads

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