You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java初学者求助:健身应用中Fitness-Equipment类构建问题

Hey there! As a fellow Java learner who’s tackled university course projects before, I totally get how frustrating vague requirements can be—especially when you’re flying mostly solo on the code. Let’s walk through building your FitnessEquipment class properly, including the linked TrainedMuscleGroup class, and add some practical extras that’ll set you up for future requirements.


Step 1: First, Build the TrainedMuscleGroup Class

Since your FitnessEquipment depends on this class, we’ll start here. This class should represent a specific muscle group (like "Quadriceps" or "Latissimus Dorsi")—I’ll add a basic description field too, which might come in handy later for your app.

public class TrainedMuscleGroup {
    // Private fields follow encapsulation best practices
    private String groupName;
    private String description;

    // Constructor to initialize the muscle group
    public TrainedMuscleGroup(String groupName, String description) {
        this.groupName = groupName;
        this.description = description;
    }

    // Getters and setters to access/update fields (since they're private)
    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    // Optional: toString() method to print readable info about the muscle group
    @Override
    public String toString() {
        return "TrainedMuscleGroup{" +
                "groupName='" + groupName + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

Step 2: Create the FitnessEquipment Class

I’ve adjusted your attribute names to follow standard Java naming conventions (no dots, use camelCase instead of spaces/dots—this is super important for readability and compatibility). Here’s the implementation with all your required fields, plus useful methods:

public class FitnessEquipment {
    // Required attributes (private for encapsulation)
    private String name;
    private String type; // e.g., "Cardio", "Strength", "Flexibility"
    private TrainedMuscleGroup trainedMuscleGroup;
    private int maxTrainingDuration; // In minutes, or adjust to hours if needed
    private double burnedCaloriesPerHour;

    // Constructor to initialize all fields at once
    public FitnessEquipment(String name, String type, TrainedMuscleGroup trainedMuscleGroup, int maxTrainingDuration, double burnedCaloriesPerHour) {
        this.name = name;
        this.type = type;
        this.trainedMuscleGroup = trainedMuscleGroup;
        this.maxTrainingDuration = maxTrainingDuration;
        this.burnedCaloriesPerHour = burnedCaloriesPerHour;
    }

    // Getters and setters for all fields
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public TrainedMuscleGroup getTrainedMuscleGroup() {
        return trainedMuscleGroup;
    }

    public void setTrainedMuscleGroup(TrainedMuscleGroup trainedMuscleGroup) {
        this.trainedMuscleGroup = trainedMuscleGroup;
    }

    public int getMaxTrainingDuration() {
        return maxTrainingDuration;
    }

    public void setMaxTrainingDuration(int maxTrainingDuration) {
        this.maxTrainingDuration = maxTrainingDuration;
    }

    public double getBurnedCaloriesPerHour() {
        return burnedCaloriesPerHour;
    }

    public void setBurnedCaloriesPerHour(double burnedCaloriesPerHour) {
        this.burnedCaloriesPerHour = burnedCaloriesPerHour;
    }

    // Practical method: Calculate total calories burned for a given session duration
    // Adds real functionality to your class for the fitness app
    public double calculateTotalBurnedCalories(double sessionDurationHours) {
        // Make sure we don't exceed the max allowed training duration
        if (sessionDurationHours > maxTrainingDuration / 60.0) {
            System.out.println("Warning: Session duration exceeds maximum allowed for this equipment.");
            return burnedCaloriesPerHour * (maxTrainingDuration / 60.0);
        }
        return burnedCaloriesPerHour * sessionDurationHours;
    }

    // Optional: toString() method for easy debugging/printing
    @Override
    public String toString() {
        return "FitnessEquipment{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", trainedMuscleGroup=" + trainedMuscleGroup +
                ", maxTrainingDuration=" + maxTrainingDuration + " minutes" +
                ", burnedCaloriesPerHour=" + burnedCaloriesPerHour +
                '}';
    }
}

Quick Usage Example

To make sure you know how to use these classes, here’s a simple main method snippet:

public class Main {
    public static void main(String[] args) {
        // Create a muscle group first
        TrainedMuscleGroup quads = new TrainedMuscleGroup("Quadriceps", "Large muscle group in the front of the thigh");

        // Create a piece of fitness equipment
        FitnessEquipment legPress = new FitnessEquipment("Leg Press Machine", "Strength", quads, 30, 250.0);

        // Calculate calories burned for a 20-minute session
        double calories = legPress.calculateTotalBurnedCalories(20.0 / 60.0);
        System.out.println("Total calories burned: " + calories);

        // Print the equipment details
        System.out.println(legPress);
    }
}

Tips for Future Requirements

  • Encapsulation: Keeping fields private and using getters/setters lets you control how data is accessed/modified—this will save you headaches if you need to add validation later (e.g., ensuring max training duration isn’t negative).
  • Extensibility: If you later need to add different types of equipment (like cardio vs strength), consider using inheritance or interfaces.
  • Validation: Add checks in setters/constructors (e.g., make sure burnedCaloriesPerHour isn’t negative) to keep your data consistent.

内容的提问来源于stack exchange,提问作者dbljn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:57:31