
Concept explainers
In Java, Consider a crime wave during two criminals simultaneously commit crimes while two detectives simultaneously
solve the crimes. A crime has an integer seriousness level between 0 (creating a public nuisance) and 4 (murder).
Criminals commit crimes with random seriousness and wait a random amount of time between 0 and 100
milliseconds between crimes. Each criminal commits 50 crimes and then retires to live from the loot. Detectives
solve the most serious crimes first, resting for 60 milliseconds after solving each crime. The application continues
running until both the criminals have retired (ie, the two threads containing the criminal Runnables have
terminated) and all the crimes have been solved. Here are the first few lines of output from a sample run of the
program:
c1 commits a crime of seriousness 1
d1 solves a crime of seriousness 1
c1 commits a crime of seriousness 4
d2 solves a crime of seriousness 4
c2 commits a crime of seriousness 0
c2 commits a crime of seriousness 0
Here are the last few lines of output from a typical run:
d1 solves a crime of seriousness 0
d2 solves a crime of seriousness 0
committed: 100
solved: 100
Complete the missing code:
package crime;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
public class CrimeWave {
Random r = new Random();
// create the priority queue for the crimes here. Call it crimes.
Integer committed = 0;
Integer solved = 0;
public void meanStreets() {
Detective d1 = new Detective("d1");
Detective d2 = new Detective("d2");
Criminal c1 = new Criminal("c1");
Criminal c2 = new Criminal("c2");
// create and start the threads (one per criminal and one per detective) here
// write a while loop here that continues as long as at least one of the criminals is
// still in business (ie, which the thread for the criminal is not terminated). If the
// loop condition is true, use Thread.sleep to wait 200 milliseconds before checking again
System.out.println("committed: " + committed);
System.out.println("solved: " + solved);
}
public synchronized void commitCrime(Crime c) {
// write one line of code to add the crime to crimes
committed++;
}
public synchronized void solveCrimes(String name) {
try {
while (crimes.isEmpty())
Thread.sleep(25);
// write code here to remove the next crime from the priority queu crimes and set an int called s to the crime's
// seriousness level
solved++;
System.out.println(name + " solves a crime of seriousness " + s);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CrimeWave c = new CrimeWave();
c.meanStreets();
System.exit(0);
}
private class Crime implements Comparable<Crime> {
// finish writing Crime here. Use Random.nextInt(10) to get the random level of seriousness.
}
private class Criminal implements Runnable {
String name;
private Criminal(String nameIn) {
name = nameIn;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(r.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
int s = (r.nextInt(5));
Crime c = new Crime(s);
System.out.println(name + " commits a crime of seriousness "
+ s);
commitCrime(c);
}
}
}
private class Detective implements Runnable {
// finish writing Detective here
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

- Create an efficient java code to solve the problem by using dynamic programmingarrow_forwardA private hospital would like to implement a Java application to manage patient-specialist appointment. The hospital has five specialists, in different areas of medicine (e.g. cardiac, renal, paediatric). These specialists have regular slots for appointments every week. For example, the cardiac specialist may have six slots available on Monday, Wednesday and Friday; for simplicity, we assume each slot starts on the hour (e.g. 9am, 10am etc).The system should be used by patients to schedule an appointment with a specialist. Each patient will first enter their unique hospital id number, and then the specialist they want to meet and desired date. The system will list the available slots for that specialist. The patient may then select one of the availableslots, select a different date, or a different specialist. The patient should also be shown a list of all booked appointments, which they have yet to attend. When the patient arrives for their appointment, they need to enter their…arrow_forwardA program in javaarrow_forward
- Write a Java procedural program that helps organise travellers with a specific travel company arriving at an airport, telling them which queue to join based on their hotel. The program asks each traveller what their hotel is (the choices are the Seaview for which they need to join Queue 1, the Majestic for which they need to join Queue 4 and the Grand for which they need to join Queue 7) and tells them the right queue. There are 1330 travellers with the tour company and each will use the program as they arrive. After all have indicated their hotel, the program then prints the percentage in each hotel. * See image for an example run of the program. Percentages should be printed truncated to two decimal places. Do this using a method you write yourself that multiplies by 100, converts to an integer then divides by 100. * must make use of a counter-controlled for loop, be in procedural programming style (not OOP)arrow_forwardWrite a FULL Java procedural program for one human player to play a “Higher or Lower” card game. In this game, each card has a value from 1..10 inclusive. There are 4 of each value in the deck, i.e., 40 cards in total. Cards are not replaced in the deck once drawn, i.e., no more than 4 of each value will be drawn. The program starts by asking the player for a target score. The game proceeds in a series of rounds with the program repeatedly drawing and showing a card from the deck to the player one at a time. Each time, it asks the player to enter "h" (higher) or "l" (lower) to guess whether the next card drawn will be higher or lower in value. The player gains a point if they guess correctly. The game continues until the player guesses incorrectly or the target score is reached. When the game ends, it prints either a "You win!" or a "Nice try, you scored …” message as illustrated below. The image provided shows two examples of the required program behaviour: (bold is keyboard input…arrow_forwardThe first image is the main question, the second image is the instructions for the program. The program has to be written in Java.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





