Question

In: Computer Science

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 that contains input statements to retrieve a year, a month, and a day from the user. Add statements to the simulated housekeeping() method that convert the String representation of the year, month, and day to ints. 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. Execute the program entering the following date: month = 5, day = 32, year =2014 Observe the output of this program. Execute the program entering the following date: month = 9, day = 21, year = 2002 Observe the output of this program.

The current coding I have worked for the valid input portion, but not the invalid portion or the prompting for the user inputs section... help if you can!

/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { //Made this static to accessible houseKeeping and endOfJob method static int year; static int month; static int day; static boolean validData = true; //this is main method which start program execution public static void main(String[] args) { houseKeeping(); endOfJob(); } /** * This method take user input using scanner class and validate as per requirement given */ public static void houseKeeping() { String yearString; String monthString; String dayString; final int MIN_YEAR =0,MIN_MONTH=1,MAX_MONTH=12,MIN_DAY=1,MAX_DAY=31; Scanner sc = new Scanner(System.in); System.out.print("Please enter Year - "); yearString = sc.next(); year = Integer.parseInt(yearString); if(year <= MAX_MONTH) { validData = false; } System.out.print("Please enter Month - "); monthString = sc.next(); month = Integer.parseInt(monthString); if(month < MIN_MONTH || month > MAX_MONTH) { validData = false; } System.out.print("Please enter Day - "); dayString = sc.next(); day = Integer.parseInt(dayString); if(day < MIN_DAY || day > MAX_DAY) { validData = false; } } /** * This method prints whether inputed date is valid or invalid */ public static void endOfJob() { System.out.println(month+"/"+day+"/"+year+ " is a "+ (validData ? "valid": "invalid") + " date."); } }

Solutions

Expert Solution

Below is the answer. If you have any doubts, let me know in the comments section. Please Upvote:)

Given a month date and year, print the output as of they were valid or not. You have completed the code for valid section. I have added a condition to check for invalid input. Generally, some months consists of 30 days and some months consists of 31 days. And days in february were changed according to leap year. Condition that check leap year is added in the program. Below is the code.

/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ 
import java.util.Scanner; 
public class BadDate { 
    //Made this static to accessible houseKeeping and endOfJob method 
    static int year; 
    static int month; 
    static int day; 
    static boolean validData = true; 
    //this is main method which start program execution 
    public static void main(String[] args) { 
        houseKeeping(); 
        endOfJob(); } 
        /** * This method take user input using scanner class and validate as per requirement given */ 
    public static void houseKeeping() { 
        String yearString; 
        String monthString; 
        String dayString; 
        final int MIN_YEAR =0,MIN_MONTH=1,MAX_MONTH=12,MIN_DAY=1,MAX_DAY=31;
        int validDateInMonth[] = new int[]{-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        Scanner sc = new Scanner(System.in); 
        System.out.print("Please enter Year - "); 
        yearString = sc.next();
        year = Integer.parseInt(yearString);
        System.out.print("Please enter Month - "); 
        monthString = sc.next(); 
        month = Integer.parseInt(monthString); 
        System.out.print("Please enter Day - ");
        dayString = sc.next();
        day = Integer.parseInt(dayString);
        //this checks if a year is leap year. And change the days in month of february in validDateInMonth array
        if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            validDateInMonth[2] = 29;
        }
        if(year <= MIN_YEAR || month < MIN_MONTH || month > MAX_MONTH || day < MIN_DAY || day > validDateInMonth[month])
        { 
            validData = false; 
            
        } 
    } 
    /** * This method prints whether inputed date is valid or invalid */ 
    public static void endOfJob() { 
        System.out.println(month+"/"+day+"/"+year+ " is a "+ (validData ? "valid": "invalid") + " date."); 
    } }

Below pasting the code screenshot for better understanding of indentation.

Output screenshots:

Consider a leap year 2000 and month as 2. In leap year, 2nd month have only 29 days. If day is given as 31, then it is invalid date.


Related Solutions

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 Notice that variables have been declared for you....
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...
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
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...
Searching an Array for an Exact Match in Java Summary In this lab, you use what...
Searching an Array for an Exact Match in Java Summary In this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten Java program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user of the program to enter a city name; your program then searches the array for that city name. If it is not found, the program should print...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78. Instructions Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if...
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus...
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus and prints the employee’s name and bonus. Bonuses are calculated based on an employee’s productivity score as shown below. A productivity score is calculated by first dividing an employee’s transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked. Productivity Score Bonus <=30 $50 31–69 $75 70–199 $100 >= 200 $200 Instructions Ensure the file...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions Ensure the file named Multiply.java is open. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize...
17.18 LAB 8A: Input and formatted output: Left-facing arrow You will be working on generating a...
17.18 LAB 8A: Input and formatted output: Left-facing arrow You will be working on generating a formatted output using characters such as *, #, -, +. You need to prompt the user for a character for the arrowhead, and a character for the arrow body, and then print a left-facing arrow. Your prompts should be exactly "Enter a character for the arrowhead:" and "Enter a character for the arrow body:", as shown below. Ex: If the user inputs a *...
The following partially completed process cost summary describes the July production activities of the Molding department...
The following partially completed process cost summary describes the July production activities of the Molding department at Ashad Company. Its production output is sent to the next department. All direct materials are added to products when processing begins. Beginning work in process inventory is 20% complete with respect to conversion. Equivalent Units of Production Direct Materials Conversion Units transferred out 41,500 EUP 41,500 EUP Units of ending work in process 3,500 EUP 2,100 EUP Equivalent units of production 45,000 EUP...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT