In: Computer Science
JAVA
Take in a user input.
if user input is "Leap Year" your program should run exercise 1
if user input is "word count" your program should run exercise 2
Both exercises should run in separate methods
Exercise 1:
write a java method to check whether a year(integer) entered by the user is a leap year or not.
Exercise 2:
Write a java method to count all words in a string.
/*If you have any query do comment in the comment section else like the solution*/
import java.util.*;
public class LeapYear {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Leap Year or Word Count: ");
String input = sc.nextLine();
if(input.equals("Leap Year")) {
System.out.print("Enter year: ");
int year = sc.nextInt();
if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
System.out.println("Leap Year");
} else {
System.out.println("Not a Leap Year");
}
} else if(input.equals("word count")) {
System.out.print("Enter a string: ");
String word = sc.nextLine();
System.out.println("Word count is: " + (word.split(" ").length));
}
}
}