Question

Java Program 4: Methods Purpose: To practice using both void and value-returning methods and understand when,...

Java Program 4: Methods Purpose: To practice using both void and value-returning methods and understand when, where, and how to use them. The following problem employs the use of both void and value-returning methods: (Be sure to start with an algorithm to help you organize your ideas and provide a blueprint as to how to approach the problem. Think about what kind of return type your method will need. Carefully examine and follow ALL the program specifications (for full credit). Leap Year Program: Design a program that asks the user to enter a four-digit year and then determines if that year is a leap year. Your program must contain at least the following four methods according to the following descriptions: (Note that your main method will consist primarily of calls to these four methods).. Methods: 1) displayInstructions : this method will tell the user what the program does, what to input, and what will be displayed as a result of running the program. It will not return any value nor will it need any formal parameters. 2) getYear has no formal parameters. It only asks the user to input a year and then returns that year as an integer value. 3) isLeap method has a formal integer parameter called year. It determines if a year is a leap year, and then returns a Boolean value (true or false) depending on whether the year is a leap year or not. How to determine if a year is a leap year? Rule: a year is a leap year if it is divisible by 4, but not divisible by 100, except when it is divisible by 400. (Hint: think about using the modulus operator here.) 4) displayResults has two formal parameters, the year and a boolean variable containing the value returned from method isLeap. This method will not need to return a value. It will simply display the year and display whether or not that year is a leap year. Be sure to carefully examine and follow ALL the above program specifications as described. When coding your methods, a technique that experienced programmers often use is to code one method at a time and test it to make sure it works before moving to the next method. This will really simplify your debugging process considerably. Is strongly suggest you employ this step-by-step process. Screen results for this program might look like this: First Run: This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year or not. Enter a year: 2010 2010 is not a leap year. Second Run: This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year. Enter a year: 2008 2008 is a leap year. Third Run: This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year. Enter a year: 1900 1900 is not a leap year. Fourth Run: This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year. Enter a year: 2000 2000 is a leap year. Lab Submission Guidelines: Be sure to make your program user friendly and add prologue comments at the top of your program as well as JavaDoc comments at the beginning of each method (follow JavaDoc examples in the text or in my PowerPoint slides).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !

===========================================================================


import java.util.Scanner;

public class LeapYearChecker {


    public static void main(String[] args) {


        displayInstruction();
        int year = getYear();
        boolean isLeapYear = isLeap(year);
        displayResults(year, isLeapYear);

    }

    /*
    Method accepts year and the leap year status
    Method prints the year and the if its a leap year or not
     */
    private static void displayResults(int year, boolean isLeapYear) {

        if (isLeapYear) {
            System.out.println( year + " is a leap year.");
        } else {
            System.out.println( year + " is not a leap year.");
        }
    }

    // displayInstructions
    private static void displayInstruction() {

        System.out.println("This program asks you to enter a year as a 4 digit number." +
                "The output will indicate whether the year you entered is a leap year.");

        System.out.println();
    }

    /*
       Method ask user for a year
       Methos returns the year as int
     */
    private static int getYear() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();
        return year;
    }

    /*
        Method accept an int for the year
        Method returns boolean if the year was a leap year else false if not
     */
    private static boolean isLeap(int year) {

        boolean flag = false;
        if (year % 400 == 0) {
            flag = true;
        } else if (year % 100 == 0) {
            flag = false;
        } else if (year % 4 == 0) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

}

=====================================================================

let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer.

Thanks!


===========================================================================

Add a comment
Know the answer?
Add Answer to:
Java Program 4: Methods Purpose: To practice using both void and value-returning methods and understand when,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design an application with a single class and two static methods: method main and method isLeap....

    Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not. public static boolean isLeap ( int year ) The year is leap year if it is divisible by 4, but not divisible by 100 , except if it is divisible by 400. Examples 2003 is not a leap year 2020 is leap year...

  • CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap...

    CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap year Create a new C++ project titled “HW 6” in the CodeBlocks IDE. Use the “Console Application” project option. Over time, the calendar we use has changed a lot. Currently, we use a system called the Gregorian Calendar and it was introduced in 1582. The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible...

  • Has to be written in C++ Write a program that asks the user to enter the...

    Has to be written in C++ Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and...

  • 3. Using no date-specific Java libraries, write a program that computes the number of days between...

    3. Using no date-specific Java libraries, write a program that computes the number of days between any two dates (inclusive). You must take into account leap years. The rule for computing a leap year is as follows: If the year is divisible by 4 it is a Leap Year ... Unless the year is divisible by 100, then it is _not_ a Leap Year ... Unless the year is divisible by 400, then it _is_ a Leap Year. During a...

  • Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for...

    Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for the month (1 = January, 2 February, etc). l An integer for the year The class should have a method named getNumberOfDays that returns the number of days in the specified month. The method should use the following criteria to identify leap years: 1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and if...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • Overview: In this lab you are to write a Java program to prompt the user for...

    Overview: In this lab you are to write a Java program to prompt the user for an integer indicating a year and then print the calendar for that year. Objective: This lab's objective is to exercise you with the use of Java’s control statements. You are required to use exactly one while statement, one for statement and one switch statement. You will also practice on how to use some basic input methods of the Scanner class and some formatting techniques...

  • Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate...

    Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate rectangle area. Some requirements: 1. User Scanner to collect user input for length & width 2. The formula is area = length * width 3. You must implement methods getLength, getWidth, getArea and displayData ▪ getLength – This method should ask the user to enter the rectangle’s length and then return that value as a double ▪ getWidth – This method should ask the...

  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT