简体   繁体   中英

Creating classes and methods in Java

This is my first post here, so I would be glad, if no one will be angry because of my question and hopefully because of my English...it is not my native language :D

To the question: For my course in the university, I have to program a fitness application (it will become much bigger in the future time). For now, I have to set up some classes and methods, which I already did, as good as I could. Here is an example:

public class Trainingsession {

    private int workoutTime;
    private int date;
    private int startingtime;
    private Fitnessequipment fitnessequipment;
    private int neededWorkoutTime;


    public Trainingsession(int workoutTime, int date, int startingtime, Fitnessequipment fitnessequipment) {
        this.workoutTime = workoutTime;
        this.date = date;
        this.startingtime = startingtime;
    }

    public boolean goalReached (int caloryGoal) {
        return (fitnessequipment.caloryConsumption(workoutTime) >= caloryGoal))
    }

    // ...

}

When I'm setting up my method goalReached , do I have to set up the caloryGoal at first, together with my other attributes like date , startingtime etc.? My problem is, that some attributes are given, but I dont know, if I have to add other attributes just by looking at the methods...

As a general answer to your question:

caloryGoal is a parameter, it doesn't need to be "set up", it's supplied by the caller of the method.

What you need to ensure is that everything required by the fitnessequipment.caloryConsumption is supplied ( Fitnessequipment is in a valid state, for example), to make the method work.

As a rule of thumb, it's good practise to not allow creation of an object that will result in RuntimeExceptions when its methods are called. Put yourself in the position of someone using your API who didn't write it, they're not going to know which fields are mandatory unless they're required in the constructor.

I'm hoping I'm not misinterpreting your question, but here goes:

caloryGoal is an argument passed in by whatever is calling goalReached() , so caloryGoal is not something that you need to initialize in the constructor. If you have values in the object that you need to specify after calling the constructor (like if you learn what the value is supposed to be after instantiating it), you can use setter methods. Here's an example:

public void setWorkoutTime(int newWorkoutTime) {
    workoutTime = newWorkoutTime;
}

After calling this method, workoutTime will have the value of whatever you passed in as newWorkoutTime .

It is a little not clear what is the purpose of the functions you described. If I understood correctly , then goalReached is a function that receive a number and returns true iff caloryGoal >= calories that were burned in this session (this instance). Therefor it should look somewhat like this:

public boolean goalReached (int caloryGoal) {
    return (this.caloryConsumption(workoutTime) >= caloryGoal))
}

when caloryConsumption will be a function that returns an instance calories burned per session. For this end I whould have added a new filed called CaloriesPerMinute which will indicate how many calories the exercise burn per minute and update the constructor. Then in CaloriesPerMinute I would add the caculation. Basically you need to know what you want. maybe writing contracts will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM