Friday, 29 September 2017

Implement Array Iterator

Array Iterator is used to traverse elements of the array. Implement Array Iterator such that, it should satisfy below requirements.

a.   Array Iterator should take starting offset (offset=0 represents start the iteration from 0th index element) and number of elements to iterate.
b.   Array Iterator should not support removal of elements.
c.   Array Iterator throws exception if offset + numberOfElementsToIterate > number of elements in array
d.   Array Iterator should be generic, it should take any kind of reference type.
e.   Throw PreconditionFailedException on failure of preconditions on array iterator initialization.

PreconditionFailedException.java
package com.sample.exception;

public class PreconditionFailedException extends Exception {

 private static final long serialVersionUID = 1L;
 
 public PreconditionFailedException(String message){
  super(message);
 }

}

ArrayIterator.java
package com.sample.util;

import java.util.Iterator;

import com.sample.exception.PreconditionFailedException;

public class ArrayIterator implements Iterator<Object> {

 private final Object[] arr;
 private int offset;
 private final int end;

 /**
  * 
  * @param arr
  * @param offset
  *            Starting point to iterate from array
  * @param noOfElements
  *            number of elements to iterate from offset
  * @throws PreconditionFailedException
  *             on failure of preconditions
  */
 public ArrayIterator(Object[] arr, int offset, int noOfElements) throws PreconditionFailedException {
  if (arr == null) {
   throw new NullPointerException("arr should not be null");
  }

  if (arr.length < offset) {
   throw new PreconditionFailedException("offset should be less than array length");
  }

  if (offset < 0) {
   throw new PreconditionFailedException("offset should be >= 0. offset : " + offset);
  }

  if (noOfElements <= 0) {
   throw new PreconditionFailedException("noOfElements should be > 0. noOfElements : " + noOfElements);
  }

  if (arr.length < (offset + noOfElements)) {
   throw new PreconditionFailedException("Array length is < (offset + noOfElements): array length : "
     + arr.length + ", offset : " + offset + ", noOfElements : " + noOfElements);

  }

  this.arr = arr;
  this.offset = offset;
  end = offset + noOfElements;
 }

 @Override
 public boolean hasNext() {
  return offset < end;
 }

 @Override
 public Object next() {
  return arr[offset++];
 }

}

Test.java
package com.sample.test;

import com.sample.exception.PreconditionFailedException;
import com.sample.util.ArrayIterator;

public class Test {
 public static void main(String args[]) throws PreconditionFailedException {
  Integer[] arr = new Integer[10];

  for (int i = 0; i < arr.length; i++) {
   arr[i] = i;
  }

  ArrayIterator arrIterator = new ArrayIterator(arr, 3, 6);

  while (arrIterator.hasNext()) {
   System.out.println(arrIterator.next());
  }
 }
}


ABAP: Concatenate two strings

'CONCATENATE' function is used to concatenate two (or) more strings into one string.

Syntax
CONCATENATE  str1 str2 str3 ... strN INTO result.
CONCATENATE  str1 str2 str3 ... strN INTO result SEPARATED BY ' '.

Second version of the CONCATENATE function is used to concatenate strings using given separator.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA: str1 TYPE STRING VALUE 'Hello',
      str2 TYPE STRING VALUE 'World',
      str3 TYPE STRING VALUE 'to',
      str4 TYPE STRING VALUE 'ABAP Programming'.

DATA result TYPE STRING.

CONCATENATE str1 str2 str3 str4 INTO result.

Write: / 'Value of str1 is : ', str1.
Write: / 'Value of str2 is : ', str2.
Write: / 'Value of str3 is : ', str3.
Write: / 'Value of str4 is : ', str4.
Write: / 'Value of result is : ', result.
As you see the ouput, string are concatenated. It looks clear if we add some spaces while appending the string. Use the second version of the CONCATENATE function.

Below statement separates every string by a space while concatenating.
CONCATENATE str1 str2 str3 str4 INTO result SEPARATED BY ' '.


Z_HELLO_WORLD

*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA: str1 TYPE STRING VALUE 'Hello',
      str2 TYPE STRING VALUE 'World',
      str3 TYPE STRING VALUE 'to',
      str4 TYPE STRING VALUE 'ABAP Programming'.

DATA result TYPE STRING.

CONCATENATE str1 str2 str3 str4 INTO result SEPARATED BY ' '.

Write: / 'Value of str1 is : ', str1.
Write: / 'Value of str2 is : ', str2.
Write: / 'Value of str3 is : ', str3.
Write: / 'Value of str4 is : ', str4.
Write: / 'Value of result is : ', result.




Previous                                                 Next                                                 Home

Thursday, 28 September 2017

ABAP: CHECK statement

CHECK statement is used to skip the loop statements when the given condition is false. If the condition is false, the control directly goes to the end statement of the current loop pass ignoring the remaining statements in the current loop pass, starts the next loop pass.

Syntax
CHECK condition.

Z_HELLO_WORLD

*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA counter TYPE I VALUE -1.

WHILE counter < 10.
  counter = counter + 1.

  CHECK counter > 5.

  Write: / 'Value of counter : ', counter.
ENDWHILE.

Write / 'End of the loop'.






Previous                                                 Next                                                 Home

ABAP: EXIT – Terminate loop unconditionally.

EXIT is used to come out of the loop, if you are from Java background, it is equivalent to break; statements in Java.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA counter TYPE I VALUE -1.

WHILE counter < 10.
  counter = counter + 1.

  IF counter > 5.
    Write / 'Exiting from the loop'.
    EXIT.
  ENDIF.

  Write: / 'Value of counter : ', counter.
ENDWHILE.

Write / 'End of the loop'.


Previous                                                 Next                                                 Home

ABAP: CONTINUE: Skip the iteration of loop

CONTINUE statement skips the current iteration of loop (DO–ENDDO, WHILE-ENDWHILE)

Syntax
CONTINUE.

Z_HELLO_WORLD

*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA counter TYPE I VALUE -1.

* Print only Even Numbers
Write 'Even counters from 0 to 10 are'.

WHILE counter < 10.
  counter = counter + 1.
  IF counter MOD 2 <> 0.
    CONTINUE.
  ENDIF.

  Write: / 'Value of counter : ', counter.

ENDWHILE.



Previous                                                 Next                                                 Home

ABAP: WHILE-ENDWHILE loop

It is also called as conditional loop. It execute the statements until the condition is true.

Syntax
WHILE condition
         * Execute block of statements.
ENDWHILE.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA counter TYPE I VALUE 0.

WHILE counter < 10.
  Write: / 'Value of counter : ', counter.
  counter = counter + 1.
ENDWHILE.



Previous                                                 Next                                                 Home

Wednesday, 27 September 2017

ABAP: DO-ENDDO loop

'DO-ENDDO' is also called as unconditional loop, it is used to execute block of statements given number of times.

Syntax
DO N times.
         * Execute this block
ENDDO.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA n TYPE I VALUE 10.
DATA counter TYPE I VALUE 0.

DO n TIMES.
  Write: / 'Value of counter : ', counter.
  counter = counter + 1.
ENDDO.



Previous                                                 Next                                                 Home