Question

B.2. Write a Java program to display a dialog box that asks you to enter your...

B.2. Write a Java program to display a dialog box that asks you to enter your user name and the age as

shown below:

The program displays in a dialog box the sum of digits of your age. For example, if you age is 19, the sum

will be 1+9 = 10 and the output will be as shown below: ( it just shows a message box that says the sum of the digits age of john is 10.

Homework Answers

Answer #1

NameAndAge.java

import javax.swing.JOptionPane;


public class NameAndAge {

  
   public static void main(String[] args) {
       String name = JOptionPane.showInputDialog("Enter the name: ");
       int age = Integer.parseInt(JOptionPane.showInputDialog("Enter the name: "));
       int sum = 0;
       while(age!=0){
           sum = sum + age % 10;
           age = age/10;
       }
       JOptionPane.showMessageDialog(null,"the sum of the digits age of "+name+" is "+sum+"." );

   }

}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
java: write an applet to display the message Welcome to Java Programming The program will have...
java: write an applet to display the message Welcome to Java Programming The program will have a checkbox to display the message in italic and or bold. The program will have a combo box that uses a string array called fontNames to store the following font names, ["Dialog", "Century", "Gothic", "Courier", "Serif"]. Depending on the selection in the font name in combo box, the applet will display the message using the font name. The program will have a radio button...
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”....
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”. If the user enters “bus” display “$1.00”. If they enter “subway” display “$1.50 and if they enter “walk” display “Free”. B. Write a java program that creates the following two arrays: String[] candidates = {“S Jones”,”Justin Fairfax”,”Clark Duncan”}; int[] votes = {7345,4324,3211}; Write the code that will search the votes array for the candidate with the largest number of votes and prints the name...
[10 marks] (SumDigits.java) Write code that asks the user to enter an integer between 100 and...
[10 marks] (SumDigits.java) Write code that asks the user to enter an integer between 100 and 999. The program finds and displays the three digits in the number. The program also calculates and displays the sum of the three digits. A sample run is shown below: Enter a number between 100 and 999: 528 The three digits in the number are: 5 2 8 The sum of the three digits is: 15
(SumDigits.java) Write code that asks the user to enter an integer between 100 and 999. The...
(SumDigits.java) Write code that asks the user to enter an integer between 100 and 999. The program finds and displays the three digits in the number. The program also calculates and displays the sum of the three digits. A sample run is shown below: Enter a number between 100 and 999: 528 The three digits in the number are: 5 2 8 The sum of the three digits is: 15 Note: You must use integer division and modulo division to...
Write a Java program that asks the user to enter a person’s age. Then the program...
Write a Java program that asks the user to enter a person’s age. Then the program should display text indicating whether the person is an infant, a toddler, a child, a teenager, an adult, or a senior. It should display it just like this: “This person’s age category: x”, where x is the person’s age category based on the following guidelines: If less than 1 year old, the person is an infant. If at least 1 year old but younger...
1. Write a complete program in C++ that does the following: [2] asks a user to...
1. Write a complete program in C++ that does the following: [2] asks a user to enter two integer numbers (display a warning message that the second number should be different from zero) [3] reads the two numbers from the keyboard; [4] displays the product, the sum, the quotient and the remainder of integer division of the first one by the second one. [3] displays the result of floating-point division. Make sure to follow programming style guidelines.
Write a program that asks the user to enter a series of numbers separated by commas....
Write a program that asks the user to enter a series of numbers separated by commas. Here is an example of valid input: 7,9,10,2,18,6 The program should calculate and print the sum of all the numbers. Sample Run java NumberSum Enter·numbers·separated·by·commas:1,2,3↵ 6↵
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT