Untitled
TEXT
views 117
,
size 22951 b
``````1 ). JAVA BASICS
(a)	Write A Java Program That A Number As Input And Prints Its Multiplication Table Upto 10.

FristProgram.java
import java.util.Scanner;
class FristProgram
{
public static void main(String args[])
{
int a,i;
System.out.println ("Enter an integer to print it's multiplication table");
Scanner in = new Scanner (System. in);
a = in.nextInt ();
System.out.println ("Multiplication table of "+a+" is :-");
for (i = 1; i<= 10 ;i++ )
System.out.println (a+"*"+i+" = "+ (a*i));
}
}
OUTPUT

(b)	Write a java program to display the following   pattern .

class Star
{
public static void main(String[] args)
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output

(c)	Write a java program to print the area and perimeter of a circle.

public class AreaCircle
{
private static final double radius = 7.5;
public static void main(String[] args)
{
double perimeter = 2 * Math.PI * radius;
System.out.println("Perimeter is = " + perimeter);
System.out.println("Area is = " + area);
}
}
Output:

(2)  USE OF OPERATORS
(a)  write a java program to add two binary numbers .
import java.util.Scanner;
{
public static void main(String[] args)
{
long binary1, binary2;
int i = 0, remainder = 0;
int[] sum = new int[20];
Scanner s = new Scanner(System.in);
System.out.print("Input first binary number: ");
binary1 = s.nextLong();
System.out.print("Input second binary number: ");
binary2 = s.nextLong();
while (binary1 != 0 || binary2 != 0)
{
sum[i++] = (int)((binary1 % 10 + binary2 % 10 +   remainder) % 2);
remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2);
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
{
sum[i++] = remainder;
}
--i;
System.out.print("Sum of two binary numbers: ");
while (i >= 0) {
System.out.print(sum[i--]);
}
System.out.print("\n");
}
}

(B) write a java program to convert a decimal number to binary number and vice verse .
import java.util.*;
class DecimalToBinary
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a decimal number");
int n=s.nextInt();
int  bin[]=new int[100];
int i = 0;
while(n > 0)
{
bin[i++] = n%2;
n = n/2;
}
System.out.print("Binary number is : ");
for(int j = i-1;j >= 0;j--)
{
System.out.print(bin[j]);
}
}
}

(c) write a java program to reverse a string .
import java.util.*;
public class ReverseString
{
public static void main(String [] arg)
{
String s1,s2="";
System.out.println("Enter any String:");
Scanner s=new Scanner(System.in);
s1=s.nextLine();
for(int i=s1.length()-1;i>=0;i--)
s2=s2+s1.charAt(i);
System.out.println("After reverse string\n"+s2);
}
}

(3) JAVA DATA TYPES
(a) Write a java  program  to count the letters, spacers ,number &other characters  of an input string.
import java.util.Scanner;
public class Count
{
public static void main(String[] args)
{
count(test);
}
public static void count(String x)
{
char[] ch = x.toCharArray();
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i = 0; i < x.length(); i++)
{
if(Character.isLetter(ch[i]))
{
letter ++ ;
}
else if(Character.isDigit(ch[i]))
{
num ++ ;
}
else if(Character.isSpaceChar(ch[i]))
{
space ++ ;
}
Else
{
other ++;
}
}
System.out.println("letter: " + letter);
System.out.println("space: " + space);
System.out.println("number: " + num);
System.out.println("other: " + other);
}
}

(b)  Implement a java function that calculates the sum of digits for a give char array consisting of the digits ‘0’ to ‘9’. The function should return the digits sum  as a long value .
import java.util.Scanner;
public class Sum_Average
{
public static void main(String[] args)
{
int n, sum = 0;
float average;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n ; i++)
{
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum:"+sum);
//        average = (float)sum / n;
}
}

(c) Find the smallest & largest element from the array .
import java.util.*;
public class LargestSmallest
{
public static void main(String[] args)
{
Scanner s=new Scanner (System.in);
int n;
System.out.println ("Enter size of array : " );
n=s.nextInt();
int a[] = new int[n];
System.out.println("Enter element : " );
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int min = a[0];
int max = a[0];
for (int i = 1; i < a.length; i++)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
System.out.println("Largest Number in a given array is : " + max);
System.out.println("Smallest Number in a given array is : " + min);
}
}

(4) METHODS & CONSTRUCTORS
(A)Designed a class sort data the contains the method asec()&desc().
import java.util.*;
class SortData
{
public void asec(int [] a,int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void desec(int [] a,int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
}
class SortDataChecek
{
public static void main(String [] arsg)
{
Scanner s=new Scanner(System.in);
int n,i;
System.out.print("Enter size of an array:");
n=s.nextInt();
int []a =new int[n];
System.out.print("Enter Elements of an array:");
for(i=0;i<n;i++)
{
a[i]=s.nextInt();
}
SortData obj=new SortData();
obj.asec(a,n);
System.out.print("Ascendig order Sorting:");
for(i=0;i<n;i++)
{
System.out.print(+a[i]+"  ");
}
System.out.println(" ");
obj.desec(a,n);
System.out.print("descendig order Sorting:");
for(i=0;i<n;i++)
{
System.out.print(+a[i]+"  ");
}
}
}

(B) Designed  a class that demonstrates the use of constructor & destructor .
class Bike1
{
Bike1()
{
System.out.println(" Bike is created");
}
public static void main(String args[])
{
Bike1 b=new Bike1();
}
}

(c) Write a java program to demonstrate the implementation of abstract class.
abstract class Bike
{
abstract void run();
}
class Honda4 extends Bike
{
void run(){System.out.println("running safely..");
}
public static void main(String args[])
{
Bike obj = new Honda4();
obj.run();
}
}

(5) INHERITANCE
a)Write a java program to implement single level inheritance.
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}

b)Write a java program to implement method overriding.
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle
{
public static void main(String args[])
{
Bike obj = new Bike();
obj.run();
}
}

c)Write a java program to implement multiple inheritance.
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}

(6) PACKAGES AND ARRAY
(a) Create a package, add the necessary classes and import the package in java class.
(b) Write a java program to add two matrices & print the resultant matrix.
import java.util.Scanner;

{
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];  //replace '+' with '-' to subtract matrices

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();
}
}
}

(d)	Write a java program for multiplying tow matrices and print the product for the same
import java.util.Scanner;

class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;

Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = 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 number of rows and columns of second matrix");
p = in.nextInt();
q = in.nextInt();

if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];

System.out.println("Enter the elements of second matrix");

for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

System.out.println("Product of entered matrices:-");

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");

System.out.print("\n");
}
}
}
}

a)	Write a java program to implement the vectors.
import java.util.*;
class Vector_demo
{
public static void main(String[] arg)
{
Vector v = new Vector();
System.out.println("Vector is " + v);
}
}

b)	 Write a java program to implement thread life cycle.
c)	Write a java program to implement multithreading.
{
public void run()
{
try
{
" is running");

}
catch (Exception e)
{
System.out.println ("Exception is caught");
}
}
}
{
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i=0; i<8; i++)
{
object.start();
}
}
}

(8)FILE HANDLING
(A) W.J. P to open a file & display the contents in the console window.
import java.io.IOException;
{
public static void main(String[] args)
{
try
{
int character;
{
System.out.print((char) character);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

(B) W.J.P to copy the contents from one file to other file .
import java.io.*;
public class FileDemo2
{
public static void main(String [] args)
{
int i;
FileWriter writer = null;
try
{
writer = new FileWriter("kajal2.txt");
{
System.out.print((char)i);
writer.write(i);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
writer.close();
}
catch(Exception e)
{
}
}
}
}

(C) W.J.P to read the student data  from user and store it in the file.
(9) GUI & EXCEPTION  HANDLING
(a) Desing a AWT program to print the factorial for an input value .
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=FactEvent width=400 height=300></applet>  */
public class FactEvent extends Applet implements ActionListener
{     TextField t1,t2;
Button b1;
int fact =1,m;
String msg;
Label l1,l2;
FactEvent e;
public void init()
{          e=this;
t1=new  TextField (3);
t2=new  TextField (10);
b1=new Button ("Find Factorial");
l1=new Label("Enter the number");
l2=new Label("Result =");
}
public void actionPerformed(ActionEvent ae)
{
String str=t1.getText();
if(str!="")
{       int num=Integer.parseInt(str);
for(int i=num;i>0;i--)
{           fact*=i;         }
msg="  "+fact;
t2.setText(msg);
fact=1;
}
}
}

(b) desing an AWT program  perform various sting operations link reverse string, string concatenation etc.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=StringOperations width=500 height=200></applet>  */
public class StringOperations extends Applet implements ActionListener
{
TextField t1,t2,t3;
Button b1,b2,b3;
String rev=" ";
Label l1,l2,l3;
public void init()
{
l1=new Label("Enter the String1");
t1=new  TextField (30);
b1=new Button ("Find Reverse");
l2=new Label("Enter the String2");
t2=new  TextField (30);
b2=new Button ("Conctenate");
l3=new Label("Result =");
t3=new  TextField (40);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
String str=t1.getText();
int i=str.length()-1;
for(int j=i;j>0;j--)
rev+=str.charAt(j);
t3.setText(rev);
}
if(ae.getSource()==b2)
{
String str=t1.getText()+t2.getText();
t3.setText(str);
}
}
}

(c) W.J.P. to impiement exception handling .

(10) GUI PROGRAMMING .
(a) desing an AWT application that contains the interface to ad student information & display the same .
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=StudentInfo width=400 height=400></applet>  */
public class StudentInfo extends Applet implements ActionListener
{
TextField t1,t2,t3;
Button b1;
Label l1,l2,l3,l4;
public void init()
{
l1=new Label("Enter Student ID:");
t1=new  TextField (30);
l2=new Label("Enter Name");
t2=new  TextField (30);
l3=new Label("Enter Course");
t3=new  TextField (30);
b1=new Button("dispaly info");
l4=new Label("");
}
public void actionPerformed(ActionEvent ae)
{
String str="ID " +t1.getText()+         "\r\n   Name:  "+t2.getText()+"\r\n    Course:  "+t3.getText();
l4.setText(str);
}
}

( b) esing a calculator based on AWT application .
import javax.swing.*;
import java.awt.event.*;
class Calc extends JFrame
{
JLabel l1 , l2, l3;
JButton b1,b2,b3,b4,b5;
JTextField t1, t2,t3;
public Calc()
{
super("Calculator");
setLayout(null);
setSize(500,500);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel(" Enter First Num : ");
l1.setBounds(10,10,150,30);
t1 = new JTextField();
t1.setBounds(170 , 10 , 150 , 30);
l2 = new JLabel(" Enter First Num : ");
l2.setBounds(10,50,150,30);
t2 = new JTextField();
t2.setBounds(170 , 50 , 150 , 30);
b1.setBounds(10 , 90 , 100 , 30);
b2 = new JButton("SUB");
b2.setBounds(120 , 90 , 100 , 30);
b3 = new JButton("MUL");
b3.setBounds(230 , 90 , 100 , 30);
b4 = new JButton("DIV");
b4.setBounds(340 , 90 , 100 , 30);

l3 = new JLabel(" Answer : ");
l3.setBounds(10,130,150,30);
t2 = new JTextField();
t2.setBounds(170 , 130 , 150 , 30);
b5 = new JButton("CLEAR ALL ");
b5.setBounds(100 , 170 , 200 , 30);
setVisible(true);
}
}
public class Program
{
public static void main(String [] args)
{
new Calc();
}
}

(c) desing an AWT application to generate result marks sheet.``````