Hello,

What I want to do is reset the pig's position to 0 and it's colour to pink. I don't how if I amended the the constructor correctly so the final line sends a resetCount() message to the newly created Animal object. The above code is not working because nothing happens. Pig object is sitting on position 0 with different colour. When I execute the code it should be on position 1, colour red then move to position 0, colour pink. All I want to know is how to use the reset method and if I should be using the loop.

Where am I going wrong?

Thanks in advance.

public class Farm
{
/* instance variable */
private Animal pig;

/*Constructor*/
public Amplifier(Pig aPig)
{
   this.number.resetCount = 0;
}


public void resetCount()
  {
    while (this.pig.getPosition() > 1)
    {
      this.pig.setColour(Colour.ORANGE);  
    }  
  }
}

First of all the this.pig is null. You declare it as attribute at the class but you never give it value. You need to pass the parameter of the constructor to it:

public Amplifier(Pig aPig)
{
   this.pig = aPig;
   this.number.resetCount = 0;
}

Also in the reset method you check for the position but you never change it. If this.pig.getPosition() is greater than 1 then you will enter the loop, the color will change but you don't change the position. So the expression: this.pig.getPosition() > 1 will not change and you will never exit the loop.
Better try an if statement and change both these values to 0 and pink. Also there is a difference between saying: > 1 and >=1

I tried to use the if statement as suggest but still not working, meaning nothing happens. Are you saying I should not use [I]this.pig[/I]

if (getPosition() > 1)
     {
       this.pig.setPosition(0);
       this.pig.setColour(Colour.PINK);

     }

I tried to use the if statement as suggest but still not working, meaning nothing happens. Also when I execute, the colour is value is showing NULL, position 6 and reset value 0. So really the values I wish to reset (the resetIndicator()) are not passing over to the new object that I have created (thats what's the problem is). When I change the value to 6, in the this.number.resetCount = 6;, still not working. What else do I need to do?

Below are two coding I have tried. I assume the below code is not really the problem.

public void resetCount()
{
   if (getPosition() > 1)
     {
       this.pig.setPosition(0);
       this.pig.setColour(Colour.PINK);

     }
}

I also tried (not really I in favor of)...

if (pig.getPosition() > 1) 
      {
        int count = pig.getPosition() - 1;
        for (int x = 0; x < count; x++)
        {
           pig.left();
        }
      }
      pig.setColour(Colour.PINK);

note: please copy your program to another file and try this:

if you want to reset the number of the aPig.. all you have to do is rewrite the resetCount()
#
#
this.number.resetCount = 0;


you do not need the while loop

you can also call the applifier

Then print the value of the method. You say that the if doesn't work, but do you know what value does the getPosition() returns?

Also post your entire code using code tags. Press the (code) button when creating a post and put code inside.

when you say print the value method do you mean println, I am using graphical objects. The actual work I am doing is different. I don't really want to give the whole code out. I will check on the return value of the getPosition.

when you say print the value method do you mean println, I am using graphical objects. The actual work I am doing is different. I don't really want to give the whole code out. I will check on the return value of the getPosition.

It doesn't matter. The print is for debugging just for you to see what value it has, in order to understand what is going on.
And yes I mean: System.out.print....

print display the following message...

An instance of class Pig: position 1, colour Colour.RED.

When it should really be in position 0, colour PINK.

print display the following message...

An instance of class Pig: position 1, colour Colour.RED.

When it should really be in position 0, colour PINK.

Can you post the latest code of what you can post.

I think I should first show you the question I am working on...

Create public instance method called reset(). The method takes no arguments and returns no result. The method should move the pig directly to the colour plate to the left of the first black stone and update its colour accordingly.

Alter the constructor for the Animal class so that the final line sends a resetCount() message to the newly created Animal object.

I have also created instance variables in the class...

public class Animal
private Pig piggy;
private int reset;
private int position;

/*Constructor of the object Animal*/
public Animal(Pig aPig)
this.position = 0;
this.reset = 0;

public void resetCount()
{

if (this.pig.getPosition() > 1)
{
this.pig.setPosition(0);
this.pig.setColour(Colour.PINK);
}
}

Could you claify if I done the reset method wrong or am I doing something wrong in the constructor or instance variables in the class.

I already explained the the piggy attribute is null because you never set its value, with the argument of the constructor:

public class Animal
private Pig piggy;
private int reset;
private int position;

/*Constructor of the object Animal*/
public Animal(Pig aPig) {
this.position = 0;
this.reset = 0;
piggy = aPig
}

public void resetCount()
{
 // do whatever the assignment says
 // change the position
 pig.setPosition(0);
 // change the color
 pig.setColour(Colour.PINK);
}

I think this is known as " Calling an Object's Methods". OK I need to look this up on various books etc. I may need couple of days to sort this out. thanks for your help.

In the constructor if I incude the following syntax

/*Constructor of the object Animal*/
public Animal(Pig aPig)
{
this.piggy.setColour(Colour.PINK);
this.piggy.setPosition(0);
this.piggy = aPig;
}

This will move the object to the position 0 and change it's colour to pink, regardless if I have the following method

public void resetCount()
{
if (this.pig.getPosition() > 1)
{
this.piggy.setPosition(0);
this.piggy.setColour(Colour.PINK);
}
}

If I am wrong can you not show me an example how method can change the value in the constructor. Or on what you are saying I have not set the value to the piggy with the argument of the constructor.

{
// piggy is NULL
this.piggy.setColour(Colour.PINK); // error

// piggy is NULL
this.piggy.setPosition(0); // error

// now you give piggy value.
this.piggy = aPig;
}

First you give value to an object or create it and then you use its methods

the default setting is position 6, colour red. Do you mean

this.piggy = aPig;
this.piggy.setColour(Colour.PINK);
this.piggy.setPosition(0);

sorted after a sort break. cheers JavaAddict

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.