Question

In: Computer Science

JAVA Take in a user input. if user input is "Leap Year" your program should run...

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.

Solutions

Expert Solution

/*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));
                }
        }
}


Related Solutions

Your Java program should perform the following things:Take the input from the user about the...
Your Java program should perform the following things:Take the input from the user about the patient name, weight, birthdate, and height.Calculate Body Mass Index.Display person name and BMI Category.If the BMI Score is less than 18.5, then underweight.If the BMI Score is between 18.5-24.9, then Normal.If the BMI score is between 25 to 29.9, then Overweight.If the BMI score is greater than 29.9, then Obesity.Calculate Insurance Payment Category based on BMI Category.If underweight, then insurance payment category is low.If Normal...
Program using java Take user input and give corresponding output. A user is considering different options...
Program using java Take user input and give corresponding output. A user is considering different options of operating air conditioning. Depending on room temperature (here, room temperature is given by user), this program should give different instructions. There are three scenarios. - If temperature is above 90, the program should output “cooling”. -If the temperature is below 70, the program should output “heating”. -Otherwise, the program should output “stopped”. For example, if user enters “95”, this is how the program...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive). 2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked. 3. Take a year input from the user (should be between 1500 and 2019) 4. If the input year is a leap year, print that year is a leap year; otherwise, print that the...
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT