Question
So the code that i have so far is through the help of answering another question through chegg but its still giving me only 10%
So the code that i have so far is through the help of answering another question through chegg but its still giving me only 10% complete. Here is the error message that im getting when i submit the program.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Calendar.main(Calendar.java:92)
Figure that means that im not allowed to use a scanner and im supposed to use args[] instead of user input, im trying to make that happen and now i keep breaking my code trying to fix it. Any help would be greatly appreciated. I also need to fix my formatting still.. Formatting needs to be exactly the same with all the dashes and everything.
Please Help!
Heres the assignment:
Write a program Calendar.java that takes two command line arguments m and y and prints out the monthly calendar for the mth month of year y. You should only use conditionals and loops (no arrays as we have not coverd it yet). For example, your output for Calendar 2 2009 should be:
---------------------
February 2009
---------------------
Su Mo Tu We Th Fr Sa
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
---------------------
This is what i do know: During leap years, an extra day is added to the 28 days of February. A year is a leap year if it is either divisible by 400 or divisible by 4 but not 100. e.g: 2000 and 2004 are leap years, but 1900 is not.
Day of Week: Given a month m, a day d, and a year y, the corresponding day of the week is computed as follows:
y0 = y - (14 - m) / 12
x = y0 + y0/4 - y0/100 + y0/400
m0 = m + 12 * ((14 - m) / 12) - 2
d0 = (d + x + (31*m0)/12) mod 7 // 0 for Sunday, ..., 6 for Saturday
Here is my code:
import java.util.Scanner;
public class Calendar
{
private static int numDays = 0;
private static int h = 0;
//this method is used to find the given year is leap year or not
public static boolean leap(int year)
{
if(((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
//this method is used to find first day of the year
public static void firstDayOfYear(int year)
{
int month = 13;
year--;
h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
String dayName = "";
switch(h)
{
case 0: dayName = "Saturday"; break;
case 1: dayName = "Sunday"; break;
case 2: dayName = "Monday"; break;
case 3: dayName = "Tuesday"; break;
case 4: dayName = "Wednesday"; break;
case 5: dayName = "Thursday"; break;
default: dayName = "Friday"; break;
}
}
//this method is used to find first day of the Month
public static void firstDayOfMonth(int month, int year)
{
if(month == 1 || month == 2)
{
month += 12;
year--;
}
h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
String dayName = "";
switch(h)
{
case 0: dayName = "Saturday"; break;
case 1: dayName = "Sunday"; break;
case 2: dayName = "Monday"; break;
case 3: dayName = "Tuesday"; break;
case 4: dayName = "Wednesday"; break;
case 5: dayName = "Thursday"; break;
default: dayName = "Friday"; break;
}
}
//this method is used to find first number of days in a month
public static void DaysInMonth(int month, int year)
{
int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && leap(year)) days[month] = 29;
numDays = days[month];
}
//this method is used to print the calender
public static void calendar(int month, int year)
{
String[] monthNames = {"","January","February","March","April","May","June","July","August","September","October","November","December"};
System.out.println(" " + monthNames[month] + " " + year);
System.out.println("Su Mo Tu We Th Fr Sa");
for (int i = 0; i < h - 1; i++)
System.out.print(" ");
for (int i = 1; i <= numDays; i++)
{
System.out.printf("%2d ", i);
if (((i + h - 1) % 7 == 0) || (i == numDays)) System.out.println();
}
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter month (1-12): ");
int month = input.nextInt();
if(month < 1 || month > 12)
{
System.out.println("Invalid month. Valids inputs are 1-12.");
System.exit(0);
}
System.out.print("Enter year: ");
int year = input.nextInt();
if(year < 1753)
{
System.out.println("Invalid year. Valid inputs are 1753 and beyond.");
System.exit(0);
}
firstDayOfYear(year);
firstDayOfMonth(month, year);
DaysInMonth(month, year);
calendar(month, year);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started