Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"repeat" structure in Java

Tags:

java

loops

I'm teaching programming to beginners (starting at 12-15 years old) and one of the choices we made (because it was natural in Python) was to teach the notion of "repeating an action" before the notion of variables.

We warted in Python with

for loop in range(10):

without speaking about variables of arrays and in C++ with

#define repeat(nb) for(int _loop = 0 ; _loop < (nb) ; _loop++)

The idea was to hide the complexity of a classical loop in order to insist on the "repeat" part. We are not hiding from the students the fact that "repeat(10)" is not a part of C++, it's just a way to simplify the learning.

In Pascal we can't do much more than

for loop := 1 to 10 do

but that's ok because its's not that difficult to remember.

I was looking for something similar in Java and I found that :

import java.util.List;
import java.util.AbstractList;

class Range {
    public static List<Integer> size(final int end) {
        return new AbstractList<Integer>() {
            @Override
            public Integer get(int index) {
                return 0 + index;
            }
            @Override
            public int size() {
                return end;
            }
        };
    };
}

public class Main {
     public static void main(String[] argv) {
    for (int loop : Range.size(10)) {
        System.out.println("xx");
    }
    }
}

The

for (int loop : Range.size(10))

is still easier to remember than

for(int loop = 0 ; loop < 10 ; loop++)

but there is two problems :

  • two variables are needed for imbricated for loops : I dont think we can do much about that
  • we are having warnings because the variable loop is not used

Do you see a better solution that what we have ?

Once again, we only want to provide some "tool" at the beginning phase in order for the students to "repeat" actions, before knowing anything about "variables". We are not hiding from them that's is not in the langage and after a few exercises (~80-100) we are asking them to use the real syntax.


We have approximately 20 exercices before introducing variables : some about printing texts but mostly we are providing one library with objects you can manipulate (hence the variables are hidden in the object state). You can think of the "logo-turtle" for example. This way the notion of "loop" can be manipulated and "seen" before introducing explicit variables and you can have interresting exercises really fast.

One example, in Python, where you want to visit every case of a 10x10 table once and only once and then be back at your starting point (lower-left corner) :

from robot import *

top()
for loop in range(4):
   for loop in range(8):
      top()
   right()
   for loop in range(8):
      bottom()
   right()
for loop in range(8):
   top()
right()
for loop in range(9):
   bottom()
for loop in range(9):
   left()

This exercise is not that easy but the syntax is really simple and allow the student to concentrate on the "algorithmic" part and not the "langage" part. After a few exercises the students are getting interrested and we can introduce more syntax and more difficult concepts like the variables.

like image 977
Loïc Février Avatar asked Feb 22 '23 20:02

Loïc Février


1 Answers

Do you really need to use Java for those exercises? If other languages works for you then why not to use them? You can always move to Java when you students know basics like variables.

I agree that variables can be quite confusing from beginners - especially that their value can change all the time, it is not something people are used from algebra where values don't change once "assigned".

If you want to use Java, you could use while loop which seems to fit better. One dirty trick how to avoid use of variable is following code - it use StackTraceElement instead of variable.

It prints

Hello A
Hello B
Hello C
Hello C
Hello C
Hello B
Hello C
Hello C
Hello C
Hello A
Hello B
Hello C
Hello C
Hello C
Hello B
Hello C
Hello C
Hello C
Hello A
Hello B
Hello C
Hello C
Hello C
Hello B
Hello C
Hello C
Hello C

Here is full source. main(Strinng[] args) method is code with loops, rest is supporting code.

import java.util.HashMap;
import java.util.Map;

public class Repeater {

    public static void main(String[] args) {
        while(range(3)) {
            System.out.println("Hello A");
            while (range(2)) {
                System.out.println("Hello B");
                while (range(3)) {
                    System.out.println("Hello C");
                }
            }
        }
    }

    public static boolean range(int size) {
        return Range.range(size);
    }


    public static class Range {

        static Map<StackTraceElement, RangePosition> ranges = new HashMap<StackTraceElement, RangePosition>();

        public static boolean range(int size) {
            final StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            //System.out.println(stackTraceElement);
            RangePosition position = ranges.get(stackTraceElement);
            if (position == null) {
                position = new RangePosition();
                position.size = size;
                ranges.put(stackTraceElement, position);
            }

            final boolean next = position.next();
            if (!next) {
                ranges.remove(stackTraceElement);
            }
            return next;
        }

     }

    public static class RangePosition {
        int current,size;
        boolean next() {
            current++;
            return current <= size;
        }
    }
}

But I'd prefer to use some language which supports this naturally.

like image 60
Arnost Valicek Avatar answered Feb 24 '23 08:02

Arnost Valicek