Advertisement
User_codes

Untitled

Mar 7th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. // --------------- P1 - multiplication table ---------------------------------------------------
  2. import java.util.Scanner;
  3.  
  4. public class main {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. System.out.println("Enter the number");
  8. int a = sc.nextInt();
  9. System.out.println("\nTable is ");
  10. for(int i=1 ; i<=10; i++){
  11. System.out.println(a + " * "+ i + " = "+a*i);
  12. }
  13. }
  14.  
  15. }
  16.  
  17. // --------------- P2 - Quadratic Equation Solver -----------------------------------------------
  18. public class QuadraticEquationSolver
  19. {
  20. public static void main(String[] args)
  21. {
  22.  
  23. int a = 2;
  24. int b = 6;
  25. int c = 4;
  26.  
  27.  
  28.  
  29. double temp1 = Math.sqrt(b * b - 4 * a * c);
  30.  
  31. double root1 = (-b + temp1) / (2*a) ;
  32. double root2 = (-b - temp1) / (2*a) ;
  33.  
  34. System.out.println("The roots of the Quadratic Equation \"2x2 + 6x + 4 = 0\" are "+root1+" and "+root2);
  35.  
  36. }
  37. }
  38.  
  39. // --------------- P3 - Add 2 matrix -----------------------------------------------
  40. import java.util.Scanner;
  41.  
  42. public class AddTwoMatrix
  43. {
  44. public static void main(String args[])
  45. {
  46. int m, n, c, d;
  47. Scanner in = new Scanner(System.in);
  48.  
  49. System.out.println("Enter the number of rows and columns of matrix");
  50. m = in.nextInt();
  51. n = in.nextInt();
  52.  
  53. int first[][] = new int[m][n];
  54. int second[][] = new int[m][n];
  55. int sum[][] = new int[m][n];
  56.  
  57. System.out.println("Enter the elements of first matrix");
  58.  
  59. for ( c = 0 ; c < m ; c++ )
  60. for ( d = 0 ; d < n ; d++ )
  61. first[c][d] = in.nextInt();
  62.  
  63. System.out.println("Enter the elements of second matrix");
  64.  
  65. for ( c = 0 ; c < m ; c++ )
  66. for ( d = 0 ; d < n ; d++ )
  67. second[c][d] = in.nextInt();
  68.  
  69. for ( c = 0 ; c < m ; c++ )
  70. for ( d = 0 ; d < n ; d++ )
  71. sum[c][d] = first[c][d] + second[c][d];
  72.  
  73. System.out.println("Sum of entered matrices:-");
  74.  
  75. for ( c = 0 ; c < m ; c++ )
  76. {
  77. for ( d = 0 ; d < n ; d++ )
  78. System.out.print(sum[c][d]+"\t");
  79.  
  80. System.out.println();
  81. }
  82. }
  83. }
  84.  
  85. // --------------- P4 - Sort names in ascending order -----------------------------------------------
  86. import java.util.*;
  87. class Sorting
  88. {
  89. void sortStrings()
  90. {
  91. Scanner s = new Scanner(System.in);
  92. System.out.println("Enter the value of n: ");
  93. int n = s.nextInt();
  94. String[] str = new String[n];
  95. System.out.println("Enter strings: ");
  96. for(int i = 0; i < n; i++)
  97. {
  98. str[i] = new String(s.next());
  99. }
  100. for(int i = 0; i < n; i++)
  101. {
  102. for(int j = i+1; j < n; j++)
  103. {
  104. if(str[i].compareTo(str[j])>0)
  105. {
  106. String temp = str[i];
  107. str[i] = str[j];
  108. str[j] = temp;
  109. }
  110. }
  111. }
  112. System.out.println("Sorted list of strings is:");
  113. for(int i = 0; i < n ; i++)
  114. {
  115. System.out.println(str[i]);
  116. }
  117. }
  118. }
  119. class Driver
  120. {
  121. public static void main(String[] args)
  122. {
  123. Sorting obj = new Sorting();
  124. obj.sortStrings();
  125. }
  126. }
  127.  
  128. // --------------- P5 - Java inheritance using extend -----------------------------------------------
  129. class Employee
  130. {
  131. float salary=40000;
  132. }
  133. class Programmer extends Employee
  134. {
  135. int bonus=10000;
  136. public static void main(String args[])
  137. {
  138. Programmer p=new Programmer();
  139. System.out.println("Programmer salary is:"+p.salary);
  140. System.out.println("Bonus of Programmer is:"+p.bonus);
  141. }
  142. }
  143.  
  144. // --------------- P6A - Method Overloading -----------------------------------------------
  145. class DisplayOverloading
  146. {
  147. public void disp(char c)
  148. {
  149. System.out.println(c);
  150. }
  151. public void disp(char c, int num)
  152. {
  153. System.out.println(c + " "+num);
  154. }
  155. public void disp(int num)
  156. {
  157. System.out.println(num);
  158. }
  159. }
  160. class test
  161. {
  162. public static void main(String args[])
  163. {
  164. DisplayOverloading obj = new DisplayOverloading();
  165. obj.disp('a');
  166. obj.disp('a',10);
  167. obj.disp(10);
  168. }
  169. }
  170. // --------------- P6B - Method Overriding ------------------------------------------------
  171. class Vehicle
  172. {
  173. void run()
  174. {
  175. System.out.println("Vehicle is running");
  176. }
  177. }
  178. class Bike2 extends Vehicle
  179. {
  180. void run()
  181. {
  182. System.out.println("Bike is running safely");
  183. }
  184.  
  185. public static void main(String args[])
  186. {
  187. Bike2 obj = new Bike2();
  188. obj.run();
  189. }
  190. }
  191.  
  192. // --------------- P7 - User Defined Exception -----------------------------------------------
  193.  
  194. // TestCustomException1.java main
  195. class TestCustomException1 {
  196. static void validate(int age) throws InvalidAgeException {
  197. if (age < 18)
  198. throw new InvalidAgeException("not valid");
  199. else
  200. System.out.println("welcome to vote");
  201. }
  202.  
  203. public static void main(String args[]) {
  204. try {
  205. validate(13);
  206. } catch (Exception m) {
  207. System.out.println("Exception occurred: " + m);
  208. System.out.println("rest of the code...");
  209. }
  210. }
  211. }
  212.  
  213. // InvalidAgeException.java
  214. class InvalidAgeException extends Exception
  215. {
  216. InvalidAgeException(String s) {
  217. super(s);
  218. }
  219. }
  220.  
  221. // --------------- P8 - Thread Life Cycle -----------------------------------------------
  222. class A extends Thread {
  223. public void run()
  224. {
  225. System.out.println("Thread A");
  226. System.out.println("i in Thread A ");
  227. for(int i=1;i<=5;i++)
  228. {
  229. System.out.println("i = " + i);
  230. }
  231. System.out.println("Thread A Completed.");
  232. }
  233. }
  234.  
  235. class B extends Thread {
  236.  
  237. public void run()
  238. {
  239. System.out.println("Thread B");
  240. System.out.println("i in Thread B ");
  241. for(int i=1;i<=5;i++)
  242. {
  243. System.out.println("i = " + i);
  244. }
  245. System.out.println("Thread B Completed.");
  246. }
  247. }
  248.  
  249.  
  250. public class Main1 {
  251.  
  252. public static void main(String[] args) {
  253.  
  254. //life cycle of Thread
  255. // Thread's New State
  256. A threadA = new A();
  257. B threadB = new B();
  258.  
  259. threadA.start();
  260.  
  261. threadB.start();
  262. System.out.println("Main Thread End");
  263. }
  264. }
  265.  
  266.  
  267. ----------------------------------------------
  268.  
  269. Explanation of the Quadratic Equation Solver Code
  270.  
  271. This code solves the quadratic equation of the form:
  272.  
  273. ax^2 + bx + c = 0
  274.  
  275. Where:
  276.  
  277. a = 2
  278.  
  279. b = 6
  280.  
  281. c = 4
  282.  
  283.  
  284.  
  285. ---
  286.  
  287. Step 1: Understanding the Quadratic Formula
  288.  
  289. The roots of a quadratic equation are calculated using the formula:
  290.  
  291. x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
  292.  
  293.  
  294. ---
  295.  
  296. Step 2: Breaking Down the Code
  297.  
  298. int a = 2; int b = 6; int c = 4;
  299. → Defines the coefficients of the equation.
  300.  
  301. double temp1 = Math.sqrt(b * b - 4 * a * c);
  302. → Calculates the discriminant (D = b² - 4ac).
  303.  
  304. If D > 0 → 2 real roots
  305.  
  306. If D = 0 → 1 real root (repeated)
  307.  
  308. If D < 0 → Complex roots (this code doesn't handle that case).
  309.  
  310.  
  311. double root1 = (-b + temp1) / (2 * a);
  312.  
  313. double root2 = (-b - temp1) / (2 * a);
  314. → These calculate the two possible roots using the quadratic formula.
  315.  
  316. System.out.println(...)
  317. → Displays the calculated roots.
  318.  
  319.  
  320.  
  321. ---
  322.  
  323. Step 3: Expected Output
  324.  
  325. The roots of the Quadratic Equation "2x2 + 6x + 4 = 0" are -1.0 and -2.0
  326.  
  327.  
  328. ---
  329.  
  330. Step 4: Important Notes
  331.  
  332. ✅ The code assumes the discriminant is non-negative (real roots only).
  333. ✅ To improve the code, you can add conditions to handle complex roots.
  334.  
  335. Would you like me to modify the code to handle complex roots as well?
  336.  
  337.  
  338.  
  339.  
  340.  
  341. This Java program demonstrates how to define and use a custom exception (InvalidAgeException) to handle specific error scenarios in a program. Here's a breakdown of the code and its functionality:
  342.  
  343. 1. InvalidAgeException Class
  344.  
  345. This is a custom exception class that extends the built-in Exception class.
  346.  
  347. The constructor accepts a String message and passes it to the parent Exception class using super(s). This allows the custom exception to include detailed error messages.
  348.  
  349.  
  350. 2. TestCustomException1 Class
  351.  
  352. Method validate(int age):
  353.  
  354. This method checks if the age parameter is less than 18.
  355.  
  356. If the condition is true, it throws an instance of InvalidAgeException with a message "not valid".
  357.  
  358. If the age is 18 or more, it prints "welcome to vote".
  359.  
  360.  
  361. main Method:
  362.  
  363. The main method attempts to call validate(13) (an age below 18) inside a try block.
  364.  
  365. When the InvalidAgeException is thrown by validate, it is caught by the catch block.
  366.  
  367. The catch block prints the message "Exception occurred: not valid".
  368.  
  369. After handling the exception, the program continues executing and prints "rest of the code...".
  370.  
  371.  
  372. -------------------------------
  373.  
  374.  
  375. Theory of the Code
  376.  
  377. This Java program demonstrates the life cycle of threads by creating and running two threads (A and B) that execute concurrently.
  378.  
  379. 1. Thread Creation Using extends Thread
  380.  
  381. The classes A and B extend the Thread class.
  382.  
  383. Each class overrides the run() method, which contains the code that will execute when the thread starts.
  384.  
  385.  
  386. 2. run() Method - Defines Thread Behavior
  387.  
  388. In both classes, the run() method prints messages and loops from 1 to 5, displaying values of i.
  389.  
  390. The run() method is where the thread's task is defined. It doesn’t start automatically — you must call .start() for the thread to begin.
  391.  
  392.  
  393. 3. Thread Initialization and Starting
  394.  
  395. In the main() method:
  396.  
  397. A threadA = new A(); → Creates a Thread A object (in New State).
  398.  
  399. B threadB = new B(); → Creates a Thread B object (in New State).
  400.  
  401. Calling .start() on each thread moves them to the Runnable State, where they are ready to run when the CPU assigns time.
  402.  
  403.  
  404. 4. Thread Execution - Random Order
  405.  
  406. Since both threads are started close together, their output order is unpredictable.
  407.  
  408. Java’s thread scheduler decides the execution order, meaning Thread A or Thread B may finish first.
  409. Thread A and B run concurrently and order of their output is non -deterministic as it depends on JVM thread scheduler.
  410.  
  411.  
  412. ---
  413.  
  414. 5. Main Thread Behavior
  415.  
  416. The main() method prints "Main Thread End" immediately after starting both threads.
  417.  
  418. Since the main thread doesn’t wait for other threads to finish (unless explicitly told using .join()), this message may appear before, during, or after the threads finish
  419.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement