Search This Blog

Thursday 29 October 2015

PWM

clc;
clear all;
close all;
F2=input('Message frequency=');
F1=input('Carrier Sawtooth frequency=');
A=5;
t=0:0.001:1;
c=A.*sawtooth(2*pi*F1*t);%Carrier sawtooth
subplot(3,1,1);
plot(t,c);
xlabel('time');
ylabel('Amplitude');
title('Carrier sawtooth wave');
grid on;
m=0.75*A.*sin(2*pi*F2*t);%Message amplitude must be less than Sawtooth
subplot(3,1,2);
plot(t,m);
xlabel('Time');
ylabel('Amplitude');
title('Message Signal');
grid on;
n=length(c);%Length of carrier sawtooth is stored to ‘n’
for i=1:n%Comparing Message and Sawtooth amplitudes
if (m(i)>=c(i))
    pwm(i)=1;
else
    pwm(i)=0;
end
end
subplot(3,1,3);
plot(t,pwm);
xlabel('Time');
ylabel('Amplitude');
title('plot of PWM');
axis([0 1 0 2]);%X-Axis varies from 0 to 1 & Y-Axis from 0 to 2
grid on;

BFSK


clc;
clear all;
close all;
fc1=input('Enter carrier frequency for binary 1: ');
fc2=input('Enter carrier frequency for binary 0: ');
fs=input('Enter sampling frequency: ');
bs=input('Enter bit sequence: ');
L1=length(bs);
I=[];
for i=1:L1
    I=[I bs(i)*ones(1,fs)];
end
L2=length(I);
t=(10/L2):(10/L2):10;
C1=sin(2*pi*fc1*t);
C2=sin(2*pi*fc2*t);
BFSK=[];
for i=1:L2
    if I(i)==1
        BFSK=[BFSK C1(i)];
    else
        BFSK=[BFSK C2(i)];
    end
end
R=[];
for l=1:length(BFSK)
    if BFSK(l)==C1(l)
        R=[R 0];
    elseif BFSK(l)==C2(l)
        R=[R 1];
    end
end
subplot(5,1,1); plot(t,I);
xlabel('Time'); ylabel('Levels'); title('Bit sequence');
subplot(5,1,2); plot(t,C1);
xlabel('Time'); ylabel('Amplitude'); title('Carrier wave for binary 1');
subplot(5,1,3); plot(t,C2);
xlabel('Time'); ylabel('Amplitude'); title('Carrier wave for binary 0');
subplot(5,1,4); plot(t,BFSK);
xlabel('Time'); ylabel('Amplitude'); title('BFSK signal');
subplot(5,1,5); plot(t,R);
xlabel('Time'); ylabel('Levels'); title('Demodulated signal'); 

BPSK


clc;
clear all;
close all;
fc=input('fc=');
fs=input('fs=');
bit=input('Bit sequence=');
L1=length(bit);
I=[];
for i=1:L1
    I=[I bit(i)*ones(1,fs)];
end
L2=length(I);
t=(10/L2):(10/L2):10;
subplot(5,1,1); plot(t,I);
xlabel('Time'); ylabel('Level'); title('Bit sequence');
I(I==0)=-1;
C=sin(2*pi*fc*t);
BPSK=I.*C;
DPSK=BPSK.*C;
L3=length(BPSK);
r=[];
for i=1:L3
    if DPSK(i)>0
        r=[r 1];
    else
        r=[r 0];
    end
end
subplot(5,1,2); plot(t,I);
xlabel('Time'); ylabel('Level'); title('Bit sequence');
subplot(5,1,3); plot(t,C);
xlabel('Time'); ylabel('Amplitude'); title('Carrier signal');
subplot(5,1,4); plot(t,BPSK);
xlabel('Time'); ylabel('Amplitude'); title('BPSK signal');
subplot(5,1,5); plot(t,r);
xlabel('Time'); ylabel('Level'); title('Original sequence');

BASK


clc;
clear all;
close all;
fs=input('fs=');
fc=input('fc=');
bs=input('Bit sequence=');
L=length(bs);
A=[];
for i=1:L
    A=[A bs(i)*ones(1,fs)];
end
L1=length(A);
t=(10/L1):(10/L1):10;
C=sin(2*pi*fc*t);
BASK=C.*A;
subplot(2,2,1); plot(t,A); xlabel('Time'); ylabel('Level');
title('Bit sequence');
subplot(2,2,2); plot(t,C); xlabel('Time'); ylabel('Amplitude');
title('Carrier signal');
subplot(2,2,3); plot(t,BASK); xlabel('Time'); ylabel('Amplitude');
title('BASK Signal');
L2=length(BASK);
r=[];
for i=1:(L2/fs)
    if sum(abs(BASK((i-1)*fs+1:i*fs)),2)
        r=[r ones(1,fs)];
    else
        r=[r zeros(1,fs)];
    end
end
subplot(2,2,4); plot(t,r); xlabel('Time'); ylabel('Level');
title('Demodulated bit sequence')

FREQUENCY MODULATION



fs=input('enter the value of sampling frequency'); fc=input('enter the value of carrier frequency'); fm=input('enter the value of modulating frequency'); t=0:(1/fs):2; m=10;
X=sin(2*pi*fm*t);
C=sin(2*pi*fc*t);

FM=sin((2*pi*fc*t)+m*sin(2*pi*fm*t)); subplot(3,1,1); plot(t,X); xlabel('time'); ylabel('frequency'); title('modulating signal'); subplot(3,1,2); plot(t,C); xlabel('time'); ylabel('frequency'); title('carrier signal'); subplot(3,1,3); plot(t,FM); xlabel('time'); ylabel('frequency'); title('frequency modulated signal'); 

AMPLITUDE MODULATION AND DEMODULATION



fs=input('enter the value of sampling frequency'); fc=input('enter the value of carrier frequency'); fm=input('enter the value of modulating frequency'); t=0:(1/fs):2;
A=cos(2*pi*fm*t); m1=1; opt=(-1)/m1;
AM=modulate(A,fc,fs,'amdsb-tc',opt); AM3=demod(AM,fc,fs,'amdsb-tc',opt); m2=0.9; opt=(-1)/m2;
AM1=modulate(A,fc,fs,'amdsb-tc',opt); AM4=demod(AM1,fc,fs,'amdsb-tc',opt); m3=1; opt=(-1)/m3;
AM2=modulate(A,fc,fs,'amdsb-tc',opt); AM5=demod(AM2,fc,fs,'amdsb-tc',opt);
subplot(3,3,1); plot(t,A); xlabel('time'); ylabel('amplitude'); title('modulating signal'); subplot(3,3,2); plot(t,AM); xlabel('time'); ylabel('amplitude'); title('AM for m=1 signal'); subplot(3,3,3); plot(t,AM1); xlabel('time'); ylabel('amplitude'); title('AM for m<1 signal'); subplot(3,3,4); plot(t,AM2); xlabel('time'); ylabel('amplitude'); title('AM for m>1 signal'); subplot(3,3,5); plot(t,AM3); xlabel('time'); ylabel('amplitude'); title('demodulating signal for m=1');
subplot(3,3,6); plot(t,AM4); xlabel('time'); ylabel('amplitude'); title('demodulating signal for m<1');

subplot(3,3,7); plot(t,AM5); xlabel('time'); ylabel('amplitude'); title('demodulating signal for m>1'); 

Wednesday 28 October 2015

PROGRAM TO CREATE HOUSE STRUCTURE WITH APPLET

import java.awt.*;
import java.applet.*; 
public class House extends Applet {
            public void paint(Graphics g) {
                        g.drawLine(100,10,10,90);
                        g.drawLine(100,10,400,10);
                        g.drawLine(400,10,490,90);
                        g.drawLine(10,90,490,90);
                        g.drawLine(60,90,60,270);
                        g.drawLine(435,90,435,270);
                        g.drawLine(130,150,130,270);
                        g.drawLine(200,150,200,270);
                        g.drawLine(130,150,200,150);
                        g.drawLine(280,150,280,210);
                        g.drawLine(350,150,350,210);
                        g.drawLine(280,150,350,150);
                        g.drawLine(280,210,350,210);
            }
}
/*<applet code=House.class width=500 height=500></applet>*/

PROGRAM TO CREATE AND DISPLAY YOUR NAME ON SCREEN WITH APPLET

import java.awt.*;
import java.applet.*;
/*
<applet code="Display.class" width=600 height=400 />
*/
public class Display extends Applet {
            public void init() {
                        setBackground(Color.white);
                        setFont(new Font("Dialog",Font.BOLD,30));
            }
            public void paint(Graphics g) {
                        g.drawString("YOGI DADA SHINDE",100,100);
            }

}

program to print 1A2B3C4D5E6F7G8H9I10J

class A extends Thread {
            public void run() {
                        int i;
                        for(i=1;i<=10;i++) {
                                    System.out.print(i);    
                                    try {
                                                sleep(1000);
                                    } catch(Exception e){}
                        }
            }
}
class B extends Thread {
            public void run() {
                        int i;
                        for(i=65;i<=74;i++) {
                                    System.out.print((char)i);
                                    try {
                                                sleep(1000);
                                    } catch(Exception e){}
                        }
            }
}

class Multithread {
            public static void main(String args[]) {
                        A t1=new A();
                        B t2=new B ();
                        t1.start();
                        t2.start();
            }
}

Program to show Multithreading using thread class

import java.util.*;
import java.util.Scanner;

class  Print_Numerics extends Thread
{
 Thread t;
 Print_Numerics()
 {
  super();
 }
 public void run()
 {
   for(int  i=1;i<=10;i++)
    {
     if(i%2!=0)
     {System.out.println(i);}
    }
  }
}

class E10A
{
 public static void main(String args[])
 {
  Print_Numerics ob1 = new Print_Numerics();
  Print_Numerics ob2 = new Print_Numerics();
  ob1.start();
  ob2.start();
 }
}

OUPUT

F:\java program\OOPM>javac E10A.java

F:\java program\OOPM>java E10A
1
3
5
7
9
1
3
5
7

9

user defined exception

import java.io.*;
import java.util.*;
class Login extends Exception {
            Login(String Message) {
                        super(Message);
            }
}
class Authentication {
            public static void main(String args[]) throws IOException {
                        Scanner sc=new Scanner(System.in);
                        String password1="Program";
                        String password2;
                        System.out.print("Enter the password = ");
                        password2=sc.nextLine();
                        try {
                                    if(password1.equals(password2))
                                                System.out.println("Password Correct.");
                                    else {
                                                Login f1=new Login("Incorrect Password.");
                                                throw f1;
                                    }
                        } catch(Login e) {
                                    System.out.print(e);
                        }
            }
}


Program to implement multiple inheritance

import java.util.*;
import java.util.Scanner;
interface Sports_marks
{  final int sm=10;
    public void display();
}
class  Student
{     int id;
      String name,dept;
       void get_data(int id,String name,String dept)
 {     this.id=id;
        this.name=name;
         this.dept=dept;
 }
}
Class  Marks extends Student
{    int m1,m2;
     void get_marks(int m1,int m2)
     {  this.m1=m1; this.m2=m2;}
}
class Result extends Marks implements Sports_marks
{      public void display()
     {
        System.out.print("\nName- "+name+"\nRoll-no= "+id+"\nDepartment "+dept);
        System.out.print("\nMarks ="+(m1+m2+sm));
}
}
class E8
{
 public static void main(String args[])
 {
  int a,b,c; String name = new String(); String dept = new String();
  Result q = new Result();
  System.out.println("Enter your Name,Department,Roll-no and marks for subject 1 and 2");
  Scanner s = new Scanner(System.in);
  name=s.nextLine();
  dept=s.next();
  a=s.nextInt();
  b=s.nextInt();
  c=s.nextInt();
  q.get_data(a,name,dept);
  q.get_marks(b,c);
  q.display();
 }
}
OUTPUT
F:\java program\OOPM>javac E8.java
F:\java program\OOPM>java E8
Enter your Name,Department,Roll-no and marks for subject 1 and 2
DONALD
COMPUTERS
24
43
45
Name- DONALD
Roll-no= 24
Department COMPUTERS

Marks =98

Program to calculate volume of sphere using multilevel inheritance demonstrating method overriding

import   java.util.Scanner;
import  java.util.*;


interface supz
{final double pi=3.14; public void display();}
class Data
{
 double r;
 public void gdata()
 {
  System.out.println("Enter Radius");
  Scanner s = new Scanner(System.in);
  r=s.nextFloat();
 }
}

class Area extends Data
{
 double area;
 public void aclc()
 {area = r*r*3.14;}
 public void display()
 {System.out.println("Area of Circle= "+area);}
}
class  Volume extends Area implements supz
{
 double v;
 public void vclc()
 {v=(4*r*r*r*pi)/3;}
 public void display()
 {super.display(); System.out.println("Volume of Sphere= "+v);}
}

class E7B
{
 public static void main(String args[])
 {
  Volume v = new Volume();
  v.gdata(); v.aclc(); v.vclc();
  v.display();
 }
}

OUTPUT
F:\java program\OOPM>javac E7B.java

F:\java program\OOPM>java E7B
Enter Radius
5
Area of Circle= 78.5

Volume of Sphere= 523.3333333333334

Program to implement single inheritance using super keyword

class Box
{
float width,length;
Box()
{
width=0;
length=0;
}
Box(float w,float l)
{
width=w;
length=l;
}
Box(Box ob)
{
width=ob.width;
length=ob.length;
}
void area()
{
System.out.println("Area is: "+(width*length));
}
}

class BigBox extends Box
{
float depth;
BigBox()
{
super(0,0);
depth=0;
}
BigBox(Box ob,float d)
{
super(ob);
depth=d;
}
void volume()
{
System.out.println("Volume is: "+(width*length*depth));
}
}

class SuperEx
{
public static void main(String args[])
{
Box b=new Box(1.5f,2.5f);
BigBox bb=new BigBox(b,3.5f);
bb.area();
bb.volume();
}
}

OUTPUT:
C:\Program Files\Java\jdk1.7.0\bin>java SuperEx
Area is: 3.75

Volume is: 13.125

Program on vector manipulation

import java.util.*;

class VectorEx
{
public static void main(String args[])
{
Vector list=new Vector();
list.add(10);
list.insertElementAt(20,0);
list.insertElementAt(30,1);
list.add("Hello World!");
if(list.isEmpty()==true)
System.out.println("List is empty.");
else
System.out.println("List is not empty.");
System.out.println("List is: "+list);
System.out.println("First element is: "+list.firstElement());
System.out.println("Last element is: "+list.lastElement());
System.out.println("List size is: "+list.size());
System.out.println("Element at location 1: "+list.elementAt(1));
list.removeElementAt(1);
System.out.println("Removed element: 30");
System.out.println("List is: "+list);
}
}

OUTPUT:

C:\Program Files\Java\jdk1.7.0\bin>java VectorEx
List is not empty.
List is: [20, 30, 10, Hello World!]
First element is: 20
Last element is: Hello World!
List size is: 4
Element at location 1: 30
Removed element: 30

List is: [20, 10, Hello World!]

Program on constructor overloading

import java.util.Scanner;

class complex
{
float x,y;
complex()
{
x=0;
y=0;
}
complex(float r,float i)
{
x=r;
y=i;
}
void show()
{
System.out.println("Sum is "+x+"+i"+y);
}
void sum(complex c1,complex c2)
{
x=c1.x+c2.x;
y=c1.y+c2.y;
}
}
class ComplexEx
{
public static void main(String args[])
{
float x,y;
Scanner s=new Scanner(System.in);
System.out.println("Enter real and imaginary parts of first complex number:");
x=s.nextFloat();
y=s.nextFloat();
complex a=new complex(x,y);
System.out.println("Enter real and imaginary parts of second complex number:");
x=s.nextFloat();
y=s.nextFloat();
complex b=new complex(x,y);
complex c=new complex();
c.sum(a,b);
c.show();
}
}

OUTPUT:

C:\Program Files\Java\jdk1.7.0\bin>java ComplexEx
Enter real and imaginary parts of first complex number:
1 4
Enter real and imaginary parts of second complex number:
5 6

Sum is 6.0+i10.0

Program on method overloading


class areaCalc
{
void area(int l)
{
System.out.println("Area of square is: "+(l*l));
}
void area(float r)
{
System.out.println("Area of circle is: "+(3.14*r*r));
}
void area(int l,int b)
{
System.out.println("Area of rectangle is: "+(l*b));
}
void area(float b,float h)
{
System.out.println("Area of triangle is: "+(0.5*b*h));
}
}

class poly
{
public static void main(String args[])
{
areaCalc ac=new areaCalc();
ac.area(30);
ac.area(10.5f);
ac.area(10,20);
ac.area(5.6f,7.8f);
}
}

OUTPUT: 5A

C:\Program Files\Java\jdk1.7.0\bin>java poly
Area of square is: 900
Area of circle is: 346.185
Area of rectangle is: 200

Area of triangle is: 21.84000016212462

Program on operations of the String class

import java.util.Scanner;
public class StringOps
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String a=new String();
String b=new String();
System.out.println("Enter two strings:");
a=s.nextLine();
b=s.nextLine();
String c=new String();
c=a.concat(b);
System.out.println("Concatenated string is: "+c);
if(a.compareToIgnoreCase(b)>0)
System.out.println(a+ " is bigger than "+b);
else
System.out.println(b+ " is bigger than "+a);
System.out.println(a + " in uppercase is "+a.toUpperCase());
System.out.println(b + " in uppercase is "+b.toUpperCase());
if(a.equals(b)==true)
System.out.println("Using equals function: True");
else
System.out.println("Using equals function: False");
if(a==b)
System.out.println("Normal '==' condition: True");
else
System.out.println("Normal '==' condition: False");
}
}

OUTPUT:

C:\Program Files\Java\jdk1.7.0\bin>java StringOps
Enter two strings:
foo
foo
Concatenated string is: foofoo
foo is bigger than foo
foo in uppercase is FOO
foo in uppercase is FOO
Using equals function: True

Normal '==' condition: False

Program to check whether a string is palindrome or not


import java.util.Scanner;

class StringEx
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String ex=new String();
Boolean pal=true;
System.out.println("Enter a string:");
ex=s.nextLine();
for(int i=0;i<(ex.length()/2);i++)
if(ex.charAt(i)!=ex.charAt(ex.length()-i-1))
{
pal=false;
break;
}
if(pal==true)
System.out.println("The string is a palindrome.");
else
System.out.println("The string is not a palindrome.");
}
}

OUTPUT:

C:\Java\jdk1.7.0\bin>java StringEx
Enter a string:
malayalam

The string is a palindrome.

Program to demonstrate classes, objects and methods

import java.util.Scanner;

class Circle {
  double radius,area;
 
  void setData(double x) {
    radius=x;
  }
 
  void calculate() {
    area=3.14*radius*radius;
    System.out.println("Area is: "+area);
  }
}

class CircleEx {
  public static void main(String args[]) {
    double x;
    Scanner sc=new Scanner(System.in);
    Circle c=new Circle();
    System.out.print("Enter radius of circle: ");
    x=sc.nextDouble();
    c.setData(x);
    c.calculaute();
  }
}


OUTPUT:

C:\jdk1.7.0\bin>java CircleEx
Enter radius of circle: 1

Area is: 3.14

Program for sequential search


import java.util.Scanner;

public class SeqSearch {
  public static void main(String args[]) {
    Scanner takeIn=new Scanner(System.in);
    int size;
    System.out.print("Enter the number of elements: ");
    size=takeIn.nextInt();
    int a[]=new int[size];
    System.out.println("Enter the elements:");
    for(int i=0;i<size;i++)
      a[i]=takeIn.nextInt();
    System.out.print("Enter the element to be searched: ");
    int x,i=-1;
    x=takeIn.nextInt();
    while(++i<size)
      if(a[i]==x) {
        System.out.println(String.format("Element %d found at location %d",x,i+1));
        break;
      }
    if(i==size)
      System.out.println(String.format("%d not found",x));
  }
}



OUTPUT:

C:\jdk1.7.0\bin>java SeqSearch
Enter the number of elements: 5
Enter the elements:
10 20 30 40 50
Enter the element to be searched: 40

Element 40 found at location 4

Program for printing Fibonacci series


import java.util.Scanner;

class fibonacci {
  public static void main(String args[]) {
    int i,n,a=0,b=1,c;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of fibonacci terms: ");
    n=sc.nextInt();
    System.out.println("0\n1");
    for(i=0;i<n-2;i++) {
      c=a+b;
      System.out.println(c);
      a=b;
      b=c;
    }
  }
}


OUTPUT:

C:\jdk1.7.0\bin>java fibonacci
Enter the number of fibonacci terms: 10
0
1
1
2
3
5
8
13
21

34