Question

In: Computer Science

Java please. No "hard coding" please. Need to ask for the file and load the sample...

Java please. No "hard coding" please. Need to ask for the file and load the sample file information into the class.

There is a complete theory about how queues work. In this problem create a limited model to study the order in which a bunch of customers will be attended by their cashiers on a supermarket. The conditions for the experiment are:

• Each cashier spends the same amount of time with each customer (this is just an exercise, not real life).

• There will be a defined number of queues but never less than 2 and never more than 5.

• There is a variable number of customers, never less than 1 and never more than 20 and they are identified by a letter (A, B, C, …)

• The customers are distributed randomly among the different queues. • If two customers are served at the same time, we would consider that they will be ordered following the queue number they are at (first will be customer in queue 1, second customer in queue 2)

Write a program to give the order in which the customers are attended, for example:

• There are 4 queues:

• Cashier number 1 spends 3 minutes on each customer

• Cashier number 2 spends 2 minutes on each customer

• Cashier number 3 spends 4 minutes on each customer

• Cashier number 4 spends 1 minutes on each customer

• Customers are distributed as follows:

• Queue 1 (Cashier 1): Customer A, customer E, customer I

• Queue 2 (Cashier 2): B, F, J, N

• Queue 3 (Cashier 3): C, G, L

• Queue 4 (Cashier 4): D, H, M, O, P, Q

With this input the customers will have been served in the following order and timing:

D (after 1 minute)

B (after 2 minutes on queue 2)

H (after 2 minutes on queue 4)

A (after 3 minutes on queue 1)

M (after 3 minutes on queue 4)

F (after 4 minutes on queue 2)

C (after 4 minutes on queue 3)

O (after 4 minutes on queue 4)

P (after 5 minutes)

E (after 6 minutes on queue 1)

J (after 6 minutes on queue 2)

Q (after 6 minutes on queue 4)

N (after 8 minutes on queue 2)

G (after 8 minutes on queue 3)

I (after 9 minutes)

L (after 12 minutes)

The input from a data file will have the number of queues on the first line, followed by the information for each queue, first the time spent by the cashier on a customer, the number of customers on a queue and then the order of the customers separated by a space.

Output to the screen the list of served customers ordered by the time spent in the queue separated by spaces.

Must use a queue data structure.

Refer to the sample output below.

Sample File: the following information is on the queues.txt file

4

3 3 A E I

2 4 B F J N

4 3 C G L

1 6 D H M O P Q

Sample Run:

Enter file name: queues.txt

The list ordered by time spent: D B H A M F C O P E J Q N G I L

Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;


public class Driver {
   public static void main(String [] args){
       Scanner sc=new Scanner(System.in);
      
       // read file name from user
       System.out.print("Enter file name : ");
       String filename=sc.next();
       sc.close();
      
       // 2d array queue and customer
       // each row is each queue
       // each queue contain corresponding customers
       String [][] lst = null;
       // corresponding time spends
       int [] time = null;
       // n for number of queue
       // total use for total customers
       int n=0,total=0;
      
       BufferedReader br = null;
     
   try {
           br = new BufferedReader(new FileReader(filename));
           int i=0;
           String line="";
           // read file line by line
           while((line=br.readLine())!=null){
               // first line
               if(i==0){
                   // create array
                   n=Integer.parseInt(line);
                   lst=new String[n][];
                   time=new int[n];                  
               }
               else{
                   String [] splts=line.split(" ");
                   time[i-1]=Integer.parseInt(splts[0]);
                   int size=Integer.parseInt(splts[1]);
                   total+=size;
                   lst[i-1]=new String[size];
                   // add customers
                   for(int j=0;j<size;j++){
                       lst[i-1][j]=splts[2+j];
                   }
               }
               i++;
           }
           br.close();
       } catch (IOException e) {
           System.out.print("Error: "+e.getMessage());
       }  
  
   int customers=0;
   int t=1;
   // number of customer , who finished service in a queue
   int [] finshed=new int[n];
   // initialize each to 0
   for(int i=0;i<n;i++){
       finshed[i]=0;
   }
     System.out.print("The list ordered by time spent: ");
   // loop until all customer finished his service
   while(customers<total){
       // each time go through each queue
       for(int i=0;i<n;i++){
           // if all customers are not finished
           if(finshed[i]<lst[i].length){
               // current time
               if(t%time[i]==0){
                   System.out.print(lst[i][finshed[i]]+" ");
                   // increment the number of customer
                   // who finished his service from the corresponding queue
                   finshed[i]++;
                   // increment total number of customers
                   customers++;
               }
           }
       }
       t++;
   }
  
   }
}


Related Solutions

File IO Java question • Ask the user to specify the file name to open for...
File IO Java question • Ask the user to specify the file name to open for reading • Get the number of data M (M<= N) he wants to read from file • Read M numbers from the file and store them in an array • Compute the average and display the numbers and average.
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation) Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects. Use your EnglishGrade and MathGrade classes Based on the input, create an appropriate array for the data to be entered. Write a helper function called recursionMergeSort such that: It is a standalone function...
Use R to load in the file “data.csv”. Assume that this is a random sample from...
Use R to load in the file “data.csv”. Assume that this is a random sample from some population with mean µ and variance σ 2 . (a) Plot a histogram of the data. (b) Compute a 95% confidence interval for the population mean µ using the formula X ± (S/√ n)tn−1,.975. (Hint: tn−1,.975 can be computed with qt(.975,df=n-1)) (c) Compute a p-value for the hypothesis H0 : µ = 5 versus HA : µ > 5, based on the test...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
Create a Matlab program to load in the attached file File_Q5_Use.csv. You will first need to...
Create a Matlab program to load in the attached file File_Q5_Use.csv. You will first need to click on the link to open it, then save it as a .csv file in the directory you are using in your Matlab programs before you can load it in to Matlab. It has 2 columns, the first column is the x values, the second column is the y values. Set Figure (1). Plot the points using red stars and a blue line with...
Create a Matlab program to load in the attached file File_Q5_Use.csv. You will first need to...
Create a Matlab program to load in the attached file File_Q5_Use.csv. You will first need to click on the link to open it, then save it as a .csv file in the directory you are using in your Matlab programs before you can load it in to Matlab. It has 2 columns, the first column is the x values, the second column is the y values. Set Figure (1). Plot the points using red stars and a blue line with...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1....
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1. It is compiled and runs natively 2. It is interpreted but not compiled 3. It is compiled into byte code and executed by the Java Virtual Machine 4. It is magic Please enter your answer 1 Sorry that answer is not correct. The correct answer is 3 Correct output: How is a Java source file executed? 1. It is compiled and runs natively 2....
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
java.. please dont change the format and give me an output sample! user need to input...
java.. please dont change the format and give me an output sample! user need to input k. public class Josephus {    /**    * All persons sit in a circle. When we go around the circle, initially starting    * from the first person, then the second person, then the third...    * we count 1,2,3,.., k-1. The next person, that is the k-th person is out.    * Then we restart the counting from the next person, go...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT