Question

SUBMIT DayGrid.java Programming Projects 195 Sun Mon Tue Wed Thu Fri Sat I 67819 | 10 11 12 I 13 14 15 16 17 18 19 I | 20 21 22 23 24 25 26 I |27 28 29 30 31 I One tricky part of this program is making the various columns line up properly with proper widths. We will learn better ways of formatting output in the next chapter. For now, you may copy the following helper method into your program and call it to turn a number into a left-padded string of a given exact width. For example, the call Additional Iverson requirements for Bellevue College CS210, WINTER 2018: Crucial Note: I know, and you soon will know, that assigning something straight from this text will create a massive hunt for the solution posted someplace on the internet. Such work does not help anyone learn to program!!! Therefore: Listed below are specific Iverson modifications that make this assignment unique. Go ahead, find what examples you can on the internet. LEARN from those examples, then close that window, and write your own code!!! 1. DO NOT use the padded helper provided, chapter 4 has better tools, like printf if needed 2. MUST write a method showGrid(month, year) that produces output, see example below 3. int month is a parameter, must be 1-12 for the twelve months of the year 4. int year is a year of the Gregorian calendar (additional detail below) 5. MUST use a method days!nMonth. Exercise #4, page 309, Chapter 4 6. showGrid works for any month (1-12), and any year (1900-2100) including leap years 7. Program Submission Requirement 8. Write your own code!!! All submissions will be processed with JPlag: jplag.ipd.kit.edu e In text project, were given the first Sunday, but the lverson requirements indicate that we do not provide this. How do we get the fhrst Sunday of a month???? My answer: use the Oracle GregorianCalendar:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.Scanner;

public class DayGrid {

/** Main method */

public static void main(String[] args) {
Scanner scan = new Scanner (System.in);

//Prompt the user to enter year
System.out.print("Enter full year (e.g., 2001): ");
int year = scan.nextInt();

// Prompt the user to enter month
System.out.print("Enter month in number between 1 and 12: ");
int month = scan.nextInt();

// Print calendar for the month of the year
if (month < 1 || month > 12 || year < 1980)
System.out.println("Wrong input!");
else
printMonth(year, month);

}
/** Print the calendar for a month in a year */

static void printMonth(int year, int month) {

//Print the headings of the calendar
printMonthTitle(year, month);

//Print the body of the calendar
printMonthBody(year, month);
}

/** Print the month title, e.g., May, 1999 */

static void printMonthTitle(int year, int month) {

System.out.println(" " + getMonthName(month) + " " + year);
System.out.println("–––––––––––––––––––––––––––––");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

}

/** Get the English name for the month */
static String getMonthName(int month) {
String monthName = null;
switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December";
}
return monthName;
}

/** Print month body */
static void printMonthBody(int year, int month) {

// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);

// Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numberOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}

/** Get the start day of the first day in a month */

static int getStartDay(int year, int month) {

//Get total number of days since 1/1/1800
int startDay1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);

//Return the start day
return (totalNumberOfDays + startDay1800) % 7;
}

/** Get the total number of days since January 1, 1800 */

static int getTotalNumberOfDays(int year, int month) {
int total = 0;

//Get the total days from 1800 to year - 1
for (int i = 1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;

//Add days from January to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumberOfDaysInMonth(year, i);

return total;
}

/** Get the number of days in a month */

static int getNumberOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;

if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;

if (month == 2) return isLeapYear(year) ? 29 : 28;

return 0; // If month is incorrect
}

/** Determine if it is a leap year */
static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
}

Output:

Enter full year (e.g., 2001): 2018 Enter month in number between 1 and 12: 1 January 2018 Sun Mon Tue Wed Thu Fri Sat 7 8 9 1

Add a comment
Know the answer?
Add Answer to:
SUBMIT DayGrid.java Programming Projects 195 Sun Mon Tue Wed Thu Fri Sat I 67819 | 10...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with Python problem. ###################################### Q3 ############################### dowt = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')...

    Help with Python problem. ###################################### Q3 ############################### dowt = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat') # day-of-week-tuple, # year = 2020 cnt = 3 # 2020/1/1 is a Wednesday, so let us start a counting index of 3 for Wednesday, and keep adding one # m = 3 # this will be a variable to be looped through eventually. I use the convention of 0–11 for the twelve months # # As the cnt value changes, we want to...

  • In Java You are to write a program that determines the day of the week for...

    In Java You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program: /** Interface for Date objects to be used by the Year3000 driver program. @author Jon Sorenson */ public interface DateInterface { /** @return the day of the month (1-31) */ public int getDay(); /** @return the day of...

  • General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You sho...

    General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You should create identifiers with sensible names; • You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. • Logical structures and statements are properly used for specific purposes. Objectives This assignment requires you to write...

  • Use Java please Creating a calendar for a given year A shell for this assignment has...

    Use Java please Creating a calendar for a given year A shell for this assignment has been provided so that you can fill in the methods Homework 4 grading rubric 1. Output examples a. One Normal Year, 10% b. One Leap Year, 10% 2. Style Meaningful variable names, 10% b. Meaningful method names, 10 % c. Comments, Total: 10% . Do not comment every line. 5% . Comment non-obvious code. 5% a d. Indentation, 10% e. Block comment with name...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

  • Complete the SpeedDating Class Write the method bodies for the 2 SpeedDating methods. Study the method...

    Complete the SpeedDating Class Write the method bodies for the 2 SpeedDating methods. Study the method declarations and Javadoc comments so that you understand what each method is supposed to do. To receive credit for a method, it must use one of the Java loops (your choice). Nested loops are not necessary. Write a Test Class for Your SpeedDating Class Your test class will have a main method that does the following: Create a SpeedDating object Since 1971, Columbus Day...

  • Need this in c programming

    Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...

  • C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪...

    C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪ Writing a while loop ▪ Write functions and calling functions Text Processing [50 points] We would like to demonstrate our ability to control strings and use methods. There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It...

  • I need help with this assignment. It's due Tuesday at 8 AM. Please follow the instructions thorou...

    I need help with this assignment. It's due Tuesday at 8 AM. Please follow the instructions thoroughly as to not use any advanced material we may not have gone over in class. The instructions let you know what all should be used. Thanks! Use only what you have learned in Chapters 1 - 7. Use of "advanced" material, material not in the text, or project does not follow the instructions, will result in a GRADE OF 0% for the project....

  • Need help writing beginner C# program, I will make sure to provide feedback to whoever can...

    Need help writing beginner C# program, I will make sure to provide feedback to whoever can help me figure it out! No public or global variables should be used. You need to consider passing arguments between the methods according to the ways described in the lecture. i.e. all variables should be declared inside the methods and passed to other methods by value/ref/out as needed Description: We want to design a Date class to represent a date using three integer numbers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT