Daily Log: November 8, 2024

After a short break, I spent some time brushing up my Java knowledge and wrote some programs to reflect on :

String class

String is a reference data type and comes with several inbuilt methods which facilitate playing with String values.

public class stringclass 
{
    public static void main(String[] args) throws Exception
    {
        String name ="DJ";
       // name.equals("DJ")
       // int result = name.compareTo("DJ");
       // int result= name.compareToIgnoreCase ("dj");
       // boolean result= name.contains("C");
       int result = name.indexOf("D");
       System.out.println(result);      
    }    
}

Wrapper Class

process of converting primitive data types like int. boolean, double, float etc. to reference datatype. The process is simple to write in full form starting with a Capital letter e.g. Boolean, Integer, Double, Float, Character.

public class wrapperclass 
{    public static void main(String[] args) throws Exception         
    {

// outboxing 
        Integer i =2435;
//unboxing
        Boolean result = i.equals (2345);
        Character ch = '@';
        System.out.println(ch);
        Double f = 1.2232445545454324d;
        System.out.println(f);
    }    
}

while loop

loop is a power tool that gives a program direction/condition. In the following code, the application will ask the user for a valid name until one is passed.

do .. while loop is a small variation from a while loop with the only difference that a piece of code will be executed at least once no matter what the condition is.

import java.util.Scanner;
public class loop 
{
    public static void main(String[] args) throws Exception 
    {
        // write a program to print " enter your name:" untill user enter a valid name
        String name ="DJ";
        Scanner scanner = new Scanner(System.in);
       do{
            System.out.print("Enter your name: ");
            name =scanner.nextLine();
         } while (name.isBlank());
        System.out.println("Welcome "+name);
    }    
}

for / nested for loop

this loop has 3 parts - declaration, condition and increment and this is a powerful programming concept to take care of repetitive tasks.

public class nestedloop {
    public static void main(String[] args) throws Exception {
        // write a program to draw star symbol in increasing order. It should have 6 rows and each row will increemnt stars to match row number

        /*
        *
        **
        ***
        ****
        *****
        ******
         */

        int i ;
        int j;
        String symbol ="*";

        for ( i=1; i<=6; i++){
            for (j=1; j<=i; j++){ 
            System.out.print(symbol);        
        }
        System.out.println();
        }
    }    
}

arrays

a useful way of storing multiple values to a single datatype and retrieving them by their index. Very useful when we have to deal with lots of values of the same type.

public class arrays 
{
    public static void main(String[] args) throws Exception
    {
         /* first declare the datatype and put a bracket [], 
            then define the name and put equal sign "=" and 
            put the string values within curly bracket "{}".*/
        String [] cars = {"Toyota","Chevrolet","BMW"};
        /* this piece of code will print the cars 
            that are available in the string */
        for ( int i =0; i<cars.length; i++)
        {
            System.out.println(cars[i]);
        }
        // let' see other ways to define an array and insert values to it
        String [] places = new String [3];
        places [0]= "Agartala";
        places [1]= "Chandigarh";
        places [2]= "Gurgaon";
        for ( int j =0; j<places.length; j++)
        {
            System.out.println(places[j]);
        }
    }
 }

2D arrays

when storing data in rows and columns use a 2D array in Java

AppleOrangeGrapes
WatermelonPeachPears
BlueberryStrawberryPumpkin
public class dArray 
{
    public static void main(String[] args) throws Exception

    {
        /* this program is to write a 2 D array. 
           It's like storing values in a table which got its rows and columns.*/
        String [][] parking = new String[2][2];
        parking [0][0] ="Trump";
        parking [0][1] = "Modi";
        parking [1][0] ="Pierre";
        parking [1][1] = "Putin";

        System.out.println();

        for ( int i=0; i<parking.length; i ++)
        {
            System.out.println();
            for ( int j=0; j<parking[i].length;j++)
            {
                System.out.print (parking[i][j] + "      ");
            }
        }
    }    
}
0
Subscribe to my newsletter

Read articles from Debajyoti Chanda directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Debajyoti Chanda
Debajyoti Chanda

I’m a tech enthusiast with a passion for continuous learning and new experiences. As a digital nomad, I approach new challenges with a strategic mindset. My interests lie in programming, automation, and the world of open-source technology. Embracing AI has been a thrilling journey, and I’ve developed some RAG models that have truly blown my mind. In a previous chapter of my career, I was a performance tester. While I still offer consultancy services, it’s no longer my main focus. I call Toronto home, where I live with my vibrant family: my wife, two children, my mother, and our beloved pets—two dogs and two cats. Our household is always bustling, especially during Christmas and Halloween when I love to decorate our home. My kids are nature enthusiasts, and to keep up with them, I’ve developed a love for hiking and nature gazing. Walking my dogs is a daily joy, especially when they take care of business along the way. In my downtime, I find immense satisfaction in painting and sketching designs on blank paper.