Question

import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...

import java.util.Stack;
import java.util.Scanner;

class Main {
public static void main(String[] args)
   {
       Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */
       Scanner scan = new Scanner(System.in);
       int num;
       for (int i=0; i<10; i++){//Read values
           num = scan.nextInt();
           new_stack.push(num);
       }
       int new_k = scan.nextInt();
System.out.println(""+smallerK(new_stack, new_k));
   }

   
public static int smallerK(Stack s, int k) {
       //TODO: Find the number of elements in stack s that are smaller (in value) than k
       //Example: if s=[50, 20, 30, 40, 10[ and k=25, the method should return: 2
  
      
   }

}

Homework Answers

Answer #1

The required method is highlighted as bold in the below source code:

import java.util.Stack;
import java.util.Scanner;

class Main
{
public static void main(String[] args)
{
Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */
Scanner scan = new Scanner(System.in);
int num;
for (int i=0; i<5; i++)
{//Read values
num = scan.nextInt();
new_stack.push(num);
}
int new_k = scan.nextInt();
System.out.println(""+smallerK(new_stack, new_k));
}


public static int smallerK(Stack <Integer> s, int k)
{
//variable declaration and initialization
int count=0;
  
//count the values less than k
while(!s.empty())
{
int x = s.pop();
if(x<k)
count++;
}
  
//return statement
return count;
}

}

OUTPUT:

50 20 30 40 10
25
2


Know the answer?
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
Similar Questions
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
//What is the output? import java.util.HashMap; public class AirportCodes {    public static void main (String[]...
//What is the output? import java.util.HashMap; public class AirportCodes {    public static void main (String[] args) {       HashMap<String, String> airportCode = new HashMap<String, String>();       airportCode.put("GRX", "Granada, Spain");       airportCode.put("ALB", "Albany, USA");       airportCode.put("IWJ", "Iwami, Japan");       System.out.print("ALB: ");       System.out.println(airportCode.get("ALB"));       airportCode.put("IWJ", "Ivalo, Finland");       System.out.print("IWJ: ");       System.out.println(airportCode.get("IWJ"));    } }
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the range between −25 and 25 THIS IS MY CODE! please edit this...Thank you import java.util.Scanner; public class Array { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int[] integs = new int[51]; System.out.println("Enter integers 0-50 [enter -1 to quit]: "); int nums = scan.nextInt(); while (nums != -1 && nums>=0 && nums<=50) { integs[nums]++; nums = scan.nextInt(); } for...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r = new Random(seed); } public int rollD6() { return r.nextInt(6) + 1; } public int score() { int roll1 = rollD6(); int roll2 = rollD6(); return scoreWithNumbers(roll1, roll2); }    // You will need to write a static method named scoreWithNumbers. // scoreWithNumbers returns a score based on the values // of its inputs, as such: // // - If both inputs are 1...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT