w3resource

Java Program for GymMembership and PremiumMembership Classes


Write a Java program to create a class called "GymMembership" with attributes for member name, membership type, and duration. Create a subclass "PremiumMembership" that adds attributes for personal trainer availability and spa access. Implement methods to calculate membership fees and check for special offers based on membership type.

Sample Solution:

Java Code:

GymMembership.java

// Define the GymMembership class
class GymMembership {
    // Attributes for GymMembership
    String memberName;
    String membershipType;
    int duration; // in months

    // Constructor for GymMembership
    public GymMembership(String memberName, String membershipType, int duration) {
        this.memberName = memberName;
        this.membershipType = membershipType;
        this.duration = duration;
    }

    // Method to calculate membership fees
    public double calculateFees() {
        double baseFee = 50.0; // Base fee per month
        return baseFee * duration;
    }

    // Method to check for special offers
    public String checkSpecialOffers() {
        if (membershipType.equalsIgnoreCase("annual")) {
            return "10% discount on annual membership.";
        } else {
            return "No special offers available.";
        }
    }

    // Method to display membership details
    public void displayDetails() {
        System.out.println("Member Name: " + memberName);
        System.out.println("Membership Type: " + membershipType);
        System.out.println("Duration: " + duration + " months");
        System.out.println("Membership Fees: $" + calculateFees());
        System.out.println("Special Offers: " + checkSpecialOffers());
    }
}

Explanation:

  • Class definition: Defines the GymMembership class.
  • Attributes: Declares three attributes: memberName (String), membershipType (String), and duration (int, in months).
  • Constructor: Initializes the memberName, membershipType, and duration attributes with provided values.
  • calculateFees() method:
    • Sets a base fee of $50.0 per month.
    • Returns the total fee by multiplying the base fee with the duration in months.
  • checkSpecialOffers() method:
    • Checks if the membership type is "annual".
    • Returns a 10% discount message if the membership type is annual.
    • Returns a message indicating no special offers for other membership types.
  • displayDetails() method:
    • Prints the member's name, membership type, and duration.
    • Calls calculateFees() to print the total membership fees.
    • Calls checkSpecialOffers() to print any applicable special offers.

PremiumMembership.java

// Define the PremiumMembership class that extends GymMembership
class PremiumMembership extends GymMembership {
    // Additional attributes for PremiumMembership
    boolean personalTrainerAvailable;
    boolean spaAccess;

    // Constructor for PremiumMembership
    public PremiumMembership(String memberName, String membershipType, int duration, boolean personalTrainerAvailable, boolean spaAccess) {
        super(memberName, membershipType, duration);
        this.personalTrainerAvailable = personalTrainerAvailable;
        this.spaAccess = spaAccess;
    }

    // Override the calculateFees method to include additional costs
    @Override
    public double calculateFees() {
        double baseFee = super.calculateFees();
        double additionalFee = 0.0;

        if (personalTrainerAvailable) {
            additionalFee += 30.0 * duration; // Additional fee per month for personal trainer
        }
        if (spaAccess) {
            additionalFee += 20.0 * duration; // Additional fee per month for spa access
        }
        return baseFee + additionalFee;
    }

    // Override the displayDetails method to include premium details
    @Override
    public void displayDetails() {
        super.displayDetails();
        System.out.println("Personal Trainer Available: " + (personalTrainerAvailable ? "Yes" : "No"));
        System.out.println("Spa Access: " + (spaAccess ? "Yes" : "No"));
    }
}

Explanation:

  • Class definition: Defines the PremiumMembership class, which extends GymMembership.
  • Additional attributes: Declares two attributes: personalTrainerAvailable (boolean) and spaAccess (boolean).
  • Constructor:
    • Calls the superclass (GymMembership) constructor to initialize memberName, membershipType, and duration.
    • Initializes personalTrainerAvailable and spaAccess attributes with provided values.
  • Override calculateFees() method:
    • Calls the superclass method super.calculateFees() to get the base fee.
    • Adds additional fees if personalTrainerAvailable and spaAccess are true.
    • Returns the total fee, which includes the base fee and any additional fees.
  • Override displayDetails() method:
    • Calls the superclass method super.displayDetails() to display base details.
    • Adds lines to print whether a personal trainer is available and whether spa access is available.

Main.java

// Main class to test the GymMembership and PremiumMembership classes
public class Main {
    public static void main(String[] args) {
        // Create an instance of GymMembership
        GymMembership basicMember = new GymMembership("Njeri Inka", "Monthly", 6);

        // Create an instance of PremiumMembership
        PremiumMembership premiumMember = new PremiumMembership("Willy Diantha", "Annual", 12, true, true);

        // Display details of the basic membership
        System.out.println("Basic Membership Details:");
        basicMember.displayDetails();

        // Display details of the premium membership
        System.out.println("\nPremium Membership Details:");
        premiumMember.displayDetails();
    }
}

Explanation:

  • Class definition: Defines the Main class to test GymMembership and PremiumMembership classes.
  • main method: Entry point of the program.
    • Create an instance of GymMembership:
      • Instantiates a GymMembership object named basicMember.
      • Initializes with memberName as "Njeri Inka", membershipType as "Monthly", and duration as 6 months.
    • Create an instance of PremiumMembership:
      • Instantiates a PremiumMembership object named premiumMember.
      • Initializes with memberName as "Willy Diantha", membershipType as "Annual", duration as 12 months, personalTrainerAvailable as true, and spaAccess as true.
    • Display details of the basic membership:
      • Prints "Basic Membership Details:" to the console.
      • Calls basicMember.displayDetails() to print the details of the basic membership.
    • Display details of the premium membership:
      • Prints "\nPremium Membership Details:" to the console.
      • Calls premiumMember.displayDetails() to print the details of the premium membership.

Output:

Basic Membership Details:
Member Name: Njeri Inka
Membership Type: Monthly
Duration: 6 months
Membership Fees: $300.0
Special Offers: No special offers available.

Premium Membership Details:
Member Name: Willy Diantha
Membership Type: Annual
Duration: 12 months
Membership Fees: $1200.0
Special Offers: 10% discount on annual membership.
Personal Trainer Available: Yes
Spa Access: Yes

For more Practice: Solve these Related Problems:

  • Write a Java program where the "GymMembership" class calculates membership renewal discounts based on years of membership.
  • Write a Java program to modify the "PremiumMembership" subclass to track and limit the number of personal training sessions.
  • Write a Java program where the "GymMembership" class automatically pauses memberships if the gym is temporarily closed.
  • Write a Java program to implement a method that tracks gym attendance and rewards frequent visitors.

Go to:


Java Code Editor:

Improve this sample solution and post your code through Disqus.

PREV : Create Java Classes for Pets with Dog and Bird Subclasses.
NEXT : Java Exception Handling Exercises Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.