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

2014/2/25

Changing Variables

1
2
3
danpost danpost

2014/2/26

#
The 'if' block is to replace the 'if' block of your 'Check Your Balance' code above. The 'get' method ('public int getCredits()') allows other classes to access the value of your 'private int Credits' field in the Return class. The method is called on a Return object; therefore, a reference to a Return object must be obtained (see line 3 of the 'if' block). I had already specified where this 'getCredits' method should go.
Hersov Hersov

2014/2/26

#
Thanks for that, but I also need to be able to use Credits as a variable in another actor as an integer. I have tried taking away the system outprint and the string but it doesn't add it as a variable as well as 'int CreditsINT = Integer.parseInt(Credits); Thanks for the help.
danpost danpost

2014/2/26

#
I thought you had 'Credits' as an int field in the 'Return' class. Maybe I do not understand what you are trying to do; but, it seems that you may be trying to make this more complicated than it needs to be. Please show the code that you have now (working or not) that you need help with and explain what you are trying to do there.
Hersov Hersov

2014/2/26

#
I am very confused and im sorry for this, ill show you all 3 of the codes which I need help with This is the Return one,
int Credits = 30;
    
    /**
     * Act - do whatever the Return wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int getCredits()
    {
        GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
      
    
    
        return Credits;
    
        
      
        
        
    }    
This is the Single one
   public int Credits = 30;
    /**
     * Act - do whatever the Single wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GreenfootImage textImage = new GreenfootImage ( "Single Ticket" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);

        if (Greenfoot.mouseClicked(this))
        {
            Credits -=3;
            System.out.println(Credits);
        }
    }
And this is the one where I want to print out the value of X. (called "Ticket")
int Credits = 30;
    /**
     * Act - do whatever the Ticket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        
        
      
        GreenfootImage textImage = new GreenfootImage ( "Check Your Balance" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
        
        if(Greenfoot.mouseClicked(this))
        {
            
          System.out.println(Credits);
        }
}
I am very confused and have changed a bit so quite alot of it will be wrong. Thank you so much for your help through all of this
NAZZA NAZZA

2014/2/26

#
j
Hersov Hersov

2014/2/26

#
Who is that ^^ :p
NAZZA NAZZA

2014/2/26

#
sorry, I accidentally posted, (It's ryan). You haven't put in the code from earlier in. I've been doing the same project as Hersov but I've done this:
    /**
     * Act - do whatever the Single wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (!getWorld().getObjects(Return.class).isEmpty())
            {
                Return ret = (Return)getWorld().getObjects(Return.class).get(0);
                ret.getCredits();
            }
        int CreditsINT = Integer.parseInt(Credits);
    }
    public int driversinput()
    {
        String computer = "computer";
        String driverinput = JOptionPane.showInputDialog("Input any string:");
        if (driverinput == computer)
        {
            int Credits= Credits-3;
            return Credits;
        }
    }
danpost danpost

2014/2/26

#
Yes, you sure changed a lot (unfortunately).
// your Return class should have this (remove the 'credits' field
public void act() 
{
    GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);
    setImage(textImage);
    if (Greenfoot.mouseClicked(this))
    {
        if (getWorld().getObjects(Ticket.class).isEmpty()) return;
        Ticket ticket = (Ticket)getWorld().getObjects(Ticket.class).get(0);
        ticket.addCredits(-5);
        System.out.println(""+ticket.getCredits());
    }
}

// your Ticket class should have this
private int credits = 30;

public Ticket()
{
    GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);  
    setImage(textImage);
}

public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        System.out.println(""+credits);
    }
}

public void addCredits(int change)
{
    credits += change;
}

public int getCredits()
{
    return credits;
}

// and your Single class should have this
public Single()
{
    GreenfootImage textImage = new GreenfootImage ( "Single Ticket" ,40, Color.WHITE, Color.BLUE);
    setImage(textImage);
}

public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        if (getWorld().getObjects(Ticket.class).isEmpty()) return;
        Ticket ticket = (Ticket)getWorld().getObjects(Ticket.class).get(0);
        ticket.addCredits(-3);
        System.out.println(""+ticket.getCredits());
    }
}
I put the 'credits' field in the Ticket class because it did not seem right in either of the 'Single' or 'Return' classes.
danpost danpost

2014/2/26

#
@NAZZA, the comparison on line 18 will most probably always return false. Even if the two String match (character-wise), they will invariably be two distinct String objects and therefore not equal. You can use the 'equals' operator to compare the two strings for content:
if (computer.equals(driverinput))
Hersov Hersov

2014/2/26

#
Thank you so much but I had a problem compiling it, when it says:
public void act()  
{  
    if (Greenfoot.mouseClicked(this))  
    {  
        System.out.println(""+credits);  
    }  
}  
the error says that it is "already defined in class ticket,
danpost danpost

2014/2/26

#
Sorry, forgot to move the setting of image in the Return class:
// your Return class should have this (remove the 'credits' field
public Return()
{
    GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);
    setImage(textImage);
}

public void act() 
{
    if (Greenfoot.mouseClicked(this))
    {
        if (getWorld().getObjects(Ticket.class).isEmpty()) return;
        Ticket ticket = (Ticket)getWorld().getObjects(Ticket.class).get(0);
        ticket.addCredits(-5);
        System.out.println(""+ticket.getCredits());
    }
}
danpost danpost

2014/2/26

#
Hersov wrote...
I had a problem compiling it, when it says that it is "already defined in class ticket,
Please post the code for the Ticket class (or if you have two 'public void act()' methods, post both and specify they were both in the same Ticket class).
Hersov Hersov

2014/2/27

#
This is my Ticket Code:
private int credits = 30;  

public Ticket()  
{  
    GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);    
    setImage(textImage);  
}  
  
    public void act() 
    {
        
        
        
      
        GreenfootImage textImage = new GreenfootImage ( "Check Your Balance" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
        
        if(Greenfoot.mouseClicked(this))
        {
            
          System.out.println(Credits);
        }
}

public void act()  
{  
    if (Greenfoot.mouseClicked(this))  
    {  
        System.out.println(""+credits);  
    }  
}  
  
public void addCredits(int change)  
{  
    credits += change;  
}  
  
public int getCredits()  
{  
    return credits;  
}  
Hersov Hersov

2014/2/27

#
Dont worry, I fixed it now, Thanks for your help.
Hersov Hersov

2014/2/27

#
Hi again, I completed it but then someone suggested making a TopUp system. This is what I have at the moment. Yet again it cannot find Credits so Do i need to add the code we used on Single and Return. Also does this make sense or not? If so can you tell me the correct commands.
    public void act() 
    {
        if (Credits<=5)
        {
            int inputstring1 = JOptionPane.showInputDialog("How much would you like to add");
            int inputstring1 + Credits;
        }    
    }
There are more replies on the next page.
1
2
3