What will happen here? Question 2

Read the code below. What do you think will happen here?

import java.util.function.Supplier;
import java.util.stream.Stream;

public class Question2 {
    public static void main(String[] args) {
        Supplier<Integer> randomSupplier = () -> (int) (Math.random() * 3999) + 1;
        Stream.generate(randomSupplier).map(Question2::convertToRoman).forEach(System.out::println);
    }

    public static String convertToRoman(int number) {
        String[] thousands = { "", "M", "MM", "MMM" };
        String[] hundreds = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
        String[] tens = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
        String[] units = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };

        return thousands[number / 1000] +
                hundreds[(number % 1000) / 100] +
                tens[(number % 100) / 10] +
                units[number % 10];
    }
}
  • A) Java will complain that it’s an infinite stream and will not run
  • B) It’ll run perfectly normally in an infinite loop
  • C) The stream will be closed after the first run, and an IllegalAccessException will be raised
  • D) Nothing will be printed
  • E) After 42 minutes, it will stop running
Read more

Programming Challenges

I have a channel of Programming Challenges on WhatsApp. I tried to find a channel like this, but without success, and I thought: Why don’t I create one?

Do here it is: The Programming Challenges WhatsApp Channel. Enjoy!

How often will I publish? Well, when a challenge comes to mind.

About what? Well, about everything: mathematical challenges, OO challenges, functional programming, REST API consuming projects, etc.

What programming language do I need to implement: anyone.

Do I need to deliver this for someone? No, it’s only for you.

What I ask you is to follow the channel and this blog.

Thanks and be challenged!

Lazy Evaluation

Java is a hybrid programming language that allows developers to write code in object-oriented, imperative, and functional styles.

Functional programming offers a set of features that enable developers to write code in a declarative style. These are the concepts:

  • Immutability
  • Pure functions
  • Higher-order functions
  • First-class functions
  • Recursion
  • Lazy evaluation
  • Tail recursion
  • etc…


In this article, we will explain how to simulate Lazy Evaluation in Java, the trade-off, and a glimpse of the future of Java.

Read more

What will happen here?

Read the code below. What will happen with it?

public class ClassA {
   private int myAttribute;

   private void multiply(int factor) {
       myAttribute *= factor;
   }

   public void doProcess(ClassA classA, int attributeValue, int factor) {
       classA.myAttribute = attributeValue;
       classA.multiply(factor);
   }

   public void printMyAttribute() {
      System.out.println(myAttribute);
   }

   public static void main(String[] args) {
       ClassA instance1 = new ClassA();
       ClassA instance2 = new ClassA();

       instance1.doProcess(instance2, 2, 3);
       instance2.doProcess(instance1, 4, 5);

       instance1.printMyAttribute();
       instance2.printMyAttribute();
   }
}
  • A) The code will not compile
  • B) The code compiles but won’t run
  • C) The code runs, but a stack overflow happens
  • D) The code prints
    • 20
    • 6
  • E) None of the above
Read more

Terraform Best Practices

introduction

Nowadays, Cloud Computing is a de-facto standard adopted by companies or individuals to create their applications. Companies that offer solutions, that could be one of the main three models (IaaS – Infrastructure as a Service, PaaS – Platform as a Service, and Software as a Service) are called cloud providers, or simply providers.

Providers offer resources that are like building blocks for DevOps people to create suitable infrastructure for their applications. The number, type, and cost of resources may vary from provider to provider. There should be a study before choosing what provider better fits the project

To create those resources, providers offer a web console that DevOps can manage them. The problem of managing the resources on the web console is to keep track of what was created. That’s nothing that a plain text file or an Excel sheet can solve, right? Nope!

Then providers created a more fun way (yeah, fun!) to create the resources: a command-line interface (CLI). Now DevOps can create scripts (Shell or Power Shell Script) to manage when creating, modifying, or destroying a resource. The problem is when adding some new resources, that are not in the scripts, a new script should be created only for this to manage its lifecycle.

Don’t worry, there is a solution that can be adopted: Terraform, which is a provider agnostic CLI that interprets its own programming language (HCL – HashiCorp Configuration Language) that describes Cloud Computing Provider resources. With this tool, it’s possible to manage the infrastructure without taking notes on Excel or Notepad or even creating Scripts to manage the infrastructure. Ok, it can do more than this as it’ll be explained here.

In this article, I will list some tips best practices I reunited during my experience with Terraform.

Read more

Folding Hash

Sometimes, it’s needed to implement a different algorithm to solve problems in the developing system. All the algorithms are challenging. Some are used to improve the system because they represent a structure that repeats with a certain frequency. They are called patterns. Others are business algorithms, that dictates the way that received data should be treated inside a system. Anyway, there’s a lot of categories of algorithms.

This article is about the implementation of the Folding Hash Algorithm. It’s based upon another article, providing an implementation.


Read more

Where in the Code is Carmen Sandiego?

Back in the ’90s, Carmen Sandiego recruited the most expert burglars around the world (and in time) to steal paintings, sculptures, and other humanity’s important legacy creations. Every time that an investigator (in this case: you) start its endeavor to arrest one member of Carmen’s gang or herself, in the scene of the crime there are two clues:

  1. The first clue is about where the burglar goes next
  2. The second is about the burglar (some characteristic)

The mission ends when the investigator is at the same location (or time) and when there’s a prison order emitted to the member of the gang. With these two things combined, the investigator can capture the gang member and complete the mission.

This article is about the search of the suspect according to the clues, that can be the gender, the color of the eyes or hair, the occupation and the burglar’s favorite food using functional programming in Java.

Read more

JEP 286 – Java 10 Local-Variable Type Inference – The var reserved word

The Java version 10 brought a new reserved word (var) and with it a new mechanism to the language: the Local-Variable Type Inference.

Source code

The code that was used to create this article is versioned in Java10Examples project on github. Fork or clone to execute in your computer. Please, star this project and follow me on github.

The old way

Sure that every developer declares local variables and follows the Java common syntax to do it:

(...)
    type variableName = variableConstruct
(...)

Everybody knows that it’s needed to tell java what kind of variable developers are working, so type is the variable type that should be told to Java.

Read more

Common method implementation in Java (equals, hashCode and toString)

Some methods in Java are special and help developers to implement rules of equality between two objects. Some classes need that those methods are implemented to system’s good behaviour. In this article will be discussed those methods.

About the code

The code that was used to create this article is versioned in JavaBasicConcepts project on github. Fork or clone to execute in your computer. Please, star this project and follow me on github.

This project will be updated sometimes with other basic Java programming concepts.

Equality in Java

Sometimes developers need the if conditional to commute to alternate flows or to test the consistency of data. The Java programming language has the == operator and the equals method to perform these tasks.

Read more