Advertisement
brandblox

ruby on rails

Apr 27th, 2025
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.HashMap;
  3.  
  4. public class prac1 {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner sc = new Scanner(System.in);
  8.         System.out.println("Enter a sentence: ");
  9.         String sentence = sc.nextLine();
  10.         String word[] = sentence.toLowerCase().split(" ");
  11.         HashMap<String, Integer> wordcount = new HashMap<>();
  12.         for(String w:word)
  13.         {
  14.             w = w.replaceAll("[.,!?]","" );
  15.             if (wordcount.containsKey(w))
  16.             {
  17.                 wordcount.put(w,wordcount.get(w)+1);
  18.             }
  19.             else{
  20.                 wordcount.put(w,1);
  21.             }
  22.         }
  23.  
  24.         System.out.println("Enter the word you want to find the frequency of: ");
  25.         String searchWord = sc.nextLine().toLowerCase();
  26.         searchWord =  searchWord.replaceAll("[,.!?]", "");
  27.         if (wordcount.containsKey(searchWord))
  28.         {
  29.             System.out.println("Frequency of '" + searchWord + "' = " + wordcount.get(searchWord));
  30.         }
  31.         else
  32.         {
  33.             System.out.println("The word '" + searchWord + "' is not found in the sentence.");
  34.         }
  35.     }
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. import java.util.Scanner;
  44. import java.util.HashMap;
  45. public class hashmap {
  46.     public static void main(String[] args) {
  47.         Scanner scanner = new Scanner(System.in);
  48.         System.out.print("Enter a sentence: ");
  49.         String sentence = scanner.nextLine();
  50.         String[] words = sentence.toLowerCase().split(" ");
  51.         HashMap<String, Integer> wordCount = new HashMap<>();
  52.         for (String word : words) {
  53.             word = word.replaceAll("[.,!?]", "");
  54.             if (wordCount.containsKey(word)) {
  55.                 wordCount.put(word, wordCount.get(word) + 1);
  56.             } else {
  57.                 wordCount.put(word, 1);
  58.             }
  59.         }
  60.         System.out.println("Word counts:");
  61.         for (String word : wordCount.keySet()) {
  62.             System.out.println(word + " = " + wordCount.get(word));
  63.         }
  64.         scanner.close();
  65.     }
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. import java.util.Scanner;
  73.  
  74. public class mat {
  75.     public static void main(String[] args) {
  76.         Scanner sc = new Scanner(System.in);
  77.  
  78.         // Input size of matrices
  79.         System.out.print("Enter number of rows for first matrix: ");
  80.         int rows1 = sc.nextInt();
  81.         System.out.print("Enter number of columns for first matrix: ");
  82.         int cols1 = sc.nextInt();
  83.         System.out.print("Enter number of rows for second matrix: ");
  84.         int rows2 = sc.nextInt();
  85.         System.out.print("Enter number of columns for second matrix: ");
  86.         int cols2 = sc.nextInt();
  87.         // sc.close();
  88.  
  89.         // Check if multiplication is possible
  90.         if (cols1 != rows2) {
  91.             System.out.println("Matrix multiplication is not possible!");
  92.             return;
  93.         }
  94.  
  95.         int[][] matrix1 = new int[rows1][cols1];
  96.         int[][] matrix2 = new int[rows2][cols2];
  97.         int[][] result = new int[rows1][cols2];
  98.  
  99.         // Input elements for first matrix
  100.         System.out.println("Enter elements of first matrix:");
  101.         for (int i = 0; i < rows1; i++) {
  102.             for (int j = 0; j < cols1; j++) {
  103.                 matrix1[i][j] = sc.nextInt();
  104.             }
  105.         }
  106.  
  107.         // Input elements for second matrix
  108.         System.out.println("Enter elements of second matrix:");
  109.         for (int i = 0; i < rows2; i++) {
  110.             for (int j = 0; j < cols2; j++) {
  111.                 matrix2[i][j] = sc.nextInt();
  112.             }
  113.         }
  114.  
  115.         // Matrix multiplication
  116.         for (int i = 0; i < rows1; i++) {
  117.             for (int j = 0; j < cols2; j++) {
  118.                 for (int k = 0; k < cols1; k++) {
  119.                     result[i][j] += matrix1[i][k] * matrix2[k][j];
  120.                 }
  121.             }
  122.         }
  123.  
  124.         // Print result
  125.         System.out.println("Resultant Matrix:");
  126.         for (int i = 0; i < rows1; i++) {
  127.             for (int j = 0; j < cols2; j++) {
  128.                 System.out.print(result[i][j] + " ");
  129.             }
  130.             System.out.println();
  131.         }
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement