Question

In: Computer Science

导入java.util。*; 公共类崩溃{    公共静态void main(String [] args){ /*************************************************************************        * Q6:        * Do...

导入java.util。*;

公共类崩溃{

   公共静态void main(String [] args){

/*************************************************************************
       * Q6:
       * Do you know how mobile phone works? They connect to base station to
       * send and receive messages and voice packages. Each base station will
       * handle a large number of smart phones' request. Assume one base station
       * can only process one request from one mobile phone. Each request need
       * a certain time to be processed. When the base station is busy, incoming
       * request should wait in line. Assume that a base station has a maximum
       * capacity N, which indicates that it can only keep N requests waiting
       * in the line, otherwise, it will crash. Given an array of incoming
       * request time and another array indicates corresponding process times.
       * Please implement the crash method in Q6 for the simulation of base station.
       * The method will return the crash time for the base station. If the system
       * will not crash for the given requests, return -1.
       * For example, given
       * arrival time = [1,4,4,5,7,10,12] and task required time = [3,9,12,9,10,4,3]
       * if the maximum capacity N=1, the result of crash time will be 5
       * if the maximum capacity N=2, the result of crash time will be 7
       * if the maximum capacity N=3, the result of crash time will be 10
       * if the maximum capacity N=4, the result of crash time will be 12
       * if the maximum capacity N=5, the result of crash time will be -1
       *************************************************************************/
       int[] arrival = {1,4,4,5,7,10,12};
       int[] task= {3,9,12,9,10,4,3};
       int capacity = 3;
       System.out.println("Q6 = [25 marks] =================================");
       System.out.println(Q6.crash(arrival, task, capacity));
   }
  
}

class Q6 {
  
   public static int crash(int[] arrival, int[] task, int capacity){
      
       //TODO
       return 0;
   }
  
}

Solutions

Expert Solution

Q6.java

import java.util.LinkedList;
import java.util.Queue;

class Q6 {
  
public static int crash(int[] arrival, int[] task, int capacity)
{
   //Why we increase capacity? It is because 1 process is being processed and others are waiting.
   //So adding 1 for that process which is being processed
   capacity++;
  
   //Use queue to hold waiting processes
   Queue<Integer> q = new LinkedList<>();
     
   //Length of process coming in
   int len = arrival.length;
         
   int i=0;
   int head;
   int current_time = 0;
     
   //Iterate over the incoming list
   for(i=0; i<len; i++)
   {
       int end_time = arrival[i] + task[i];
       current_time = arrival[i];
         
       //We will iterate our queue and remove all process which have already finished processing
       while(!q.isEmpty())
       {
           head = q.peek();
           if(head <= current_time)
           {
               q.remove(); // process is complete. Remove it from queue.
               capacity++;
           }
           else
           {
               break;
           }
       }
         
       //If there is still capacity to hold this current process, add it to queue, else return the arrival time of this process.
       if(capacity > 0)
       {
           q.add(end_time);
           capacity --;
       }
       else
       {
           return arrival[i];
       }
   }
     
//If no crash occurred, return -1
return -1;
}
  
}

Main.java


public class Main {
  
   public static void main(String args[])
   {
   int[] arrival = {1,4,4,5,7,10,12};
   int[] task= {3,9,12,9,10,4,3};
   int capacity = 1;
   int result = Q6.crash(arrival, task, capacity);
   System.out.println("Capacity = "+capacity+" crash = "+result);
  
   capacity = 2;
   result = Q6.crash(arrival, task, capacity);
   System.out.println("Capacity = "+capacity+" crash = "+result);
     
   capacity = 3;
   result = Q6.crash(arrival, task, capacity);
   System.out.println("Capacity = "+capacity+" crash = "+result);
     
   capacity = 4;
   result = Q6.crash(arrival, task, capacity);
   System.out.println("Capacity = "+capacity+" crash = "+result);
     
   capacity = 5;
   result = Q6.crash(arrival, task, capacity);
   System.out.println("Capacity = "+capacity+" crash = "+result);
  
   }
   }


Sample I/O and output


Related Solutions

// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args)...
Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args) {             // TODO Auto-generated method stub             System.out.println("*** 1 ***");             ArrayList<Integer>list1=new ArrayList<Integer>();             for(int i=0;i<30;i++)             {                   list1.add(new Integer(i));             }             System.out.print("[");             for(int i=0;i<list1.size();i++)             {                   System.out.print(list1.get(i)+" ");             }             System.out.println("]");                           System.out.println("*** 2 ***");             ArrayList<Integer>list2=new ArrayList<Integer>();             for(int i=30;i<60;i++)             {                   list2.add(i); //Auto Boxing an Integer not need to use new Integer             }             System.out.println(list2); //toString for an ArrayList --created by the Java Programmers                           System.out.println("*** 3 ***");             ArrayList<Integer>list3=new ArrayList<Integer>();             list3.add(list1.remove(22)); //when...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
could you do all the challenges? public class Main { public static void main(String[] args) {...
could you do all the challenges? public class Main { public static void main(String[] args) { // declare an array so you can easily use them under // one name //ex1 array of test score 0-100 int[] testScores = new int[100]; //ex2 array of 200 gpa double[] gpa = new double[200]; //ex3 50 element array of age int[] age; //1 - declares age array age = new int[50]; //2 - instantiates age array age[0] = 10; //3 - put initial...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[]...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] strings = new String[100];        for (int i = 0; i < strings.length; i++) {            System.out.println("Enter Strings: stop ");            strings[i]=input.nextLine();            if(strings[i].equalsIgnoreCase("stop"))                break;        }               MaxLength(strings);//here is the error    }    public static int MaxLength(String[] array) {        int max = array[0].length();       ...
public static void main(String[] args) {            //Part1: using three stacks           ...
public static void main(String[] args) {            //Part1: using three stacks            iQueue<Integer> main = new ourLinkedList<Integer>();                       for (int i = 0; i<10; i++) {                int num = -15 + (int) (Math.random() * ((15 - (-15)) + 1));            main.add(num);            }                       System.out.println("main: " +main);            iQueue<Integer> Q1 = new ourLinkedList<Integer>();           ...
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);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
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) {       ...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT