The Core Essentials: Basic Java Programs Every Beginner Should Know

The Core Essentials: Basic Java Programs Every Beginner Should Know

Practice can make someone perfect, and when it comes to becoming a Java developer, it's crucial to have hands-on experience in solving problems. One of the best ways is to master basic Java programs that every beginner should know. On this page, some of the top basic Java programs are discussed. So what are you waiting for? Jump to them and learn.

Calculator.

This is found to be one of the high beginner-level programs that everyone uses to make. Building a Simple Calculator in Java helps in understanding how the basic arithmetic concepts work and how to code them. A basic calculator requires basic operations like addition, subtraction, multiplication, division, and percentage, and you can add more. To create a calculator in Java, you can make functions of each operation separately and then combine them to call as per the user. 

# Code.

~~ Snippet of Calculator code ~~

 

Sting Manipulation in Java.

Another cool concept that a Java beginner must be aware of is to learn Manipulating Strings in Java. This is very common in any programming language like Java, and it has various built-in functions that help in manipulating a string to achieve the required output. Using string manipulation, one can easily build programs like comparing, reversing, optimizing, sorting a string, and more. 

There are some important ways to manipulate a string in Java, and here is the list. 

  • String Concatenations: Adding up two or more operate strings into one with the help of the "+" operator.
  • Changing the case of the given string: Changing the lowercase to uppercase or vice versa using toUpperCase() and toLowerCase().
  • String inside a string: Used to find whether there is a sub-string inside a string. Contain() and substring() are used. 
  • Cleaning a string by removing unwanted characters: The Trim () function in Java is used to remove extra spaces from the beginning and the end of a string. 
  • String Reverse: This is one of the parts used in String manipulation, and this can be easily done by getting the string from the last index to the first index. 

Let's find out some more examples of Java programs that are quite significant from the logic-building and interview perspective. These are some of the top questions that are generally asked in a technical round of interviews so that they can remove candidates from the hordes.

Check whether the string is palindrome.

Jumping to the first one, write a program to check if a string is a palindrome. This is another cool program that you must try out. Here, checking if a number is a palindrome or not is pretty simple, right? How about you have to check for a string? Interesting, right? First, try it out with yourself by creating your side of logic, and then see the below to find out what the code actually looks like.

A string is said to be a palindrome only when it can be read out the same from left to right or right or left, and below is an example.

Input string: Rotator

Output: Yes

Input String: Java

Output: No

Let's see what the code will be like.

import Java.io.*;

import Java.util.Scanner;

public class CheckString{

public static void main (String[] args){

String s="Hello";

String rev="";

for (int i=s.length()-1; i>=0;i++)

rev=rev+s.charAt(i);

if(s.equals(rev))

System.out.println("String is palindrome");

else

System.out.println("String is not palindrome");

}
}

Output: String is not Palindrome

Calculate the Factorial.

Moving to the next program, which is on how to Write a program to calculate Factorial of a number. In this Java program, the idea is simple: you have been given a number as input and get the factorial of it as output. 

A factorial is the multiplication of all integers smaller than or equal to n till n reaches one. The concept of factorial is given below.

n! = n*(n-1)*(n-2)*(n-3)*(n-4)*.......*1

For example, let's take a number 5.

5!= 5*(5-1)*(5-2)*(5-3)*(5-4)

5!= 5*4*3*2*1

5!= 120

Let's create a code for this question in Java. There are many ways to write a factorial function, 

  • Iterative method
  • Using Recursion
  • Using ternary operator

Here are the solutions using each method.

#code:

class test {

static int factorial (int n){

int res=1, i;

for (i=2; i<=n; i++)

 res*=i;

return res;

}

public static void main(String[] args){

 int num=5;

 system.out.println("Factorial of "+num+"is "+factorial(5));

}

}

You just have to use this function for the recursion method. 
static int factorial(int n){

if (n==0) return 1;

return n*factorial(n-1);

}

When using ternary operator, code will be like

int factorial(int n){

return (n==1||n==0)?1 : n*factorial(n-1);

}

Your output will be given for all the cases Factorial of 5 is 120.

Mirror Strings or Reverse Strings.

Let's move forward and learn another cool program that will surely help you play with strings in Java. Write a program to reverse a string. Here, one will be giving a string value as an input, and the program will give the reverse of the string as an output. It may sound easy to make the logic for this program, but when it comes to writing for it, people are stuck where they think to use a for loop and just reverse the loop to print, but it's not that easy. 

Before starting this, you must have a basic idea of the logic. The main idea is to traverse the length, extracting each character of the string while traversing and adding each one in front of the existing string. Here's how it will be done.

Input: "hello"

Output: "olleh"

#Code:

import Java.io.*;

import Java.util.Scanner;

class reverse_string

{

 public static void main(String[] args)

{

  String str = "Hello", nstr=""; 

  char ch;

  System.out.println("Input String : "+str);

  for (int i=0; i<str.length(); i++)

  {

    ch=str.charAt(i);  // this will be extracting each character 

    nstr=ch+nstr;  //concatenates each character in front of the existing string
  }
  System.out.println("Reversed String: "+nstr); // this will print your output as "Reversed Sting: olleh"
 }
}

Final Words.

Hopefully, you have learned these basic Java programs and understand the basic concept behind their logic building. One significant thing in writing code in every Java program is having a basic understanding of the functions and keywords, and you will find it easy to create the code. To improve your logic building, you must keep practicing and solving problems.