Multiply two matrices

  1. package com.instanceofjava;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Multiply2Matrices{
  6.  
  7.    public static void main(String args[])
  8.    {
  9.  
  10.       int m, n, p, q, sum = 0, c, d, k;
  11.  
  12.       Scanner in = new Scanner(System.in);
  13.  
  14.       System.out.println("Enter the number of rows and columns of first matrix");
  15.  
  16.       m = in.nextInt();
  17.       n = in.nextInt();
  18.  
  19.       int first[][] = new int[m][n];
  20.  
  21.      System.out.println("Enter the elements of first matrix");
  22.  
  23.       for ( c = 0 ; c < m ; c++ )
  24.          for ( d = 0 ; d < n ; d++ )
  25.             first[c][d] = in.nextInt();
  26.  
  27.       System.out.println("Enter the number of rows and columns of second matrix");
  28.  
  29.       p = in.nextInt();
  30.       q = in.nextInt();
  31.  
  32.      if ( n != p )
  33.          System.out.println("Matrices with entered orders can't be multiplied with each other.");
  34.       else
  35.       {
  36.          int second[][] = new int[p][q];
  37.          int multiply[][] = new int[m][q];
  38.          System.out.println("Enter the elements of second matrix");
  39.  
  40.          for ( c = 0 ; c < p ; c++ )
  41.             for ( d = 0 ; d < q ; d++ )
  42.                second[c][d] = in.nextInt();
  43.  
  44.          for ( c = 0 ; c < m ; c++ )
  45.          {
  46.             for ( d = 0 ; d < q ; d++ )
  47.             {   
  48.                for ( k = 0 ; k < p ; k++ )
  49.                {
  50.                   sum = sum + first[c][k]*second[k][d];
  51.                }
  52.                multiply[c][d] = sum;
  53.                sum = 0;
  54.             }
  55.          }
  56.  
  57.          System.out.println("Multiplication of entered matrices:-");
  58.  
  59.          for ( c = 0 ; c < m ; c++ )
  60.          {
  61.             for ( d = 0 ; d < q ; d++ )
  62.                System.out.print(multiply[c][d]+"\t");
  63.             System.out.print("\n");
  64.          }
  65.  
  66.       }
  67.  
  68.    }
  69. }  

OutPut:


  1. Enter the number of rows and columns of first matrix
  2.  
  3. 2 2
  4.  
  5. Enter the elements of first matrix
  6.  
  7. 2 2
  8. 2 3
  9.  
  10. Enter the number of rows and columns of second matrix
  11.  
  12. 2 2
  13.  
  14. Enter the elements of second matrix
  15.  
  16. 1 1
  17. 1 1
  18.  
  19. Multiplication of entered matrices:-
  20.  
  21. 4    4    
  22. 5    5  


add two matrices

  1. package com.instanceofjava;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Add2Matrix
  6. {
  7.  
  8.    public static void main(String args[])
  9.    {
  10.  
  11.       int rows, cols, c, d;
  12.  
  13.       Scanner in = new Scanner(System.in);
  14.  
  15.       System.out.println("Please Enter number of rows and columns");
  16.  
  17.       rows = in.nextInt();
  18.       cols  = in.nextInt();
  19.  
  20.       int first[][] = new int[rows][cols];
  21.       int second[][] = new int[rows][cols];
  22.       int sum[][] = new int[rows][cols];
  23.  
  24.       System.out.println("Please Enter elements of first matrix");
  25.  
  26.       for (  c = 0 ; c < rows ; c++ )
  27.          for ( d = 0 ; d < cols ; d++ )
  28.             first[c][d] = in.nextInt();
  29.  
  30.       System.out.println("Please Enter elements of second matrix");
  31.  
  32.       for ( c = 0 ; c < rows ; c++ )
  33.          for ( d = 0 ; d < cols ; d++ )
  34.             second[c][d] = in.nextInt();
  35.  
  36.       for ( c = 0 ; c < rows ; c++ )
  37.          for ( d = 0 ; d < cols ; d++ )
  38.              sum[c][d] = first[c][d] + second[c][d];  //replace '+' with '-' to subtract matrices
  39.  
  40.       System.out.println("Sum of entered matrices:-");
  41.  
  42.       for ( c = 0 ; c < rows ; c++ )
  43.       {
  44.          for ( d = 0 ; d < cols ; d++ )
  45.             System.out.print(sum[c][d]+"\t");
  46.          System.out.println();
  47.       }
  48.    }

  49.  }
  50.    
     

 

Output:

  1. Please Enter number of rows and columns
  2.  
  3. 3
  4. 3
  5.  
  6. Please Enter elements of first matrix
  7.  
  8. 1 1 1
  9. 1 1 1
  10. 1 1 1
  11.  
  12. Please Enter elements of second matrix
  13.  
  14. 2 2 2
  15. 2 2 2
  16. 2 2 2
  17.  
  18. Sum of entered matrices:-
  19.  
  20. 3    3    3    
  21. 3    3    3    
  22. 3    3    3  

You might like:



IO Streams

The basic idea of  IOStreams is
  • Storing and reading data from files
  • reading data from keyword
First let us understand some basic terminology.

Persistent media:

  • The environment that allows us to store data permanently is called persistent media.
  • We can store data permanently in three places
  1. File
  2. Database
  3. Remote Computer 

Persistence:

  •  The process of storing data permanently in a persistence media.

Persistence Logic:

  • The logic that persist data in a persistence media is called persistence logic.
  • Ex: IOStreams based logic, JDBC logic, Networking based logic.

Persistence Technologies:

  •  The technology that provides API to develop persistence logic is called persistence technology.
  • Well known persistence technologies are 
  1. IOStreams : to persist data in files
  2. JDBC, EJB, Hibernate: to persist data in db
  3. Networking: to persist data in remote computer

Where can we store data permanently? 

  • In persistence medias either files or in databases.
  • Storing data in variables and arrays  is temporary. Data will be lost when a local variable goes out of scope or when the program terminates.
  • programmers use files or databases for long term storage of large amount of data. it is available even after termination of the program. We refer to data maintained on files as persistent data, because the data exists beyond the duration of the program execution.
  • To store data in files and databases  Oracle has given in  built API. We all need to do is creating the particular class object calling methods for storing and reading data from that persistence media.
  • IOStreams API is given to store and read data from files
  • JDBC API is given to store and read data from Databases. 

How java application can store or read data from a file?

  • Using stream object.

Introduction to Streams:

  • Stream is logical connection between java program and a file.
  • To store the data in the persistence media there should be a way to connect to persistence media from java application either physically or logically. Stream provides logical connection.
  • "Its a continuous flow of data between java program and persistence media"

Direction of stream flow:

  • Stream has a direction and its direction depends on the viewer's view. In java the viewer is java Application. If you look from Java Application it is sending out from Java Application.


Type of Streams:

  • Generally Streams are divided in to two types based on data flow direction.
  1. InputStream.
  2. OutPutStream.

InputStream:

  • The stream that allows data to come into the java application from the persistent media is called input Stream.

OutPutStream:

  • The stream that allows data to send out from the java application to be stored into the persistence media is called OutPutStream.
  • Basically InputStreams are used to read data from a persistence media , and Oputstreams are used to write or store  data in a persistence media from a java application

Types of Java Streams:

  • In java we are allowed to send data through streams only either in the format of bytes or characters. So based on the type of the data passed through streams .
  • In java streams are divided in to two types.
  1. Binary Streams.
  2. Character Streams.

 1.Binary Streams: 

  • The streams which read and write data in the format of  bytes is called Character streams.

 2.Character Streams:

  • The streams which read and write data in the format of  characters is called Character streams.
Java Streams

Programing Inrerview Questions on try catch

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("instance of java");    
  10.  
  11.     }
  12.  
  13. }
  14. }





2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("try block");   
  10.  
  11.     }
  12.    finally{
  13.  
  14.     System.out.println("finally block");    
  15.  
  16.   }
  17.  
  18. }
  19. }







3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.             
  8.         try {
  9.  
  10.             int a = 0;
  11.             int b = 10;
  12.             int c = b / a;
  13.  
  14.             System.out.print("try block");
  15.  
  16.         }
  17.         catch(Exception e) {
  18.  
  19.               System.out.print("catch block");
  20.  
  21.         }    
  22.  
  23. }
  24. }




String programing interview questions

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.charAt(3));
  9. }
  10. }



2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.length());
  9. }
  10. }








3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str1="abc";
  8.       String str2="abc";
  9.  
  10.        System.out.println(str1.equals(str2));
  11. }
  12. }






4. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.         String str1 = "abc";
  8.         String str2 = "abc";
  9.         String str3= new String("abc");
  10.  
  11.         System.out.println("str1 == str2 ? "+(str1==str2));
  12.         System.out.println("str1 == str3 ? "+(str1==str3));
  13.         System.out.println("str1 equals str3 ? "+(str1.equals(str3)));
  14.  
  15.  
  16. }
  17. }






Inheritance programming interview questions

1. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("super class method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.   System.out.println("sub class method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  SubDemo subobj=new SubDemo();
  10.  subobj.show();
  11. }







2. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.  super.x=y+2;
  9.   System.out.println("x="+super.x+"y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }




3. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo {

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2.  
  3. public class SubDemo extends SuperDemo{
  4.  
  5.  int y;
  6.  
  7. public void show(){
  8.  
  9.  super.x=y+2;
  10.   System.out.println("x="+super.x+"y="+y);
  11.  
  12. }
  13.  
  14.  public static void main(String args[]){
  15.  
  16.  SubDemo subobj=new SubDemo();
  17.  subobj.x=2;
  18.  subobj.y=2;
  19.  subobj.show();
  20.  
  21. }




4. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4.  int x;
  5. SuperDemo(){
  6.  
  7.  x=24;

  8. }
  9. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.   System.out.println("x="+super.x);
  9.   System.out.println("y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }





Arrays in java

Array:

  • Array is referenced data type used to store multiple values.
  • we can not change size of array at runtime.
  • memory can not be altered Once the memory has been allocated for an array at the run time.
  • Arrays are objects


Need of array:

  • In  real time projects array is used to collect same type of objects to send all values with single method call.

Problem of primitive data types:

  • We can not store values in continuous memory locations using primitive data types .
  • We have two problems due to this limitation

1.we cant store multiple values:

  • If we want to store multiple values , say 1 to 10 , we must create 10 variables .
  • All those 10 variables are created at different locations.

2.In single method call we can not pass multiple values :

  • we can not pass all values to the remote computer with single network call or method call.Using primitive variables  , which increases burden on network and also increase number of lines of code in program.

Solution:

  • Values must be stored in continuous memory locations with single variable name. To solve above two problems ,
  • This can be possible using array.
  • In java array is reference data type . it is used to store fixed number of multiple values of same type in continuous memory locations.
  • Like other data types array is not a keyword it is a concept. it creates continuous memory locations using other primitive or reference types.

 Array Limitation: 

  •  Array size is fixed , means we can not increase or decrease its size after its creation.

Array Declaration:

  • <Accessibility modifier><Modifier><datatype>[] <array variable name>;

For example:

  • public static int[] i;
  • public static Example[] e;
  • Like in C or C++ , in java we can not mention array size in declaration part. It leads Compile time error.
  • int[5] i; Compile time Error: illegal start of expression
  • int [] i;

Possible declaration:

  1. After data type : int[] i;
  2. Before variable name : int []i;
  3. After variable name : int i[];

Type of arrays:

  1.  Single dimensional : int [] i;
  2. Two dimensional : int[][] i;
  3. Three dimensional : int [][][] i;

Array object creation:

  • <Accessibility Modifier><Modifier><data type>[]<array name>={<list of values with , separator>};
  • int [] ia={10,20,30,40};
Select Menu