Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write code in java. object oriented programming. the output should be same as the sample output below in the question. Chapter stack & queue. pls

write code in java.

object oriented programming.

the output should be same as the sample output below in the question.

Chapter stack & queue.

pls answer. urgent

image text in transcribed

image text in transcribed

image text in transcribed

***********************************************************************************************

Queue.java

public class Queue { private Node first, last; int N = 0;

private class Node { Item item; Node next; } public boolean isEmpty() { return first == null; } public void enqueue(Item item) { Node oldlast = last; last = new Node(); last.item = item; last.next = null; if (isEmpty()) first = last; else oldlast.next = last; N++; } public Item dequeue() { Item item = first.item; first = first.next; N--; if (isEmpty()) last = null; return item; } public int size() { return N; } } ***********************************************************

Voter.java

import java.util.*; public class Voter { private int arrivalTime; // 0..60, the minute of the hour when a voter arrives private int serviceTime; // depends on types of voter private int voterType; /ormal (1), disable (2), fussy (3),

public Voter(int arrTime, int vType) { arrivalTime = arrTime; voterType = vType; setServiceTime(vType);

}

public void setArrivalTime(int arrTime) { arrivalTime = arrTime; } public int getArrivalTime() { return arrivalTime; }

// add method setServiceTime()

public int getServiceTime() { return serviceTime; }

public int getVoterType() { return voterType; }

}

********************************************************************

PollingStation.java

import java.io.File; import java.io.IOException; import java.util.Scanner;

public class PollingStation { Voter voter;

int numArrivals; // number of arrivals in any minute Queue queue; Queue queue1; int totalWaitingTime,totalWaitingTime1; int numVoterServed = 0,numVoterServed1 =0; int fussy1 =0, disable1 =0, normal1 =0; int fussy =0, disable =0, normal =0;

public static void main(String[] args) throws IOException { // TODO Auto-generated method stub PollingStation po = new PollingStation(); po.simulate(); }

public PollingStation(){ numArrivals = 0; totalWaitingTime = 0; numVoterServed = 0; numVoterServed1=0; queue = new Queue(); queue1 = new Queue(); }

private void displayStatistics() { //complete the method to display }

public void simulate(){ int serviceTime = 0, serviceTime1 =0; int minServTime = 5,minServTime1 =5; int startService = 0,startService1 =0;

try { Scanner sc = new Scanner(new File("input.dat")); int num = sc.nextInt();

while (sc.hasNextLine()) { int arrTime = sc.nextInt(); int voterType = sc.nextInt(); int channelNo = sc.nextInt();

// Create Voter and add to queue for channel 5 & 6 For each new voter If channelNo equals to 5 Place the new voter in the queue1 Else if channelNo equals to 6 Place the new voter in the queue2 }

While there are voters waiting in each queue { If (service time more than 1 hour considering condition*) Finish Else Remove a voter from each queue; Determine whether the voter is normal, disable or fussy, and keep the record Determine and print the time the voter can actually start voting (having completed all 3 prerequisite processes)

Increment the number of voters that have been given services; } sc.close(); } catch (IOException e){ e.printStackTrace(); } displayStatistics();

}

}

*****************************************************

input.dat

15 5 1 5 10 1 6 13 2 5 15 3 6 20 2 5 24 1 6 26 2 6 30 1 5 34 2 5 40 3 6 45 3 5 50 1 6 55 1 6 56 2 5 58 1 6

Problem Description In the recent General Election, many were unsatisfied as they had to wait for a very long time. Voters above the age of 30 are distributed to channels 1, 2, 3 and 4, while voters between the age of 21-30 are distributed to channels 5 and 6. New voters were excited to vote and were frustrated when some of them were unable to vote even after queueing for 3 to 4 hours since they are too many voters assigned to channels 5 and 6. To improve the service in the future, a simulation is carried out to focus on voters aged 21-30 by assigning voters to channels according to their age, namely channel 5 (age 26-30) and 6 (age 21-25). There are 3 processes involved, each managed by an Election Commission staff: 1. Checking identity card and polling number. 2. Dipping finger into dye. 3. Passing stamped ballot paper. Each process requires a specific amount of time, based on the voter's category. There are people with disability, fussy people who raise a lot of questions, as well as normal people. Usually, the Election Commission staffs take extra time in servicing disable and fussy people. In this simulation 2 channels (5 and 6) are opened and waiting for the young voters. The following table shows the time needed to cater for each voters. Each voter will have to complete all 3 processes before voting. Types of Service (Process) Service time (Minutes) 1 (Normal) 2 (Disable) 3 (Fussy) 1 (Checking identity card and polling number) 2 (Dipping finger into dye) 3 (Passing stamped ballot paper) 2 3 4 2. 2 3 1 2 3 Write a program to read from a file input.dat. The content of the file starts with N representing the number of voters. The next lines each contain 4 integers, indicating the voter's arrival time, service types (1, 2, or 3), voter's type (1, 2, 3), and the channel number (5 or 6). Determine how many voters can be given services in less than 1 hour the channels start to operate. (condition : considering the arrival of the voters and the service time). Also display the time they get their service. Given Queue.java, Voter.java, Polling Station.java, and input.java. 1. In Voter.java, add method tsetServiceTime (int votertype):void which determine the serviceTime required to complete all three processes, which is based on the voter's type. For example, for normal, disable and fussy categories, all three processes take 5, 7 and 10 minutes respectively. 2. Complete Polling Station.java using the following algorithm: The Algorithm: For each new voter If channelNo equals to 5 Place the new voter in the queuel Else if channelNo equals to 6 Place the new voter in the queue2 While there are voters waiting in each queue { IE (service time more than 1 hour considering condition) Finish Else Remove a voter from each queue; Determine whether the voter is normal, disable or fussy, and keep the record Determine and print the time the voter can actually start voting (having completed all 3 prerequisite processes) Increment the number of voters that have been given services; } Print the summary statistics, Input First line of input is an integer N (1<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Step: 3

blur-text-image

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

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago