and age for two persons. You need to use separate dialog boxes for each input (i
ID: 3905423 • Letter: A
Question
and age for two persons. You need to use separate dialog boxes for each input (i.e., six separate dialog boxes for the inputs). You should store the names in String variables and the ages in int variables. Your program should compare the two ages and print out the information in a JoptionPane dialog box in the following format (Note that Smith and Doe are the last names in the examples shown below): First Person Smith, John (31) Second Person: Doe, Mike (35) smith is younger than Doe or First Person: Doe, John (31) Second Person: Smith, Mike (31) Doe is of same age as Smith or First Person: Doe, John (35) Second Person: Smith, Mike (31) Doe is older than Smith You should name your program file Hw1.java.Explanation / Answer
Programming code :
import javax.swing.*;
public class JoptionpaneEx
{
public static void main(String args[])
{
String fname1, fname2, lname1, lname2;
int age1, age2;
fname1 = JOptionPane.showInputDialog(null,"Enter First Name");
lname1 = JOptionPane.showInputDialog(null,"Enter Last Name");
age1 = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Age"));
fname2 = JOptionPane.showInputDialog(null,"Enter First Name");
lname2 = JOptionPane.showInputDialog(null,"Enter Last Name");
age2 = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Age"));
if(age1 > age2)
JOptionPane.showMessageDialog(null, lname1 + " is older than " + lname2);
else if(age1 < age2)
JOptionPane.showMessageDialog(null, lname1 + " is younger than " + lname2);
else
JOptionPane.showMessageDialog(null, lname1 + " is of same age as " + lname2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.