Question

In: Computer Science

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

  • 2007 is not a leap year
  • 2008 is leap year
  • 1700 is not leap year
  • 1600 is leap year.

In the main method, test your code with above four years and another five years that will be generated randomly by the code. Use two separate for-loops. In the first for-loop in each of the four iterations user should be asked to provide one year and when you test the program you input the above four years. For each year program should report whether it is leap or non-leap. The second loop should have five iterations and at each iteration program should generate a random integer for year in the range 1582 and 2019 (including the limits) and report whether it is leap or non-leap.

Solutions

Expert Solution

//LeapYear.java
import java.util.Random;
import java.util.Scanner;
public class LeapYear {
  public static boolean isLeap(int year){
    if(year<1582){
      return false;
    }
    else if(year % 4 == 0 && year % 100 != 0){
      return true;
    }
    else if(year % 400 == 0){
      return true;
    }
    return false;
  }

  public static void main(String args[]){
    int yearNumber;
    Scanner scanner = new Scanner(System.in);
    for(int i = 0;i<4;i++) {
      System.out.println("Enter an year number: ");
      yearNumber = scanner.nextInt();
      if (isLeap(yearNumber)) {
        System.out.println(yearNumber + " is a leap year");
      } else {
        System.out.println(yearNumber + " is not a leap year");
      }
    }

    Random random = new Random();
    for(int i = 0;i<5;i++){
      yearNumber = random.nextInt(437) + 1582;
      if (isLeap(yearNumber)) {
        System.out.println(yearNumber + " is a leap year");
      } else {
        System.out.println(yearNumber + " is not a leap year");
      }
    }
  }
}


Related Solutions

Design a Geometry class with the following methods: * A static method that accepts the radius...
Design a Geometry class with the following methods: * A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula: Area=?r^2 * A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula: Area = Length x Width * A static method that accepts the length of a triangle's base and the triangle's hight. The method should return...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
1) Only a single main class is required 2) The main class must contain a static...
1) Only a single main class is required 2) The main class must contain a static method called "getInputWord" that prompts a user to input a word from the keyboard and returns that word as a String. This method must use "JOptionPane.showInputDialog" to get the input from the user (you can find info about JOptionPane in JavaDocs) 3) The main class must contain a static method called "palindromeCheck" that takes a String as an input argument and returns a boolean...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
Static methods can be called directly from the name of the class that contains the method....
Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5); System.out.println(var); } } Create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is a constant defined in...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
In Java... Create a class named _MyArrays which has a main( ) and other static methods....
In Java... Create a class named _MyArrays which has a main( ) and other static methods. Part I (30%) [Main method] In the main() - Request the number of reviewers (r) and movies (m) - Declare a r x m two-dimensional array of integers - Allow the user to enter distinct values , row by row to fill the two dimensional array - Display the two-dimensional array using a _displayArray method. - Using the _printHighestLowestMovieRating method print the highest and...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface: public interface Create { int change(int something); --------------------------------------------------------------------- The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example Create one = Creater.substractor(5); Create two = Creater.subtractor(-3); System.out.println(one.change(5)); // should print 0 System.out.println(second.change(3); // should print -6 The answer should be is a single...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT