Question

In Java, using only recursion. Ask the user for a list of integers. Display the greatest...

In Java, using only recursion. Ask the user for a list of integers. Display the greatest number in the sequence. Allow any non-integer to end the input. DO NOT USE LOOPS.

We are currently learning recursion and can only use recursion. Please provide notes so to better understand. Please describe how inputting a string will end the input section and then go on to execute what number is greater within the entire user input.

--------------------------------------------------------------------------------

Standard Input                
5 20 5 done
Enter several numbers. Enter a non-integer to end.\n
Greatest number in that sequence is 20\n
Standard Input                
57      81      24      43      9 ENTER
59      77      42      35      52 ENTER
complete
Enter several numbers. Enter a non-integer to end.\n
Greatest number in that sequence is 81\n
Standard Input                
35      65      86      68      81 ENTER
87      36      59      44      35 ENTER
59      10      2       31      36 ENTER
78      45      11      74      60 ENTER
73      87      6       9       94 ENTER
92      36      48      16      48 ENTER
44      9       15      76      97 ENTER
72      14      35      18      37 ENTER
60      95      5       93      64 ENTER
59      93      93      15      9 ENTER
24      33      49      11      3 ENTER
30      98      12      15      21 ENTER
34      99      87      32      19 ENTER
9       39      80      7       1 ENTER
94      76      31      65      23 ENTER
56      53      82      59      76 ENTER
80      44      42      74      62 ENTER
85      21      92      82      35 ENTER
1       37      23      38      30 ENTER
30      63      23      15      56 ENTER
no
Enter several numbers. Enter a non-integer to end.\n
Greatest number in that sequence is 99\n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complete code with proper commenting, Copy the code and run it into your system :

import java.util.*;

public class Main
{
//list to store all the integer values input by the user
static ArrayList<Integer> number_list = new ArrayList<Integer>();
  
//Scanner class object to get the input from User
static Scanner scan = new Scanner(System.in);
  
//Main Function
   public static void main(String[] args) {

   System.out.print("Enter Several Numbers : ");
     
   //Calling Recursive Function
   askuser();
     
     
     
   }
  
   //Recursion Function
   public static void askuser()
   {
   //taking a input from user and store it into num variable
   String num = scan.next();
  
   //We are using try, so that if user enter a non numerical value, Exception will thrown
   //catch block catches that exception, where we print the largest value in the sequence
   try {
   //Converting the string into int data type
   int n = Integer.parseInt(num);
     
   //adding the int value to the number_list
   number_list.add(n);
     
   //again running the function, recursive call
   askuser();
  
   }
   catch(Exception e) {
   //This variable will store the largest value
   int temp = 0;
  
   //iterating the number list
   for(Integer value : number_list)
   {
  
   if(temp < value)
   {
   temp = value;
   }
   }
  
   System.out.println("Largest Value in the sequence : "+ temp);
   return;
   }
  
   }
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------

Output :

Enter Several Numbers : 2 3 4 1 23 77 34 End Largest Value in the sequence : 77

Enter Several Numbers : 34 -4 22 56 99 10 12 Done Largest Value in the sequence : 99

---------------------------------------------------------------------------------------------------------------------------------------------------------------

Explanation :

1. Arraylist<Interger> : It is a generic datatype, which takes an integer value as an input

2. In the askUser() function, we are asking an input from the user and store it into String variable

3. Now we are trying to convert the value into Integer Datatype

4. If it's succesfully converted :

4.1. we add it to the number list

4.2 And call the askUser() function again

5. If it's falied, it will throw an error which will catched in catch block

6. In catch block, we iterarte over the List and store the largest value in temp var and display it

7. at last we use return, which transfer the control back to the main function from the askuser() function.

Add a comment
Know the answer?
Add Answer to:
In Java, using only recursion. Ask the user for a list of integers. Display the greatest...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  • Please show how you did this in excel. :13-19 Every home football game for the past...

    Please show how you did this in excel. :13-19 Every home football game for the past eight years at Eastern State University has been sold out. The revenues from ticket sales are significant, but the sale of food, beverages, and souvenirs has contrib- uted greatly to the overall profitability of the football program. One particular souvenir is the football pro- gram for each game. The number of programs sold at each game is described by the following probabil- ity distribution:...

  • Create a JavaFX application that generates a 10 x 10 grid. Populate each cell in the grid with a ...

    Create a JavaFX application that generates a 10 x 10 grid. Populate each cell in the grid with a random integer in the range [0, 99]. If the integer is divisible by 2, color the cell blue. If the integer is divisible by 3, color the cell yellow. If the integer is divisible by 6, color the cell green. Here's a sample run: Number Grid 74 74 38 57 62 40 42 65 27 38 44 8 48 60 30...

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • python program do not use dictionary, list only Complete the program found in assignment4.py. You may...

    python program do not use dictionary, list only Complete the program found in assignment4.py. You may not change any provided code. You may only complete the sections labeled: #YOUR CODE HERE Write a program that does the following. Reads the contents of Text from the include file, input.txt Create a dictionary of key-value pairs called index.txt Key: This represents the individual word Value: This is a list of the line number from Text where Key appeared Example: If the word...

  • Calculate the range, mean, mode, median, Standard deviation Calculate the skewness and kurtosis for the above...

    Calculate the range, mean, mode, median, Standard deviation Calculate the skewness and kurtosis for the above data and interpret the data. The following is data collected from the daily salary employees of ZZ COMPANY.. 68 19 43 11 37 30 19 67 65 34 96 23 93 73 46 39 21 12 89 52 33 21 18 57 80 56 91 62 56 48 84 23 78 96 49 36 90 42 65 15 43 36 65 59 34 71...

  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • use java thanks Write a program that prompts a user to enter number of Math question that she wishes the system to gene...

    use java thanks Write a program that prompts a user to enter number of Math question that she wishes the system to generate. The system will random generate Math questions which consisted of ±' , x and / of two positive integer numbers. The sample run programs are depicted as below Sample run 1 How many Math question you want to create? 4 Sample run 2 How many Math question you want to create? 10 4 questions have been created...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT