IN C++
In a previous assignment you wrote pseudo code for the calculation
of the days into a year given the date.
Write a function that returns an int. It takes three parameters , all int(s),
int DaysIntoYear ( int Year, int Month, int day);
The return value is the number of days in the year from January 1 to the specified date.
Year is included so you can determine if this is a leap year ( see below).
If it is a leap year February will have 29 days not the typical 28.
I suggest using several lower level functions to break up the work.
Please use this enum :
enum daysInMonth { Jan = 31, Feb = 28, Mar = 31, Apr = 30, May = 31, Jun = 30, Jul = 31, Aug = 31, Sep = 30, Oct = 31, Nov = 30, Dec = 31 };
Typical code that uses the enum elements would be :
switch (month) case 1 : return Jan; // etc.
at least one switch statement in your code.
Advanced coders: Please do not use arrays in this solution. ( next time )
SOURCE CODE:
#include<iostream>
using namespace std;
enum daysInMonth
{Jan=31,Feb=28,Mar=31,Apr=30,May=31,Jun=30,Jul=31,Aug=31,
Sep=30,Oct=31,Nov=30,Dec=31};
//enum that stores days in the month
int getDaysInMonth(int Month) //getDaysInMonth() function for
getting days in the respecitve month
{
switch(Month) //switch-case block
{
case 1: //case 1 then returning
Jan
return
Jan;
case 2: //case 2 then returning
Feb
return
Feb;
case 3: //case 3 then returning
Mar
return
Mar;
case 4: //case 4 then returning
Apr
return
Apr;
case 5: //case 5 then returning
May
return
May;
case 6: //case 6 then returning
Jun
return
Jun;
case 7: //case 7 then returning
Jul
return
Jul;
case 8: //case 8 then returning
Aug
return
Aug;
case 9: //case 9 then returning
Sep
return
Sep;
case 10: //case 10 then returning
Oct
return
Oct;
case 11: //case 11 then returning
Nov
return
Nov;
case 12: //case 12 then returning
Dec
return
Dec;
}
}
int checkLeapYear(int Year) //function for checking a year is leap
or not
{
if(((Year%4==0) && (Year%100!=0)) ||
(Year%400==0)) //condition for the year to become leap
{
return 1; //returning 1
}
else
return 0; //returning 2
}
int DaysIntoYear(int Year,int Month,int day)
{
int Count=0; //Count variable to store no of
days
for(int i=1;i<Month;i++) //iterating upto given
Month-1
{
if(i==1) //if 1 then adding days of
Jan to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==2) //if 2 then adding
days of Feb to Count
{
if(checkLeapYear(Year)==1) //checking year is leap or not
{
Count=Count+getDaysInMonth(i)+1;
}
else
Count=Count+getDaysInMonth(i);
}
else if(i==3) //if 3 then adding
days of Mar to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==4) //if 4 then adding
days of Apr to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==5) //if 5 then adding
days of May to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==6) //if 6 then adding
days of Jun to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==7) //if 7 then adding
days of Jul to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==8) //if 8 then adding
days of Aug to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==9) //if 9 then adding
days of Sep to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==10) //if 10 then adding
days of Oct to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==11) //if 11
then adding days of Nov to Count
{
Count=Count+getDaysInMonth(i);
}
else if(i==12) //if 12 then adding
days of Dec to Count
{
Count=Count+getDaysInMonth(i);
}
}
Count=Count+(day-1); //adding days of the current
month to count
return Count; //returning count
}
int main()
{
int Month,day,Year;
cout<<"Enter Year: ";
cin>>Year; //reading Year from user
cout<<"Enter Month: ";
cin>>Month; //reading Month from user
cout<<"Enter Day: ";
cin>>day; //reading day from user
int NoOfDays=DaysIntoYear(Year,Month,day); //calling
DaysIntoYear() function
cout<<"No of days upto the date
"<<Year<<"/"<<Month<<"/"<<day<<"
are: "<<NoOfDays<<endl; //displaying no of days
}
OUTPUT:
IN C++ In a previous assignment you wrote pseudo code for the calculation of the days...
Complete the following program in C; Have the user input the current month and return the previous month. E.g. input -> 3, returns 2 input -> 1, returns 12 #include <stdio.h> #include <stdlib.h> /* Write a function called previous_month that returns the previous month. start with the following code */ enum month {jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; typedef enum month month; int main() { return0; }
Questions 9 and 10 are based on the code fragment below: if ((currentYear % 4 0) && (currentYear 100 !-0)) 11 (current Year 400 0)) daysInMonth [0]-31; days InMonth [1)-29; daysInMonth [2] -31; daysInMonth [3]-30; days InMonth [4]-31; daysInMonth [5]-30; daysInMonth [6]-31; days InMonth [7]-31; daysInMonth [8] -30; daysInMonth [9] -31; days InMonth [10]-30; daysInMonth [11)-31; ) else daysInMonth [0]-31; days InMonth [1]-28 daysInMonth [2] -31; daysInMonth [3] -30; days InMonth [4] -31 daysInMonth [5] -30; daysInMonth [6] -31; days...
My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0; public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); } public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...
# JAVA In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on. Fill it with the appropriate values (31, 28, 31, 30, ...).The program reads a month and year from the user.When the year is a leap year, change the entry for February to 29.Print out how many days the given month has.CODE:import java.util.Scanner;public class NumberOfDays{ public static void main(String[] args) { // Declare and initialize daysOfMonth ....
Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.1. Increment a Date to the next day (for example, 1-31-2011 increments to 2-1-2011; 12-31-2010 increments to 1-1-2011).2. Decrement a Date to the previous day (for example, 2-1-1953 decrements to 1-31-1953; 1-1-1954 decrements to 12-31-1953).. Extra Credit (up to 5 points) Re-write the Date member functions Input() and Output() to usedialog boxes to...
Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...
Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...
Java 4. Write the code of data type class ExerciseMonitor_yourLastName including data members with an double array size 7 to store the distance of 7 days, constructors, and all the following methods Total rainfall for the year The average monthly rainfall The month with the most rain . The month with the less rain The method toString to display the result in the following format: For example if the input from the keyboard for each month as data in the...
Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your main method(function) in a separate source file demonstrating use of the try/catch block to catch the IllegalArgumentException that the MonthDay class may throw in the event of an invalid month, or year. class MonthDays { private int month; private int year; public MonthDays(int aMonth, int...
Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender { MyDate myDate; Day day; enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter date below :"); System.out.println("Enter day :"); int day = sc.nextInt(); System.out.println("Enter Month :"); int month = sc.nextInt(); System.out.println("Enter year :"); int year = sc.nextInt(); MyDate md = new MyDate(day, month, year); if(!md.isDateValid()){ System.out.println("\n Invalid date . Try...