Sunday 12 December 2021

JAVA PROGRAMMING CODES || AryanTechsource

 



PART-A

1. Implement a stack using a stack class which has methods push, pop.

import java.util.Stack;
class StackMain
{
public static void main(String[] args)
{
Stack<String> courses= new Stack <String>();
courses.push("BBA");
courses.push("BCA");
courses.push("BA");
System.out.println("stack: " + courses);
courses.pop();
System.out.println("stack after pop: " + courses);
}
}



2.Shapes using function overloading.

3.Geometrical figures using abstract class.

4.Sort the vector and display.

import java.util.*;
import java.util.Vector;
public class VectorSort
{
public static void main(String args[])
{
Vector<Integer>in=new Vector<Integer>();
in.add(30);
in.add(70);
in.add(50);
in.add(10);
in.add(40);
in.add(20);
in.add(60);
in.add(100);
System.out.println("Original vector:"+ in);
Collections.sort(in);
System.out.println("ln sorted vector:"+ in);
}
}



5.Multiple catch claues to handle exceptions

public class MultipleCatchBlock1
{
public static void main(String[] args)
{
try
{
int a[]=new int[5];
a[5]=25/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
System.out.println("rest of the code");
}
}



6.Applet which recieves font name, font size as parameters.

7.Reads a file and displays the files with a line no before each line.

import java.util.*;
import java.io.*;
class Rfile
{
public static void main(String args[])throws IOException
{
int j=1;
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter File name:");
String str=scr.next();
FileInputStream f=new FileInputStream(str);
System.out.println("\n contents of the files are");
int n=f.available();
System.out.print(j+": ");
for(int i=0;i<n;i++)
{
ch=(char)f.read();
System.out.println(ch);
if(ch=='\n')
{
System.out.print(++j+": ");
}
}
}
}



8.Displays the number of characters, lines and words in a text file.

import java.util.*;
import java.io.*;
class Cfile
{
public static void main(String args[])throws IOException
{
int n1=1,nw=0;
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter file name:");
String str=scr.nextLine();
FileInputStream f=new FileInputStream(str);
int n=f.available();
for(int i=0;i<n;i++)
{
ch=(char)f.read();
if(ch=='\n')
n1++;
else if(ch==' ')
nw++;
}
System.out.println("\nNumber of lines:"+ n1);
System.out.println("\nNumber of words:"+(n1+nw));
System.out.println("\nNumber of character:"+n);
}
}



                                  PART-B

1.Find Factorial.

public class Factorial
{
public static void main(String args[])
{
int[] arr=new int[10];
int fact;
if(args.length==0)
{
System.out.println("No command line arguments");
return;
}
for(int i=0;i<args.length;i++)
{
arr[i]=Integer.parseInt(args[i]);
}
for(int i=0;i<args.length;i++)
{
fact=1;
while(arr[i]>0)
{
fact=fact*arr[i];
arr[i]--;
}
System.out.println("Factorial of"+ args[i]+"is:"+fact);
}
}
}



2.Prime no. b/w two limits.

class PrimeNumber
{
public static void main(String args[])
{
int i,j;
if(args.length<2)
{
System.out.println("No command line Arguments");
return;
}
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
System.out.println("Prime number between"+num1+ "and"+ num2+" are:");
for(i=num1; i<num2; i++)
{
for(j=2;j<i;j++)
{
int n=i%j;
if(n==0)
{
break;
}
}
if(i==j)
{
System.out.println("  "+i);
}
}
}
}



3.Implement all string operation.

class Stringoperation
{
public static void main(String args[])
{
String s1="Skyward";
String s2="Publishers";
System.out.println("The Strings are" +s1+"and" +s2);
int len1=s1.length();
int len2=s2.length();
System.out.println("length of"+s1+"is="+ len1);
System.out.println("Length of"+s2+"is="+ len2);
System.out.println("The concatenation of two strings="+s1.concat(s2));
System.out.println("First character of" +s1+"is="+s1.charAt(0));
System.out.println("The Uppercase of"+s1+"is="+s1.toUpperCase());
System.out.println("The Lowercase of"+s1+"is="+s1.toLowerCase());
System.out.println("y occurs at position"+s1.indexOf("y")+"in"+s1);
System.out.println("Substring of" +s1+"starting from index 3 and ending at 6 is="+s1.substring(3,7));
System.out.println("Replacing'a' with 'o' in"+s1+"is="+ s1.replace('a','o'));
boolean check=s1.equals(s2);
if(check==false)
{
System.out.println(""+s1 +" and"+s2+" are not same");
}
else
{
System.out.println(""+s1+"and"+s2+"are same");
}
}
}




4.Determine if the given year is leap year or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Leapyear
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter year");
int year=Integer.parseInt(br.readLine());
if((year%400==0) || (year%4==0) && (year%100!=0))
System.out.println("year"+year+"is a leap year");
else
System.out.println("year"+year+" is a not a leap year");
}
}



5.Find max. & min. elements in the array.

public class Number
{
public static void main (String args[])
{
int nums []={88,-3,70,59,100};
int min,max;
min=max=nums[0];
for(int i=1;i<nums.length;i++)
{
if(nums[i]<min)
min=nums[i];
if(nums[i]>max)
max=nums[i];
}
System.out.println("min="+min+"max="+max);
}
}



6.Find sum of digits of a number.

import java.io.*;
public class Digitsum
{
public static void main(String args[]) throws IOException
{
int r=0, sum=0,num;
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter Number:");
num=Integer.parseInt(br.readLine());
while(num>0)
{
r=num%10;
sum=sum+r;
num=num/10;
}
System.out.println("sum of digits is:"+sum);
}
}



7.Find reverse of a number.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Reverse
{
public static void main(String []args) throws IOException
{
int rev=0,num,digit;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
num=Integer.parseInt(br.readLine());
while(num>0)
{
digit=num%10;
num=num/10;
rev=rev*10+digit;
}
System.out.println("Reverse of number is:" +rev);
}
}




8.Generate fibonacci series.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Fibonacci
{
public static void main(String args[]) throws IOException
{
int fib1=0,fib2=1,temp=0,num;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter limit:");
num=Integer.parseInt(br.readLine());
System.out.print(fib1);
System.out.print("\t");
System.out.print(fib2);
for(int i=2; i<num; i++)
{
temp=fib1+fib2;
System.out.print("\t");
System.out.print(temp);
fib1= fib2;
fib2= temp;
}
}
}



9.Check the given number is palindrome or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Palindrome
{
public static void main (String args[]) throws IOException
{
int num,rev=0,digit,temp;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number:");
num = Integer.parseInt(br.readLine());
temp=num;
while(num>0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(temp==rev)
System.out.println("The no is plaindrome");
else
System.out.println("The no is not plaindrome");
}
}






Monday 27 April 2020

DS TEST-3 SHORT ANSWER

QUESTIONS AND ANSWERS:



Q1.What is stack? Explain the algorithm of push() and pop() operation.

stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. A stack is a limited access data structure - elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top.

Algorithm of push() and pop() operations:

Push operation
 Algorithm to push an item into stack.
            
    1) IF TOP = MAX   then
    Print “Stack is full”;
    Exit;
    2) Otherwise
    TOP: = TOP + 1;        /*increment TOP*/
    STACK (TOP):= ITEM;
    3) End of IF
    4) Exit
Pop operation
Algorithm to pop an element from stack.

    1) IF TOP = 0 then
        Print “Stack is empty”;
        Exit;
    2) Otherwise
        ITEM: =STACK (TOP);
        TOP:=TOP – 1;
    3) End of IF
    4) Exit

Q2.Define recursion and types of recursion?

Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls.

Example, sorting, searching, and traversal problem.

Types of recursion:

linear recursion
Tail recursion
Binary recursion
Exponential recursion.
Nested Recursion.
Mutual Recursion.

Q3.Explain tower of hanoi rules and write program.


Tower of Hanoi consists of three pegs or towers with n disks placed one over the other. The objective of the puzzle is to move the stack to another peg following these simple rules. Only one disk can be moved at a time. No disk can be placed on top of the smaller disk.

program:
#include<stdio.h>
#include<conio.h>
int main ()
{
clrscr();
int n;
printf("Enter number of disks required: \n");
scanf ("%d",  &n);
TOH (n, 'A', 'B',' C');
getch();
return 0;
}
void TOH (int n, char src, char spare, char dest)
{
if (n==1)
printf("Move from %c to %c \n", src, dest);

else
{
TOH(n-1, src, dest, spare) ;
TOH(1, src, spare, dest);
TOH(n-1, spare, src, dest);
}
}
Copy
Q4.Algorithm of postfix evaluation: 231*+ 9-

Let the given expression be “2 3 1 * + 9 -“. 

We scan all elements one by one.

1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’

2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)

3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’

4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1 which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’.

5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’.

6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’.

7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’.

8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack).

Q5.Algorithm of infix to post fix conversion:(a+b)x(c-d)+e

"COMING SOON" 

NOTE: all answer are explained so before copying just go with answers once. Ping me for any help or support. 

thankyou:)




Saturday 18 April 2020

SELECTION SORT

|| SELECTION SORT ||


WHAT IS SELECTION SORT?

Selection sort is a simple sorting algorithm.This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.


EXAMPLE:
Consider the array:
[10,5,2,1]
The first element is 10. The next part we must find the smallest number from the remaining array. The smallest number from 5 2 and 1 is 1. So, we replace 10 by 1.
The new array is [1,5,2,10] Again, this process is repeated.
Finally, we get the sorted array as [1,2,5,10].


ALGORITHM:

selectionSort(array, size)
  repeat (size - 1) times
  set the first unsorted element as the minimum
  for each of the unsorted elements
    if element < currentMinimum
      set element as new minimum
  swap minimum with first unsorted position
end selectionSort

CODE:

 // Selection sort in C

#include <stdio.h>

// function to swap the the position of two elements
void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

void selectionSort(int array[], int size) {
  for (int step = 0; step < size - 1; step++) {
    int min_idx = step;
    for (int i = step + 1; i < size; i++) {

      // To sort in descending order, change > to < in this line.
      // Select the minimum element in each loop.
      if (array[i] < array[min_idx])
        min_idx = i;
    }

    // put min at the correct position
    swap(&array[min_idx], &array[step]);
  }
}

// function to print an array
void printArray(int array[], int size) {
  for (int i = 0; i < size; ++i) {
    printf("%d  ", array[i]);
  }
  printf("\n");
}

// driver code
int main() {
  int data[] = {20, 12, 10, 15, 2};
  int size = sizeof(data) / sizeof(data[0]);
  selectionSort(data, size);
  printf("Sorted array in Acsending Order:\n");
  printArray(data, size);
}

THANKYOU:)

RAJ ARYAN

Monday 13 April 2020

BUBBLE SORT

BUBBLE SORT


WHAT IS BUBBLE SORT?

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

EXAMPLE:

enter image description here


ALGORITHM:

begin BubbleSort(list)

   for all elements of list
      if list[i] > list[i+1]
         swap(list[i], list[i+1])
      end if
   end for
   
   return list
   
end BubbleSort

CODE:

#include <stdio.h>
void bubble_sort(int a[], int n) {
    int i = 0, j = 0, tmp;
    for (i = 0; i < n; i++) {   // loop n times - 1 per element
        for (j = 0; j < n - i - 1; j++) { // last i elements are sorted already
            if (a[j] > a[j + 1]) {  // swop if order is broken
                tmp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tmp;
            }
        }
    }
}
int main() {
  int a[100], n, i, d, swap;
  printf("Enter number of elements in the array:\n");
  scanf("%d", &n); 
  printf("Enter %d integers\n", n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  bubble_sort(a, n);
  printf("Printing the sorted array:\n");
  for (i = 0; i < n; i++)
     printf("%d\n", a[i]);
  return 0;
}

WATCH FULL VIDEO ON YOUTUBE
  🔻🔻🔻🔻🔻🔻🔻


THANKYOU

RAJ ARYAN

BCA ASSIGNMENT 2K20.

ASSIGNMENT SOLUTION



DATABASE MANAGEMENT SYSTEM

ASSIGNMENT-1

CLICK HERE TO VIEW THE PDF.

DATA STRUCTURE

ASSIGNMENT-1

CLICK HERE TO VIEW DS PDF.

MATHEMATICS

ASSIGNMENT-1

NOTE: PING ME FOR ANY HELP OR SUPPORT !!

THANKS FOR VISITING:)

RAJ ARYAN

Wednesday 8 April 2020

ARDUINO BOARD

Arduino Board 


   

Definition: 


Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. 

Specification: 

The Arduino board includes the following specifications. 
It is an ATmega328P based Microcontroller 
The Operating Voltage of the Arduino is 5V 
The recommended input voltage ranges from 7V to 12V 
The i/p voltage (limit) is 6V to 20V 
Digital input and output pins-14 
Digital input & output pins (PWM)-6 
Analog i/p pins are 6 
DC Current for each I/O Pin is 20 mA 
DC Current used for 3.3V Pin is 50 mA 
Flash Memory -32 KB, and 0.5 KB memory is used by the boot loader 
SRAM is 2 KB 
EEPROM is 1 KB 
The speed of the CLK is 16 MHz 
In Built LED 
Length and width of the Arduino are 68.6 mm X 53.4 mm 
The weight of the Arduino board is 25 g 

Application: 


  • Arduino Uno is used in Do-it-Yourself projects prototyping. 
  • In developing projects based on code-based control. 
  • Development of Automation System. 
  • Designing of basic circuit designs.



  • THANKYOU:)
  • ARYAN


JAVA PROGRAMMING CODES || AryanTechsource

  PART-A 1. Implement a stack using a stack class which has methods push, pop. import java.util.Stack; class StackMain { public static void ...

Popular Posts