Get 2 days of premium access
Study Guides (380,000)
US (220,000)
UMD (10,000)
CMSC (1,000)
Anwar (4)
Study Guide

CMSC 433- Final Exam Guide - Comprehensive Notes for the exam ( 86 pages long!)


Department
Computer Science
Course Code
CMSC 433
Professor
Anwar
Study Guide
Final

This preview shows pages 1-3. to view the full 86 pages of the document.
UMD
CMSC 433
Final EXAM
STUDY GUIDE

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

package cmsc433.p2;
import java.util.LinkedList;
/**
* A Machine is used to make a particular Food. Each Machine makes
* just one kind of Food. Each machine has a capacity: it can make
* that many food items in parallel; if the machine is asked to
* produce a food item beyond its capacity, the requester blocks.
* Each food item takes at least item.cookTimeS seconds to produce.
*/
public class Machine {
// Types of machines used in Ratsie's. Recall that enum types are
// effectively "static" and "final", so each instance of Machine
// will use the same MachineType.
public enum MachineType { fountain, fryer, grillPress, oven };
// Converts Machine instances into strings based on MachineType.
public String toString() {
switch (machineType) {
case fountain: return "Fountain";
case fryer: return "Fryer";
case grillPress: return "Grill Presss";
case oven: return "Oven";
default: return "INVALID MACHINE";
}
}
public final MachineType machineType;
public final Food machineFoodType;
//YOUR CODE GOES HERE...
public final int capacity;
public int currCap;
private static Object Lock = new Object();
/**
* The constructor takes at least the type of the machine,
* the Food item it makes, and its capacity. You may extend
* it with other arguments, if you wish. Notice that the
* constructor currently does nothing with the capacity; you
* must add code to make use of this field (and do whatever
* initialization etc. you need).
*/
public Machine(MachineType machineType, Food food, int capacityIn) {
this.machineType = machineType;
this.machineFoodType = food;
//YOUR CODE GOES HERE...
this.capacity = capacityIn;
currCap = 0;
}
/**
* This method is called by a Cook in order to make the Machine's
* food item. You can extend this method however you like, e.g.,
* you can have it take extra parameters or return something other
* than Object. It should block if the machine is currently at full
* capacity. If not, the method should return, so the Cook making
* the call can proceed. You will need to implement some means to
* notify the calling Cook when the food item is finished.
*/
public boolean makeFood(Object obj, LinkedList<Food> cookedFood) throws
InterruptedException {
//YOUR CODE GOES HERE...
Thread cookFood = new Thread(
new CookAnItem(machineFoodType, capacity)
);
cookFood.start();
cookFood.join();
cookedFood.add(machineFoodType);
synchronized(obj){
obj.notifyAll();
}
return true;
}
//THIS MIGHT BE A USEFUL METHOD TO HAVE AND USE BUT IS JUST ONE IDEA
private class CookAnItem implements Runnable {
private Food food;
private int quantity;
private int capacity;
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

public CookAnItem(Food FoodType, int capacity){
this.food = FoodType;
this.capacity = capacity;
}
public void run() {
try {
//YOUR CODE GOES HERE...
synchronized(Lock){
while(currCap > capacity){
try{
Lock.wait();
}catch(Exception e){}
}
}
Simulation.logEvent(SimulationEvent.machineCookingFood(Machine.this,machineFoodT
ype));
currCap += quantity;
if(FoodType.wings == food){
System.out.println("Cooking 350 for wings");
Thread.sleep(350);
}else if(FoodType.pizza == food){
System.out.println("Cooking 600 for pizza");
Thread.sleep(600);
}else if(FoodType.soda == food){
System.out.println("Cooking 15 for soda");
Thread.sleep(15);
}else{
System.out.println("Cooking 200 for sub");
Thread.sleep(200);
}
currCap -= quantity;
synchronized(Lock){
//System.out.println("Notifying to let other Machines");
Simulation.logEvent(SimulationEvent.machineDoneFood(Machine.this,machineFoodType
));
Lock.notifyAll();
}
} catch(InterruptedException e) { }
}
}
}
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version