Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SIMPLE JAVA CODING HELP NEEDED so I have the below code working ok... basically

ID: 3802355 • Letter: S

Question

SIMPLE JAVA CODING HELP NEEDED

so I have the below code working ok... basically i am reading in from a .txt file, then going through each word in the file and doing different methods depending on the word at hand. I am dealing with max binary heap and so when the word read in is "add" i add the following info from the .txt file to the queue as an item in that queue. The problem is that i need to have a counter for ID number of items in the priority queue/heap, and as you can see i tried to do this but its not working. I cant select a specific id number from the queue to use the remove function on that specific element in the queue.

If anyone is able to correctly add the counter in the code and make it so that I can delete it and display status ( the task info ) i would appreciate it!

import java.io.*;
import java.util.Scanner;

/**
* The class Task keeps things organized and creates new instances of a Task to
* be completed by the scheduler
**/
class Task {
   // initialize all variables
   int id = 0;
   String login;
   String time;
   int priority;
   int size;
   int handle;

   // this method is called when the class object is created
   public Task(String login, String time, int priority, int size, int handle) {
       id = id + 1;
       this.login = login;
       this.time = time;
       this.priority = priority;
       this.size = size;
       this.handle = handle;
   }

   /** toString() **/
   public String toString() {
       return "add" + " " + login + " " + time + " " + priority + " " + size + " " + "0";
   }

   public int getId() {
       return this.id;
   }
}

/** Class PriorityQueue **/
class PriorityQueue {
   private Task[] heap;
   private int heapSize, capacity;
   int currentsize = 0;
   int ID = 1;

   /** Constructor **/
   public PriorityQueue(int capacity) {
       this.capacity = capacity + 1;
       heap = new Task[this.capacity];
       heapSize = 0;
   }

   /** function to clear **/
   public void clear() {
       heap = new Task[capacity];
       heapSize = 0;
   }

   /** function to check if empty **/
   public boolean isEmpty() {
       return heapSize == 0;
   }

   /** function to check if full **/
   public boolean isFull() {
       return heapSize == capacity - 1;
   }

   /** function to get Size **/
   public int size() {
       return heapSize;
   }

   /**
   * Method adds a new request to the queue; returns a sequential ID# if
   * successful, or FULL if the total file size of all pending requests
   * exceeds the maximum spool size of 50MB. The input argument "handle" is
   * set to null. The ID cycles in the range of 1 - 1024.
   *
   * @param login
   * @param time
   * @param priority
   * @param size
   * @param handle
   * @return
   */
   public void add(String login, String time, int priority, int size, int handle) {
       Task newJob = new Task(login, time, priority, size, 0);
       currentsize += size;
       heap[++heapSize] = newJob;
       int pos = heapSize;
       // if size is not exceeded

       if (currentsize <= 50000000) {
           while (pos != 1 && newJob.priority > heap[pos / 2].priority) {
               heap[pos] = heap[pos / 2];
               pos /= 2;
           }
           heap[pos] = newJob;
           System.out.print(ID + " ");
           ID += 1;
       }
       // if size is exceeded print error code -1
       else {
           System.out.print("-1 ");
       }
   }

   /** function to remove Task **/
   public Task remove() {
       int parent, child;
       Task item, temp;
       if (isEmpty()) {
           System.out.println("EMPTY");
           return null;
       }
       item = heap[1];
       temp = heap[heapSize--];
       parent = 1;
       child = 2;
       while (child <= heapSize) {
           if (child < heapSize && heap[child].priority < heap[child + 1].priority)
               child++;
           if (temp.priority >= heap[child].priority)
               break;

           heap[parent] = heap[child];
           parent = child;
           child *= 2;
       }
       heap[parent] = temp;
       //System.out.print();
       return item;
   }

   public int findnext() {
       Task temp = remove();
       // System.out.println(temp.toString());
       add(temp.login, temp.time, temp.priority, temp.size, temp.handle);
       return temp.id;
   }

   public int cancelJob(int jobId) {
       Task temp = remove();
       // System.out.println(temp.toString());
       if (temp.id == jobId) {
           return temp.id;
       }
       add(temp.login, temp.time, temp.priority, temp.size, temp.handle);
       return 0;
   }
}

public class PrintScheduler {
   public static void main(String args[]) throws IOException {

       Scanner sc = new Scanner(new File("input.txt"));
       String commands[] = new String[1000];
       int c = 0;

       while (sc.hasNext()) {
           String input = sc.next();
           if (input.isEmpty())
               continue;
           commands[c++] = input;
       }

       PriorityQueue pq = new PriorityQueue(50000000);

       // loop through the commands array
       for (int i = 0; i < commands.length; i++) {

           if (commands[i] != null) {
               // if word is add print program is working to test
               if (commands[i].equals("add")) {
                   String name = commands[i + 1];
                   String time = commands[i + 2];
                   String priorityz = commands[i + 3];
                   int priority = Integer.parseInt(priorityz);
                   String sizez = commands[i + 4];
                   int size = Integer.parseInt(sizez);
                   Task task = new Task(name, time, priority, size, 0);
                   System.out.println(task);
                   pq.add(name, time, priority, size, 0);
               }

               else if (commands[i].equals("print_next")) {
                   pq.remove();
               } else if (commands[i].equals("find_next")) {
                   pq.findnext();
               } else if (commands[i].equals("cancel")) {
                   if (Character.isDigit(commands[i].charAt(0))) {
                       int jobtocancel = Integer.parseInt(commands[i]);
                       pq.cancelJob(jobtocancel);
                   } else {
                       // cancel the name you want canceled
                   }
               } else if (commands[i].equals("status")) {
                   for(int j=0; j<pq.size(); j++){
                      
                   }
               }

           }

       }

   }
}

Explanation / Answer

import java.io.*;
import java.util.Scanner;

/**
* The class Task keeps things organized and creates new instances of a Task to
* be completed by the scheduler
**/
class Task {
   // initialize all variables
   int id = 0;
   String login;
   String time;
   int priority;
   int size;
   int handle;

   // this method is called when the class object is created
   public Task(String login, String time, int priority, int size, int handle) {
       id = id + 1;
       this.login = login;
       this.time = time;
       this.priority = priority;
       this.size = size;
       this.handle = handle;
   }

   /** toString() **/
   public String toString() {
       return "add" + " " + login + " " + time + " " + priority + " " + size + " " + "0";
   }

   public int getId() {
       return this.id;
   }
}

/** Class PriorityQueue **/
class PriorityQueue {
   private Task[] heap;
   private int heapSize, capacity;
   int currentsize = 0;
   int ID = 1;

   /** Constructor **/
   public PriorityQueue(int capacity) {
       this.capacity = capacity + 1;
       heap = new Task[this.capacity];
       heapSize = 0;
   }

   /** function to clear **/
   public void clear() {
       heap = new Task[capacity];
       heapSize = 0;
   }

   /** function to check if empty **/
   public boolean isEmpty() {
       return heapSize == 0;
   }

   /** function to check if full **/
   public boolean isFull() {
       return heapSize == capacity - 1;
   }

   /** function to get Size **/
   public int size() {
       return heapSize;
   }

   /**
   * Method adds a new request to the queue; returns a sequential ID# if
   * successful, or FULL if the total file size of all pending requests
   * exceeds the maximum spool size of 50MB. The input argument "handle" is
   * set to null. The ID cycles in the range of 1 - 1024.
   *
   * @param login
   * @param time
   * @param priority
   * @param size
   * @param handle
   * @return
   */
   public void add(String login, String time, int priority, int size, int handle) {
       Task newJob = new Task(login, time, priority, size, 0);
       currentsize += size;
       heap[++heapSize] = newJob;
       int pos = heapSize;
       // if size is not exceeded

       if (currentsize <= 50000000) {
           while (pos != 1 && newJob.priority > heap[pos / 2].priority) {
               heap[pos] = heap[pos / 2];
               pos /= 2;
           }
           heap[pos] = newJob;
           System.out.print(ID + " ");
           ID += 1;
       }
       // if size is exceeded print error code -1
       else {
           System.out.print("-1 ");
       }
   }

   /** function to remove Task **/
   public Task remove() {
       int parent, child;
       Task item, temp;
       if (isEmpty()) {
           System.out.println("EMPTY");
           return null;
       }
       item = heap[1];
       temp = heap[heapSize--];
       parent = 1;
       child = 2;
       while (child <= heapSize) {
           if (child < heapSize && heap[child].priority < heap[child + 1].priority)
               child++;
           if (temp.priority >= heap[child].priority)
               break;

           heap[parent] = heap[child];
           parent = child;
           child *= 2;
       }
       heap[parent] = temp;
       //System.out.print();
       return item;
   }

   public int findnext() {
       Task temp = remove();
       // System.out.println(temp.toString());
       add(temp.login, temp.time, temp.priority, temp.size, temp.handle);
       return temp.id;
   }

   public int cancelJob(int jobId) {
       Task temp = remove();
       // System.out.println(temp.toString());
       if (temp.id == jobId) {
           return temp.id;
       }
       add(temp.login, temp.time, temp.priority, temp.size, temp.handle);
       return 0;
   }
}

public class PrintScheduler {
   public static void main(String args[]) throws IOException {

       Scanner sc = new Scanner(new File("input.txt"));
       String commands[] = new String[1000];
       int c = 0;

       while (sc.hasNext()) {
           String input = sc.next();
           if (input.isEmpty())
               continue;
           commands[c++] = input;
       }

       PriorityQueue pq = new PriorityQueue(50000000);

       // loop through the commands array
       for (int i = 0; i < commands.length; i++) {

           if (commands[i] != null) {
               // if word is add print program is working to test
               if (commands[i].equals("add")) {
                   String name = commands[i + 1];
                   String time = commands[i + 2];
                   String priorityz = commands[i + 3];
                   int priority = Integer.parseInt(priorityz);
                   String sizez = commands[i + 4];
                   int size = Integer.parseInt(sizez);
                   Task task = new Task(name, time, priority, size, 0);
                   System.out.println(task);
                   pq.add(name, time, priority, size, 0);
               }

               else if (commands[i].equals("print_next")) {
                   pq.remove();
               } else if (commands[i].equals("find_next")) {
                   pq.findnext();
               } else if (commands[i].equals("cancel")) {
                   if (Character.isDigit(commands[i].charAt(0))) {
                       int jobtocancel = Integer.parseInt(commands[i]);
                       pq.cancelJob(jobtocancel);
                   } else {
                       // cancel the name you want canceled
                   }
               } else if (commands[i].equals("status")) {
                   for(int j=0; j<pq.size(); j++){
                      
                   }
               }

           }

       }

   }
}