Let's write program with Java (the fundamental basics) - Part 2

Let's write program with Java (the fundamental basics) - Part 2

*** Previous Part ***: Let's write program with Java (the fundamental basics) - Part 1

N.B: Please read the following links for better understanding before continue reading this article.

? ??????????????????:?https://lnkd.in/gYG9EXTt

? ????????:?https://lnkd.in/gxq3K4uv

? ????????????????:?https://lnkd.in/gdHNiXUS

Basic Syntax of Java:

I try to keep the things simple enough as much as possible. Without any outside discussion I only mention the main focus point. So lets start..

Condition (if-else and if-else if-else):

Java if-else and and if-else if-else or if-else ladder syntax is similar to C++.

Example:

import java.io.*

public?class?IfElseExample?{?
    public?static?void?main(String[]?args)?{??
????    int?age = 20;??
????
    ????if(age>18){??
    ????????System.out.print("Age?is?greater?than?18");??
    ????}
        else {
            System.out.print("Age?is?not greater?than?18");
        }
    }??
}???
        

Please note infinite level of nesting of if else or else if ladder is support by the java program.

import java.io.*

public?class?ElseIfLadderExample?{?
    public?static?void?main(String[]?args)?{??
????    int?age = 20;??
????
    ????if(age>18){??
    ????????System.out.print("Age?is?greater?than?18");??
    ????}
        else if(age == i8){
            System.out.print("Age?is?equal to?18");
        }
        else {
            System.out.print("Age?is?not greater?than?18");
        }
    }??
}???        

Condition (switch case):

Java switch case syntax is also similar to C++. The difference between if else and switch condition is if condition can check the all relational condition but switch can only check equal with case value. You can write the nested switch statements also.

import java.io.*
?
class Sample {
????public static void main (String[] args) {
????????int num=20;
??????????switch(num){
??????????case 5 :? System.out.println("It is 5");
????????????????????break;
??????????case 10 : System.out.println("It is 10");
????????????????????break;
??????????case 15 : System.out.println("It is 15");
????????????????????break;
??????????case 20 : System.out.println("It is 20");
????????????????????break;
??????????default:? System.out.println("Not present");
?????????????
????????}
????}
};        

Break statements:

The switch case example have a break statements. Like it name suggests break statement break a execution block so that above code of that block will be ignored. So program encountered a break statements then it immediately break that block.

Array:

Array is a non primitive data types of java. Array is nothing a collection of similar type of data. That means when we need more than one similar and related data then we should use the array. Array is being declared using [ ]. Also it can be one dimensional and multi dimensional. Array can be declare in 3 ways.

class Sample {
	public static void main(String[] args)
	{
		int[] arr1; // type 1 declaration
        int arr2[]; // type 2 declaration
        int []arr3; // type 3 declaration

        // multi dimensional array declaration
        int marr1[][]; // type 1 declaration
        int[][] marr2; // type 2 declaration
        int [][]marr3; // type 3 declaration
	}
}        

Loop:

Java supports 4 types of loop. They are:

while loop:

import java.io.*

class whileLoopDemo {
	public static void main(String args[])
	{
		int i = 1;

		while (i < 6) {
			System.out.println("Hello World");
			i++;
		}
	}
}        

do while loop:

import java.io.*

class DoWhileDemo {
	public static void main(String[] args)
	{
		int i = 0;
		do {
			System.out.println("Print statement");
			i++;
		} while (i < 0);
	}
}        

for loop:

import java.io.*

class forLoopDemo {
	public static void main(String args[])
	{
		for (int i = 1; i <= 5; i++){
            if(i == 3) continue;
			System.out.println("Hello World");
        }
	}
}        

for each loop:

import java.io.*

class ForEachDemo	
{
	public static void main(String[] arg)
	{
		{
			int[] marks = { 125, 132, 95, 116, 110 };
			
            for (int num : marks){
  			    System.out.println(num);
            }
  		}
  	}
  }        

Continue statements:

Continue also ignore the execution of the existing below code of a blocks but unlike break it continue it executions for the next iterations.

Function or Method:

Java function also known as method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. It is used to achieve the?reusability?of code. The declaration signature of a method is as below:

Access_Modifier Return_type Method_Name (Parameter_List) {
   Method_body (collection of statements)
   
   return return_value; // when void type it do not return anything
}        

Lets write a method for finding maximum value from a array.

import java.io.*

class FunctionDemo	
{
	public static void main(String[] arg)
	{
		{
			int[] marks = { 125, 132, 95, 116, 110 };
			
            // call the maximum function to get highest marks
			int highest_marks = maximum(marks);

			System.out.println("The highest score is " + highest_marks);
		}
	}

    // This is our maximum finding method
	public static int maximum(int[] numbers)
	{
		int maxSoFar = numbers[0];
		
		for (int num : numbers)
		{
			if (num > maxSoFar)
			{
				maxSoFar = num;
			}
		}
	    return maxSoFar;
	}
}        

Lets Practice:

The best way of practicing programing is solving programming problem. Lets solve the problem?LeetCode 1704.?Determine if String Halves Are Alike?by using the knowledge that we learn till now.

Here problem says return true if first half and the second half of the string contains equal number of vowel otherwise return false.

Solution:

class Solution {
? ? public boolean halvesAreAlike(String s) {
? ? ? ? int len = s.length();
? ? ? ? return countVowel(s, 0, len/2) == countVowel(s, len/2, len);
? ? }


? ? public int countVowel(String s, int start, int end){
? ? ? ? int count = 0;

? ? ? ? for(int i=start;i<end;i++){
? ? ? ? ? ? if(isVowel(s.charAt(i))) 
? ? ? ? ? ? ? ? count++;
? ? ? ? }

? ? ? ? return count; 
? ? }


? ? public boolean isVowel(char toCheckValue){
? ? ? ? char arr[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
? ? ? ? for (char element : arr) {
? ? ? ? ? ? if (element == toCheckValue) {
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
}        


To be Continued ...

要查看或添加评论,请登录

Saiful Islam Rasel的更多文章

社区洞察

其他会员也浏览了