Change daughter class output in a time counter

Asked

Viewed 31 times

0

Good morning, I’m trying to create a basketball scoreboard to start learning programming in java/android studio.

I have created a class for the game time clock called timer that is working very well. Now I want to extend this class to create the shootclock (it is the clock that counts the possession of the ball, it rotates along with the scoreboard, but has only 24 seconds.)

Since the whole implementation is the same, I decided to extend it to the Shootclock() class and just override the visualization method and the constructor so it has only 24 seconds. However, I am trying to create an override for the update methodContagem(){}, which displays the time on the scoreboard, but I cannot pull the mother class variables.

That’s the mother class method.

public void atualizarContagem() {
          int minutes = (int) (tempoAteAcabar / 1000) / 60;
          int seconds = (int) (tempoAteAcabar / 1000) % 60;

         tempoAteAcabarFormatado = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
         tempoPartida.setText(tempoAteAcabarFormatado);
    }

At first I thought I could solve by just changing the code. But obviously I didn’t understand how the syntax works at the time

public void atualizarContagem() {
          int seconds = (int) (super.tempoAteAcabar / 1000) % 60;

         tempoAteAcabarFormatado = String.format(Locale.getDefault(),"%02d:%02d", seconds);
         tempoPartida.setText(tempoAteAcabarFormatado);
    }
  • 1

    I didn’t quite understand your problem, but at first, the Timer class should (or should) have a method, I don’t know, countdown(int seconds), where you would spend the amount of seconds. Then just pass the equivalent in seconds for the duration of the match (or the fourth) and, in another call, the 24 seconds of possession of the ball. All this to say that you do not need to create different objects and logics to deal with apparently two identical problems.

  • I fully agree with @Statelessdev. You don’t need to have an accountant for everything, a generic accountant already solves everything.

1 answer

0


Okay if I understand correctly, your problem is that you can’t access the variables tempoAteAcabarFormatado, and tempoPartida correct ? If the answer is yes, in its main (inherited) class, these variaves probably should look like this:

private String tempoAteAcabarFormatado;
private VARIAVEL tempoPartida;

To be able to access them you must have the getters and Setter methods. Your IDE should ta show them to you but it is not letting you operate them because you should not have getters/setters.

You can also use the LOMBOK to generate it, but if you are learning not recommend using framework or plugins.

So your methods would look like this...

public void atualizarContagem() {
      int seconds = (int) (super.tempoAteAcabar / 1000) % 60;

     setTempoAteAcabarFormatado(String.format(Locale.getDefault(),"%02d:%02d", seconds));
     getTempoPartida().setText(getTempoAteAcabarFormatado());
}

Browser other questions tagged

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