Question

In: Computer Science

This has to be in java and will be uploaded onto zybooks. Write a Java program...

This has to be in java and will be uploaded onto zybooks.

Write a Java program that asks the user for a date entered as 4 integers: dayNumber monthNumber date year. Where:

dayNumber

An integer from 1-7, where 1 = Sunday, 2 = Monday, ..., 7 = Saturday

monthNumber

An integer from 1-12, where 1 = January, 2 = February, ..., 12 = December

date

An integer from 1-31 representing the date.

year

An integer representing the year.

Your prompt to the user should be:

    Enter 4 integers representing dayNumber monthNumber date year:
  

You program will compute the proper dayName from the specified dayNumber and the proper monthName from the specified monthNumber. Your program will print out the specified date in the following format:

    dayNumber monthNumber date year is dayName monthName date, year
  

Please note your program will have to error check the input as follows:

Please note your program will have to error check the input as follows:

  1. First the dayNumber is checked for being in the range 1..7. If that is not the case, your program should print out the following message and terminate execution:
        Invalid day number: dayNumber, please enter a number from 1..7.
          
  2. Then the monthNumber is checked for being in the range 1..12. If that is not the case, your program should print out the following message and terminate execution:
        Invalid month number: monthNumber, please enter a number from 1..12.
          
  3. Then the date is checked for being in the range 1..31. If that is not the case, your program should print out the following message and terminate execution:
        Invalid date number: date, please enter a number from 1..31.
          
  4. Then the date must be checked for validity. For example, there is no February 30, February 31, April 31, June 31, September 31, November 31. If the date is invalid, your program should print out the following message and terminate execution:
        Invalid date: monthName, does not have date days, please enter a valid date.
          
  5. Lastly, if the monthNumber = 2, and date = 29 you must verify that the year entered is a leap year, which is the only time that February 29 exists. Please see Wikipidia's Page on Leap Year. If the year is not a leap year and the user entered a date = 29, your program should print out the following message and terminate execution:
        Invalid date: year is not a leap year, February does not have date days, please enter a valid date.
          

Please note that your class should be named DateConverter.

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.util.Scanner;

public class DateConverter{

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        int dayNumber, monthNumber, date, year;

        String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

        int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        String dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

        System.out.print("Enter 4 integers representing dayNumber monthNumber date year:");

        dayNumber = scnr.nextInt();

        monthNumber = scnr.nextInt();

        date = scnr.nextInt();

        year = scnr.nextInt();

        if(dayNumber<1 || dayNumber>7){

            System.out.println("Invalid day number: "+dayNumber+ "please enter a number from 1..7.");

            System.exit(0);

        }

        if(monthNumber<1 || monthNumber>12){

            System.out.println("Invalid month number: "+monthNumber+ "please enter a number from 1..12.");

            System.exit(0);

        }

        if(date<1 || date>31){

            System.out.println("Invalid date number: "+date+ "please enter a number from 1..31.");

            System.exit(0);

        }

        if((date>30 && (monthNumber==4 || monthNumber==6 ||monthNumber==9 ||monthNumber==11)) || (date>29 && monthNumber==2)){

            System.out.println("Invalid date: "+monthNames[monthNumber-1]+", does not have "+date+" days, please enter a valid date.");

            System.exit(0);

        }

        if(monthNumber==2 && date>=29 && !(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))){

            System.out.println("Invalid date: year is not a leap year, February does not have "+date+" days, please enter a valid date.");

            System.exit(0);

        }

        System.out.println(dayNumber +" "+monthNumber+" "+date+" "+year+" is "+dayNames[dayNumber-1]+" "+ monthNames[monthNumber-1]+" "+date+" "+year);

    }

}


Related Solutions

DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
1. Write comments on each line of the uploaded Java file. 2. Add two more controls...
1. Write comments on each line of the uploaded Java file. 2. Add two more controls to the Java file. These controls are listed below. JTextField JRadioButton 3. Excecute your program and upload the following. 1. Image showing you have added the controls 2. Your updated Java files showing your comments and your added code for above two controls. --------------------------------------------------------------------------------------------------------------------------------- package swing; import javax.swing.*; public class SwingInheritance extends JFrame {    private static final long serialVersionUID = 1L;    SwingInheritance()...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
How to write IO program in java?
How to write IO program in java?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT