Hi, I'm new to this forum and look forward to contributing. I am currently working on a project where I have to build a Java simulator which consists of three input lines of food, three processing machines and two packing machines. There are 2 processing machines dedicated to processing perishable foods and one dedicated to non-perishable. The packing machines can pack anything. I am currently developing the the machine classes, which cosists of a packMachine and a processMachine, which are both subclasses of machine, which can jam randomly with a probability of 0.001.

I am struggling to develop the Random function, along with making the machine objects interact with the food objects. I am wondering how to make them communicate when they are not hierarchally related to eachother.

I am new to Java, so just confused by the language-specific qualities. Any pointers or tips would be greatly appreciated.

Recommended Answers

All 12 Replies

Choose a unit of time, something that divides all the important time durations you will be dealing with, such as how long it takes to process food, to pack food, and to unjam a machine. Then all you have to do is make a loop where each iteration represents one unit of time and has each machine advance its state appropriately.

Then each machine is a finite state machine that takes food from queues, puts food into queues, and jams as appropriate. I can't imagine any difficulty in interactions between machines and food. Surely the food won't be doing anything to the machines, and once a machine has taken a food object from a queue it can call any methods that it likes on the food object.

Member Avatar for martija1

I am just confused how I can actually take a food object.. (new to java)

If you define a Food class, and create an instance of that class then you have an object no different from a String or an Integer in terms of how you can access or pass it to/from a method. It's just like bguild says, just imagine how you would do it with Strings, then use Food objects instead of the Strings.

So how would I set the value of the the food's processingtime field to a field in my machine class?

Ok, so we're adding the food into the machine via a "conveyer" class, which uses a linked list of food types and I need to acquire the details of each individual food item.

Lets assume you have something like

class Food {
   ...
   private int processingTime
   ...
   public int getProcessingTime() {
      return processingTime;
   }
   ...
}

and somewhere in your Machine class maybe you have a method something like

   void processFood(Food aFoodItem) {
      ...
      int timeNeeded = aFoodItem.getProcessingTime();
      ...

It's as easy as that!

Thanks a bunch! :)

is there any way I can do that but from a linked list of food items in the conveyer class?

For example, IF it is one object type, then process for this long, otherwise process for this long. From out of the linked list.

Yes, sure, if Conveyor has a method you can call to get the next Food item from Conveyor's list, maybe like the following. Once you've got the Food item you can use it's public methods to find out whatever you need to know about it.

class Conveyor {
   ...
   private List<Food> foodToProcess...
   ...
   public Food getNextFoodItem() {
      return foodToProcess.get(...

Thanks alot! I have the bulk of it works, however I need to trigger it to jam through calculation a random value between 0.001 and 1. I have to code to carry out the jam, but I am unsure of how to calculate this value. Any tips?

Just give each machine a java.util.Random and then you can do something like this:

random.nextDouble() * (1.0 - 0.001) + 0.001
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.