Answer to Question #273955 in Java | JSP | JSF for harryy

Question #273955

4. The Big One

by CodeChum Admin

Remember that time when we've tried identifying the largest digit among the given integer? Well, let's take it to the next level, and figure out the same thing on arrays/lists!


Let's do this one more time!


Instructions:

  1. Create a variable that accepts a positive integer.
  2. Create an empty list. Then, using loops, add random integer values into the list one by one. The number of values to be stored is dependent on the inputted positive integer given in the first instruction.
  3. Print out the largest integer among all the elements on the list, by any means necessary.

Input

The first line contains the size of the array/list.

The next lines contains an integer on each.

5
5
34
3
23
10

Output

A line containing an integer.

34





1
Expert's answer
2021-12-01T15:35:07-0500
package com.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("Input number: ");
        Scanner in = new Scanner(System.in);
        Integer itemNumber = Integer.valueOf(in.nextLine());
        List<Integer> list = new ArrayList<Integer>();
        Integer nextInt;
        for (int i = 0; i < itemNumber; i++) {
            nextInt = new Random().nextInt(100);
            list.add(nextInt);
            System.out.println("Generated integer: " + nextInt);
        }

        Integer maxInt = 0;
        for (Integer listInt : list) {
            if (listInt > maxInt) {
                maxInt = listInt;
            }
        }

        System.out.println("Max integer: " + maxInt);

    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS