/* * Implement the "I only know how to add and subtract 1" * example using recursion. * * @author gtowell * Created: Aug 11, 2021 */ public class AddOne { public int adder(int base, int adder) { if (adder == 1) return base + adder; if (adder == 0) return base; if (adder < 0) return -999; return adder(base, adder - 1) + 1; } /** * Alternate implementation of add one. * @param num1 one of the numbers * @param num2 the other number * @return */ public int rAdder(int num1, int num2) { if (num2<=0) return num1; return rAdder(num1+1, num2-1); } public static void main(String[] args) { AddOne addr = new AddOne(); System.out.println("5+1=" + addr.adder(5, 1)); System.out.println("5+3=" + addr.adder(5, 3)); System.out.println("5+5=" + addr.adder(5, 5)); } }