REPORT Z_PRIMENUMBER.

CLASS ZCL_NTH_PRIME DEFINITION FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    METHODS prime
      IMPORTING
        input         TYPE i
      RETURNING
        VALUE(result) TYPE i
      RAISING
        cx_parameter_invalid.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS ZCL_NTH_PRIME IMPLEMENTATION.
  METHOD prime.
    IF input = 0.
      RAISE EXCEPTION TYPE cx_parameter_invalid.
    ENDIF.

    DATA prime_number TYPE i.
    DATA prime_count TYPE i.
    prime_number = 2.
    prime_count = 0.

    DO.
      IF check_prime(prime_number) = abap_true.
        prime_count = prime_count + 1.
      ENDIF.

      IF prime_count = input.
        result = prime_number.
        EXIT.
      ENDIF.

      prime_number = prime_number + 1.
    ENDDO.
  ENDMETHOD.

  METHOD check_prime.
    DATA number TYPE i.
    DATA is_prime TYPE abap_bool.
    number = 2.
    is_prime = abap_true.

    DO number = 2 TO sqrt(input).
      IF input MOD number = 0.
        is_prime = abap_false.
        EXIT.
      ENDIF.
    ENDDO.

    result = is_prime.
  ENDMETHOD.
ENDCLASS.

DATA: nth_prime TYPE i,
      result TYPE i.

nth_prime = 10.

DATA: prime_calculator TYPE REF TO ZCL_NTH_PRIME.

CREATE OBJECT prime_calculator.

TRY.
    prime_calculator->prime( nth_prime )->result.
    result = nth_prime.
    WRITE: / 'The', nth_prime, 'th prime number is', result.
CATCH cx_parameter_invalid INTO DATA(exception).
    WRITE: / 'Error:', exception->get_text( ).
ENDTRY.

It's showing errors:

"Field "CHECK_PRIME" is unknown" at this line: IF check_prime(prime_number) = abap_true.

"The method "CHECK_PRIME" is not declared or inherited in class "ZCL_NTH_PRIME" at this line: METHOD check_prime

I was expecting that this code will show me the prime numbers after nth number. The errors at specific lines are not solving I am trying for 4 days and its still the same I have done everything maybe I have less experience but can someone suggest me to solve this error.

1

There are 1 best solutions below

0
Philipp On

The error message The method "CHECK_PRIME" is not declared or inherited in class "ZCL_NTH_PRIME" means exactly what is says. You are trying to implement and call a method you didn't declare between CLASS ZCL_NTH_PRIME DEFINITION and the corresponding ENCLASS..

You have a method that is called prime in your class definition, but not a method called check_prime. So what you need to do is add the declaration for that second method. Guessing from context and from the undeclared variables you are using in the implementation of that method, I would say that it would have to look something like this:

METHODS check_prime
  IMPORTING
    input         TYPE i
  RETURNING
    VALUE(result) TYPE abap_bool.

Where exactly does that definition belong? Considering that the method doesn't seem to get called from outside the class, it doesn't need to be in the PUBLIC SECTION. Considering that the class is FINAL, there can't be any class that inherits from it. Which means that there is no point in putting anything into the PROTECTED SECTION. So you should probably put it into the PRIVATE SECTION.