JAVA For this assignment you will write a Ranker container T

JAVA:

For this assignment, you will write a Ranker container. This is a lot like a stack in that it supports push() and pop(), except for one important thing: the item popped will be the remaining item with the smallest value. So, if we push 3, 7, 2, 1, 5 and 4 in order, a pop() will return 1, and a second pop() will return 2.

Be sure to implement the Generic mechanism (so, your class should start out something like public class Ranker). In the main routine of Ranker ( public static void main(String[] args) ), you should create a couple of Ranker objects for different types of data – maybe one for ints and one for Strings – and test them out.

Solution

mport java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ranker {
public static List stack = new ArrayList();

public static void main(String[] args) {
while (true) {
System.out.println(\"Enter numbers u for push\");
System.out.println(\"Enter numbers p for pop\");
System.out.println(\"Enter numbers q for quit\");
System.out.println(\"Enter numbers d for display\");
Scanner temp = new Scanner(System.in);
if (temp.hasNext(\"u\")) {
System.out.print(\"Enter number for push\");
Scanner temp1 = new Scanner(System.in);
push(temp1.nextInt());
}
if (temp.hasNext(\"p\")) {
pop();
display();
}
if (temp.hasNext(\"d\")) {
display();
}

}
}

public static void push(int number) {
stack.add(number);
}

public static void pop() {
if (stack.size() != 0) {
int min = Integer.parseInt(stack.get(0).toString());
int index = 0;
for (int i = 0; i < stack.size(); i++) {
if (Integer.parseInt(stack.get(i).toString()) < min) {
min = Integer.parseInt(stack.get(i).toString());
index = i;
}
}
stack.remove(index);
}
}

public static void display() {
if (stack.size() != 0) {
for (int i = 0; i < stack.size(); i++) {
System.out.println(stack.get(i));
}
}
}
}

JAVA: For this assignment, you will write a Ranker container. This is a lot like a stack in that it supports push() and pop(), except for one important thing: t
JAVA: For this assignment, you will write a Ranker container. This is a lot like a stack in that it supports push() and pop(), except for one important thing: t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site