92.6k views
5 votes
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Assume that the list will always contain less than 20 integers.

Ex: If the input is:

5 50 60 140 200 75 100
the output is:

50 60 75
The 5 indicates that there are five integers in the list, namely 50, 60, 140, 200, and 75. The 100 indicates that the program should output all integers less than or equal to 100, so the program outputs 50, 60, and 75. For coding simplicity, follow every output value by a space, including the last one.

Such functionality is common on sites like Amazon, where a user can filter results.

Write your code to define and use two methods:
public static void getUserValues(int[] myArr, int arrSize, Scanner scnr)
public static void outputIntsLessThanOrEqualToThreshold(int[] userValues, int userValsSize, int upperThreshold)

Utilizing methods helps to make main() very clean and intuitive.
CODE:
import java.util.Scanner;

public class LabProgram {

/* Define your methods here */

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] userValues = new int[20];
int upperThreshold;
int numVals;

numVals = scnr.nextInt();
getUserValues(userValues, numVals, scnr);

upperThreshold = scnr.nextInt();
outputIntsLessThanOrEqualToThreshold(userValues, numVals, upperThreshold);
}
}

1 Answer

6 votes

Answer:

Here is the implementation of the getUserValues and outputIntsLessThanOrEqualToThreshold methods to solve the problem:

import java.util.Scanner;

public class LabProgram {

public static void getUserValues(int[] myArr, int arrSize, Scanner scnr) {

for (int i = 0; i < arrSize; i++) {

myArr[i] = scnr.nextInt();

}

}

public static void outputIntsLessThanOrEqualToThreshold(int[] userValues, int userValsSize, int upperThreshold) {

for (int i = 0; i < userValsSize; i++) {

if (userValues[i] <= upperThreshold) {

System.out.print(userValues[i] + " ");

}

}

}

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

int[] userValues = new int[20];

int upperThreshold;

int numVals;

numVals = scnr.nextInt();

getUserValues(userValues, numVals, scnr);

upperThreshold = scnr.nextInt();

outputIntsLessThanOrEqualToThreshold(userValues, numVals, upperThreshold);

}

}

Step-by-step explanation:

The given Java code solves the problem of reading a list of integers from input and outputting all the integers that are less than or equal to a given threshold value. The program reads the number of integers to be entered from the user, followed by the list of integers, and then reads the threshold value from the user. Finally, it outputs all integers from the list that are less than or equal to the threshold.

The program defines two methods, getUserValues and outputIntsLessThanOrEqualToThreshold, which are used in the main method to solve the problem.

The getUserValues method takes three parameters: an integer array myArr, an integer arrSize, and a Scanner object scnr. This method reads arrSize integers from the user using the Scanner object scnr and stores them in the myArr array.

The outputIntsLessThanOrEqualToThreshold method takes three parameters: an integer array userValues, an integer userValsSize, and an integer upperThreshold. This method iterates over the userValues array and outputs all integers that are less than or equal to upperThreshold.

The main method initializes an integer array userValues with a size of 20 and reads the number of integers to be entered from the user using the Scanner object scnr. It then calls the getUserValues method to read the list of integers from the user. Next, it reads the threshold value from the user and calls the outputIntsLessThanOrEqualToThreshold method to output all integers from the list that are less than or equal to the threshold.

User Tenzolinho
by
8.3k points

No related questions found