Write a Java program to do the following: Specifics: Write an application that a
ID: 3674607 • Letter: W
Question
Write a Java program to do the following:
Specifics:
Write an application that asks a user to type an even number to continue or to type 999 to stop. When the user types an even number, display the message “Good job!” and then ask for another input. When the user types an odd number, display an error message and then ask for another input. When the user types 999, end the program. Save the file as EvenEntryLoop.java.
Again, Don’t forget to create the application/project EvenEntryLoop Test.javaClass that has the main method and an object to use the EvenEntryLoop class
Explanation / Answer
/*
EvenEntryLoop.java
Create the package name app1
This program contains a class EvenEntryLoop having a function name test
which get a number from user and test whether it is even number or not. If it is even number it shows Good Job! message otherwise show Error ! message
This process is repeateadly performed untill user eneter 999 (to stop)
*/
package app1;
import java.io.*;
public class EvenEntryLoop
{
public void test()
{
int inum=0;
InputStreamReader r = new InputStreamReader(System.in); // InputStreamReader to read number from keyboard
BufferedReader br=new BufferedReader(r);
String num="";
while(!num.equals("999"))
{
System.out.println("Enter a number (999 to stop)");
try
{
num=br.readLine();
inum = Integer.parseInt(num); //convert the given text to integer
}
catch(Exception e)
{
System.out.println("Enter an integer number");
}
if(inum%2==0) //test whether the given number is even or not
{
System.out.println(" Good job!");
}
else
{
System.out.println("Error ! " );
}
}
br.close();
r.close();
}
}
/* Main Class
This program import the class from package app1 which is created using previous program
EvenEntryLoopTest
*/
import app1.EvenEntryLoop;
public class EvenEntryLoopTest
{
public static void main(String args[])
{
EvenEntryLoop obj =new EvenEntryLoop();
obj.test();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.