This site requires JavaScript, please enable it in your browser!
Greenfoot back
Jemy
Jemy wrote ...

2021/3/17

How to find the smallest value from objects created

1
2
3
Jemy Jemy

2021/3/19

#
My code in My World Class
public void serverPopulation()
    {      
       for(int i=0; i<NUMBER_OF_SERVER; i++)
       {
        int x= Greenfoot.getRandomNumber(getWidth());
        int y= Greenfoot.getRandomNumber(getHeight());
        int server= Greenfoot.getRandomNumber(151)+150;
        addObject(new Server(server), x, y);
       }
       
        Server lowServer = null;
        int lowNumber = 201;
         for (Object obj : getObjects(Server.class))
         {
          Server server = (Server) obj;
          if (server.number < lowNumber)
          {
            lowNumber = server.number;
            lowServer = server;
            //lowServer.setLowState(true);
            showText("ElectionTimeOut: "+lowNumber, 100, 20);
          }
         }
My code in Server class
public void Candidate()
    {
        electionTimeOut = lowNumber;
        for(int i=1; i<=ELECTION_TIME;i++)
         {
         electionTimeOut--;
         if(electionTimeOut==0)
         {
         isCandidate=true;
         setLowState(true);
         break;
         }
         
         }
    }
    
    public void setLowState(boolean isLow)  
    {
            
         if (isLow)
         {
            setImage("Candidate.png");lowNumber= number;
         }
         else  setImage("Follow.png");
    }
The code is working but the image change is not happening. however if I remove the comment on line 20 in My World class, I have more than one server change immediately. I wanted the server with the lowest number to change after the lowest number has been reduce to zero. that's what i was trying to do in my candidate method. can you see why is not working that way.
danpost danpost

2021/3/19

#
Jemy wrote...
The code is working but the image change is not happening. however if I remove the comment on line 20 in My World class, I have more than one server change immediately.
Lines 20 and 21 should go after the for loop.
Jemy Jemy

2021/3/19

#
Thanks again. but it seems my candidate method is redundant. your suggestion worked but i wanted a situation where the change does not happen immediately, there should be a delay that's why i set up the loop to reduce the lowest number to zero before the change happens.
danpost danpost

2021/3/19

#
Jemy wrote...
Thanks again. but it seems my candidate method is redundant. your suggestion worked but i wanted a situation where the change does not happen immediately, there should be a delay that's why i set up the loop to reduce the lowest number to zero before the change happens.
A loop will not do. Well, not a for , do or while loop. You need to make use of the repeated cycle of act calls to produce a loop-like iterating of code.
Jemy Jemy

2021/3/19

#
The repeated cycle of act calls I have not heard that before. i was even thinking if i could call lowServer.setLowState() within the for loop basically to replace line 10 in server class. Could you please give me a skeletal approach to the repeated cycle of act calls that you talk about? so that i can pick up from there.
danpost danpost

2021/3/20

#
Jemy wrote...
Could you please give me a skeletal approach to the repeated cycle of act calls that you talk about? so that i can pick up from there.
Maybe with the class fields:
public static final int ELECTION_TIME_OUT = 1; // other phases to be given values as well
public static int phase = ELECTION_TIME_OUT;
and this (notice no loop):
public void Candidate()
{
    if (--electionTimOut == 0)
    {
        isCandidate = true;
        setLowState(true);
        phase++;
    }
}
with this in act:
if (phase = ELECTION_TIME_OUT) Candidate();
Jemy Jemy

2021/3/20

#
danpost wrote...
Jemy wrote...
Could you please give me a skeletal approach to the repeated cycle of act calls that you talk about? so that i can pick up from there.
Maybe with the class fields:
public static final int ELECTION_TIME_OUT = 1; // other phases to be given values as well
public static int phase = ELECTION_TIME_OUT;
and this (notice no loop):
public void Candidate()
{
    if (--electionTimOut == 0)
    {
        isCandidate = true;
        setLowState(true);
        phase++;
    }
}
with this in act:
if (phase = ELECTION_TIME_OUT) Candidate();
Hi @danpost, thank you again for your time. But I am confused, if phase = ELECTION_TIME_OUT in the class fields, how will they ever be equal in their comparison since phase is incrementing while ELECTION_TIME_OUT is constant.
if (phase = ELECTION_TIME_OUT) Candidate();
Its like the ELECTION_TIME_OUT value in the code above represent the lowServer value in which case the candidate method is called when phase matches it. so what value does electionTimeOut represents? I am sorry that am bothering you. it's just that am new to programming
danpost danpost

2021/3/20

#
Jemy wrote...
if phase = ELECTION_TIME_OUT in the class fields, how will they ever be equal in their comparison since phase is incrementing while ELECTION_TIME_OUT is constant.
if (phase = ELECTION_TIME_OUT) Candidate();
The class constant (static final field), plus others that you will add, represent different phases in the process. The phase field will, or should, contain the value of the phase that is currently under way.
Its like the ELECTION_TIME_OUT value in the code above represent the lowServer value in which case the candidate method is called when phase matches it. so what value does electionTimeOut represents? I am sorry that am bothering you. it's just that am new to programming
ELECTION_TIME_OUT is a class constant only to be used with phase. The electionTimeOut instance variable acts as a counter, or timer, used during that phase. Once a timer goes to zero, that phase completes and a new phase begins. Hence the change in the phase value.
danpost danpost

2021/3/20

#
While your scenario runs, the act methods of the active world and the actors in that world are all being called about 60 times every second (at normal running speed). In essence, that, in itself, creates a "loop". The act methods are to be coded for what will happen each moment -- not like a list of what to do during its existence. Otherwise, it would try to perform its life duties 60 times every second (or every animation frame).
Jemy Jemy

2021/3/20

#
@danpost, sincerely I couldn't understand your explanation. Remember, all i want to do is to bring in a delay for the lowServer to change its image after its value stored in lowNumber is equal to zero by decrementing it. I am still confused at your suggestion. please can you illustrate further so that i can understand maybe using codes. Thanks for your help so far
danpost danpost

2021/3/20

#
To time something:
private int timer;

public void act()
{
    if (timer > 0 && --timer == 0) doSomething();
}
No loop required. To start timer:
timer = somePositiveValue;
Jemy Jemy

2021/3/20

#
danpost wrote...
To time something:
private int timer;

public void act()
{
    if (timer > 0 && --timer == 0) doSomething();
}
No loop required. To start timer:
timer = somePositiveValue;
Thank you very much Is there any way I can call lowServer to the server class which is currently in the serverpopulation method MyWorld class such that i will have lowServer.setLowState(true) in the candidate method. I believe it will do exactly what I want it to do.
danpost danpost

2021/3/21

#
Jemy wrote...
Is there any way I can call lowServer to the server class which is currently in the serverpopulation method MyWorld class such that i will have lowServer.setLowState(true) in the candidate method. I believe it will do exactly what I want it to do.
If all servers have their electionTimeOut field set to their respective number value to count down from, then the first server to reach zero would be the low server.
Jemy Jemy

2021/3/21

#
danpost wrote...
Jemy wrote...
Is there any way I can call lowServer to the server class which is currently in the serverpopulation method MyWorld class such that i will have lowServer.setLowState(true) in the candidate method. I believe it will do exactly what I want it to do.
If all servers have their electionTimeOut field set to their respective number value to count down from, then the first server to reach zero would be the low server.
public void Candidate()
    {
        electionTimeOut = number;
        for(int i=1; i<=ELECTION_TIME;i++)
         {
         
         if(electionTimeOut > 0 &&--electionTimeOut==0)
         {
         isCandidate=true;
         setLowState(true);
         break;
         }
         
         }
    }
I have done this, it causes all of the server object to change concurrently when i call the candidate method in the act method of Server class but when i comment out the call, lowServer.setLowState(true) causes only the low server to change immediately i reset the simulation. it does not give room to see the smallest value of the low server and when it changes
danpost danpost

2021/3/21

#
Move line 3 to the constructor and remove the enclosing for loop. Call method from act.
There are more replies on the next page.
1
2
3