In: Computer Science
JAVA PROGRAMMING
RecallNoIF!
Write a program named RecallNoIF that reads an integer representing a year. (In this case there is no prompt from the program.) The program prints out RECALL if the year was 2004, 2010 or 2015 and NO RECALL otherwise.
CONSTRAINT: Nowhere in the program is an if statement used.
REMINDER: the program's output is shown here in bold; the user's data entry is shown here in italics.
Sample Interactive Run 1:
2010
RECALL
Sample Interactive Run 2:
2012
NO RECALL
Java program prompts the user to enter the input year value then check if input year is 2004 or 2010 or 2015 then print RECALL otherwise print the NO RECALL on java console window.
//RecallNoIF.java
//import Scanner class
import java.util.Scanner;
public class RecallNoIF
{
public static void main(String[] args)
{
//Create an object of Scanner
class
Scanner kboard=new
Scanner(System.in);
//declare an integer variable,
inputYear
int inputYear;
//read an integer value from
keyboard
inputYear=Integer.parseInt(kboard.nextLine());
//Ternary operator
//(Condition )? (expression1 if
condition is true) : (expression2 if condition is false)
//Check if inputYear is 2004 or
2010 or 2015
//Then set the result to RECALL
otherwise set result to NO RECALL
String result=(inputYear==2004
||inputYear==2010 ||inputYear==2015)?
"RECALL":"NO RECALL";
//print result on java
console
System.out.println(result);
} //end of main method
} //end of the class,RecallNoIF
Sample output:
Sample Interactive Run 1:
Sample Interactive Run 2:
Update : The below is output for input of 1999 and 2004 for year input.