Method using JAVA thread and inheritance

Asked

Viewed 94 times

0

I am trying to create a class Balldefutebol that receives attributes of the class father Ball, the class ball possesses the attributes

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

Ball Class(does not have 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;
    }



}

Boladefutebol class(does not have 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();
    }
}

Everything is working, but in this method(stick) of the class Boladefutebol I can not subtract the value of the attribute ar:

    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();
    }

Every 1 second I want the ball to lose air, however, I can not change the value of the variable air, I’m a little rusty with superclasses in java I would like help and know where I am missing.

Test Class(does not have Imports):

public class UsaBola {

    public static void main(String[] args) {

        BolaDeFutebol bola = new BolaDeFutebol();
        bola.Mostrar();
        bola.Furar();
    }

}

1 answer

1


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

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

Your drill() 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 stopping condition of the loop for <=, and also the increment (--ar), for the percentage display to occur correctly both at the beginning of the hole and at the end.

The output of your show:

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’s any doubt, just ask.

  • 1

    Thank you so much your answer helped me a lot to understand, I learned a lot from her

Browser other questions tagged

You are not signed in. Login or sign up in order to post.