Monday, 7 September 2020

Write a note on compiling and executing a Java program

 Write a note on compiling and executing a Java program


First, we have to open a text file using notepad or any text editor and then write the java program and then save this java program by any name with extention java example <anyname.java>.
Now compile the java program and then run the java program.

Compilation process of java using command prompt:

  •    open command prompt
  •    goto the place where java program is saved.
  •    now type below command

                d:\> javac   <filename>.java


If no compilation errors are there then we get .class file which consists of bytecode.
Once after sucess compilation we have to run the java program

Process of running the java program using command prompt:

  •      d:\> java <file name>

    
     name of the file which consists of main method and main method should be public static void main(String args[]).
 This bytecode gets interpreted on different machines.

Java virtual machine (JVM) is responsible for allocating memory space.

Friday, 4 September 2020

The add values of matrix horizontally with another vertically

 Java program which will add the values of 2d matrix horizontally with another 2d matrix vertically

import java.util.*;
class MatrixAddition{
    public static void main(String args[]){
    int matrix1[][]=new int[2][2];
    int matrix2[][]=new int[2][2];
    int res_matrix[][]=new int[2][2];
    int i, j;
   Scanner sc=new Scanner(System.in);

    // reading the elements of first matrix
    System.out.println(" enter elements of first matrix ");
    for( i=0; i<2 ;i++){
        for(j=0;j<2;j++){
            matrix1[i][j]=sc.nextInt();
        }
    }

    // reading the elements of second matrix
    System.out.println(" enter the elements of second matrix ");
    for( i=0; i<2; i++){
        for(j=0; j<2; j++){
            matrix2[i][j]=sc.nextInt();
        }
    }

    // adding two  matrices`
    for( i=0; i<2; i++){
        for (j=0; j<2; j++){
            res_matrix[i][j]= matrix1[i][j] + matrix2[j][i];
        }
    }

    // printing the resultant matrix  
    System.out.println(" the resultant matrix is  \n");
    for (i=0; i<2; i++){
        for( j=0; j<2; j++){
            System.out.print(res_matrix[i][j]+" ");
        }
        System.out.println("");
    }
  }
}

Wednesday, 2 September 2020

Java program for finding double letter sequence word in Java

 

 

 

 

Java program for finding double letter sequence word in Java

Double-letter words:

   These are the words which contain atleast one set of characters used twice consecutively.

 For example:
    add,all, bee, boo, ebb, ell, egg, fee, goo, tee, too,  see.


import java.util.*;
class DoubleSequenceWord{
    public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    System.out.println("enter a word to check whether it is double sequence or not");
    String str=sc.nextLine();
    int count=0;
        for(int i=0;i<str.length()-1;i++){
            if(str.charAt(i)==str.charAt(i+1)){
                count++;
                break;
            }
        }
        if(count>0)
            System.out.println("It is a double sequence word");
        else
            System.out.println("It is a not a double sequence word");

   }

}
       

Saturday, 29 August 2020

Java program to find heat capacity

 Java program to find heat capacity


import java.util.*;
class HeatCapacity{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter amount of heat transferred (in joules)");
        float heat=sc.nextFloat();
        System.out.println("Enter rise (difference) in temperature");
        float temperature=sc.nextFloat();
    
        System.out.println("Amount of heat supplied "+heat);
        System.out.println("Rise in temperature "+temperature);
         float heatCapacity;
         heatCapacity = heat/temperature;


        System.out.println("Heat Capacity = "+heatCapacity+"j/c");
    
    
        }
}

Sunday, 16 August 2020

Return common characters of two strings using substr in java

Java program to return common characters of two strings using substring


import java.util.*;
class CommonCharacters{
    public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    System.out.println("enter first string");
    String s1=sc.nextLine();
    System.out.println("enter second string");
    String s2=sc.nextLine();
    int index;
    System.out.println("you entered first string as "+s1);
    System.out.println("you entered second string as "+s2);
    char ch;
    String str;
    for(int i=0;i<s1.length();i++){
        index=0;
         str=s1.substring(i,i+1);
        ch=str.charAt(0);//System.out.println(ch);
        index=s2.indexOf(ch);
        if(index>=0){
            System.out.println(ch);
            //s1=s1.replaceAll(""+ch,"");
            
           }
       }
    }
}

Wednesday, 12 August 2020

Java program for words which do not contain any vowels in a sentence

How to write a java program words which do not contain any vowels in a sentence using java

 
Here we are going to see a java program to input a sentence and print those word which
  do not contain any vowels
and simultaneously count and print those words.  
 

  •   First read a sentence
  •   Now using StringTokenizer get each word
  •   Now from each word check word contains any vowel or not 


import java.util.*;
class Highest{
 public static void main(String args[]){
    
     //read a sentence
     System.out.println("enter a sentence");
     Scanner sc=new Scanner(System.in);
     String sentence=sc.nextLine();
     StringTokenizer st=new StringTokenizer(sentence);
     String word;
 
     int index=-1,count=0;
     char vowel[]={'a','e','i','o','u'};
     System.out.println("Words which do not contain vowels are ");


     // iterating each word
     while(st.hasMoreTokens()){
        word=st.nextToken();


        // check word contains any vowel or not        


        for(char c:vowel){
            index=-1;
            index = word.indexOf(c);
            if(index>=0) break;
        }
        if(index==-1){
            System.out.println(word);
            count++;
        }

    
     }//while closing
     System.out.println("Total number of words which do not contain vowels are "+count);
    }
 }

Monday, 10 August 2020

Input runs scored in a cricket and display the highest

 Input runs scored in a cricket and display the highest


In this java program we are going to see the runs scored by 11 batsman
in a cricket match and display the highest score among them.
so first read the runs scored by 11 batsman in a match and find the highest score.



import java.util.*;
class Highest{
 public static void main(String args[]){
    int score[]=new int[11];  
    Scanner sc=new Scanner(System.in);
    //reading the runs scored by individual players
    for(int i=0;i<11;i++){
        System.out.println("Enter runs scored by player "+i+1+".");
        score[i]=sc.nextInt();
    }
    //displaying the runs scored by individual players
    for(int i=0;i<11;i++){
       System.out.println("Runs scored by player"+i+1+" is "+score[i]);
    }
    //finding maximum
    int max=0;
    for(int i=0;i<11;i++){
       if(score[i]>max)
           max=score[i];
       }
    System.out.println("Highest scored runs = "+max);
    }
}

 

output: