Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use this interface definition to solve this problem. 1 public interface QueueADT { 2 /* Set the queue to its initial state. */ public

image text in transcribed image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Use this interface definition to solve this problem. 1 public interface QueueADT { 2 /* Set the queue to its initial state. */ public void clear(); AWN 3 4 5 /* Add an element to the queue (on rear). */ public boolean enqueue (E it); 6 7 in 8 9 10 11 12 /* Returns the element from front of the queue without removing it. */ 13. 14 public E frontValue(); 15 16 /* Return the number of elements in the queue. */ 17 public int numElements(); 18 19 /* Is the queue empty? */ 20 public boolean isEmpty(); 21 } 25 /* Remove the element from front of the queue and return it. */ public E dequeue (); HH Write a method that removes all non-positive numbers stored in a queue and keeps the rest. Remember that you must remove elements from the queue by using dequeue () and add elements using enqueue (). The general algorithm ought to be: remove an element, and if it is positive add it back to the queue. If it is not, discard it. Repeat this until you have explored all elements in the queue. You must keep track of your progress, do not rely on A numElements () as that number will change as you start eliminating elements from the list, thus making iteration G based on that number unreliable. Your Answer: 1 void removeNonPositive (QueueADT queue) H ~ M & in 2 { 3 4} Check my answer! Reset Next exercise Feedback Your feedback will appear here when you check your answer. Your Answer: 1 public int compareTo (Course o) H ~ M tin 2 { 3 4} 5 Check my answer! Reset Next exercise Feedback Your feedback will appear here when you check your answer. X1068: Compare Two Course Numbers You are writing a system to keep track of courses offered by your department. You have to implement a method to compare two Course objects and order them in numeric sequence, ignoring the name of the department. This would allow us to sort a list of courses by their number, ignoring the department (e.g., ITSC). This problem uses the Course definition as shown below: public class Course { private String dept; // ITSC, SPAN, etc. private int num; // 2214, 1212, etc. 4 5 6 String getDept() { return dept; } 7 int getNum () { return num; } 8} Write a compare() method to compare two Course objects. The comparison should order the courses based on their numbers ignoring the department. 1 public class CompareCourseNumbers 2 implements Comparator { 3 4 5 6} public Course (String d, int n) {...} // you must define int compare (Course o1, Course 02) { .. } This method should return: -1 if 01.getNum () less than 02.getNum () O if 01.getNum() equals 02.getNum() otherwise return 1. You are not allowed to use equals() to make the comparison. Your Answer: Feedback X1064: Complete CompareTo(Course) You are writing a system to keep track of courses offered by your department. You have to implement a compare To method as defined in the Comparable () interface. This problem uses the Course class definition as shown below: 1 public class Course implements Comparable { 2 private String dept; // ITSC, SPAN, etc. 3 private int num; // 2214, 1212, etc. 4 5 public Course(String d, int n) { ... } String getDept () { return dept; } int getNum() { return num; } 67 8 D CO 9 10 11} // you must define public int compareTo (Course o) { - } Write a compare to method for the Course class that compares an object of type Course with the argument o, also of type Course. The method must first compare the department (i.e. ITSC) and then if the department is the same, then compare the number (e.g., 2214). The logic is as follows. -1 if this.getDept () is less than o.getDept() 1 if this.getDept() is greater than o.getDept() if this.getDept() equals o.getDept() o O if this.getNum() equals o.getNum() o -1 if this.getNum () less than o.getNum() o 1 if this.getNum() greater than o.getNum() Attempts remaining: 20 1 public int compare (Course o1, Course 02) HN M&in 2 { 3 4} 5 X1066: Remove At Most n Use this interface definition to solve this problem. 1 public interface QueueADT { 2 public void clear(); 3 public boolean enqueue (E it); 4 public E dequeue (); 5 public E frontValue(); 6 public int numElements(); 7 public boolean isEmpty(); 8} Write a method that removes at most n elements from the queue. If the queue has less than n elements, then just empty the queue by calling clear(). Make sure you use the interface from above. Your Answer: Feedback 1 public void removeAtMost (QueueADT queue, int n) 2|{ 3 4} 5 Your feedback will appear here when you check your answer.

Step by Step Solution

3.40 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

Method removeNonPositiveQueueADT Integer queue java void removeNonPositiveQueueADT Integer queue Cre... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions