Write a method that, given a non-empty queue of integers, returns the largest value. The queue...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a method that, given a non-empty queue of integers, returns the largest value. The queue should have the same contents as before the call. Of course, you can remove elements to inspect them, but you need to add them back into the queue. import java.util.Queue; import java.util.Arrays; import java.util.LinkedList; Queues.java Queue Tester.java public class QueueTester { public static void main(String[] args) { Queue<Integer> q = new Linked List<>(Arrays.asList(1, 2, 3, 4, 5)); int largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 5"); System.out.println(q); System.out.println("Expected: [1, 2, 3, 4, 5]"); q = new Linked List<>(Arrays.asList(5, 4, 3, 1, 2)); largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 5"); System.out.println(q); System.out.println("Expected: [5, 4, 3, 1, 2]"); q = new Linked List<>(Arrays.asList(4, 5, 6, 5)); largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 6"); System.out.println(q); System.out.println("Expected: [4, 5, 6, 5]"); q = new Linked List<>(Arrays.asList(3)); largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 3"); System.out.println(q); System.out.println("Expected: [3]"); } } Write a method that, given a non-empty queue of integers, returns the largest value. The queue should have the same contents as before the call. Of course, you can remove elements to inspect them, but you need to add them back into the queue. Queues.java Queue Tester.java 1 import java.util.Queue; 2 public class Queues 3 { 1234567800-234 14 } 9 { 10 11 12 13 } /** * Returns the largest number from the queue. The queue has the same contents after the call. public static Integer getLargest (Queue<Integer> q) * Your code goes here */ Check Write a method that, given a non-empty queue of integers, returns the largest value. The queue should have the same contents as before the call. Of course, you can remove elements to inspect them, but you need to add them back into the queue. import java.util.Queue; import java.util.Arrays; import java.util.LinkedList; Queues.java Queue Tester.java public class QueueTester { public static void main(String[] args) { Queue<Integer> q = new Linked List<>(Arrays.asList(1, 2, 3, 4, 5)); int largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 5"); System.out.println(q); System.out.println("Expected: [1, 2, 3, 4, 5]"); q = new Linked List<>(Arrays.asList(5, 4, 3, 1, 2)); largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 5"); System.out.println(q); System.out.println("Expected: [5, 4, 3, 1, 2]"); q = new Linked List<>(Arrays.asList(4, 5, 6, 5)); largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 6"); System.out.println(q); System.out.println("Expected: [4, 5, 6, 5]"); q = new Linked List<>(Arrays.asList(3)); largest = Queues.getLargest (q); System.out.println(largest); System.out.println("Expected: 3"); System.out.println(q); System.out.println("Expected: [3]"); } } Write a method that, given a non-empty queue of integers, returns the largest value. The queue should have the same contents as before the call. Of course, you can remove elements to inspect them, but you need to add them back into the queue. Queues.java Queue Tester.java 1 import java.util.Queue; 2 public class Queues 3 { 1234567800-234 14 } 9 { 10 11 12 13 } /** * Returns the largest number from the queue. The queue has the same contents after the call. public static Integer getLargest (Queue<Integer> q) * Your code goes here */ Check
Expert Answer:
Answer rating: 100% (QA)
Heres a method named getLargest that meets the given requirements import javautilQueue public class ... View the full answer
Related Book For
Starting Out With Java From Control Structures Through Data Structures
ISBN: 9780133957051
6th Edition
Authors: Tony Gaddis
Posted Date:
Students also viewed these programming questions
-
answer the question clearly You are building a flight-control system for which a convincing safety case must be made. Would you assign the tasks of safety requirements engineering, test case...
-
If two krypton atoms are held together by a stabilization energy of 1.005 kJ/mol at what temperature would you expect these atoms to transition from bound to unbound? Show your thought process for...
-
Fourteen different second-year medical students at Bellevue Hospital measured the blood pressure of the same person. The systolic readings (mm Hg) are listed below. 138 130 135 140 120 125 120 130...
-
Consider the balance sheet of the Fed in Table 24-1.Construct a corresponding balance sheet for banks (like the one in Table 23-3 in the previous chapter) assuming that reserve requirements are 10...
-
Suppose you have a \(1 \mathrm{~g}\) sample of \({ }^{226} \mathrm{Ra}\), half-life 1600 years. How long will it be until only \(0.1 \mathrm{~g}\) of radium is left? \(\begin{array}{llll}\text { A. }...
-
Axon Corporation, which has three divisions, is preparing its sales budget. Each division expects a different growth rate because economic conditions vary in different regions of the country. The...
-
Perform a risk assessment or information technology audit to determine your organization's unique information security needs.
-
Q3- Assuming monthly payments, which one of the following plans are better for buying a $15,000 van? a) 6% on the full amount for 4 years. b) $2500 discount and 12% interest on the remaining amount...
-
Assume we are in the year 2010. In 2008, John started working for Tictac Inc.This year the company decided to offer all its employees a defined benefit plan.The DB plan offers a pension of 1.4% of...
-
Are there situations in which employee empowerment would hurt rather than improve quality and why would this occur? Data: https://asq.org/quality-resources/employee-empowerment
-
Assume Highline Company has just paid an annual dividend of $1.07. Analysts are predicting an 10.8% per year growth rate in earnings over the next five years. After then, Highline's earnings are...
-
6. Rewrite the following BNF grammar using the form of EBNF. Make the resulting grammar as short as possible (fewest rules). Note: It is possible to replace all eight BNF rules with a single EBNF...
-
Consider the following Product Structure Tree/Diagram for a stool: Stool Seat Ass'y (1) Nuts (3) Top tipped leg (1) Bottom Seat Nuts Top (1) (2) tipped leg (1) leg (1) Leg tips (2) How many Nuts...
-
The U.S. Defense Department is enlisting an open-source approach to software development, which is an about-face for such a historically top-down organization. The Department of Defense (DoD) says...
-
You work as an operations consultant for a textile company. Your client has a well-established distribution system in the US market. The company has hundreds of stores and four distribution centers....
-
True or False: You are not required to catch exceptions that inherit from the RuntimeException class.
-
Write an if statement that displays the message "The number is valid" if the variable speed is within the range 0 through 200.
-
Assume x is a double variable, and rand references a Random object. What does the following statement do? x = rand.nextDouble();
-
Based on your reading of this chapter, the following job description, and the two rsums below, analyze the two applicants for the position. What are their strengths and weaknesses as highlighted by...
-
Write a rsum that you could use in your job search. As your instructor directs, a. Write a rsum for the field in which you hope to find a job. b. Write two different rsums for two different job paths...
-
Use a web platform such as Wordpress to create a portfolio that highlights your professional and academic accomplishments. Include course projects, workplace samples, and other documents that support...
Study smarter with the SolutionInn App