Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Birthday.java // Die // import java.util.*; //------------------------------------------------------------------------------ public class Birthday{ public static final String[] months = {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,

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 // 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. //-------------------------------------------------------------------------- // 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)) 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; } //-------------------------------------------------------------------------- // Define toString for every class. // Return a string that reports the state of the class. Used for debugging. public String toString(){ return month +" " + date ; } //-------------------------------------------------------------------------- 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() ); } } 

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Donald A. Carpenter Fred R. McFadden

1st Edition

8178088045, 978-8178088044

More Books

Students also viewed these Databases questions

Question

2. Provide recommendations for effective on-the-job training.

Answered: 1 week ago