Book Image

Java Coding Problems

By : Anghel Leonard
Book Image

Java Coding Problems

By: Anghel Leonard

Overview of this book

The super-fast evolution of the JDK between versions 8 and 12 has increased the learning curve of modern Java, therefore has increased the time needed for placing developers in the Plateau of Productivity. Its new features and concepts can be adopted to solve a variety of modern-day problems. This book enables you to adopt an objective approach to common problems by explaining the correct practices and decisions with respect to complexity, performance, readability, and more. Java Coding Problems will help you complete your daily tasks and meet deadlines. You can count on the 300+ applications containing 1,000+ examples in this book to cover the common and fundamental areas of interest: strings, numbers, arrays, collections, data structures, date and time, immutability, type inference, Optional, Java I/O, Java Reflection, functional programming, concurrency and the HTTP Client API. Put your skills on steroids with problems that have been carefully crafted to highlight and cover the core knowledge that is accessed in daily work. In other words (no matter if your task is easy, medium or complex) having this knowledge under your tool belt is a must, not an option. By the end of this book, you will have gained a strong understanding of Java concepts and have the confidence to develop and choose the right solutions to your problems.
Table of Contents (15 chapters)
Free Chapter
1
Strings, Numbers, and Math
2
Objects, Immutability, and Switch Expressions
3
Working with Date and Time
5
Arrays, Collections, and Data Structures
6
Java I/O Paths, Files, Buffers, Scanning, and Formatting
7
Java Reflection Classes, Interfaces, Constructors, Methods, and Fields
8
Functional Style Programming - Fundamentals and Design Patterns
9
Functional Style Programming - a Deep Dive
10
Concurrency - Thread Pools, Callables, and Synchronizers
11
Concurrency - Deep Dive
13
The HTTP Client and WebSocket APIs

Problems

Use the following problems to test your string manipulation and mathematical corner case programming prowess. I strongly encourage you to give each problem a try before you turn to the solutions and download the example programs:

  1. Counting duplicate characters: Write a program that counts duplicate characters from a given string.
  2. Finding the first non-repeated character: Write a program that returns the first non-repeated character from a given string.
  3. Reversing letters and words: Write a program that reverses the letters of each word and a program that reverses the letters of each word and the words themselves.
  4. Checking whether a string contains only digits: Write a program that checks whether the given string contains only digits.
  1. Counting vowels and consonants: Write a program that counts the number of vowels and consonants in a given string. Do this for the English language, which has five vowels (a, e, i, o, and u).
  2. Counting occurrences of a certain character: Write a program that counts the occurrences of a certain character in a given string.
  3. Converting String into int, long, float, or double: Write a program that converts the given String object (representing a number) into int, long, float, or double.
  4. Removing white spaces from a string: Write a program that removes all white spaces from the given string.
  5. Joining multiple strings with a delimiter: Write a program that joins the given strings by the given delimiter.
  6. Generating all permutations: Write a program that generates all of the permutations of a given string.
  7. Checking whether a string is a palindrome: Write a program that determines whether the given string is a palindrome or not.
  8. Removing duplicate characters: Write a program that removes the duplicate characters from the given string.
  9. Removing given characters: Write a program that removes the given character from the given string.
  10. Finding the character with the most appearances: Write a program that finds the character with the most appearances in the given string.
  11. Sorting an array of strings by length: Write a program that sorts by the length of the given array of strings.
  12. Checking that a string contains a substring: Write a program that checks whether the given string contains the given substring.
  13. Counting substring occurrences a string: Write a program that counts the occurrences of a given string in another given string.
  14. Checking whether two strings are anagrams: Write a program that checks whether two strings are anagrams. Consider that an anagram of a string is a permutation of this string by ignoring capitalization and white spaces.
  15. Declaring multiline strings (text blocks): Write a program that declares multiline strings or text blocks.
  16. Concatenating the same string n times: Write a program that concatenates the same string a given number of times.
  17. Removing leading and trailing spaces: Write a program that removes the leading and trailing spaces of the given string.
  1. Finding the longest common prefix: Write a program that finds the longest common prefix of given strings.
  2. Applying indentation: Write several snippets of code to apply indentation to the given text.
  3. Transforming strings: Write several snippets of code to transform a string into another string.
  4. Computing the minimum and maximum of two numbers: Write a program that returns the minimum and maximum of two numbers.
  5. Summing two large int/long numbers and operation overflow: Write a program that sums two large int/long numbers and throws an arithmetic exception in the case of an operation overflow.
  6. String as an unsigned number in the radix: Write a program that parses the given string into an unsigned number (int or long) in the given radix.
  7. Converting into a number by an unsigned conversion: Write a program that converts a given int number into long by an unsigned conversion.
  8. Comparing two unsigned numbers: Write a program that compares the given two numbers as unsigned.
  9. Division and modulo of unsigned values: Write a program that computes the division and modulo of the given unsigned value.
  10. double/float is a finite floating-point value: Write a program that determines whether the given double/float value is a finite floating-point value.
  11. Applying logical AND/OR/XOR to two boolean expressions: Write a program that applies the logical AND/OR/XOR to two boolean expressions.
  12. Converting BigInteger into a primitive type: Write a program that extracts the primitive type value from the given BigInteger.
  13. Converting long into int: Write a program that converts long into int.
  14. Computing the floor of a division and modulus: Write a program that computes the floor division and the floor modulus of the given dividend (x) and divisor (y).
  15. Next floating-point value: Write a program that returns the next floating-point adjacent to the given float/double value in the direction of positive and negative infinity.
  16. Multiplying two large int/long values and operation overflow: Write a program that multiplies two large int/long values and throws an arithmetic exception in the case of operation overflow.
  1. Fused Multiply Add (FMA): Write a program that takes three float/double values (a, b, c) and computes a * b + c in an efficient way.
  2. Compact number formatting: Write a program that formats the number 1,000,000 to 1M (US locale) and to 1 mln (Italian locale). In addition, parse 1M and 1 mln from a string into a number.