Question

JAVA turn this psuedo code into java code. USING NO LOOPS! ALL LOOPS MUST BE TURNED...

JAVA

turn this psuedo code into java code.

USING NO LOOPS! ALL LOOPS MUST BE TURNED INTO RECURSIVE CALLS.

English:

1. Prompt for and read a number between 1 and 5. Repeat this step until the input is 1..5.
2. Repeat the following multiple times according to the number read in step 1.

a. Read in a list of integers ending with a 0. The 0 marks the end of the input and is not considered part of the list
b. Print the largest and smallest integers in the list.
c. If only a zero appears in the list, print an error message

Psuedo code:

begin
repeat
prompt user to enter a number from 1 to 5
read number
until number is from 1 to 5


repeat number times
decrement number
read input


if input = 0
print error message
else
set min to input
set max to input
read input
repeat while input is not= 0
if input < min then
set min to input
else
if input > max then
set max to input
end if
end if
read input
end repeat
print max
print min
end if
end repeat
end

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class RecursiveCalls {

   /**
   * Finds the min/max of the numbers and returns an array containing min and max
   */
   private static int[] findMinMax(Scanner in, int[] minMax) {
       int num = in.nextInt();
      
       // Check if num is 0
       if (num == 0)
           return minMax;
       else {
           if (num < minMax[0])
               minMax[0] = num;
           else if (num > minMax[1])
               minMax[1] = num;
          
           return findMinMax(in, minMax);
       }
   }
  
   /**
   * Read a list of numbers and finds the min and max
   */
   private static void readList(Scanner in, int times) {
       if (times == 0)
           return;
       else {
           // Read numbers
           System.out.print("Enter a list of numbers(0 to end list): ");
           // Read number
           int input = in.nextInt();
           int[] minMax = new int[2];

           if (input == 0) {
               System.out.println("No numbers in the list");
           } else {
               // Find min from a list of integers
               minMax = findMinMax(in, new int[]{input, input});

               // Display min and max
               System.out.println("Minimum is: " + minMax[0]);
               System.out.println("Maximum is: " + minMax[1]);
           }

           // Read a list of numbers
           readList(in, times - 1);
       }
   }


   /**
   * Reads n(1..5) from the user
   */
   private static void readN(Scanner in) {
       System.out.print("Enter a number from 1 to 5: ");
       int n = in.nextInt();

       if ((1 <= n) && (n <= 5))
           readList(in, n);
       else {
           System.out.println("Invalid input.\n");
           readN(in);
       }
   }

   public static void main(String[] args) {

       // Scanner to get user input
       Scanner in = new Scanner(System.in);

       // Read n from user
       readN(in);

       // Close scanner
       in.close();
   }
}


SAMPLE OUTPUT:

Enter a number from 1 to 5 2 Enter a list of numbers (0 to end list):35-1720 Minimum is: -1 Maximum is: 7 Enter a list of num

Add a comment
Know the answer?
Add Answer to:
JAVA turn this psuedo code into java code. USING NO LOOPS! ALL LOOPS MUST BE TURNED...
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
  • Specifics:  Use only the instructions covered to date. No jal and jr. Do not use...

    Specifics:  Use only the instructions covered to date. No jal and jr. Do not use any pseudo-instructions.  Do not store any values into memory. There are no lw or sw instructions in this program. Just read the integers and process them.  Document the beginning of your program with your name, project description, and class ID.  comment every instruction. Write a MIPS assembly language program to do the following: 1. Print your name 2. Prompt the user...

  • How would I code this method in Java using a scanner? Begin by asking how many...

    How would I code this method in Java using a scanner? Begin by asking how many spaces the row should be with a prompt using System.out.print like the following, with 40 as the maximally allowed number of spaces: (user input shown bold) Please enter the number of spaces for the game (1-40): eight To do this, use the promptNumberReadLine method that you will write, described on the back page. If the user does not type a number in the correct...

  • Write a section of Java code using while, do-while and for loops Write a section of...

    Write a section of Java code using while, do-while and for loops Write a section of Java code that will print out random integers between 1 and 100 while N is less than or equal to 5. Sample output > random # 1 is: 73 random # 2 is: 68 random # 3 is: 76 random # 4 is: 64

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • Translate psuedo code for computing chromatic number of a grapgh to java code 1 //graph G,...

    Translate psuedo code for computing chromatic number of a grapgh to java code 1 //graph G, vertices n, vertices are numbered o,1-- //G i //colors are stored in arrayq //qlil has color of vertex i, initially all0 s stored in adjacency list or adjacency matrix p , returns chromatic number //colors G using mininum number of colors int color () for (i 1 to n) //if G can be colored using i colors starting at vertex 0 if (color (0,...

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

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