JAVA PROGRAMMING ** THE LAST EXPERT DID NOT ANSWER THIS CORRECTLY. FOR THE MONTH
ID: 3592697 • Letter: J
Question
JAVA PROGRAMMING
** THE LAST EXPERT DID NOT ANSWER THIS CORRECTLY. FOR THE MONTH REQUEST, IT MUST BE DONE WITH SWITCHES. CAN ANOTHER EXPERT FIX THIS FOR ME PLEASE. ALSO, THE DIALOG BOXES SHOULD BE JOPTIONPANE.
Create the class Random:
Ask and check for user name
Ask for month of birth (use a switch)
For Jan, Apr, Jul, and Oct, say "Go Dodgers!"
For Feb, May, Aug, and Nov, say "Go Steelers!"
For Mar, Jun, Sep, and Dec, say "Go Bernie!"
Ask user for favorite non-negative number
For even numbers, say "blah"
For odd numbers, say "oops!"
At the end, show user his/her inputs
The code must:
Use dialog boxes to allow user input and messages to the user
Use While loops to check for invalid user inputs (e.g., empty strings, strings instead of numbers or vice-versa, negative numbers, floats instead of integers or vice-versa, etc.)
Explanation / Answer
Random.java
import javax.swing.JOptionPane;
public class Random {
public static void main(String args[]) throws Exception{
Random random = new Random();
String user = "";
while(true)
{
user = JOptionPane.showInputDialog("Enter user name");
if(random.isAlpha(user))
break;
else
JOptionPane.showMessageDialog(null,"Please enter proper user name","Error Message", JOptionPane.ERROR_MESSAGE);
}
Object[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String month = (String)JOptionPane.showInputDialog(null,"Please enter your month of birth","Select a value",
JOptionPane.PLAIN_MESSAGE,
null,
months,
"Jan");
String msg="";
switch(month){
case "Jan": msg = "Go Dodgers!";break;
case "Feb": msg = "Go Steelers!";break;
case "Mar": msg = "Go Bernie!";break;
case "Apr": msg = "Go Dodgers!";break;
case "May": msg = "Go Steelers!";break;
case "Jun": msg = "Go Bernie!";break;
case "Jul": msg = "Go Dodgers!";break;
case "Aug": msg = "Go Steelers!";break;
case "Sep": msg = "Go Bernie!";break;
case "Oct": msg = "Go Dodgers!";break;
case "Nov": msg = "Go Steelers!";break;
case "Dec": msg = "Go Bernie!";break;
}
JOptionPane.showMessageDialog(null, msg);
int number=0;
while(true)
{
String num = JOptionPane.showInputDialog("Enter your favourite non-negative number");
if(random.isInt(num))
{
number = Integer.parseInt(num);
break;
}
else
JOptionPane.showMessageDialog(null,"Please enter proper value","Error Message", JOptionPane.ERROR_MESSAGE);
}
if((number%2) == 0)
{
JOptionPane.showMessageDialog(null, "blah");
}
else
{
JOptionPane.showMessageDialog(null, "oops!");
}
String userInput = "User name = "+user+" Month of birth = "+month+
" Favourite non-negative number = "+number;
JOptionPane.showMessageDialog(null, userInput,"Values you have provided", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
//This function validate the username
public boolean isAlpha(String user)
{
for (int i=0; i<user.length(); i++) {
char c = user.charAt(i);
if (!Character.isLetter(c))
return false;
}
return true;
}
//This function validates the favourite number
public boolean isInt(String value)
{
try
{
int i = Integer.parseInt(value);
/*If the value is not an integer then it will automatically throw an exception
* which will be handled by CATCH block. From the CATCH block we will return false*/
if(i >= 0)
{
return true; //If it's a positive value,then returns true,else false
}
else
return false;
}
catch(Exception e)
{
return false;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.