Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --------------- P1 - multiplication table ---------------------------------------------------
- import java.util.Scanner;
- public class main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the number");
- int a = sc.nextInt();
- System.out.println("\nTable is ");
- for(int i=1 ; i<=10; i++){
- System.out.println(a + " * "+ i + " = "+a*i);
- }
- }
- }
- // --------------- P2 - Quadratic Equation Solver -----------------------------------------------
- public class QuadraticEquationSolver
- {
- public static void main(String[] args)
- {
- int a = 2;
- int b = 6;
- int c = 4;
- double temp1 = Math.sqrt(b * b - 4 * a * c);
- double root1 = (-b + temp1) / (2*a) ;
- double root2 = (-b - temp1) / (2*a) ;
- System.out.println("The roots of the Quadratic Equation \"2x2 + 6x + 4 = 0\" are "+root1+" and "+root2);
- }
- }
- // --------------- P3 - Add 2 matrix -----------------------------------------------
- import java.util.Scanner;
- public class AddTwoMatrix
- {
- public static void main(String args[])
- {
- int m, n, c, d;
- Scanner in = new Scanner(System.in);
- System.out.println("Enter the number of rows and columns of matrix");
- m = in.nextInt();
- n = in.nextInt();
- int first[][] = new int[m][n];
- int second[][] = new int[m][n];
- int sum[][] = new int[m][n];
- System.out.println("Enter the elements of first matrix");
- for ( c = 0 ; c < m ; c++ )
- for ( d = 0 ; d < n ; d++ )
- first[c][d] = in.nextInt();
- System.out.println("Enter the elements of second matrix");
- for ( c = 0 ; c < m ; c++ )
- for ( d = 0 ; d < n ; d++ )
- second[c][d] = in.nextInt();
- for ( c = 0 ; c < m ; c++ )
- for ( d = 0 ; d < n ; d++ )
- sum[c][d] = first[c][d] + second[c][d];
- System.out.println("Sum of entered matrices:-");
- for ( c = 0 ; c < m ; c++ )
- {
- for ( d = 0 ; d < n ; d++ )
- System.out.print(sum[c][d]+"\t");
- System.out.println();
- }
- }
- }
- // --------------- P4 - Sort names in ascending order -----------------------------------------------
- import java.util.*;
- class Sorting
- {
- void sortStrings()
- {
- Scanner s = new Scanner(System.in);
- System.out.println("Enter the value of n: ");
- int n = s.nextInt();
- String[] str = new String[n];
- System.out.println("Enter strings: ");
- for(int i = 0; i < n; i++)
- {
- str[i] = new String(s.next());
- }
- for(int i = 0; i < n; i++)
- {
- for(int j = i+1; j < n; j++)
- {
- if(str[i].compareTo(str[j])>0)
- {
- String temp = str[i];
- str[i] = str[j];
- str[j] = temp;
- }
- }
- }
- System.out.println("Sorted list of strings is:");
- for(int i = 0; i < n ; i++)
- {
- System.out.println(str[i]);
- }
- }
- }
- class Driver
- {
- public static void main(String[] args)
- {
- Sorting obj = new Sorting();
- obj.sortStrings();
- }
- }
- // --------------- P5 - Java inheritance using extend -----------------------------------------------
- class Employee
- {
- float salary=40000;
- }
- class Programmer extends Employee
- {
- int bonus=10000;
- public static void main(String args[])
- {
- Programmer p=new Programmer();
- System.out.println("Programmer salary is:"+p.salary);
- System.out.println("Bonus of Programmer is:"+p.bonus);
- }
- }
- // --------------- P6A - Method Overloading -----------------------------------------------
- class DisplayOverloading
- {
- public void disp(char c)
- {
- System.out.println(c);
- }
- public void disp(char c, int num)
- {
- System.out.println(c + " "+num);
- }
- public void disp(int num)
- {
- System.out.println(num);
- }
- }
- class test
- {
- public static void main(String args[])
- {
- DisplayOverloading obj = new DisplayOverloading();
- obj.disp('a');
- obj.disp('a',10);
- obj.disp(10);
- }
- }
- // --------------- P6B - Method Overriding ------------------------------------------------
- class Vehicle
- {
- void run()
- {
- System.out.println("Vehicle is running");
- }
- }
- class Bike2 extends Vehicle
- {
- void run()
- {
- System.out.println("Bike is running safely");
- }
- public static void main(String args[])
- {
- Bike2 obj = new Bike2();
- obj.run();
- }
- }
- // --------------- P7 - User Defined Exception -----------------------------------------------
- // TestCustomException1.java main
- class TestCustomException1 {
- static void validate(int age) throws InvalidAgeException {
- if (age < 18)
- throw new InvalidAgeException("not valid");
- else
- System.out.println("welcome to vote");
- }
- public static void main(String args[]) {
- try {
- validate(13);
- } catch (Exception m) {
- System.out.println("Exception occurred: " + m);
- System.out.println("rest of the code...");
- }
- }
- }
- // InvalidAgeException.java
- class InvalidAgeException extends Exception
- {
- InvalidAgeException(String s) {
- super(s);
- }
- }
- // --------------- P8 - Thread Life Cycle -----------------------------------------------
- class A extends Thread {
- public void run()
- {
- System.out.println("Thread A");
- System.out.println("i in Thread A ");
- for(int i=1;i<=5;i++)
- {
- System.out.println("i = " + i);
- }
- System.out.println("Thread A Completed.");
- }
- }
- class B extends Thread {
- public void run()
- {
- System.out.println("Thread B");
- System.out.println("i in Thread B ");
- for(int i=1;i<=5;i++)
- {
- System.out.println("i = " + i);
- }
- System.out.println("Thread B Completed.");
- }
- }
- public class Main1 {
- public static void main(String[] args) {
- //life cycle of Thread
- // Thread's New State
- A threadA = new A();
- B threadB = new B();
- threadA.start();
- threadB.start();
- System.out.println("Main Thread End");
- }
- }
- ----------------------------------------------
- Explanation of the Quadratic Equation Solver Code
- This code solves the quadratic equation of the form:
- ax^2 + bx + c = 0
- Where:
- a = 2
- b = 6
- c = 4
- ---
- Step 1: Understanding the Quadratic Formula
- The roots of a quadratic equation are calculated using the formula:
- x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
- ---
- Step 2: Breaking Down the Code
- int a = 2; int b = 6; int c = 4;
- → Defines the coefficients of the equation.
- double temp1 = Math.sqrt(b * b - 4 * a * c);
- → Calculates the discriminant (D = b² - 4ac).
- If D > 0 → 2 real roots
- If D = 0 → 1 real root (repeated)
- If D < 0 → Complex roots (this code doesn't handle that case).
- double root1 = (-b + temp1) / (2 * a);
- double root2 = (-b - temp1) / (2 * a);
- → These calculate the two possible roots using the quadratic formula.
- System.out.println(...)
- → Displays the calculated roots.
- ---
- Step 3: Expected Output
- The roots of the Quadratic Equation "2x2 + 6x + 4 = 0" are -1.0 and -2.0
- ---
- Step 4: Important Notes
- ✅ The code assumes the discriminant is non-negative (real roots only).
- ✅ To improve the code, you can add conditions to handle complex roots.
- Would you like me to modify the code to handle complex roots as well?
- 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:
- 1. InvalidAgeException Class
- This is a custom exception class that extends the built-in Exception class.
- 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.
- 2. TestCustomException1 Class
- Method validate(int age):
- This method checks if the age parameter is less than 18.
- If the condition is true, it throws an instance of InvalidAgeException with a message "not valid".
- If the age is 18 or more, it prints "welcome to vote".
- main Method:
- The main method attempts to call validate(13) (an age below 18) inside a try block.
- When the InvalidAgeException is thrown by validate, it is caught by the catch block.
- The catch block prints the message "Exception occurred: not valid".
- After handling the exception, the program continues executing and prints "rest of the code...".
- -------------------------------
- Theory of the Code
- This Java program demonstrates the life cycle of threads by creating and running two threads (A and B) that execute concurrently.
- 1. Thread Creation Using extends Thread
- The classes A and B extend the Thread class.
- Each class overrides the run() method, which contains the code that will execute when the thread starts.
- 2. run() Method - Defines Thread Behavior
- In both classes, the run() method prints messages and loops from 1 to 5, displaying values of i.
- 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.
- 3. Thread Initialization and Starting
- In the main() method:
- A threadA = new A(); → Creates a Thread A object (in New State).
- B threadB = new B(); → Creates a Thread B object (in New State).
- Calling .start() on each thread moves them to the Runnable State, where they are ready to run when the CPU assigns time.
- 4. Thread Execution - Random Order
- Since both threads are started close together, their output order is unpredictable.
- Java’s thread scheduler decides the execution order, meaning Thread A or Thread B may finish first.
- Thread A and B run concurrently and order of their output is non -deterministic as it depends on JVM thread scheduler.
- ---
- 5. Main Thread Behavior
- The main() method prints "Main Thread End" immediately after starting both threads.
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement