3

I have slight problems in understanding two little tasks. I try to be specific as I can but I have to translate everything from German, so keep this please in mind. The task is to program a sort of "training-game" where you can build training-sessions and so on. My first task is to implement an interface which is filled with three different methods:

  • void setCaloryGoal (int caloryGoal) -> sets the caloryGoal
  • int getCaloryGoal -> returns the caloryGoal
  • double getAchievementDegree -> returns the achievementdegree (burnedcalories / caloryGoal)

Than we have to implement a new class TrainingsessionWithGoal where you use the constructor of the superclass Trainingsession but you have to add the caloryGoal and I have no idea, how I can implement it from the interface (I am sure, that we have to implement it with the set-method. My current code is this:

public TrainingsessionWithGoal(int trainingtime, Fitnessdevice fitnessdevice, int day, int month, int year, int hh, int mm, int caloryGoal) {
    super(trainingtime, fitnessdevice, day, month, year, hh, mm);
    this.caloryGoal = caloryGoal;
}

This totally works, but I am pretty sure, that the way I implemented the caloryGoal is not the correct way, because I never use the set-method from the interface. Does anyone have an idea for that?

In the other task, we have to create a new class TrainingProgramm in which we implement an arraylist which is filled with different objects of the class Trainingsession and the new class TrainingsessionWithGoal) and there I have absolutely no idea how to process. I googled everything but couldnt find something that helps me.

This is what I have so far:

private ArrayList <Objects> list = new ArrayList <Objects> ();

public TrainingProgramm (String programmName, ArrayList<Objects> list) {
    this.programmName = programmName;
    this.list = list;
}

but that obviously doesnt work. It would be very helpful if someone can guide me to the right way.

10
  • 2
    Does Trainingsession already implement that interface? If not, then TrainingsessionWithGoal must be declared as implementing that interface, which will force you to implement those three methods. That could be the place to start. Commented May 28, 2018 at 10:55
  • I guess that this.kalorienZiel = kalorienZiel; should actually be this.caloryGoal = caloryGoal; Commented May 28, 2018 at 10:56
  • Yes, I already implemented the interface in TrainingsessionWithGoal with this: @Override public void setKalorienZiel(int kalorienZiel) { this.kalorienZiel = kalorienZiel; } but I dont think, that this is the solution either. Commented May 28, 2018 at 10:57
  • 1
    @Lino yes thats true, I forgot to translate this little line :D Commented May 28, 2018 at 10:59
  • but I am pretty sure, that the way I implemented the caloryGoal is not the correct way. This is pretty opinion-based, but I think you did it the right way. As long as caloryGoal is not final in TrainingsessionWithGoal the user of your class can still use the setter to change the value of caloryGoal Commented May 28, 2018 at 11:01

1 Answer 1

1

You could, as suggested by Lino, change the ArrayList<Object> to ArrayList<? extends Trainingsession>. Even better you use your (however named) Interface for this. And type of the field and the parameter of the method should be a List, not an ArrayList. And those should be named properly - instead of list give them more semantic names. That way you would have sth. like this:

private List<YourInterface> sessions = new ArrayList <YourInterface> ();
public TrainingProgramm (String programmName, List<TrainingSessionInterface> sessions) { ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.