Sie sind auf Seite 1von 5

I.

Java CODE // Java Assignment 1 - Generic Queue implementation public interface IQueue<T> { public void put(T t); public T get(); } // please code your ThreadSafe implementation of the queue // make sure your implementation has O(1) efficiency // You are not allowed to use any lib containers, basically you have to import n othing // Good luck ! package com.moody.sample; public interface IQueue { public void put(Object t); public Object get() throws InterruptedException; } package com.moody.sample; public class QueueImplem implements IQueue { protected Object[] obj; protected int start, end; protected boolean full; public QueueImplem(int size) { obj = new Object[size]; start=0; end =0; full = false; } public boolean checkEmpty() { return((start == end) && !full); } @Override public Object get() throws InterruptedException { if(full) { full = false; } synchronized(obj) { return obj[end = (++ end % obj.length)];

}} @Override public void put(Object t) { synchronized(obj) { if(!full) { obj[start = (++ start % obj.length)] = t; obj.notify(); } if(start == end) { full = true; } } }

II.SQL CODE -------------------------------------------------------------------------------------------------------------- you have 2 tables : Resource table and Event Table -CREATE TABLE OfficeRESOURCE ( ResourceID INTEGER PRIMARY KEY ,Name VARCHAR2(50) ); ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CREATE TABLE Event ( EventName VARCHAR2(25) PRIMARY KEY ,ResourceID INTEGER ,StartTime DATE ,EndTime DATE ); ALTER TABLE Event ADD CONSTRAINT fkEvent foreign KEY (ResourceID) references O fficeRESOURCE; -------------------------------------------------------------------------------------------------------------- you need to write a stored procedure for booking that resource -- you should garantee correcteness of the booking (no double book, no booking for non existing resource etc) -- the correct syntax is not so important in that test - you can use PL/SQL or TransactSQL - it is your choice -- the correct business logic implementation is important

-- put your implementation here create or replace PROCEDURE BOOKRESOURCE(pResourceID in INTEGER, pEvenName in VARCHAR2, pStartTime in DATE, pEndTime in DATE) AS resID sTime etime label slots INTEGER; DATE; DATE; NUMBER; NUMBER;

cursor resource_available is select count(*) from officeResource where resourcei d= pResourceId; cursor time_available is select count(*) into label from Event where resourceid = pResourceId and (pEndTime NOT BETWEEN StartTime and EndTime); BEGIN OPEN resource_available; LOOP FETCH resource_available into resID; EXIT when resource_available%NOTFOUND; IF resID = 0 THEN dbms_output.put_line('Resource Invalid'); ELSE dbms_output.put_line('Resource Valid'); OPEN time_available; LOOP FETCH time_available into slots; EXIT when time_available%NOTFOUND; dbms_output.put_line(slots); IF slots =0 THEN dbms_output.put_line('Time Slot Taken'); ELSE INSERT into Event(resourceId,EventName,startTime,EndTime) values(pRes ourceID, pEvenName, pStartTime, pEndTime); END IF; END LOOP; CLOSE time_available; END IF; END LOOP; close resource_available; END BOOKRESOURCE; -- TSQL

CREATE PROCEDURE Booking @name varchar(50),officeResource @StartTime date, @EndTime date,book @EventName varchar(25) --PL/SQL PROCEDURE bookResource(pResourceID IN INTEGER , pEventName IN VARCHAR2, pStartTi me IN DATE, pEndTime DATE); -- Good luck !

III. Java Concurrency CODE //************************************************** // you have //************************************************** public interface IAccount { String getAccountNumber(); // immutable account ID double getBalance(); void setBalance(double amount); void transfer(IAccount otherAccount,double amount /*could be<0*/); } //*************************************************** // your solution package com.moody.Bankingsample; public interface IAccount { String getAccountNumber(); double getBalance(); void setBalance(double amount); void transfer(IAccount otherAccount,double amount)throws InterruptedE xception; } package com.moody.Bankingsample; public class Account implements IAccount { private double balance; protected String accountNumber; protected void Account() { } @Override public String getAccountNumber() {

return this.accountNumber; }

@Override public double getBalance() { return this.balance; } @Override public synchronized void setBalance(double amount) { double accountHolding = amount + balance; balance = balance + accountHolding; } @Override public synchronized void transfer(IAccount otherAccount, double amount)t hrows InterruptedException { if(this.balance < amount) { wait(0); } }

// public class Account implements IAccount { // put your implementation below assu ming you are writing component for multi-threaded server side environment. // assume there is only one instance for every bank Account in the system // account balance can't be negative } // Good luck ! }

Das könnte Ihnen auch gefallen