Skip to content

User-Defined Customer Interface (Exercise Problem)

This is an exercise problem for your practice. Try to come up with an approach and solve it by yourself. Good Luck!

Gopi Gorantala
Gopi Gorantala
1 min read

Table of Contents

Introduction

Given an array of strings, with the help of the Consumer interface applies some string operations.

Problem Statement

Write a program to capital case all the lists of fruits using the Consumer interface.

Input = {"apple", "orange", "Guava", "mango", "banana", "kiwi"}

Output = {"APPLE", "ORANGE", "GUAVA", "MANGO", "BANANA", "KIWI"}

Thought process

This is a simple problem asking us to capitalize each string and print them. To do string operations on all of the strings in the list, we need to iterate over all of the strings and apply String operations.

As we are iterating over the entire list, the time complexity or the time taken to run our algorithm is directly proportional to O(n), where n is the number of strings present in the list.

Note: O(n) is usually read as Order of n, which is linear. This is a term we use in software to determine the time taken by an algorithm to solve a problem (Big-O complexity analysis).

Coding exercise

import java.util.function.Consumer;

class ConsumerImpl implements Consumer<String> {
  @Override
  public void accept(String str) {
    // WRITE-YOUR-CODE-HERE
  }
}

Now, try to come up with an approach. This problem is designed for your practice, so try to solve it yourself first. If you get stuck, you can always refer to the solution below. Good luck!

Hint: Go back to Consumer interface and read through the topic to understand steps to implement it.

First, look at these code snippets below and think of a solution.

The solution will be explained below.

import java.util.function.Consumer;

class ConsumerImpl implements Consumer<String> {
  @Override
  public void accept(String str) {
    System.out.println(str.toUpperCase());
  }
}
import java.util.function.Consumer;
import src.dev.ggorantala.constants.Constants;

class Solution {
  public static void main(String[] args) {
    Consumer<String> fruitsConsumer = new ConsumerImpl();
    Constants.FRUITS_LIST.forEach(fruitsConsumer);
  }
}
Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.