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: