Results 1 to 1 of 1
  1. #1
    Inky pinkie is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Question Loop programming exercise. Need review of my solution.

    Currently studying loops in Java. I have made a solution to a programming exercise, it works, but generally when I do something large I tend to make it more complex and messy than it is needed. Maybe you can help me find better solutions?

    The problem:

    Write a program that prompts the user to enter the year and first day of the year and displays the calendar table for the year on the console. For example, if the user entered the year 2005, and 6 for Saturday, January 1, 2005, your program should display the calendar for each month in the year, as follows:

    Java Code:
            January 2005
    ---------------------------
    Sun Mon Tue Wed Thu Fri Sat
                             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  31
    My program:

    Java Code:
    /** The program prompts the user to enter the year and first day of the year
     *  and displays the calendar table for the year on the console.
     */
    
    package chapter4;
    
    import java.util.Scanner;
    
    public class DisplayCalendar {
    
    	public static void main(String[] args) {
    
    		//Create Scanner
    		Scanner input = new Scanner(System.in);
    		
    		//Prompt the user to enter a year
    		System.out.print("Enter a year: ");
    		int year = input.nextInt();
    		//Prompt the user to enter a first day of the year
    		System.out.print("Enter first day of the year: ");
    		String firstDayOfYearString = input.next();
    		
    		int firstDayOfMonth = -1;
    		//Find a number of the first day
    		switch (firstDayOfYearString) {
    			case "Sunday"    : firstDayOfMonth = 0; break;
    			case "Monday"    : firstDayOfMonth = 1; break;
    			case "Tuesday"   : firstDayOfMonth = 2; break;
    			case "Wednesday" : firstDayOfMonth = 3; break;
    			case "Thursday"  : firstDayOfMonth = 4; break;
    			case "Friday"    : firstDayOfMonth = 5; break;
    			case "Saturday"  : firstDayOfMonth = 6; break;
    			default : System.out.println("Error: there is no such day of week");
    					  System.exit(0);
    		}
    		
    		//Whether the year is a leap year
    		boolean isLeapYear = (year % 4 == 0) ^ (year % 100 == 0) || (year % 400 == 0);
    		
    		
    		//Display the calendar
    		for (int month = 1; month <= 12; month++) {
    		
    			//Proper spacing between months
    			if (firstDayOfMonth == 0)
    				System.out.print("\n");
    			else
    				System.out.print("\n\n");
    			
    			//Get name of the month
    			String monthString = "";
    			switch (month) {
    				case 1  : monthString = "January"; break;
    				case 2  : monthString = "February"; break;
    				case 3  : monthString = "March"; break;
    				case 4  : monthString = "April"; break;
    				case 5  : monthString = "May"; break;
    				case 6  : monthString = "Jule"; break;
    				case 7  : monthString = "July"; break;
    				case 8  : monthString = "August"; break;
    				case 9  : monthString = "September"; break;
    				case 10 : monthString = "October"; break;
    				case 11 : monthString = "November"; break;
    				case 12 : monthString = "December"; break;
    			}
    		
    		
    			//Display the heading of the month
    			System.out.println("        " + monthString + " " + year);
    			System.out.println("---------------------------");
    			System.out.println("Sun Mon Tue Wed Thu Fri Sat");
    		
    			//Find the length of the month
    			int length = 30 + ((month + (int)(month / 8.0)) % 2);
    			if (month == 2) //Adjust February length
    				if (isLeapYear)
    					length -= 1; //Leap year
    				else
    					length -= 2; //Non-leap year
    		
    			int counter = 1; //Counter to determine end of the week
    			
    			//Display blank space before first day
    			for (int i = 0; i < firstDayOfMonth; i++) {
    				System.out.print("    ");
    				counter++;
    			}
    			
    			//Display days
    			for (int day = 1; day <= length; day++) {
    				System.out.printf("%2d", day);
    				System.out.print((counter++ % 7 != 0) ? "  " : "\n");
    			}
    			
    			//Determine the first day of the next month
    			firstDayOfMonth = (firstDayOfMonth + length) % 7;
    			
    		}
    		
    	}
    
    }
    Last edited by Inky pinkie; 09-21-2012 at 05:58 PM.

Similar Threads

  1. Programming exercise
    By FOX427 in forum New To Java
    Replies: 4
    Last Post: 02-22-2012, 10:09 PM
  2. Programming exercise help
    By atac57 in forum New To Java
    Replies: 4
    Last Post: 01-13-2012, 03:37 PM
  3. Having trouble with programming exercise, help?
    By atac57 in forum New To Java
    Replies: 1
    Last Post: 01-12-2012, 06:11 AM
  4. Programming exercise for beginners
    By FOX427 in forum New To Java
    Replies: 5
    Last Post: 07-16-2011, 07:43 AM
  5. Need help with a programming exercise
    By ararar in forum New To Java
    Replies: 9
    Last Post: 11-19-2010, 05:57 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
  •