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 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"));    } }
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • GPS Watches Please submit your finalized marketing plan. Conduct a final proof read.
    asked 12 minutes ago
  • A business PhD student who lived on a naval base, looked at prices of items at...
    asked 22 minutes ago
  • 1 Consider diversity, interpersonal communication, & technology in the workplace & explain the roles they may...
    asked 30 minutes ago
  • 1 Explain how "triggers to imbalance" in work-life balance influence the imbalance & how they can...
    asked 45 minutes ago
  • Spaulding is the leading maker for basketballs in the US. Spaulding prides itself on the quality...
    asked 56 minutes ago
  • Select True or False for the following statements about Heisenberg's Uncertainty Principle.  True False  It is possible to...
    asked 1 hour ago
  • Explain why, to maximize entropy, ice must remain at 0 degrees Celsius until all of it...
    asked 1 hour ago
  • THE CODE MUST BE PYTHON superHeroes = {   "MoleculeMan": {       "age": 29,       "secretIdentity": "Dan Jukes",       "superpowers":...
    asked 1 hour ago
  • Which of the four complexes of the mitochondrial electron transfer chain does not directly contribute to...
    asked 1 hour ago
  • Police response time to an emergency call is the difference between the time the call is...
    asked 2 hours ago
  • A space vehicle is traveling at 5030 km/h relative to Earth when the exhausted rocket motor...
    asked 2 hours ago
  • You have 3 parallel production lines which supply the assembly line. Acceptable quality percentage(AQP) for lines...
    asked 2 hours ago