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/22

#
danpost wrote...
Move line 3 to the constructor and remove the enclosing for loop.
Thank you for your help. It is working but the lowServer is not the one changing. look at my code: The constructor:
public Server(int num)
    {
        number = num;
        electionTimeOut = number;
        GreenfootImage numImg = new GreenfootImage(""+number, 24, Color.BLACK, new Color(0, 0, 0, 0));
        int x = (getImage().getWidth()-numImg.getWidth())/2;
        int y = (getImage().getHeight()-numImg.getHeight())/2;
        getImage().drawImage(numImg, x, y);
        
    }
The act method:
public void act() 
    {
     move();
     Candidate();  
    }
The candidate method:
public void Candidate()
    {
         
         if(electionTimeOut >0 && --electionTimeOut==0)
         {
         isCandidate=true;
         setLowState(true);
         }
         
    }
danpost danpost

2021/3/22

#
Please tell me what the random numbers represent and how it is to be used; because, of the two possibilities I can think of, what you suggest you want to do does not make sense with either.
Jemy Jemy

2021/3/22

#
danpost wrote...
Please tell me what the random numbers represent and how it is to be used; because, of the two possibilities I can think of, what you suggest you want to do does not make sense with either.
The random number represent election timeout meaning for a server to become a candidate which i am representing as image change, after a specified time, the server with the smallest value(election timeout value) is expected to timeout first(that is become zero in our representation) and become the candidate. we have succeeded but just that the one changing is not the one with the smallest value but the server with the smallest value is in lowServer.setLowState(true). example: if we have servers with values as 4,15,8,17.19, it is expected that the server with value 4 will timeout first that is reduced to zero according to the program and then change status by changing image
danpost danpost

2021/3/22

#
So, the random value has nothing to do with identifying a server. Also, once timed out, the initial value is irrelevant and is not used again; correct?
Jemy Jemy

2021/3/22

#
danpost wrote...
So, the random value has nothing to do with identifying a server. Also, once timed out, the initial value is irrelevant and is not used again; correct?
Yes. A new value is generated once the simulation is run again. The random number is just used to determine which server changes status after a period of time. This period of time is assumed to be the smallest random value one of the server has
danpost danpost

2021/3/22

#
World serverPopulation method:
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 class:
import greenfoot.*;

public class Server extends Actor
{
    public static Server lowServer;
    public static GreenfootImage baseImage;
    
    private int timer;
    private boolean done;
    
    public Server()
    {
        if (baseImage == null) baseImage = getImage();
        timer = 150+Greenfoot.getRandomNumber(150);
    }
    
    public void act()
    {
        if (done) return;
        if (lowServer != null) 
        {
            setImage("Follow.png");
            done = true;
            return;
        }
        if (--timer == 0)
        {
            lowServer = this;
            setImage("Candidate.png");
            done = true;
            return;
        }
        GreenfootImage numImg = new GreenfootImage(""+timer, 24, Color.BLACK, new Color(0, 0, 0, 0));
        GreenfootImage img = new GreenfootImage(baseImage);
        int x = (img.getWidth()-numImg.getWidth())/2;
        int y = (img.getHeight()-numImg.getHeight())/2;
        img.drawImage(numImg, x, y);
        setImage(img);
    }
}
Jemy Jemy

2021/3/22

#
danpost wrote...
World serverPopulation method:
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 class:
import greenfoot.*;

public class Server extends Actor
{
    public static Server lowServer;
    public static GreenfootImage baseImage;
    
    private int timer;
    private boolean done;
    
    public Server()
    {
        if (baseImage == null) baseImage = getImage();
        timer = 150+Greenfoot.getRandomNumber(150);
    }
    
    public void act()
    {
        if (done) return;
        if (lowServer != null) 
        {
            setImage("Follow.png");
            done = true;
            return;
        }
        if (--timer == 0)
        {
            lowServer = this;
            setImage("Candidate.png");
            done = true;
            return;
        }
        GreenfootImage numImg = new GreenfootImage(""+timer, 24, Color.BLACK, new Color(0, 0, 0, 0));
        GreenfootImage img = new GreenfootImage(baseImage);
        int x = (img.getWidth()-numImg.getWidth())/2;
        int y = (img.getHeight()-numImg.getHeight())/2;
        img.drawImage(numImg, x, y);
        setImage(img);
    }
}
The server constructor in the server class you define has no parameter but the addobject has a parameter in my world class. i had to remove it since it's equivalent to the timer in the server constructor.
danpost danpost

2021/3/22

#
Jemy wrote...
The server constructor in the server class you define has no parameter but the addobject has a parameter in my world class. i had to remove it since it's equivalent to the timer in the server constructor.
Yes, line 8 in serverPopulation should be:
addObject(new Server(), x, y);
Jemy Jemy

2021/3/22

#
danpost wrote...
Jemy wrote...
The server constructor in the server class you define has no parameter but the addobject has a parameter in my world class. i had to remove it since it's equivalent to the timer in the server constructor.
Yes, line 8 in serverPopulation should be:
addObject(new Server(), x, y);
Thanks. I appreciate. if i click on Reset, it does not generate any value unless i recompile before it generate the values. i thought that Reset button is for recompilation
danpost danpost

2021/3/22

#
Jemy wrote...
if i click on Reset, it does not generate any value unless i recompile before it generate the values. i thought that Reset button is for recompilation
In world constructor, add the following line:
Server.lowServer = null;
"Reset" does not recompile. It only creates and sets a new initial world active.
Jemy Jemy

2021/3/23

#
danpost wrote...
Jemy wrote...
if i click on Reset, it does not generate any value unless i recompile before it generate the values. i thought that Reset button is for recompilation
In world constructor, add the following line:
Server.lowServer = null;
"Reset" does not recompile. It only creates and sets a new initial world active.
Thanks for helping again. The issue is corrected.
Jemy Jemy

2021/4/25

#
Hi danPost, when i run the method below, it runs the first time but if i try to run it the second time it gives me nullpointer exception
private void leader()
    {
        if(totalVoteReceived >= 21)
        {
            lowServer.setImage("Leader.png");
        }
    }
i called the method in my acts method
public void act()
    {
        move();
        Candidate();
        if(isCandidate){
            Candidate_RequestVote();
        }
        grantVote();
        leader();
    }
There is a Server.lowServer=null in myWorld's constructor
danpost danpost

2021/4/26

#
Change line 3 in leader code to:
if (totalVotesReceived >= 21 && lowServer != null)
OR: maybe the server timers need to be reset (?)
Jemy Jemy

2021/4/26

#
danpost wrote...
Change line 3 in leader code to:
if (totalVotesReceived >= 21 && lowServer != null)
OR: maybe the server timers need to be reset (?)
I tried your suggestion, it worked but when i tried to run it again instead of changing to candidate for a new leader to emerge, it changes to the leader. i think server timers need to reset to allow another low server to be candidate and eventually be elected each time the simulation is run
private void leader()
    {
        if(totalVoteReceived >= 21 && lowServer!=null )
        {
            lowServer.setImage("Leader.png");
        }
    }
please at danpost i still need assistance to this.
You need to login to post a reply.
1
2
3