7. Method

7.3. Practical Examples

double getHeightInMeters() {

    return height * 0.3048; // Expression with arithmetic operator

}

 

// Method to describe the person

String getDescription() {

    return name + " is " + age + " years old.";

}

}

public class BasicOOPConcepts { public static void main(String[] args) { // Create object Person person = new Person("Alice", 25, 5.9, 'A');

    // Use attributes and methods

    int yearsUntilRetirement = 65 - person.age; // Expression with arithmetic operator

    double heightInMeters = person.getHeightInMeters(); // Call method

 

    // Output results

    System.out.println("Person Details:");

    System.out.printf("Name: %s%n", person.name);

    System.out.printf("Age: %d%n", person.age);

    System.out.printf("Height: %.1f feet (%.2f meters)%n", person.height, heightInMeters);

    System.out.printf("Initial: %c%n", person.initial);

    System.out.printf("Is adult? %b%n", person.isAdult);

    System.out.printf("Description: %s%n", person.getDescription());

    System.out.printf("Years until retirement: %d%n", yearsUntilRetirement);

    person.sayHello();

}

}