Saturday, 20 November 2021

Q1_COPY_ARRAY

COPY

Program to copy all elements of one array into another array

In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of the first array into the second array at the corresponding position.

Array 1

1 2 3 4 5

Array 2

1 2 3 4 5

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={1, 2, 3, 4, 5}
  • 3.CREATE arr2[] of size arr1[].
  • 4.COPY elements of arr1[] to arr2[]
  • 5. arr2[i] =arr1[i]
  • 6.DISPLAY elements of arr1[].

Program

class Q1_copy {

public static void main(String[] args)

//declare array

int arr2[]=new int [5];

int arr[]={11,2,33,4,5};

System.out.println("original array is");

for(int i=0;i< arr.length;i++)

{

System.out.print(arr[i] + " ");

}

//copy array

for(int i=0;i< arr.length;i++)

{

arr2[i]=arr[i];

}

//display copy array

System.out.println("copy the array");

for(int i=0;i< arr.length;i++)

{

System.out.print(arr[i] + " ");

}

output

original array is:

11 2 33 4 5

copy array is:

11 2 33 4 5

Next Topic

Friday, 12 November 2021

single_inheritance

oops

Object Orinted programming

4 Inheritance

Inheritance is the producer by which one object acquries all the proprties and behavior of a parent object.It is Inheritance the properties of parent class into chlid class.

It is always better pre existing rather than creating all over agian .this is done using Inheritance. inheritance provide us alway using which we can use pre-existing our current programming/application.

it is with inheritance one class derive the properties of another class.

java:- By using extends Keyword to achive inheritance

Advantage of Inheritance

  • Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.
  • Reusability enhanced reliability. The base class code will be already tested and debugged.
  • Inheritance makes the sub classes follow a standard interface.
  • Inheritance helps to reduce code redundancy and supports code extensibility.
  • Inheritance facilitates creation of class libraries.

DisAdvantage of Inheritance

  • Inherited functions work slower than normal function as there is indirection.
  • Improper use of inheritance may lead to wrong solutions.
  • Often, data members in the base class are left unused which may lead to memory wastage.
  • Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.

Object

Type of Inheritance:--------- 5

1. Single Inheritance

When one base class is being to derived class (Single) is called as Single inheritance.

Object

how to calcaulate area of reactangle by using single Inheritance ?

Single Inheritance

  • import java.util.Scanner;
  • public Class a
  • {
  • public
  • int lenght,breath ;
  • public void get()
    {
  • Scanner sc = new Scanner(System.in);
  • System.out.println( " enter the lenght ");
  • lenght=sc.nextInt();
  • System.out.println( " enter the breath ");
  • breath=sc.nextInt();
  • }
  • Class b extends a
  • {
  • void display()
  • {
  • System.out.println( " Area of reactangle = "+lenght+breath);
  • }
  • }
  • Class simple
  • {
  • public static void main (String args[])
    {
  • b aa = new b () ; ................//create the object of class
  • aa.get() ; ................//call the method
  • aa.display() ; ................//call the method
  • }
  • }
  • }
  • class Animal :
  • def make_eat(self):
  • print("i am eatting"")
  • a1=Animal
  • a1.eat()+
  • CONTENT FIR TAB 4

    Input

    enter the lenght =10

    enter the breath =10

    output

    Area of reactangle = 100.

    Thursday, 11 November 2021

    object_oop

    oops

    Object Orinted programming

    3 object

    Object is instance of class of real world entity ,it is occupies memory Any entity that has state and behavior is known as an object

    Object consist of :--
    1.Identity
    2.state/artibute
    3.Behavior own

    How to create objects :--------- 5 Ways

    • By using new Keyword
    • by using new instance
    • by using clone()
    • by using factory method
    • by using delecatrive method

    Object

    # syntax

    ClassName objectName = new ClassName

    dot (.) operator is used to calling classs method or class Behavior in the given class.

    class demo example

  • public Animal
  • {
  • public void eat()
    {
  • System.out.println( " i am eating ");
  • }
  • public static void main (String args[])
    {
  • Animal aa = new Animal () ; ................//create the object of class
  • aa.eat() ; ................//call the method
  • }
  • }
  • C++ is cooming soon

    comming soon

    CONTENT FIR TAB 4

    i am eating

    Monday, 8 November 2021

    DBMS_SHORT

    DBMS

    DBMS /SQL Interview Questions

    Structer Query language is a computer language for Management of database and data manipluation . SQL is used to query ,insert,update and modify datain a database.
    it contains a lot of component which a user can execute to perform operation on a database.

    component of sql /database language is:-
    1.DDL
    2.DML
    3.DCL
    4.DQL

    create statement is used to created database schema and to define the type and structer of the data to be strored in the database.
    Create statement can be used for:-
    Creating a Database
    Creating a Table

    drop query Truncate query
    Drop Table does delete complete table but it delete table structure too. Truncate Table doesn't delete the structer of the table.

    Alter is used to modify the structer of the rxisting table. it is used to add,delete,modify,or rename column in the existing table. It is used to add and drop various constrains on an existing table.

    Alter table -Add column = for adding new column in a table.
    Alter table -Drop column = for removing existing column in table.
    Alter table -Modify column = To Modify existing column in a table.
    Alter table -Rename column = To rename an existing column in a table.

    Drop-table statement completely removes a table from the database. this command will destroy the table structer and the data stroedin it.
    Drop Table table-name.

    Truncate statement is used to remove all rows (complete data) from a table .it is similar to the delete statement with no WHERE clause.
    Syntax:-
    Truncate table table_name;

    c_short_answer

    Document

    C Programming Interview Questions

    C is a mid-level and procedural programming language. The Procedural programming language is also known as the structured programming language

    is a technique in which large programs are broken down into smaller modules, and each module uses structured code. This technique minimizes error and misinterpretation

    C is known as a mother language because most of the compilers and JVMs are written in C language.

    Most of the languages which are developed after C language has borrowed heavily from it like C++, Python, Rust, javascript, etc. It introduces new core concepts like arrays, functions, file handling which are used in these languages.

    Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into parts

    Fast Speed: C language is very fast as it uses a powerful set of data types and operators.

    Memory Management: C provides an inbuilt memory function that saves the memory and improves the efficiency of our program.

    Structured: C is a structured language as the C program is broken into parts.

    Local variable Global variable
    A variable which is declared inside function or block is known as a local variable. A variable which is declared outside function or block is known as a global variable.
    The scope of a variable is available within a function in which they are declared. The scope of a variable is available throughout the program.
    Variables are stored in a stack unless specified. The compiler decides the storage location of a variable.

    1.C functions are used to avoid the rewriting the same code again and again in our program.

    2.C functions can be called any number of times from any place of our program.

    3.C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes the C program more understandable.

    When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.

    A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast.

    Whenever a variable is declared inside a program, then the system allocates some memory to a variable.

    The memory contains some address number. The variables that hold this address number is known as the pointer variable.

    Accessing array elements: Pointers are used in traversing through an array of integers and strings. The string is an array of characters which is terminated by a null character '\0'.

    Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data structures like tree, graph, linked list, etc.

    Call by Reference: The pointers are used to pass a reference of a variable to other function.

    In case of static memory allocation, memory is allocated at compile time, and memory can't be increased while executing the program. It is used in the array.

    The lifetime of a variable in static memory is the lifetime of a program. The static memory is allocated using static keyword. The static memory is implemented using stacks or heap.

    The pointer is required to access the variable present in the static memory. In static memory, more memory space is required to store the variable.

    In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.

    An allocation or deallocation of memory is done at the execution time of a program. No dynamic pointers are required to access the memory. The dynamic memory is implemented using data segments. Less memory space is required to store the variable.

    Friday, 5 November 2021

    Job Sequencing Problem

    DAA

    Job Sequencing with deadline

    If there are n jobs i.e j1,j2, j3,......jn & those jobs are assosiated with deadline d1,d2,d3 ... dn
    having some profit p1,p2,p3 ... pn

    Then object is to achieve max profit when only one job is scheduling at any given time.

    Job Sequencing With Deadlines :-

    The sequencing of jobs on a single processor with deadline constraints is called as Job Sequencing with Deadlines. How can the total profit be maximized if only one job can be completed at a time?
    Here :-

    • You are given a set of jobs.
    • Only one processor is available for processing all the jobs.
    • The profit of a job is given only when that job is completed within its deadline.
    • Each job has a defined deadline and some profit associated with it.
    • Processor takes one unit of time to complete a job.

    Approach to Solution:-

    • Value of the feasible solution would be the sum of profit of all the jobs contained in the subset.
    • A feasible solution would be a subset of jobs where each job of the subset gets completed within its deadline.
    • An optimal solution of the problem would be a feasible solution which gives the maximum profit.

    Greedy Algorithms:-

    • Greedy Algorithm is adopted to determine how the next job is selected for an optimal solution.
    • The greedy algorithm described below always gives an optimal solution to the job sequencing problem.

    Step of solving problem :-

    Step- 01.

    • Sort all the given jobs in decreasing order of their profit.

    Step- 02.

    • Check the value of maximum deadline.
    • Draw a Gantt chart where maximum time on Gantt chart is the value of maximum deadline.

    Step- 03.

    • Pick up the jobs one by one.
    • Put the job on Gantt chart as far as possible from 0 ensuring that the job gets completed before its deadline.

    PRACTICE PROBLEM BASED ON JOB SEQUENCING WITH DEADLINES:-

    Problem-
    Given the jobs, their deadlines and associated profits as shown-


    jobs Deadline Profit
    j1 20 2
    j2 15 2
    j3 10 1
    j4 5 3
    j5 1 3

    soluation:- maximum deadline = 3.

    jobs Slot select job profit
    j1 [1,2] j1 20
    j2 [0,1] [1,2] j1, j2 20+15
    j4 [0,1][1,2][2,3] j1 , j2 ,j4 20+15+5
    Total 40

    Answer:- maximum profit = 40.


    How to write algorithm :-

    • Arrange all jobs in descending order of profit
    • for each job (m) do Linear search
    • finding particular slot in order of size (n)
    • where : n = maximum deadline
    • m = total job

    Note:-

    1.It is uniprocesser i.e if select one job they have to complet the given job . No preemption allowed

    2. every job take 1 unit Time

    Implement coding

    • tab1

      import java.util.Arrays;
      import java.util.Collections;
      import java.util.List;
      import java.util.stream.Collectors;

      // Data structure to store job details. Each job has an identifier,
      // a deadline, and profit associated with it.

      class Job
      {
      public int taskID, deadline, profit;
      public Job(int taskID, int deadline, int profit)
      {
      this.taskID = taskID;
      this.deadline = deadline;
      this.profit = profit;
      }
      }

      class Main
      {
      // Function to schedule jobs to maximize profit
      public static void scheduleJobs(List jobs, int T)
      {
      // stores the maximum profit that can be earned by scheduling jobs
      int profit = 0;
      // array to store used and unused slots info
      int[] slot = new int[T];
      Arrays.fill(slot, -1);
      // arrange the jobs in decreasing order of their profits
      Collections.sort(jobs, (a, b) -> b.profit - a.profit);
      // consider each job in decreasing order of their profits
      for (Job job: jobs)
      {
      // search for the next free slot and map the task to that slot
      for (int j = job.deadline - 1; j >= 0; j--)
      {
      if (j < T && slot[j] == -1)
      {
      slot[j] = job.taskID;
      profit += job.profit;
      break;
      }
      }
      }

      // print the scheduled jobs
      System.out.println("The scheduled jobs are " + Arrays.stream(slot).filter(val -> val != -1).boxed() .collect(Collectors.toList()));
      // print total profit that can be earned
      System.out.println("The total profit earned is " + profit);
      } public static void main(String[] args)
      {
      // List of given jobs. Each job has an identifier, a deadline, and
      // profit associated with it
      List jobs = Arrays.asList(
      new Job(1, 9, 15), new Job(2, 2, 2),
      new Job(3, 5, 18), new Job(4, 7, 1),
      new Job(5, 4, 25), new Job(6, 2, 20),
      new Job(7, 5, 8), new Job(8, 7, 10),
      new Job(9, 4, 12), new Job(10, 3, 5)
      ); // stores the maximum deadline that can be associated with a job
      final int T = 15;
      // schedule jobs and calculate the maximum profit
      scheduleJobs(jobs, T);
      }
      }

    • # A class to store job details. Each job has an identifier,
      # a deadline, and profit associated with it.

      class Job:

      def __init__(self, taskID, deadline, profit):
      self.taskID = taskID
      self.deadline = deadline
      self.profit = profit

      # Function to schedule jobs to maximize profit
      def scheduleJobs(jobs, T):

      # stores the maximum profit that can be earned by scheduling jobs
      profit = 0

      # list to store used and unused slots info
      slot = [-1] * T

      # arrange the jobs in decreasing order of their profits
      jobs.sort(key=lambda x: x.profit, reverse=True)

      # consider each job in decreasing order of their profits
      for job in jobs:

      # search for the next free slot and map the task to that slot
      for j in reversed(range(job.deadline)):

      if j < T and slot[j] == -1:
      slot[j] = job.taskID
      profit += job.profit
      break

      # print the scheduled jobs
      print("The scheduled jobs are", list(filter(lambda x: x != -1, slot)))
      # print total profit that can be earned
      print("The total profit earned is", profit)

      if __name__ == '__main__':
      # List of given jobs. Each job has an identifier, a deadline, and
      # profit associated with it

      jobs = [
      Job(1, 9, 15), Job(2, 2, 2),
      Job(3, 5, 18), Job(4, 7, 1),
      Job(5, 4, 25), Job(6, 2, 20),
      Job(7, 5, 8), Job(8, 7, 10),
      Job(9, 4, 12), Job(10, 3, 5)
      ]

      # stores the maximum deadline that can be associated with a job
      T = 15

      # schedule jobs and calculate the maximum profit
      scheduleJobs(jobs, T)

    the website developed by Narayan Mane ©:2021

    Home

    Thursday, 4 November 2021

    Knapsack problem

    DAA

    Greedy Algorithm Introduction

    "Greedy Method finds out of many options, but you have to choose the best option." In this method, we have to find out the best method/option out of many present ways. In this approach/method we focus on the first stage and decide the output, don't think about the future. This method may or may not give the best output.

    What is Greedy Algorithm?

    Greedy Algorithm solves problems by making the best choice that seems best at the particular moment. Many optimization problems can be determined using a greedy algorithm. Some issues have no efficient solution, but a greedy algorithm may provide a solution that is close to optimal

    Why study Greedy Algorithm?

    It is solving a optimization problem . A soluation to satify solution of the given problem is know as feasible soluation.

    Greedy method use to solving optimization problem

    Properties of Greedy Algorithms

    • Greedy Choice Property:A globally optimal solution can be reached at by creating a locally optimal solution.
    • Optimal substructure: Optimal solutions contain optimal subsolutions. In other words, answers to subproblems of an optimal solution are optimal.

    Advantages of an Greedy Algorithm

    • Feasible: we check whether it satisfies all possible constraints or not, to obtain at least one solution to our problems.
    • Unalterable:Once the decision is made, at any subsequence step that option is not altered.
    • Local Optimal Choice: the choice should be the optimum which is selected from the currently available

    Example of Greedy Algorithm

    • 1. machine scheduling
    • 2. Fractional Knapsack Problem
    • 3. Minimum Spanning Tree
    • 4. Huffman Code
    • 5. Job Sequencing
    • 6. Activity Selection Problem

    How to write algorithm

    • Algo Greedy(a,n)
    • {
    • for i=1 to n
    • x=select(a)
    • if feasible (x) then
    • solation=soluation+x
    • }

    Note:

    1.Min cost + feasible soluation ==> optimal soluation

    Knapsack problem

    What is Knapsack_problem?

    The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible

    I found the Knapsack problem tricky and interesting at the same time. I am sure if you are visiting this page, you already know the problem statement but just for the sake of completion :

    The Greedy algorithm could be understood very well with a well-known problem referred to as Knapsack problem. Although the same problem could be solved by employing other algorithmic approaches, Greedy approach solves Fractional Knapsack problem reasonably in a good time. Let us discuss the Knapsack problem in detail.

    How to write algorithm

    • for i=1 to n
    • calculate p/w
    • sort obj in descending oder of p/w ratio
    • for i=1 to n
    • if m > 0 and w < m
    • m=m-wi;
    • p=p+pi;
    • else
    • if(m>0)
    • p=p+pi(m/wi)

    the website developed by Narayan Mane ©:2021

    Home

    How do you select data from a table in SQL?

    Create Table How do you select data a table in SQL? The SELECT statement is used to se...