fbpx

How to create a file in unix – 3Minutes: Tutorial

In this video, I have explained How to create a file in unix?

(Create, edit, view and save text file using unix terminal.)

This video covers the below listed commands

  1. pwd – present working directory
  2. cd – change directory
  3. touch – to create file
  4. cat – view content of the file
  5. less – same as cat command, but opens in editor
  6. mkdir – make or create directory
https://youtu.be/xDe6OLATxQc

How to create a file in unix ?

[email protected]:~/dev/folder1$ touch hello.txt
[email protected]:~/dev/folder1$ vi hello.txt
[Edit the file] to save press Esc + :wq (w is for save and q is for quit)
[email protected]:~/dev/folder1$ cat hello.txt

Keep the learning short, not to waste your precious time.

How to create a file in unix - 3Minutes: Tutorial
Java Programming: Why do we have main method ? can this method name be changed ?

Java Programming: Why do we have main method ? can this method name be changed ?

I was wondering always, why it is always main method is the starting point for the Java program.

Well When i was matured enough to read the Java Specification i learnt why and whether can it be changed ? Okay Lets See !!

Why main method ?

Main method is the starting point for the java program, JVM initialize the class and invokes main method, having below listed specifications (reference 12.1.4). 

  • Should be named as main 
  • Should be public 
  • Should be static 
  • Should accept String array as argument. 
  • Return type is void 

As based on the above specification, we can conclude that we cannot rename the main method. 

Some Additional Info.!

public static void main(String[] args)  // this is valid

public static void main(String… args)  //String…  this is also valid where it is available from the java 5

public static void main(String args[]) //This is valid but not recommended because the array brackets in the variable)

String Java Program: Palindrome

String Java Program: Palindrome

Interview Questions

Refer this for preparing yourself on how to write java program to check given string is a Palindrome or not.

Palindrome

public class PalindromeApp {
	public static void main(String[] args) {
		String input = "dad";
		if (PalindromeApp.isPalindrome(input)) {
			System.out.println(String.format("%s is palindrome", input));
		} else {
			System.out.println(String.format("%s is NOT palindrome", input));
		}
	}

	private static Boolean isPalindrome(String input) {
		char[] inputArr = input.toLowerCase().toCharArray();
		char[] reverse = new char[inputArr.length];

		for (int i = 0; i < inputArr.length; i++) {
			reverse[i] = inputArr[(inputArr.length - 1) - i];
		}
		String r = new String(reverse); // String.copyValueOf(reverse);
		return (input.equals(r));
	}
}
JAVA PROGRAM TO PRINT PATTERNS

JAVA PROGRAM TO PRINT PATTERNS

Interview Questions

Refer this for preparing yourself on how to write java program to print patterns (Note: NOT Design Patterns)
  • Start Pattern
  • Number Pattern

Star Pattern

 Print pattern like below, By giving the max length of the print.  (max. length is 5)
(max. length is 5)
public class StarPatternApp {
	public static void main(String[] args) {
		StarPatternApp.printPattern("*", 5);
	}
	private static void printPattern(String pattern, int size) {
		StringBuilder patternBuilder = new StringBuilder();
		for (int i = 1; i <= size; i++) {
			patternBuilder.append(pattern);
			System.out.println(patternBuilder.toString());
		}
		for (int i = size; i > 1; i--) {
			System.out.println(patternBuilder.deleteCharAt(i - 1).toString());
		}
	}

	private static String addSpaceAndPrint(int spaceCount, String printStr) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < spaceCount; i++) {
			sb.append("" + i);
		}
		sb.append(printStr);
        	return sb.toString();
	}
}

Number Pattern

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NumberReducingPatternApp {

    public static void main(String[] args) {
       int n = 5;
       int length = 1;
       List<Integer> numberPatternItems = new ArrayList<>();
       for(int i=n; i>0;i--){
           numberPatternItems.add(listRangeInReverse(n,length));
           length++;
       }

       //This line actually reverst the entire list, so that the expected number pattern is printed
       Collections.reverse(numberPatternItems);
        for(Integer x: numberPatternItems){
            System.out.println(x);
        }
    }

    private static Integer listRangeInReverse(int k, int count) {
        StringBuilder sb = new StringBuilder();
        for (int c = k; c >=count;c--){
            sb.append(c);
        }
        return Integer.parseInt(sb.toString());
    }
}

Just a Note

There are N-ways of writing the above codes, please feel free to explore by playing around it.