Question

In: Computer Science

Writing a Modular Program in Java In this lab, you add the input and output statements...

Writing a Modular Program in Java
In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31.

Instructions

  1. Notice that variables have been declared for you.
  2. Write the simulated housekeeping() method that contains input statements to retrieve a year, a month, and a day from the user.
  3. Add statements to the simulated housekeeping() method that convert the String representation of the year, month, and day to ints.
  4. Include the output statements in the simulated endOfJob() method. The format of the output is as follows:
    month/day/year is a valid date.
    or
    month/day/year is an invalid date.
  5. Execute the program entering the following date:
    month = 5, day = 32, year =2014
    Observe the output of this program.
  6. Execute the program entering the following date:
    month = 9, day = 21, year = 2002
    Observe the output of this program.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// DateValidorNot.java

import java.util.Scanner;

public class DateValidorNot {

   public static void main(String[] args) {

       housekeeping();

   }

   private static void housekeeping() {
       int day, month, year;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       String date;
       System.out.print("Enter Date (in MM/DD/YYYY format) :");
       date = sc.next();

       int indx1 = date.indexOf("/");
       month = Integer.parseInt(date.substring(0, indx1));
       int indx2 = date.lastIndexOf("/");
       day = Integer.parseInt(date.substring(indx1 + 1, indx2));
       year = Integer.parseInt(date.substring(indx2 + 1));

       endOfJob(day, month, year);

   }

   private static void endOfJob(int day, int month, int year) {
       boolean bool1 = false, bool2 = false, bool3 = false;

       if (month >= 1 && month <= 12) {
           bool1 = true;
       }
       if (day >= 1 && day <= 31) {
           bool2 = true;
       }
       if (year > 0) {
           bool3 = true;
       }

       if (bool1 && bool2 && bool3) {
           System.out.println(month + "/" + day + "/" + year
                   + " is valid date.");
       } else {
           System.out.println(month + "/" + day + "/" + year
                   + " is an invalid date.");
       }

   }

}
_________________________

Output#1:

Enter Date (in MM/DD/YYYY format) :5/32/2014
5/32/2014 is an invalid date.
_______________________

Output#2:

Enter Date (in MM/DD/YYYY format) :9/21/2002
9/21/2002 is valid date.


_______________________Thank You


Related Solutions

Writing a Modular Program in Python In this lab, you add the input and output statements...
Writing a Modular Program in Python In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared...
Summary In this lab, you add the input and output statements to a partially completed Java...
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method...
java program: Input and output the following details. Your program can only receive the correct input....
java program: Input and output the following details. Your program can only receive the correct input. (For example: a notification will be given if you not enter Char data type for Name) Name: Ali bin Ahmad Occupation: Technician Age: 30 Hometown: Negeri Sembilan Years of Service: 12 Gender: Male
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and...
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring:...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically,...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically, reading from and writing to local files, and formatting output. Instructions Read in file input.csv and generate a cleanly formatted table in output.txt. See the samples below. input.csv name,ID,salary,years experience foo,1,13890,12 bar,2,2342,3 baz,3,99999,24 output.txt Name | ID | Salary | Years experience -----+----+--------+----------------- Foo | 1 | 13890 | 12 Bar | 2 | 2342 | 3 Baz | 3 | 99999 | 24
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is...
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 The output is: 8 3 Your program must define and call the following function. swap_values() returns the two values in swapped order. def swap_values(user_val1, user_val2) **in Python, please
***USING JAVA Scenario: You will be writing a program that will allow a user to find...
***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word. Requirements: Do not use Arrays for this assignment. Do not use any String class methods (.phrase(), replace methods, regex methods)...
Program using java Take user input and give corresponding output. A user is considering different options...
Program using java Take user input and give corresponding output. A user is considering different options of operating air conditioning. Depending on room temperature (here, room temperature is given by user), this program should give different instructions. There are three scenarios. - If temperature is above 90, the program should output “cooling”. -If the temperature is below 70, the program should output “heating”. -Otherwise, the program should output “stopped”. For example, if user enters “95”, this is how the program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT