Method using thread and JAVA inheritance

0

I'm trying to create a class BallField that receives attributes from the parent class Ball, the ball class has the attributes

private int ar = 100; //100%
private boolean furada = false;

Ball class (no imports):

public class Bola {

    private int ar = 100;
    private boolean furada = false;


    public Bola() {
    }


    public Bola(int ar, boolean furada) {
        this.ar = ar;
        this.furada = furada;
    }


    public int getAr() {
        return ar;
    }
    public void setAr(int ar) {
        this.ar = ar;
    }
    public boolean isFurada() {
        return furada;
    }
    public void setFurada(boolean furada) {
        this.furada = furada;
    }



}

FootballBall class (no imports):

public class BolaDeFutebol extends Bola{

    private String material = "Capotão";
    private String esporte = "Futebol";

    public BolaDeFutebol(String material, String esporte) {
        super();
        this.material = material;
        this.esporte = esporte;
    }

    public BolaDeFutebol() {
    }


    public String getMaterial() {
        return this.material;
    }
    public void setMaterial(String material) {
        this.material = material;
    }
    public String getEsporte() {
        return this.esporte;
    }
    public void setEsporte(String esporte) {
        this.esporte = esporte;
    }


    public void Mostrar(){
        System.out.println("Quantidade de ar: " + super.getAr() + "%");
        System.out.println("Furada status: " + super.isFurada());
        System.out.println("Material: " + getMaterial());
        System.out.println("Esporte: " + getEsporte());
    }

    public void Mostrar(int ar){
        System.out.println("Quantidade de ar: " + super.getAr() + "%");
    }


    public void Furar(){
        setFurada(true);
        int ar = getAr();
        new Thread(){
            @Override
            public void run() {
                for(int i = 0; i < ar; setAr(ar--)){
                    try {
                        Mostrar(ar);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    }
                }
                super.run();
            }
        }.start();
    }
}

It's all working, but in this method (stick) of the FootballBall class I can not subtract the value from the air attribute:

    public void Furar(){
        setFurada(true);
        int ar = getAr();
        new Thread(){
            @Override
            public void run() {
                for(int i = 0; i < ar; setAr(ar--)){
                    try {
                        Mostrar(ar);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    }
                }
                super.run();
            }
        }.start();
    }

asked by anonymous 08.10.2018 / 01:28

1 answer

1

In anonymous classes, such as that implicitly created by thread , the variables must be final , that is, constants. For this reason, the compiler does not allow you to change the ar variable.

For your program to work, just take this thread creation, which is unnecessary, and call only the Thread.sleep() , which will run on top of the main thread in which your program runs:

Your stick () method looks like this:

public void Furar() throws InterruptedException{
    setFurada(true);
    int ar = getAr();
    for (int i = 0; i <= ar; setAr(--ar)) {
        Mostrar(ar);
        Thread.sleep(1000);
    }
}

In addition to eliminating the creation of threads , I changed the loop stop condition to <= , as well as the (--ar) increment, so that percentage occurred correctly both at the beginning of the hole and at the end.

The output of your program looks like this:

Quantidade de ar: 100%
Furada status: false
Material: Capotão
Esporte: Futebol
Quantidade de ar: 100%
Quantidade de ar: 99%
Quantidade de ar: 98%
Quantidade de ar: 97%
[...]
Quantidade de ar: 2%
Quantidade de ar: 1%
Quantidade de ar: 0%

If there is doubt, just ask.

    
08.10.2018 / 13:31