Question

In: Computer Science

Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...

  • Code in Java
  • Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed.
  • Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it.

Input Format

A series of integers

Constraints

None

Output Format

The smallest number

Sample Input 0

1
10
6
4
-5
-4
0
.

Sample Output 0

Enter a series of integers, each on a different line. When done, enter a period.
-5

Sample Input 1

100
12
89
66
.

Sample Output 1

Enter a series of integers, each on a different line. When done, enter a period.
12

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Scanner;

public class MinRec {
        public static void main(String[] args) {
                System.out.println("Enter a series of integers, each on a different line. When done, enter a period.");
                Scanner sc = new Scanner(System.in);
                ArrayList<Integer> list = new ArrayList<>();
                while (sc.hasNextInt()) {
                        list.add(sc.nextInt());
                }
                int smallest = getSmallest(list);
                System.out.println(smallest);
        }

        private static int getSmallest(ArrayList<Integer> list) {
                return getMin(list, list.size());
        }

        private static int getMin(ArrayList<Integer> list, int n) {
                if (n == 1)
                        return list.get(0);

                return Math.min(list.get(n - 1), getMin(list, n - 1));
        }

}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write a method that takes an array of integers as input. The method should find and...
Write a method that takes an array of integers as input. The method should find and display two indices m and n such that if you sort the elements from index m to index n the entire array would be sorted. The method should minimize m − n, that is it should find the smallest subsection for the array that needs to be sorted. For example, int[] a = {2, 4, 6, 7, 9, 8, 12, 15, 5, 13, 18,...
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method...
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method has the following header(signature): public static void removeDuplicate(ArrayList<Integer> list) Write a test program (with main method) that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is what the input and output should look like:      Enter ten integers: 28 4 2 4 9 8 27 1 1 9      The distinct...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest...
Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest common divisor. *I am using Eclipse DS-5 Community Workspace with A64 Instruction Set) Use the following algorithm: // Given two integers m and n: if (m < n) gcd(n, m) if n is a divisor of m gcd(m, n) = n else gcd (m, n) = gcd (n, m % n) Your program must be recursive. You must create a function that calls itself,...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT