0

I'm working on some homework programs and I'm required to do stuff in Java using functional programming principles as much as possible. These are the important bits of a program that receives a list of numbers and prints the even ones:

   public static void main( String args[ ] )
   {
      ArrayList<Double> listEven = new ArrayList<Double>();
      inputRecursion( );
      outputRecursion( );
   }

   public static void inputRecursion( )
   {
      Scanner in = new Scanner( System.in );
      if( in.hasNextDouble() )
      {
         if( (in.nextDouble() % 2) == 0 )
         {
            listEven.add( in.nextDouble() );
         }
         inputRecursion( );
      }
   }

   public static void outputRecursion( )
   {
      Iterator<Double> it = listEven.iterator();

      if( it.hasNext() )
      {
         System.out.println( it.next() );
         outputRecursion( );
      }
   }

It's a work in progress, but I haven't got to run it and check the logic because I still have two compilation errors in the lines:

listEven.add( in.nextDouble() );
Iterator<Double> it = listEven.iterator();

These two lines throw "cannot find symbol", and I know this is because the ArrayList was declared in a way that makes in inaccessible from methods outside main. I have a rough understanding of the ways of fixing this. I know about setters and getters and they look simple enough from what I researched, but I consider those the last resource since I'm trying to avoid the use of variables (I am aware that my program has other mutable stuff; I'm working on it) in order to meet the restrictions of the exercise as much a possible. I also know that I can make it public static, but then that causes like 15 more errors to appear, and from what I looked up it requires to be "initialized" and it also involves variables.

Are there any other ways to make the ArrayList accessible from those two methods? I'm specially interested in ways that don't involve variables or iteration.

George S.
  • 17
  • 2
  • Considering that you need to use functional programming principles, making all your methods `void` is probably not the best approach. Also, the bit where you do an integer modulo on a double, and then read another double, is that really intended? – SeverityOne Apr 06 '18 at 23:57
  • @SeverityOne Yeah the double thing is just in case the user decides to input a non-integer. The even numbers will always be integer anyway, but I don't want the inputting of the values to stop if they try to input 2.5 or something; that's really my only reason for all the doubles, and as far as I've thought it it can't cause false positives. Regarding the void part, I was under the impression that arguments and returns count as variables and shouldn't be used, but I guess you're implying that returning stuff is fine then. – George S. Apr 07 '18 at 00:09
  • Why do you mean by 'functional'? – jax Apr 07 '18 at 00:15
  • I'm speculating that "functional" in this case means to use immutable objects. You can do that with functions, but they would have to return a value. Something like `int square(int x){return x*x;}`. – SeverityOne Apr 07 '18 at 00:21
  • 1
    @jax Basically it's an undying meme paradigm where you have to program without using iteration or variables, or mutable states of any kind. There are some languages designed specifically for it, but Java isn't one of them. Doing it often means that you make things way harder than needed for no apparent reason, but I guess some teachers think it's like training with weighted clothing or something. – George S. Apr 07 '18 at 00:22
  • Also, using recursion in this case is just plain wrong. Java doesn't do tail recursion optimization so if the input or output is long you'll eventually run out of stack space. As @GeorgeS. said Java is not a functional language and forcing it like this is like using a chisel as a screwdriver. – Jim Garrison Apr 07 '18 at 00:23
  • @GeorgeS Got it, because it is a common mistake for beginners to refer to procedural programming as functional. – jax Apr 07 '18 at 00:44
  • Why not just passed it into the method as an argument? – jax Apr 07 '18 at 00:48

1 Answers1

0

Declare the ArrayList as static but outside of the main function like below:

   static ArrayList<Double> listEven = new ArrayList<Double>();

   public static void main( String args[ ] )
   {
      inputRecursion( );
      outputRecursion( );
   }

   public static void inputRecursion( )
   {
      Scanner in = new Scanner( System.in );
      if( in.hasNextDouble() )
      {
         if( (in.nextDouble() % 2) == 0 )
         {
            listEven.add( in.nextDouble() );
         }
         inputRecursion( );
      }
   }

   public static void outputRecursion( )
   {
      Iterator<Double> it = listEven.iterator();

      if( it.hasNext() )
      {
         System.out.println( it.next() );
         outputRecursion( );
      }
   }
Jake Miller
  • 2,432
  • 2
  • 24
  • 39