Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using java, I need to write a program that creates a menu with the following cho

ID: 3806502 • Letter: U

Question

Using java, I need to write a program that creates a menu with the following choices:

1. Show Happy sequence for a number.

2. Show all happy numbers in a range.

Example for part 1,

A happy number is a positive integer when the sum of the squares of the digits leads up to 1

Example 13 is happy since 1*1 + 3*3 = 10 and 1*1 + 0*0 = 1.

input 13000

output: 13000, 10, 1 - Happy

input: 643

output: 643, 61, 37, 58, 89, 145, 42, 20, 4, 16, 37 - Not Happy

For second part

The program should prompt the user to enter any two integers that will display all the happy numbers in a range. For example, It needs to look like this please. It asks the user for two numbers which the user inputs 1 and 1000 in the example below and displays all the happy numbers in between with .... to show empty spaces.

Number 1 1 Number 2 1000 Here are the happy numbers 10 19 28 3132 44 100 68.70 94 97 79 82 86 91 103 109 139 129130 133 167 188 190 1921 93 176 203 208 226 230 236 239 219 291.293 262263 280 3 01 302 329. 331 310 3 13 31 93 20 326 338 376 379 356 362 36 5.36 73 68 383 386 39 13 92 3 97 404 A 40 464 469 446 478 487 490 496 556 563 5655 66 536 608 617 6226 23 653 65 56 56 632 63 5. 637 638 644 649 665 671 700 694 709 683 716 680 673 748 739 761.763 736 8 02 790 793 806 784 847 836 860 863 8 18 820 833 899 901 904 907 888 881 874 93 1932 937 940 946 921.923 9 10.912913 989 973 998 970 964 Done

Explanation / Answer

Here is the working solution in java-

//Note-: Please read the comment carefully for better understanding.

//HappyMenu.java (Driver class to run the program)
public class HappyMenu {

   /**method to run this program
   *
   * @param args
   */
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       displayMenu();        //to display menu to the user
       int userChoice=0;
       try{
           userChoice= sc.nextInt();   //capturing user choice
           switch (userChoice) {
           case 1:
               System.out.println("Option 1 selected");
               System.out.println("Please enter number to check for happy sequence ");
               int input = sc.nextInt();   // taking user input
               checkForHappyNumber(input);   //check for happy number
               break;
           case 2:
               System.out.println("Option 2 selected");
               System.out.println("enter lower bound of happy number");
               int input1 = sc.nextInt();   // taking user input
               System.out.println("enter upper bound of happy number");
               int input2 = sc.nextInt();   // taking user input
               printHappyNumberInRange(input1, input2); //print the happy number which lies including given range between input1 to input2
               break;
           case 3:
               System.out.println("Exit selected");
               System.out.println("Exit");
               break;
           default:
               System.out.println("Invalid selection");
               break; // This break is not really necessary
           }
       }catch(InputMismatchException e){
           System.out.println("You have entered invalied choice");       //if user entered anything else except from integer value
       }

      

   }

   /**method of print happy number between range from input1 to input2
   *
   * @param input1
   * @param input2
   */
   private static void printHappyNumberInRange(int input1, int input2) {
       /*A happy number is a number defined by the following process: Starting with any positive
       * integer, replace the number by the sum of the squares of its digits, and repeat
       * the process until the number either equals 1 (where it will stay), or it loops endlessly
       * in a cycle which does not include 1
       * */
       int i, j, k;
       for (k = input1; k <=input2; k++) {
           i = k;
           while ((j = isHappy(i)) / 10 != 0) // You have to check the sum of
                                               // all digits until you get a single
                                               // digit is .
                                               // sum=1,2,3,..9
           {
               i = j; // If sum of digits= 19 it then again goes to 1+9 =10 and
                       // again 1+0= '1' a single digit to check if 1 or not
           }
           System.out.print(j == 1 ? k : ".");   // using ternary operator to print the result based on condition. if j==1 then print number else print .
       }
   }

   /**method to check whether a number is happy number or not
   *
   * @param input
   */
   private static void checkForHappyNumber(int input) {
       int j,userInput=input;
       while ((j = isHappy(input)) / 10 != 0) // You have to check the sum of
                                               // all digits until a single
                                               // digit is achieved i.e.
                                               // sum=1,2,3,..9
       {
           input = j; // If sum of digits= 19 it then again goes to 1+9 =10 and
                       // again 1+0= '1' a single digit to check if 1 or not
       }
       if (j == 1)
           System.out.println(userInput + " It is a happy number.");
       else
           System.out.println(userInput + " Not a happy number.");
   }

   /**method to test happy number
   *
   * @param n
   * @return
   */
   static int isHappy(int n) {
       if (n / 10 == 0)
           return n * n;
       else
           return (int) Math.pow(n % 10, 2) + isHappy(n / 10); // recursively adding the squares of digit in a number
   }

   /**method to display menu to the user
   *
   */
   private static void displayMenu() {
       System.out.println("Happy Menu");
       System.out.println("1. Show Happy sequence for a number.");
       System.out.println("2. Show all happy numbers in a range.");
       System.out.println("3. Exit.");
       System.out.println("Please choose an option..");

   }
}

//output of the program-

Happy Menu
1. Show Happy sequence for a number.
2. Show all happy numbers in a range.
3. Exit.
Please choose an option..
2
Option 2 selected
enter lower bound of happy number
1
enter upper bound of happy number
1000
1.....7..10..13.....19...23....28..3132...........44....49..................68.70........79..82...86....91..94..97..100..103.....109...................129130..133.....139...........................167........176...........188.190.192193.........203....208..........219......226...230.....236..239......................262263................280..........291.293.......301302.......310..313.....319320.....326..329.331......338.................356.....362..365.367368.......376..379...383..386....391392....397......404....409..............................440.....446.................464....469........478........487..490.....496.......................................536...................556......563.565566.........................................608........617....622623........632..635.637638.....644....649...653.655656........665.....671.673......680..683..........694.....700........709......716...................736..739........748............761.763....................784.....790..793........802...806...........818.820............833..836..........847............860..863..........874......881......888..........899.901..904..907..910.912913.......921.923.......931932....937..940.....946.................964.....970..973...............989........998.1000

//Note: use below method if you want to display around 50 characters(it will not exactly print next line after 50 characters but once character length is more than or equal to 50 it will start printing from next line) per line

/**method of print happy number between range from input1 to input2
   *
   * @param input1
   * @param input2
   */
   private static void printHappyNumberInRange(int input1, int input2) {
       /*A happy number is a number defined by the following process: Starting with any positive
       * integer, replace the number by the sum of the squares of its digits, and repeat
       * the process until the number either equals 1 (where it will stay), or it loops endlessly
       * in a cycle which does not include 1
       * */
       int i, j, k;
       String charLength="";   //charLength string for storing character length
       for (k = input1; k <=input2; k++) {
           i = k;
           while ((j = isHappy(i)) / 10 != 0) // You have to check the sum of
                                               // all digits until you get a single
                                               // digit is .
                                               // sum=1,2,3,..9
           {
               i = j; // If sum of digits= 19 it then again goes to 1+9 =10 and
                       // again 1+0= '1' a single digit to check if 1 or not
           }
           if(j==1){
               System.out.print(k);
               charLength+=k;
           }else{
               System.out.print(".");
               charLength+=".";
           }
           if(charLength.length()>=50 ){       //if character length is more than or equal to 50 printing new line
               System.out.println();
               charLength="";
           }
      
       }
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote