please help me!
Fullfill 10 philosophers eating and thinking transaction at least 5 times and make them to be full.(with java)

My Best Regards...

Recommended Answers

All 14 Replies

How can we help you write your program?
Do you have any specific questions?

not even taking the time to post the entire assignment?
this must be "really urgent"...

this is given code and my teacher want to complete this code.

package diningphilosophers;



public class DiningPhilosophers {

    public static void main(String[] args) {
        
        Table table = new Table();
        
        for(int i = 0; i < 10; i++) {
            Philosopher newGuy = new Philosopher(i);
            table.sitAt(newGuy);
            Thread starter = new Thread(newGuy);
            starter.start();            
        }
    }
}---------------------------------------
package diningphilosophers;


public class Fork {
    Philosopher heldBy;
    
    public Fork grabFork(Philosopher grabber) {
        if(heldBy == null) {
            heldBy = grabber;
            return this;
        }
        return null;
    }
    
    public boolean releaseFork(Philosopher releaser) {
        if(heldBy.equals(releaser)) {
            heldBy = null;
            return true;
        }
        return false;
    }
    
    public boolean isAvailable() {
        if(heldBy == null) {
            return true;
        }
        
        return false;
    }
}--------------------------------------------------------------
package diningphilosophers;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Philosopher implements Runnable{

    private Fork leftFork, rightFork;
    private Table atTable;
    private int tablePosition;
    private boolean isFull;
    private Random randomGenerator = new Random();
    
    public Philosopher(int tablePosition) {
        this.tablePosition = tablePosition;
        leftFork = null;
        rightFork = null;
        isFull = false;
    }
    
    
    @Override
    public void run() {
        for(int i = 0; i < 5; i++) {
            think();
            eat();
        }
        isFull = true;
        System.out.println("Philosopher " + tablePosition + " is done!");
    }
    
    private void think() {        
        System.out.println("Philosopher " + tablePosition + " is thinking...");
        try {
            Thread.sleep(randomGenerator.nextInt(500));
        } catch (InterruptedException ex) {
            Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void setTable(Table table) {
        atTable = table;
    }
    
    private void eat() {
        getLeftFork();
        System.out.println("Philosopher " + tablePosition + " got his left fork!");
        getRightFork();
        System.out.println("Philosopher " + tablePosition + " got his right fork!");
        System.out.println("Philosopher " + tablePosition + " is eating...");
        
        try {
            Thread.sleep(randomGenerator.nextInt(500));
        } catch (InterruptedException ex) {
            Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        releaseForks();
    }
    
    private void getLeftFork() {
        while (true) {
            leftFork = atTable.getLeftFork(tablePosition);
            if(leftFork != null) {
                return;
            }
            else {
                try {
                    Thread.sleep(randomGenerator.nextInt(100));
                } catch (InterruptedException ex) {
                    Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    private void getRightFork() {
        while (true) {
            rightFork = atTable.getLeftFork(tablePosition);
            if(rightFork != null) {
                return;
            }
            else {
                try {
                    Thread.sleep(randomGenerator.nextInt(100));
                } catch (InterruptedException ex) {
                    Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    private void releaseForks() {
        leftFork = null;
        rightFork = null;
        atTable.releaseForks(tablePosition);
    }
    
    public int tablePosition() {
        return tablePosition;
    }        
    
}---------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diningphilosophers;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 *
 */
public class Table {
    
    private Fork[] forks = new Fork[10];
    private Philosopher[] philosophers = new Philosopher[10];
    
    public Table() {
        for(int i = 0; i < forks.length; i++) {
            forks[i] = new Fork();
            philosophers[i] = null;
        }
    }
    
    public boolean sitAt(Philosopher eater) {
        int index = eater.tablePosition();
        if(index >= 0 && index < philosophers.length) {
            philosophers[index] = eater;
            eater.setTable(this);
            return true;
        }
        return false;
    }
    
    public Fork getLeftFork(int index) {
        if (index < 0 && index >= forks.length) {
            System.out.println("Something went wrong...");
            System.exit(1);
        }        
        return forks[index].grabFork(philosophers[index]);
    }
    
    public Fork getRightFork(int index) {
        if (index < 0 && index >= forks.length) {
            System.out.println("Something went wrong...");
            System.exit(1);
        }
        return forks[(index + 1)%forks.length].grabFork(philosophers[index]);    
    }
    
    public void releaseForks(int index) {
        forks[index].releaseFork(philosophers[index]);
        forks[(index+1)%forks.length].releaseFork(philosophers[index]);
    }
    
}

Please edit your posted code and wrap it in code tags. See icon above, right.
Also be sure to include the import statements.

package diningphilosophers;

public class DiningPhilosophers {

    public static void main(String[] args) {

        Table table = new Table();

        for(int i = 0; i < 10; i++) {
            Philosopher newGuy = new Philosopher(i);
            table.sitAt(newGuy);
            Thread starter = new Thread(newGuy);
            starter.start();            
        }
    }
}

------------------------------------------

package diningphilosophers;

public class Fork {
    Philosopher heldBy;

    public Fork grabFork(Philosopher grabber) {
        if(heldBy == null) {
            heldBy = grabber;
            return this;
        }
        return null;
    }

    public boolean releaseFork(Philosopher releaser) {
        if(heldBy.equals(releaser)) {
            heldBy = null;
            return true;
        }
        return false;
    }

    public boolean isAvailable() {
        if(heldBy == null) {
            return true;
        }

        return false;
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diningphilosophers;



public class Table {

    private Fork[] forks = new Fork[10];
    private Philosopher[] philosophers = new Philosopher[10];

    public Table() {
        for(int i = 0; i < forks.length; i++) {
            forks[i] = new Fork();
            philosophers[i] = null;
        }
    }

    public boolean sitAt(Philosopher eater) {
        int index = eater.tablePosition();
        if(index >= 0 && index < philosophers.length) {
            philosophers[index] = eater;
            eater.setTable(this);
            return true;
        }
        return false;
    }

    public Fork getLeftFork(int index) {
        if (index < 0 && index >= forks.length) {
            System.out.println("Something went wrong...");
            System.exit(1);
        }        
        return forks[index].grabFork(philosophers[index]);
    }

    public Fork getRightFork(int index) {
        if (index < 0 && index >= forks.length) {
            System.out.println("Something went wrong...");
            System.exit(1);
        }
        return forks[(index + 1)%forks.length].grabFork(philosophers[index]);    
    }

    public void releaseForks(int index) {
        forks[index].releaseFork(philosophers[index]);
        forks[(index+1)%forks.length].releaseFork(philosophers[index]);
    }

}

package diningphilosophers;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Philosopher implements Runnable{

    private Fork leftFork, rightFork;
    private Table atTable;
    private int tablePosition;
    private boolean isFull;
    private Random randomGenerator = new Random();

    public Philosopher(int tablePosition) {
        this.tablePosition = tablePosition;
        leftFork = null;
        rightFork = null;
        isFull = false;
    }


    @Override
    public void run() {
        for(int i = 0; i < 5; i++) {
            think();
            eat();
        }
        isFull = true;
        System.out.println("Philosopher " + tablePosition + " is done!");
    }

    private void think() {        
        System.out.println("Philosopher " + tablePosition + " is thinking...");
        try {
            Thread.sleep(randomGenerator.nextInt(1000));
        } catch (InterruptedException ex) {
            Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setTable(Table table) {
        atTable = table;
    }

    private void eat() {
        getLeftFork();
        System.out.println("Philosopher " + tablePosition + " got his left fork!");
        getRightFork();
        System.out.println("Philosopher " + tablePosition + " got his right fork!");
        System.out.println("Philosopher " + tablePosition + " is eating...");

        try {
            Thread.sleep(randomGenerator.nextInt(1000));
        } catch (InterruptedException ex) {
            Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
        }

        releaseForks();
    }

    private void getLeftFork() {
        while (true) {
            leftFork = atTable.getLeftFork(tablePosition);
            if(leftFork != null) {
                return;
            }
            else {
                try {
                    Thread.sleep(randomGenerator.nextInt(1000));
                } catch (InterruptedException ex) {
                    Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void getRightFork() {
        while (true) {
            rightFork = atTable.getLeftFork(tablePosition);
            if(rightFork != null) {
                return;
            }
            else {
                try {
                    Thread.sleep(randomGenerator.nextInt(1000));
                } catch (InterruptedException ex) {
                    Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void releaseForks() {
        leftFork = null;
        rightFork = null;
        atTable.releaseForks(tablePosition);
    }

    public int tablePosition() {
        return tablePosition;
    }        

}

Thanks, that's a lot easier to read.

Now you need to do some work and come up with some questions.

ı need to feed 10 philosophers at least five times and let them to thınk at least five times.

Yes. That is the assignment. Now you need to come up with a design to solve the problem.
What things do you need to consider to do that?

ı need to feed 10 philosophers at least five times and let them to thınk at least five times. (with semaphore and round rubin algorithm)

How is your design coming?
Do you have any coding questions yet?

What Norm is trying to politely convey is that homework help is only offered to those who demonstrate effort.

This is also found in our forum rules, which you can review here.

excuse me ı dont understand what do you mean?
teacher gave us and ı have no ıdea for this homework.

Then you'd better talk with the teacher and find out what is expected.
The teacher must think you have enough ideas on how to solve this problem.
It's not a simple problem.

ok thanks alot..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.