Please help me with this two-part question. Here is the first part:
(Part 2: I have updated the code since - requirements have changed a
bit.)
I am trying to implement the Librarian problem in Java. The Semaphore page on Wikipedia gives the library analogy of a Semaphore. In the first part, I am trying to model this problem. In my case, I am using a [Subject Matter Expert] instead of a Room as a resource.
Suppose a library has 10 identical study rooms, intended to be used by one student at a time. To prevent disputes, students must request a room from the front counter if they wish to make use of a study room. When a student has finished using a room, the student must return to the counter and indicate that one room has become free. If no rooms are free, students wait at the counter until someone relinquishes a room.
Since the rooms are identical, the librarian at the front desk does not keep track of which room is occupied, only the number of free rooms available. When a student requests a room, the librarian decreases this number. When a student releases a room, the librarian increases this number. Once access to a room is granted, the room can be used for as long as desired, and so it is not possible to book rooms ahead of time.
The problem I am facing in my implementation is regarding association of a Student with a Subject Matter Expert. How would you do this in the following secnario? All that the SubjectMatterExpert needs to do is print the Student Id (for now).
Part 2: New requirements:
- There are fixed number of Students, SMEs, and Book Closets
- Students have certain number of Books at the beginning (presently, books are just numbers)
- SMEs add or check out books from the Boook Closet at a Student's request
- Students specify add or check out action, number of books, and the Book Closet
This is the modified (edited) Student class:
package librarysimulation;
public class Student extends Thread {
String studentId = "";
Librarian librarian = null;
int bookCount = 0;
public Student(String id, Librarian lib, int book) {
studentId = id;
librarian = lib;
bookCount = book;
}
#Override
public void run() {
System.out.println("Student " + studentId + " is requesting SME...");
librarian.requestSME();
try {
// Do something
System.out.println("Student " + studentId + " has access to an SME.");
//How do I ask the SME to add OR checkOut 'x' number of books
//from a given BookCloset?
} finally {
librarian.releaseSME();
}
}
}
This is the modified (edited) Librarian class:
package librarysimulation;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Librarian {
public Semaphore sme;
public int bookClosetCount = 0;
public Librarian(int smeCount, int bookCloset) {
sme = new Semaphore(smeCount, true);
bookClosetCount = bookCloset;
//openLibrary(smeCount);
}
//Receive SME request from the Student here
public void requestSME() {
try {
sme.acquire();
//assign student to SME
} catch (InterruptedException ex) {
Logger.getLogger(Librarian.class.getName()).log(Level.SEVERE, null, ex);
}
}
//Release SME from the Student here
public void releaseSME() {
sme.release();//release SME
}
//Set the SME threads active (from constructor)
//i.e., when the library opens, have the SMEs ready
public final void openLibrary(int roomCount) {
for (int i = 0; i < roomCount; i++) {
SubjectMatterExpert s = new SubjectMatterExpert(String.valueOf(i));
s.start();
}
}
}
This is the modified (edited) Subject Matter Expert class:
package librarysimulation;
public class SubjectMatterExpert extends Thread {
String smeId = "";
SubjectMatterExpert(String id) {
smeId = id;
}
#Override
public void run(){
//Handle Student request
//Students specify if they are checking out books or returning books
//Students specify number of books
//Students specify which closet
//SME simply executes the method from the Book Closet instance
}
}
This is the modified (edited) Simulator class:
package librarysimulation;
public class Simulator extends Thread {
public static final int STUDENT_COUNT = 50;
public static final int SME_COUNT = 3;
public static final int BOOKCLOSET_COUNT = 10;
public static final int BOOK_PER_STUDENT_COUNT = 10;
#Override
public void run() {
//Instantiate Library//New library with 3 SMEs
Librarian lib = new Librarian(SME_COUNT, BOOKCLOSET_COUNT);
//Create students
int i = 0;
while (i < STUDENT_COUNT) {
Student s = new Student(String.valueOf(i), lib, BOOK_PER_STUDENT_COUNT);
s.start();
i++;
}
}
public static void main(String[] args) {
Simulator s = new Simulator();
s.start();
}
}
an this is the (new) Book Closet class:
package librarysimulation;
public class BookCloset {
int closetId;
int bookCount = 0;
public BookCloset(int id, int book) {
closetId = id;
bookCount = book;
}
public int addBook(int book){
return bookCount + book;
}
public int checkOutBook(int book){
int finalBookCount = bookCount - book;
//Change book count iff it makes sense to do so
if(finalBookCount >= 0)
bookCount = finalBookCount;
//If return value is -ve, handle accordingly
return finalBookCount;
}
}
In the original librarian problem you described, the problem doesn't care which student is in which room, therefore uses a simple thread safe counter (i.e. a Semaphore) to implement control of the resources. Following that description of the problem there still needs to be an alteration of your implementation. One approach is to 2 methods on the librarian class, one for requesting the SME, the other for returning it.
class Librarian {
Semaphore sme = new Semaphore(NUMBER_OF_SMES);
void requestSme() throws InterruptedException {
sme.acquire();
}
void releaseSme() {
sme.release();
}
}
class Student {
Librarian librarian;
public void run() {
libarian.requestSme();
try {
// Do something
finally {
librarian.releaseSme();
}
}
}
However if you do need to know which Student is working with which SME, then you need a different construct for managing the resources, a Semaphore is no longer sufficient. One example could be a Queue.
class Librarian {
BlockingQueue<SubjectMatterExpert> q =
new ArrayBlockingQueue<SubjectMatterExpert>(NUMBER_OF_SMES);
public Librarian() {
for (int i = 0; i < NUMBER_OF_SMES; i++)
q.put(new SubjectMatterExpert(String.valueOf(i));
}
SubjectMatterExport requestSme() throws InterruptedException {
q.take();
}
void releaseSme(SubjectMatterExpert toRelease) {
q.put(toRelease);
}
}
class Student {
Librarian librarian;
public void run() {
SubjectMatterExpert sme = libarian.requestSme();
try {
System.out.println("Student: " + this + ", SME: " sme);
finally {
if (sme != null)
librarian.releaseSme(sme);
}
}
}
It makes sense to have SMEs as threads running in a while loop. Check out some starting codes below. Also, you need to initialize the book closet somewhere at the beginning of the simulation. I don't know about the whole approach you are taking though.
package librarysimulation;
public class SubjectMatterExpert extends Thread {
String smeId = "";
SubjectMatterExpert(String id) {
smeId = id;
}
#Override
public void run(){
while(true){
//acquire a student (semaphor)
//acquire a lock (semaphor(1))
//critical region -
//Handle Student request
//Students specify if they are checking out books or returning books
//Students specify number of books
//Students specify which closet
//release yourself (semaphor - define in library)
//release lock (semaphor(1))
}
//SME simply executes the method from the Book Closet instance
}
}
Implement and double check with others in the forum. I am new here. More experienced voices may have a better say though. Hope this helps (= does not hurt) at the end.
Related
I had this object oriented programming project last semester and it was all about simulating a simple dormitory.
1.There should have been a manager class in which the main method and like 80 percent of the other methods had to be there.
2.A dormitory class containing an arraylist of blocks.
3.A block class containing an arraylist of rooms.
4.A room class containing an arraylist of students.
5.All of these classes contained getters and setters for their private variables.
There were a few methods that we should've added to the program which has nothing to do with my question,so I will not write them in here,but the thing is,a few of theses methods needed to look through these arraylists to find a specific object.For example,a student with a specific student number.I created these methods with nested loops.But I know there is some way I could avoid heavy nesting.
Here is my reduced code.I will only let the manager class contain one nested loop method as an example:
import java.util.Scanner;
public class Manager {
private String name;
private String familyName;
private String userName;
private String passWord;
private static Scanner scan = new Scanner(System.in);
private Dormitory dorm = new Dormitory();
private static Menu menu = new Menu();
private Student tempStudent;
private Block tempBlock;
private Room room;
private boolean bool;
{
dorm.setDormManager(this);
}
public Manager(String managerName, String managerID) {
name = managerName;
userName = managerID;
}
//find student with its number
public void findStudent() {
//Taking the student number from the user.
System.out.println("Please enter the student number:");
String studentNum = scan.nextLine();
for (int i = 0; i < dorm.getBlockList().size(); i++)
for (int j = 0; j < dorm.getBlockList().get(i).getRooms().size(); j++)
for (int k = 0; k < dorm.getBlockList().get(i).getRooms().get(j).getRoomStudents().size(); k++)
if (dorm.getBlockList().get(i).getRooms().get(j).getRoomStudents().get(k).getStudentNumber().equals(studentNum)) {
tempStudent = dorm.getBlockList().get(i).getRooms().get(j).getRoomStudents().get(k);
break;
}
}
public void create() {
//Used loops for the original program.
Block block1 = new Block("1");
Block block2 = new Block("2");
dorm.getBlockList().add(block1);
dorm.getBlockList().add(block2);
Room room1 = new Room("1");
Room room2 = new Room("2");
dorm.getBlockList().get(0).getRooms().add(room1);
dorm.getBlockList().get(1).getRooms().add(room2);
Student student1 = new Student("12345678");
Student student2 = new Student("98765432");
dorm.getBlockList().get(0).getRooms().get(0).getRoomStudents().add(student1);
dorm.getBlockList().get(1).getRooms().get(0).getRoomStudents().add(student2);
}
public static void main(String[] args) {
Manager manager = new Manager("Dumbledore", "#1112");
manager.create();
}
}
public class Dormitory {
private int blocks;
private Manager dormManager;
private long allMembers;
private ArrayList<Block> blockList = new ArrayList<Block>();
}
public class Block {
private String blockNumber;
private ArrayList<Room> rooms = new ArrayList<Room>();
private Dormitory dorm = new Dormitory();
public Block(String blockNum) {
blockNumber = blockNum;
}
}
public class Room {
private String roomNumber;
private ArrayList<Student> roomStudents = new ArrayList<Student>();
private Block roomBlock;
private Student roomManager;
public Room(String roomNum) {
roomNumber = roomNum;
}
}
public class Student {
private String studentName;
private String studentFamilyName;
private String studentNumber;
private Room room;
public Student(String studentNum) { //Creates a student object using the student number.
studentNumber = studentNum;
}
}
I tried my best to reduce the code.
I searched a lot and asked a lot of people about this but I didn't get my desired answer.I'm not sure why I couldn't find anything about this,but I'd really appreciate it if you'd lend me a hand or give me the link of a related article.
Short answer: No, you should never loop through everything checking for getStudentNumber().equals(studentNum). This has linear time complexity O(N)
Long answer: You should index your data based on your queries
Eg: Indexing with HashMaps which have constant time complexity O(1). (Note: This code is not thread safe)
public class SchoolService {
private Map<String, Student> studentsById = new HashMap<>();
private Map<Long, Dorm> dormsById = new HashMap<>();
/// dormsByAreaCode is showing an example of an index which groups objects into lists
private Map<String, List<Dorm>> dormsByAreaCode = new HashMap<>();
public void addStudent(Student student) {
if (studentsById.containsKey(student.getName()) {
throw new IllegalStateException("Duplicate student " + student.getName());
}
studentsById.put(student.getId(), student);
}
public Student getStudentById(String studentId) {
Student student = studentsById.get(studentId);
if (student == null) {
throw new IllegalStateException("No such student " + studentId);
}
return student;
}
public void addDorm(Dorm dorm) {
// TODO: validation
dormsById.put(dorm.getId(), dorm);
List<Dorm> areaDorms = dormsByAreaCode.get(dorm.getAreaCode());
if (areaDorms == null) {
areaDorms = new ArrayList<>();
dormsByAreaCode.put(dorm.getAreaCode(), areaDorms);
}
areaDorms.add(dorm);
}
public Dorm getDormById(long dormId) {
Dorm dorm = dormsById.get(id);
// TODO: validation
return dorm;
}
public List<Dorm> getDormsByAreaCode(String areaCode) {
List<Dorm> areaDorms = dormsByAreaCode.get(areaCode);
// TODO: validation
return areaDorms;
}
// etc
}
The following quote is from tutorialspoint. This is the perfect use case of the forEach method from the Stream interface. The link I provided and further reading on Streams can help avoid repetitive code.
Using collections framework in Java, a developer has to use loops and make repeated checks. Another concern is efficiency; as multi-core processors are available at ease, a Java developer has to write parallel code processing that can be pretty error-prone.
dorm.getBlockList().stream().forEach((b) -> {
b.getRooms().stream().forEach((r) -> {
...
})
});
You can also read about parallelStreams from here.
I've been asked to program a small online sales aplication.
It sounds very simple in theory (but it's been a hell for me). I'm just supposed to have an arrayList with about 5 products and then have a client buy 1 to 5 products and print the sales total.
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String printInfo() {
return "Product: " + name + " Cost: " + price;
}
}
Then I have a client class:
public class Cliente {
private String name;
private int numPedido;
ArrayList<Producto> products = new ArrayList<Producto>();
public void listBuilder() {
Producto shirt = new Producto("Shirt", 30);
Producto tshirt = new Producto("T-Shirt", 40);
Producto sweater = new Producto("Sweater", 50);
}
public Cliente(String name, int numPedido) {
this.name = name;
this.numPedido = numPedido;
}
public Cliente() {
}
public String getName() {
return name;
}
public int getNumPedido() {
return (int) (Math.random() * 100);
}
public void addNewClient() {
name = JOptionPane.showInputDialog("Nombre: ");
}
public String printInfo() {
return "Nombre: " + name;
}
}
Right now I'm stuck thinking on how to make a client select a product and get that attached to him. I was thinking on making an arrayList of an arrayList but I'm sure that would complicate things. I know there is probably an easier way to connect them but I can't think of any. The option I have in mind is a method which shows numbers from 1 to 3(corresponding to each product) and when the user picks one it should return the price of the item.
Still not sure how to implement it in a way that the user can pick multiple products.
EDIT:
I also have an admin class that goes like this:
public class Admin {
private Client[] clientList;
public AdminPedidos() {
clientList = new Client[2];
}
public void AddContact() {
clienteList[0] = addProduct();
clienteList[1] = addProduct();
fillList();
}
public Cliente addProduct() {
String contactoString = JOptionPane.showInputDialog("Are you a new client? Press 1 if yes.");
if (contactoString.equals("1")) {
return new Cliente();
} else {
return new Cliente(); //just for testing
}
}
private void fillList() {
for (Client i : clientList) {
i.addNewClient();
}
}
public void printContact() {
for (Client i : clientList) {
System.out.println(i.printInfo());
}
}
}
You can have some purchaseProduct method attached to each Client.
public void purchaseProduct(Product product) { this.products.add(product); }
Then each Client you instantiate (Client client = new Client(name, id);) can add Products to his/her cart with the purchaseProduct method.
I'm assuming you are using some kind of user input method (Scanner). With that you can read the user's input of which Product they want and accordingly call the function with the right Product.
The listBuilder function doesn't quite make sense to me btw (and after your edit, it's really hard to make sense of what the Admin class should be/represent).
Edit: You would probably want to create an ArrayList<Product> which will be attached to each client, which you already have. I sense that you have a difficulty deciding where to put your actual Products. You should not put them inside your Client class for sure.
You should think about who/where they are going to be used. Probably in main right? So just instantiate them there first and then the Client could choose which one to purchase (via the method I introduced before):
client.purchaseProduct(product);
I have this class which will be used from my main class and will be creating objects of class Sale:
import acm.program.*;
public class Sale {
int size = 0;
public int scode = 0 ; // sell code
public String cname ; // client name
public int cphone ; // client phone
public String sdate ; // selling date
public int cost ; // final cost
public int aItems[] // sold product
public Sale (Item aItems[],String cname, String cphone, String sdate) {
this.aItems = aItems ;
this.cname= cname;
this.sphone= sphone;
this.sdate= sdate;
}
public void setsItems(Items aItems) {
this.aItems= aItems;
}
public void setCname (String name) {
this.name= name;
}
public void setCphone(String cphone) {
this.cphone= cphone;
}
public void setSDate(String sdate) {
this.sdate= sdate;
}
The project says that in my main class I must have a way to review any sale made by entering the unique id number of the sale which will be created in this class. My problem is that I don't know how to set my class so that every time it's called from my main it will generate a new id number, starting from 1 and increasing by 1 every time.
Any ideas?
Use a static member variable to store the ID number of the last sale. A static member is a variable that is part of the class, not the object. This value can get incremented like you want.
Make a new method to access that variable that also increments the variable by one each time you generate a new ID.
In your main, call the Sale.generateNewID() method, then pass that new ID into the constructor of your Sale class.
public class Sale {
public static int idCount = 0;
public static int generateNewID() {
return ++idCount;
}
// this is a new member variable to store the id of the sale
private int id;
// note: added id parameter to constructor
public Sale (int theId, Item aItems[],String cname, String cphone, String sdate) {
this.id = theId;
// other constructor assignments that you had go here.
}
... // rest of your code
}
This is my Question
A health center has employed two doctors that examine and treat at most 25 patients in a day.
A patient is examined and treated by any one of the two doctors. Each patient has to register his name so that the doctors can examine and treat the patient on first-come-first-serve bases.
Exercise 2
Part a: For the scenario2 mentioned above, develop a program that creates patients and doctors (both are threads). Patients register in a queue and the doctors pick patient from the same queue on first-come-first-serve bases and examine and treat them. Use the queue that is not thread safe (For example ArrayDeque). Make sure your program has no synchronization issues.
Part b: Use the queue that is thread safe (For example, ArrayBlockedQueue) and check if your solution has synchronization issues. (Make sure that you solution does not provide synchronized methods or synchronized blocks)
This is my CODE
package lab8;
import java.util.ArrayDeque;
public class LAB8 {
class Doctor implements Runnable {
private String name;
private Patient patient;
Doctor (String n){
name = n;
}
public void examine (){
System.out.println("Doctor is now examining the patient");
}
public void treat(){
System.out.println("Doctor is now treating the patient");
}
#Override
public void run (){
}
}
static class Patient implements Runnable {
private String name;
Patient (String n){
name = n;
}
public void register(String name){
System.out.println(name + " is registering in Queue");
}
#Override
public void run(){
ArrayDeque<Patient> Patients = new ArrayDeque(25);
for(int i = 0;i<25;i++){
Patients.add(new Patient("Patient No " + i));
Patients.removeFirst().register("Patient No " + i);
}
}
}
public static void main(String[] args) {
ArrayDeque<Patient> Patients = new ArrayDeque(25);
for(int i = 0;i<25;i++){
(new Thread (Patients.removeFirst())).start();
}
}
}
This is the error I am getting when I run it.
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayDeque.removeFirst(ArrayDeque.java:278)
at lab8.LAB8.main(LAB8.java:50)
Java Result: 1
I am only trying Part A right now.
In your main function, the Patients ArrayDeque object has no Patient objects. Rather it is empty, so there is nothing to remove from the queue. You need to add some Patients first.
ArrayDeque<Patient> Patients = new ArrayDeque(25);
for(int i = 0;i<25;i++){
(new Thread (Patients.removeFirst())).start();
}
You would need to add new Patients to the Patients ArrayDeque first like you do here.
Patients.add(new Patient("Patient No " + i));
So you have have something like this...
public static void main(String[] args) {
ArrayDeque<Patient> Patients = new ArrayDeque(25);
for(int i = 0;i<25;i++){
Patient p = new Patient("Patient No " + i);
(new Thread (p)).start();
Patients.add(p);
}
}
So I'm creating a student database thing for a school project. My first issue is that upon creating a new student I should see "Application number ### has registered successfully". Now the problem is that we have to have that number generate (### referring to the number) sequentially from 1 every time a new application is recorded. How would I go about doing that?
So far this is what there is but I can't seem to get the number to generate incrementally.
public TestApplication(String Surname, String personalIdNo)
{
if (isValidpersonalIdNo(personalIdNo) == true)
{
Student.add(Surname);
Application.put(personalIdNo, Student);
System.out.println("Application number ### " + "has registered successfully");
}
else
{
System.out.println("Application has failed, Personal id: " + personalIdNo);
}
}
Any help with this would be appreicated.
Since you seem to be using lots of static methods, I believe the best thing for you to do in this case is to create a static field called latestId and a static method called generateId, both in the Student class. Then you can call the generateId method whenever you call Student.add.
However, please note that this solution does not work if your application is multithread.
public class Student {
private static int latestId = 0;
public static int generateId() {
return ++latestId;
}
...
}
You can write a singleton class that will produce the ids for you:
class Generator {
private AtomicInteger count = new AtomicInteger(1);
private static Generator generator = new Generator();
private Generator() { }
public static Generator getInstance() {
return generator;
}
public int generate() {
return count.getAndIncrement();
}
}
Now, when you need to get a new id, you just call the generate method. The AtomicInteger is used because you might need the id from multiple threads and it will make the concurrent access safe.
The singleton Generator provides a single entry point to the id-generating facility.
You can use your storage type to give you the amount of added students that were put into DB.
I don't know what type you use to store your students. If it is hashmap or vector you can use size method to print students count. So I assume if you have Application.put you probably have a field in your Application type that is used to store each student. Then you can add a method like getStudentsCount to it and you should be all set. Since I don't know much about your Application type the above is all assumptions. Below you can find how I would solve that:
import java.util.HashMap;
import java.util.Vector;
class Student{
private String name;
private int personalID;
public Student(String name, int personalID){
this.name = name;
this.personalID = personalID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPersonalID() {
return personalID;
}
public void setPersonalID(int personalID) {
this.personalID = personalID;
}
}
class DB{
private HashMap<Integer, Student> students = new HashMap<Integer, Student>();
public boolean addStudent(Student student) {
Integer studentId = new Integer(student.getPersonalID());
if( !students.containsKey(studentId)){
students.put(new Integer(studentId), student);
return true;
}
else
return false;
}
public int getStudentCount() {
return students.size();
}
}
class Operations{
DB db;
public Operations(DB db){
this.db = db;
}
public boolean addStudent(String name, int personalID){
Student student = new Student(name, personalID);
return db.addStudent( student );
}
}
public class SimpleStudentDB {
public static void main(String [] args){
DB db = new DB();
Operations operations = new Operations(db);
if( operations.addStudent( "Jason", db.getStudentCount()+1) )
System.out.println("Student added successfully. DB contains ###"+db.getStudentCount()+" elements");
else
System.out.println("Operation failed");
}
}