Westonci.ca connects you with experts who provide insightful answers to your questions. Join us today and start learning! Join our Q&A platform to connect with experts dedicated to providing precise answers to your questions in different areas. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.

LAB: Output values below an amount - methods
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
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 will help to make main() very clean and intuitive.


Sagot :

Answer:

The program in Java is as follows:

import java.util.*;

public class Main{

public static void getUs erValues(int[ ] myArr, int arr Size, Scanner scnr){

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

        myArr[i] = scnr.nextInt();     }

    outputIntsLessThanorEqualToThreshold (myArr, arrSize, myArr[arrSize-1]);

}

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 n;

 n = scnr.nextInt();

 int [] myArr = new int[n];

 getUserValues(myArr,n,scnr); }

}

Explanation:

See attachment for complete program where comments are used to explain each line

View image MrRoyal