Java fundamentals practical exercises

Why is it important to keep practicing?

Practice is the most important thing in the process of becoming a developer. When you exercise, the brain changes the way you think and becomes more adaptable to solving problems.

It takes a lot of effort and patience to build confidence, but after some time you will change the way you approach the problems and every challenge becomes more manageable. Exercising helps you develop different ways to tackle the same issue and teaches you how to break problems into smaller parts. 

Do not worry if you run into errors. Practicing regularly means running into errors – lots of them. This is a good thing because you learn to read and understand error messages and you will get better at debugging, which is half the battle in coding. 

Another argument to keep you practicing is that most jobs, interviews and projects won’t hand you step-by-step instructions. Practicing helps you learn how to approach unfamiliar challenges. You will be more confident and capable when building real applications.

 

In our roadmap we talked about the first phase of the process: a phase of becoming confident about solving problems using Java. We prepared a set of Java Fundamentals practical exercises to help you in the process of becoming a developer.

Beginner Level

  • download and install an editor: we recommend IntelliJ IDEA Community Edition which is free to use
  • write a Java program that prints “I want a fresh lemonade!” to the console.
  • declare variables of different types (int, double, char, boolean, String) and print them out.
  • write a program to swap two numbers using a temporary variable.
  • write a program that calculates the sum of all numbers from 1 to 100 using a for loop.
  • Print the multiplication table for a given number using a while loop.
  • write a program to check if a number is even or odd. The program should take an integer input from the user. Write a method to verify the number.
  • write a program that takes two integer inputs from the user and performs addition, subtraction, multiplication, and division between the numbers. After each operation the result is printed.
  • write a program that takes three integer inputs from the user and finds the maximum of the three numbers given by the user.
  • Write a program to check if a number is prime. The number is given by the user.
  • write a program that takes an integer as input and checks if it is a palindrome (reads the same forwards and backward).
  • write a program that generates the Fibonacci sequence up to a given number of terms.

Arrays exercises

Write a program that takes an array of integers as input and performs the following operations:

  • find the average of the array elements
  • find the maximum and minimum elements
  • reverse the array
  • check if the array contains a value 
  • remove the duplicates
  • count the occurrences of a number
  • merge two arrays

String exercises

  • write a program that takes a string as input and reverses it.
  • write a program that takes a string as input and checks if it is a palindrome.
  • write a program to count the number of words in a string.
  • count the number of vowels, consonants, digits, and white spaces in a string.
  • check if two strings are anagrams (contain the same characters in a different order).
  • remove all duplicates from a string.
  • transform a sentence so that the first letter of each word is uppercase.
  • count the number of words in a sentence.

Writing tests

  • write a method that sums two integers. Write a unit test to verify it with multiple test cases using asserts.
  • write a method isPrime(int n) and write tests for: a known prime, a known non-prime, negative numbers, an edge case: n = 1.
  • write tests for the methods from the above exercises

Exceptions

  • write a program that divides two numbers. Use a try-catch block to handle ArithmeticException (e.g., division by zero).
  • create an array and try to access an invalid index. Handle the ArrayIndexOutOfBoundsException.
  • read an integer from user input and divide a number by it. Handle both InputMismatchException and ArithmeticException.
  • demonstrate how a “finally” block works even if an exception is thrown.
  • write a method that throws an IllegalArgumentException if a negative number is passed.
  • define your own exception class InvalidAgeException, and throw it if age < 18 in a method checkVotingEligibility(int age).
  • write a method that reads a file and declares it with throws IOException. Call it from main() and handle the exception there.
  • create a BankAccount class. Add methods like withdraw(double amount) that throws InsufficientFundsException if the balance is too low.

Regex

  • write a method that checks if a string contains only numbers.
  • create a method that checks if an email address is valid using a regex pattern.
  • write a regex to validate date strings in dd/mm/yyyy format.
  • use regex to extract all words from a string that start with an uppercase letter.
  • use String.replaceAll() with regex to change all spaces/tabs into underscores.
  • use a Regex for password validation. The password must be at least 8 characters, containing uppercase, lowercase, number, and special character.

Collections 

  • create an ArrayList of strings. Add several strings to the list and iterate through the list and print each string. 
  • given a list of integers, find the element that appears most frequently.
  • given a list with duplicates, remove the duplicates using a HashSet.
  • given a string, count the frequency of each word using a HashMap.
  • sort a list of strings alphabetically and integers numerically using Collections.sort().
  • create a phonebook using a Map where names map to phone numbers. Allow adding, searching, and deleting entries.
  • given a HashMap<String, Integer>, sort it by values (not keys).

 Object-Oriented Programming (OOPs)

  • Create a Lemonade class with attributes name and price.
  • add constructors to the Lemonade class: one default, one parameterized. Create objects using both.
  • make class fields private and provide public getter and setter methods to access them.
  • create an instance of the Lemonade class and display the attributes using toString.
  • implement inheritance — a Product class and two subclasses Lemonade and Coffee. The Product class should have three attributes: id, name, price. The Lemonade class should have an attribute for the type: small/big and the coffee should have an attribute for the type: americano/espresso/latte
  • create a class with multiple methods of the same name but different parameters (Overloading).
  • override a method in a subclass. Example: Product class has a getPrice() method. Lemonade and Coffee can override it by adding a tax for each type (for lemonade we can add 5 and for coffee we can add 7)

After you feel confident with solving problems and OOP, you can check the next phase page of creating projects using OOP.

java developer roadmap 1