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...
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...
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 )...
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...
In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you will be able to create Object from * EmployeeInfo class to use fields and attributes.Demonstrate as many methods as possible * to use with proper business work flow.Think as a Software Architect, Product Designer and * as a Software Developer.(employee.info.system) package is given as an outline,you need to elaborate * more to design an application that will meet for fortune 500 Employee Information *...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts. As a reminder, you can...
Let's get some practice with maps! Create a public class CountLetters providing a single static method...
Let's get some practice with maps! Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts. As...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT