Question

In: Computer Science

Written in JAVA A valid month value mm must be from 1 to 12 (January is...

Written in JAVA

A valid month value mm must be from 1 to 12 (January is 1) and it must contain two digits. The day value dd must be from 1 to a value that is appropriate for the given month and it also must contain two digits. The year value yyyy must contain four digits and be a positive number. September, April, June, and November each have 30 days. February has 28 days except for leap years when it has 29. The remaining months all have 31 days. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.

After you have the three substrings for the month, day, and year, you need to convert those strings into int values. This is done with a line of code like the following (for the year).

      int year = Integer.parseInt( yearString );

As soon as you find a problem with the user's input, output an appropriate error message and return from the checkDate method. Here is an example of how that might look.

      if ( 4 != yearString.length() )
      {
         System.out.println("Error with " + savedDate + ": The year must have four digits.");
         return;  // exit from the checkDate method
      }

You need to determine which years are leap years. This is tricky. The main tool for doing this is the "integer remainder" operator, %, which is described on pages 68-70 of the textbook. So, for example, the year is divisible by 4 when 0 == (year % 4) is true. A year is not divisible by 100 when 0 != (year % 100) is true.

Solutions

Expert Solution

check out the solution and do Comments if any queries.

----------------------------------------------------------------

import java.util.*;

public class MyClass {
public static void checkDate(String dayString, String monthString, String yearString) {
String savedDate = yearString + "/" + monthString + "/" + dayString;
  
int day, month, year;
  
// convert the user input of type 'String' into 'int'
day = Integer.parseInt(dayString);
month = Integer.parseInt(monthString);
year = Integer.parseInt(yearString);
  
// validate the year length
if(yearString.length() != 4)
{   
System.out.println("\nError with " + savedDate + ": The year must have four digits.");
return; // exit from the checkDate method
}
// validate the month value
if(month < 1 && month > 12)
{   
System.out.println("\nError with " + savedDate + ": The month must be from 1 to 12.");
return; // exit from the checkDate method
}
// validate the day value
if(month == 2)
{
// if month is 2 then validate the day value as either 28 or 29
if(day < 1 || day > 29)
{
System.out.println("\nError with " + savedDate + ": The day value must be either 28 or 29 days in the month of February");
return; // exit from the checkDate method
}
else
{
System.out.println("\nValid date : " + savedDate);
// if its 28 days or 29 days .. check for leap year
if(year%4 == 0 && year%100 != 0)
System.out.println("\nA Leap year");
else
System.out.println("\nNot a Leap year");
}
}
// check for other months except February month
else
{
if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day != 30)
{
System.out.println("\nError with " + savedDate + ": The day value must be 30 in the month of April, June, September or November");
return; // exit from the checkDate method   
}
// print the valid date
else
System.out.println("\nValid date : " + savedDate);
}
else
{
if(day != 31)
{
System.out.println("\nError with " + savedDate + ": The day value must be 31 in all the months other than April, June, September or November");
return; // exit from the checkDate method   
}
// print the valid date
else
System.out.println("\nValid date : " + savedDate);
}
}
} // checkDate method ends
  
public static void main(String args[]) {
// required variable declaration
String dayString, monthString, yearString;
int choice;
  
// creation of Scanner class object to get the user input
Scanner sc = new Scanner(System.in);
  
while(true)
{
System.out.print("\n1. Enter Date\n2. Exit\n");
System.out.print("Enter your choice : ");
choice = sc.nextInt();
if(choice == 1)
{
// get the user input
System.out.print("\nEnter month : ");
monthString = sc.next();
System.out.print("Enter day : ");
dayString = sc.next();
System.out.print("Enter year : ");
yearString = sc.next();
  
// function call
checkDate(dayString, monthString, yearString);
}
else
{
System.out.println("\nProgram Exited!!!");
break;
}
} // while ends
} // main method ends
} // main class ends

----------------------------------------------------------------------

--------------------------------------------------------------

OUTPUT ::


Related Solutions

Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
This code must be written in Java. This code also needs to include a package and...
This code must be written in Java. This code also needs to include a package and a public static void main(String[] args) Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week (Sunday..Saturday). The following example shows what the user will see on the screen: This program calculates the...
12. On January 1, a company issues bonds dated January 1 with a par value of...
12. On January 1, a company issues bonds dated January 1 with a par value of $740,000. The bonds mature in 3 years. The contract rate is 10%, and interest is paid semiannually on June 30 and December 31. The bonds are sold for $719,000. The journal entry to record the first interest payment using straight-line amortization is: 16. On January 1, a company issues bonds dated January 1 with a par value of $330,000. The bonds mature in 5...
If the outside of a cell has a potassium concentration of 12 mM what must be...
If the outside of a cell has a potassium concentration of 12 mM what must be the inside concentration of potassium for the condition of no net flux of potassium across the membrane at Vm = - 75 mV
Must be written in Java: After completing this lab, you should be able to: Write a...
Must be written in Java: After completing this lab, you should be able to: Write a Java class to represent Time. Create default and parameterized constructors. Create accessor and mutator methods. Write a toString method. This project will represent time in hours, minutes, and seconds. Part 1: Create a new Java class named Time that has the following instance fields in the parameterized constructor: hours minutes seconds Create a default constructor that sets the time to that would represent the...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Must be written in JAVA Code Modify the program you created in previous project to accomplish...
Must be written in JAVA Code Modify the program you created in previous project to accomplish the following: Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' Store the data in an ArrayList object Program to Modify import java.util.ArrayList; import java.util.Scanner; public class Address { String firstname; String lastname; int zipcode;    Address(String firstname,String...
1. What kind of contract must be in writing and signed in order to be valid...
1. What kind of contract must be in writing and signed in order to be valid and legally enforceable? A. a contract for the sale of a farm B. All of these C. a contract for Joe to pay Sam's debt D. a contract for the lease of an apartment for 2 years E. a contract for the sale of a car for $15,000 2. An agreement that is often unenforceable under common law is A. a contract that contains...
On January 1, 2016, Hillenbrand purchased 12-year, 12% bonds having maturity a value of $781,000. Interest...
On January 1, 2016, Hillenbrand purchased 12-year, 12% bonds having maturity a value of $781,000. Interest is paid annually on December 31 and the bonds provide the bondholders a 7% yield. Hillenbrand uses the effective-interest method to amortize discount or premium. At the time of acquisition, the bonds were classified as available-for-sale. The fair value of the bonds on December 31, 2018 is $756,000. The fair value of the bonds as of December 31 of the immediately preceding year (prior...
On January 1, 2019 Maxxum PLC issued 12% bonds with a face value of £800,000 and...
On January 1, 2019 Maxxum PLC issued 12% bonds with a face value of £800,000 and offering bondholders a 10% yield. The bonds are dated January 1, 2019 and mature January 1, 2024 with interest payable December 31 each year.  (100 POINTS) Instructions Prepare the journal entry at the date of the bond issuance. Prepare a schedule of interest expense and bond amortization for 2019-2021 Prepare the journal entry to record the interest payment and amortization for 2019 Prepare the journal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT