-4

So I have created a ArrayList within a GrownUp class that then produces a for loop to call a method from within another class. When I'm trying to call the object of this new method, I am getting an error that it is not recognising the object. This is what the method looks like:

public void personShowering()
{
    PowerShower callshower = new PowerShower(1,1,1,1);

if(people.size()>1)
callshower.shower(people.get(0));
    }

}

Person

   import java.util.ArrayList;

public abstract class Person 
{    
    ArrayList<Person> people;


Person(int age, String name) 
{


}
public void shower(Person x)
{
    people.get(0).shower(//what goes here?);
}

}

Error:

method shwer() in class perosn cannot be applied to given types;
required: Person
found: no given types

The callshower should be refering to a class called PowerShower which has been created using PowerShower callshower = new PowerShower(1,1,1,1); so I'm confused as to why it's looking in the Person class? The PowerShower class is quite big but I will post it:

PowerShower

public class PowerShower extends Shower
{

    public int isOn = -1;
    public int isOff = 0;
    public int incrementTime;
    public int varPass = -1;

     @Override
     public int currentState()
    {

        if (varPass == 0)
        return isOff;
        else
        {
            return isOn;
        }
        //returns isOn;
}

    @Override
        public void useTime(int defaultTime)
        {

            defaultTime = 15;
            incrementTime = 1;

        }

    @Override
        public void shower()
        {

            PowerShower shower = new PowerShower(1,1,1,1);

            shower.timePasses();
        }

        @Override
         public void timePasses()
         {

             if(varPass == isOff)
                 varPass = 0;
             else
             {

             GasMeter.getInstance().incrementConsumed(waterUse);  
             GasMeter.getInstance().incrementConsumed(10);     
             int gasconsumed = GasMeter.getInstance().getGasUsed();    

             WaterMeter.getInstance().incrementConsumed(waterUse);  
             WaterMeter.getInstance().incrementConsumed(5);     
             int waterconsumed = WaterMeter.getInstance().getWaterUsed();

             System.out.println("Power shower water consumption = " + waterconsumed);
             System.out.println("Power shower gas consumption = " + gasconsumed);

             }

         }

    PowerShower(int electricityUse, int gasUse, int waterUse, int timeOn)  
{
    super(electricityUse, gasUse, waterUse, timeOn);

    this.electricityUse = 0 * incrementTime;
    this.gasUse = 10 * incrementTime; //10x1;
    this.waterUse = 5 * incrementTime; //5x1;
    this.timeOn = 15 * incrementTime;

} 

}

I'm not too sure why i am getting this error because I have created a object of that class and called it but it doesn't seem to be recognizing that object? Any is would be great, thanks.

Harry
  • 109
  • 1
  • 8
  • 1
    And the error is... – JB Nizet Dec 03 '16 at 08:43
  • `cannot find symbol` – Harry Dec 03 '16 at 08:45
  • 1
    Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Dec 03 '16 at 08:47
  • No, that's only 3 words of the complete error message you get. Jeez, you're making it very hard to help you. – JB Nizet Dec 03 '16 at 08:47
  • `symbol: variable callshower, location:: class Person`. Sorry, wasn't doing it on purpose. – Harry Dec 03 '16 at 08:51
  • Please include the complete error *in the question*, and please always format your code so it's more readable. (Another user has done it now, but please do it yourself in future.) I also strongly suspect that you could reduce this to a shorter example - a [mcve] is always preferable. It doesn't help that you haven't shown us the `Person` or `PowerShower` types... – Jon Skeet Dec 03 '16 at 08:52
  • OK. So there is no variable named `callshower` in the class `Person`. That's what the error, which should be in the question itself, is telling you. You haven't posted the class Person, so... – JB Nizet Dec 03 '16 at 08:53

1 Answers1

2

What you are trying to do is not possible. I don't know why you believe it could be possible. This is not how multiple-inheritance could look like which is not supported by Java except for interfaces and default methods. If it was possible it would rather look like this (WON'T COMPILE):

public class GrownUp extends Person, SuperShower
.... 
people.get(0).shower()

You can call a method of an object. What you can't do is this:

people.get(0).callshower

A Person probably has no method (or field) callshower. callshower is a variable containing an object. You can call the method shower on it if existing.

It seems that you want to put the person and to have a shower together. What you can do is:

  • Add a method shower to Person and call it: people.get(0).shower()

  • Change PowerShower.shower() to have a parameter Person and then do the following call: callshower.shower(people.get(0))

mm759
  • 1,404
  • 1
  • 9
  • 7
  • I've updated my question, eveything works apart form one error. Thanks for your feedback. – Harry Dec 03 '16 at 09:13