Birthday.java Can you please add following changes to above code in java. 1)The
ID: 3882788 • Letter: B
Question
Birthday.java
Can you please add following changes to above code in java.
1)The above program has three private data members; add two more: the number of the month (1..12) and the day of the year (1..365).
2)Change the CalculateDay function to initialize the number of the month after it searches for the month-string.
3) Add a public static final array of integers for the number of days in each month.
4) Write a function that will use the new array and data member to calculate the day of the year.
5) Be sure you test January 1 and December 31.
• Sample output:
Birthday Day Calculator,
Welcome! Months are: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Please enter your birth month and date:
May 20
Your May 20 birthday is on Wednesday this year
it is day 140 of the year
Explanation / Answer
Birthday.java
//Die
//
import java.util.*;
//------------------------------------------------------------------------------
public class Birthday{
public static final String[] months = {"Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
public static final String[] days = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" };
public static final int[] startsOn = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5, }; // Day of week months start on
public static final int[] monthDays={31,28,31,30,31,30,31,31,30,31,30,31};
// The collection of data members stores the STATE of an object.
// Data members of a class are normally private. Document each one.
private String month; // 3-letter abbreviation for the month.
private int date; // Will be 1..31
private String day; // The day of the week.
private int noOfMonth;
private int dayOfYear;
//--------------------------------------------------------------------------
// Compute a new random value for the die.
// Postcondition: the return value is between 1 and faces.
Birthday( String m, int d){
month = m;
date = d;
calculateDay();
}
//--------------------------------------------------------------------------
private void calculateDay() {
int found, k, answer;
for(k=0; k<12; ++k) {
if (months[k].equals(month))
{
noOfMonth=k;
break;
}
}
found = k;
if (found == 12)
System.out.println("Your month name was not a valid 3-letter abbreviation.");
else {
answer = (startsOn[k] + date -1)%7;
day = days[answer];
}
}
//---------- A get function gives read-only access to a private data member.
public String getDay(){ return day; }
public int getMonth(){return noOfMonth;}
//--------------------------------------------------------------------------
// Define toString for every class.
// Return a string that reports the state of the class. Used for debugging.
public String toString(){
return month +" " + date ;
}
//This method returns the no of day of the year
public int calDayOfTheYear()
{
int dayOfyear=0;
for(int i=0;i<noOfMonth;i++)
{
dayOfyear+=monthDays[i];
}
return dayOfyear;
}
//--------------------------------------------------------------------------
public static void main( String[] args ) {
int date;
String monthname;
Scanner sc = new Scanner( System.in );
System.out.println(" Birthday Day Calculator, Welcome!");
System.out.print ("Months are: ");
for( String s : months) System.out.print( s+" " );
System.out.println(" Please enter your birth month and date:");
monthname = sc.next();
date = sc.nextInt();
Birthday b = new Birthday (monthname, date);
System.out.printf ( "Your %s birthday is on %s this year ",
b.toString(), b.getDay() );
System.out.print("It is day "+(b.calDayOfTheYear()+date)+" of the year.");
}
}
______________
Output#1:
Birthday Day Calculator, Welcome!
Months are: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Please enter your birth month and date:
May 20
Your May 20 birthday is on Saturday this year
It is day 140 of the year.
_______________
Output#2
Birthday Day Calculator, Welcome!
Months are: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Please enter your birth month and date:
Jan 1
Your Jan 1 birthday is on Sunday this year
It is day 1 of the year.
_____________
Output3#:
Birthday Day Calculator, Welcome!
Months are: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Please enter your birth month and date:
Dec 31
Your Dec 31 birthday is on Sunday this year
It is day 365 of the year.
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.