Monday 30 September 2019

How to Push an Existing Project to GitHub

         In daily basis developer work is to do code check in, check out, pull, fetch and so on. But in the current post, we will learn how to push the existing local project to GitHub repository.

For example:- I have a project EmployeeManagement(EMS) in my local but no one can able to access or modify code without creating and pushing code to GitHub repository.

The steps to push local project to GitHub repository as follows,


1) Create a repository on GitHub.

Create a GitHub account if you don't have.
Then Go to Account --> Your  repositories 
Click on New as shown in below screenshot. 

GitHub New Repo
Github New Repo Creation









When click on New button, will open Create New Repository page.

Create Repository
Create Repository

Provide repository name(Usually project name)

Select public or private repository option

     To avoid errors, do not initialize the new repository with README, 
license, or gitignore files. You can add these files after your project has been pushed to GitHub.
Click on Button/action ""Create repository"

2) Initialize the local directory as a Git repository.

    Change the current working directory to your local project and then initialize the local directory as a git repository using command "git init"

e.g your project directory is C:\\ProjectRepo\EMS
then 


C:\ProjectRepo\EMS>git init


3) Add the files in your new local repository(using git add command). This stages them for the first commit.

Add files to the GitHub,



C:\ProjectRepo\EMS>git add .



If you want to add each file, then use git add "file path" 


C:\ProjectRepo\EMS>git add "file path to add to git"


# Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.


4) Commit the files that you've staged in your local repository.

Use command -  git commit -m "First commit" to commit the added file to gitHub.


C:\ProjectRepo\EMS>git commit -m "First commit"


5) At the top of your GitHub repository's Quick Setup page, click  to copy the remote repository URL.

Copy below HTTPS or SSH Project URL

Remote Repo HTTPS o SSH URL
         
    In the Command prompt, add the URL for the remote repository where your local repository will be pushed.
>git remote add origin "remote repository URL"



C:\ProjectRepo\EMS>git remote add origin "remote repo HTTPS or SSH URL"


6) Push the changes in your local repository to GitHub.

 Using git push command, you can the committed files to the remote repository as follow,


C:\ProjectRepo\EMS>git push origin master


master - is the branch name.


Related Post:--
Permission denied(publickey), fatal:couldn't read from remote repository - Gihub Error

Saturday 28 September 2019

Java Program to Perform Matrix Addition and Subtraction

          In the previous post, written code for matrix transpose. In the current post, I have written a java program to perform a simple matrix addition and subtraction. 

Below is the source code of the Java Program to Perform Matrix Multiplication,

MatrixAdditionSubtraction.java,

package com.example.demo;

public class MatrixAdditionSubtraction {

        public static void main(String[] args) {

                int[][] a = { { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
                int[][] b = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

                int rows = 3;
                int columns = 3;

                int[][] c = new int[rows][columns];

                for (int i = 0; i < rows; i++) {
                       for (int j = 0; j < columns; j++) {
                              c[i][j] = a[i][j] + b[i][j];
                       }
                }

                System.out.println("The sum of the two matrices is");

                for (int i = 0; i < rows; i++) {
                       for (int j = 0; j < columns; j++) {
                              System.out.print(c[i][j] + " ");
                       }
                       System.out.println();
                }

                for (int i = 0; i < rows; i++) {
                       for (int j = 0; j < columns; j++) {
                               c[i][j] = a[i][j] - b[i][j];
                       }
                }

                System.out.println("The subtraction of the two matrices is");

                for (int i = 0; i < rows; i++) {
                       for (int j = 0; j < columns; j++) {
                              System.out.print(c[i][j] + " ");
                       }
                       System.out.println();
                }

        }
}

Output:--
The sum of the two matrices is
5 7 9 
11 13 15 
17 19 21 
The subtraction of the two matrices is
3 3 3 
3 3 3 
3 3 3 

Related Posts:--
1) Java code to move all zero of an integer array to the end of the array
2) Java Program to Perform Matrix Multiplication
3) Write a Java program to print the Floyd's triangle
4) Java Code to Print the Numbers as Pyramid - Examples
5) Java Program to find the Missing Number in an Array
6) Java program to print the first n prime numbers
7) Swap two variables without using third variable in Java
8) Java program to find second largest number in an array
9) Sort a binary array using one traversal in Java
10) Difference between NoClassDefFoundError and ClassNotFoundException in Java

Sunday 15 September 2019

Java Program to Transpose a Matrix

            In the previous post, written code for matrix  multiplication, in the current post, we will a write a java code to transpose a given matrix. The transpose of matrix is to interchange the rows and columns for the given matrix. 

Example:-
Transpose of Matrix
Transpose of Matrix












The below code is the source code of java program to transpose of a given matrix,

MatrixTranspose.java,

package com.example.demo;

public class MatrixTranspose {
 
       public static void main(String[] args) {
  
               int matrix[][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
       
               int m = 3;    //no. of rows
               int n = 4;    //no. of columns
       
               System.out.println("Matrix before transpose is - ");

               for (int i = 0; i < m; i++) {
                      for (int j = 0; j < n; j++) {
                             System.out.print(matrix[i][j]+" ");
                      }
                      System.out.println(" ");
               }
       
               int transMatrix[][] = new int[n][m];
       
               System.out.println("Matrix after transpose is - ");

               for (int i = 0; i < n; i++) {
                      for (int j = 0; j < m; j++) {
                              transMatrix[i][j] = matrix[j][i];
                              System.out.print(transMatrix[i][j]+" ");
                      }
                      System.out.println(" ");
               }  
       }
}
Output:--
Matrix before transpose is - 
1   2   3   4  
5   6   7   8  
9 10 11 12  
Matrix after transpose is - 
1 5  9  
2 6 10  
3 7 11  
4 8 12

Related Posts:--
1) Java code to move all zero of an integer array to the end of the array
2) Java Program to Perform Matrix Multiplication
3) Write a Java program to print the Floyd's triangle
4) Java Code to Print the Numbers as Pyramid - Examples
5) Java Program to find the Missing Number in an Array
6) Java program to print the first n prime numbers
7) Swap two variables without using third variable in Java
8) Java program to find second largest number in an array
9) Sort a binary array using one traversal in Java
10) Difference between NoClassDefFoundError and ClassNotFoundException in Java

Saturday 14 September 2019

Java Program to Perform Matrix Multiplication

          In the current post, I have written a java program to perform a simple matrix multiplication. For matrix multiplication the column of the first matrix should be equal to the row of the second matrix, then only we can perform matrix multiplication.

         In matrix multiplication, one row element of first matrix is multiplied by all columns of second matrix.
Matrix multiplication
Matrix multiplication example













Below is the source code of the Java Program to Perform Matrix Multiplication,

MatrixMultiplication.java,

package com.example.demo;

public class MatrixMultiplication {
 
        public static void main(String[] args) {
  
                int a[][] = {{1,2,3},{4,5,6},{7,8,9}};    
  
                int b[][] = {{1,2,3},{4,5,6},{7,8,9}};    
      
                int c[][] = new int[3][3];  //rows - 3 and columns -3 
      
                for (int i = 0; i < 3; i++) {    
   
                       for (int j = 0; j < 3; j++) {    
    
                                 c[i][j] = 0;      
    
                                 for (int k = 0; k < 3; k++) {      
     
                                       c[i][j]+= a[i][k]*b[k][j];      
    
                                 } 
    
                                 System.out.print(c[i][j]+" ");   
                         } 
   
                        System.out.println(); 
                }    
        }
}
Output:--
30 36 42 
66 81 96 
102 126 150


Related Posts:--
1) Java code to move all zero of an integer array to the end of the array
2) Write a Java program to print the Floyd's triangle
3) Java Code to Print the Numbers as Pyramid - Examples
4) Java Program to find the Missing Number in an Array
5) Java program to print the first n prime numbers
6) Difference between NoClassDefFoundError and ClassNotFoundException in Java
7) Swap two variables without using third variable in Java
8) Java program to find second largest number in an array
9) Sort a binary array using one traversal in Java

Monday 9 September 2019

Java code to move all zero of an integer array to the end of the array

          In this post, we will implement a java code to move all zero's of given array integers to an end of an array. Given array is mixed of zero's and non zero's numbers.

For example,

Input array is = {2,1,0,3,5,0,9,0}
Output array is = {2,1,3,5,9,0,0,0}


package com.example.demo;

public class ZeroNonZerosSeparate {
 
          public static void main(String[] args) {
  
                   int[] nums = {2,1,0,3,5,0,9,0};
  
                   int index = 0;
                   
                   for (int i = 0; i<nums.length; i++) {
                         if (nums[i] != 0) {
                                nums[index] = nums[i];
                                index++;
                         }
                   }
  
                   while (index < nums.length) {
                          nums[index] = 0;
                          index++;
                   }
  
                   for (int i = 0; i<nums.length; i++) {
                          System.out.print(nums[i]+" ");
                   }
           }

 }

Output:-
2 1 3 5 9 0 0 0 

Saturday 7 September 2019

Difference between NoClassDefFoundError and ClassNotFoundException in Java

        Both NoClassDefFoundError and ClassNotFoundException occurs when particular class is not found at run time.They related to java classpath only but occurs different scenario's.

Difference:-

ClassNotFoundException Vs NoClassDefFoundError
ClassNotFoundException Vs NoClassDefFoundError

1) Java Program to illustrate ClassNotFoundException

    The below code is to throw the ClassNotFoundException because JavaClass class is not found in the classpath.



ClassNotFoundExceptionExample.java

package com.example.demo;

public class ClassNotFoundExceptionExample {
 
         public static void main(String[] args) {
  
                  try {
                       Class.forName("JavaClass");
                  } catch (Exception ex) {
                       ex.printStackTrace();
                  }
         }

}
Output:-
java.lang.ClassNotFoundException: JavaClass
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.example.demo.ClassNotFoundExceptionExample.main(ClassNotFoundExceptionExample.java:8)


2) Program to illustrate NoClassDefFoundError

      For this you don't use any IDE, use command prompt, compile and run separately. In the below code example, created Employee class in the current class. After compilation JVM will create two classes NoClassDefFOundErrorExample.class and Example.class, before run main class delete Example.class and run the main class.

NoClassDefFoundErrorExample.java

package com.example.demo;

public class NoClassDefFOundErrorExample {
 
         public static void main(String[] args) {
                  Example ex = new Example();
                  ex.getNumber();
         }
}

Run this command for compilation,

>javac NoClassDefFoundErrorExample.java

After compilation, delete Example.class file and run the below command,

> java NoClassDefFoundErrorExample

Exception in thread "main" java.lang.NoClassDefFoundError.


Related Posts:-
1) Exception Handling Interview questions and answers
2) String Interview Questions and Answers
3) Java Program to check the given number is a palindrome or not
4) Factory Design Pattern in Java
5) Java 8 features with examples
6) Difference between Loose Coupling and Tight Coupling in Java With Examples.
7) Difference between final,finalize and finally in Java