S . N . V . T GOVT. JUNIOR COLLEGE

TANUKU – 534 211, WEST GODAVARI DISTRICT

INTERMEDIATE VOCATIONAL COURSE

PRACTICAL RECORD

Thisis certified that Mr./Miss.______bearing the Register Number ______Student of 2nd Year CSE student has Completed Oops & JavaPracticalRecordwork done in our S.N.V.T. GOVT. JR. COLLEGE, TANUKU, W.G.District during the academic year 2014 to 2015.

Internal Examiner Head of the Department

External Examiner

JAVA PROGRAMS INDEX

1. (a)Write a simple Java program to print a line of text.

(b)Write a simple Java Program to Add, Subtract, Multiply and divide two integers.

2. (a)Write a Java program to convert the temperature from the command line inCelsius to Fahrenheit.

(b)Write a simple Java program to find if a number from the command line is even or not.

3. (a)Write a Java program to print numbers from 1 to n using the do … while structure.

(b) Program to print sum of n natural Number

4. (a)Write a Java program to print the multiplication table of a given number n using the while structure.

(b)Find if a given number is prime or not using the ‘ for’ statement.

5. (a)Write a program that prints the first 20 Fibonacci numbers.

(b) Create an array of integers and print its values, sum of values and average of the values

6. (a)Find the maximum value from the given array of numbers.

(b)Arrange the given array into ascending order.

7. (a)Create a string array, write values into it and print its values.

(b) Write a program for addition of two matrices.

8. (a)Write a program to Multiplication two matrix

(b)Write a program to find whether given number is perfect or not.

9. (a)Write a program to find the maximum value among three values.

(b)Write a program to find roots of a quadratic equation in all cases.

10. (a)Write a program to find Given number is palindrome or not

(b)Write a Program to find the factorial of given number

11. (a)Create a class called test with private data x and y.

(b)Write all necessary constructors and methods to accept data from user,display user data and return

the sum and product of these variable to theuser.

12. (a) Create a class called “complex” in Java containing data by name” r” and“img” to represent the real

and imaginary parts of a complex number.

(b) Write all necessary constructors and destructor. Write methods that“accept” two objects of complex

class and “return” the sum , differenceand product of these two complex objects.

13. (a) Write a program to create an interface by name” welcome” that contains an unimplemented method “greeting”

(b) Create classes called “English” and “hindi” that implement this method toprint the message “hello”

and “namasthe” respectively. Create necessaryuser functions touse these classes.

14. (a)Develop an applet to display a simple message “ hello”.

(b)Display any image on the applet

15. (a)Develop an applet that contains two text fields and a button. Write programso that the

second text field displays the factorial of the number in thefirst field when the button is clicked

(b)Display a simple message “FACTORIAL”

1.(a) AIM:-Write a simple Java program to print a line of text.

// prints a line of text

class sample

{

public static void main(String args[])

{

System.out.println("welcome to CSE students");

}

}

Output

welcome to CSE students

1.(b)AIM:-Write a program to add , subtract , multiply and divide two integers

classArithmeticOperations

{

public static void main(String args[])

{

int a , b ,x1,x2,x3,x4 ;

a = 10 ;

b = 6 ;

x1 = a+b;

x2 = a-b;

x3 = a*b;

x4 = a/b;

System.out.println("a = "+ a + " b= " + b);

System.out.println("a + b = "+ x1);

System.out.println("a - b = "+ x2);

System.out.println("a * b = "+ x3);

System.out.println("a / b = "+ x4);

}

}

Output

a = 10 b= 6

a + b = 16

a - b = 4

a * b = 60

a / b = 1

2. (a)AIM:-Write a Java program to convert the temperature from the command line inCelsius to Fahrenheit.

// converts temperature from celsius to fahrenheit

import java.util.*;

classCelsiusToFahrenheit

{

public static void main(String args[])

{

floatc,f ;

Scanner in = new Scanner(System.in);

System.out.println("Enter temperature in Celsius");

c = in.nextInt();

f = 9*c/5 + 32;

System.out.println("Temperature in Celsius = " + c);

System.out.println("Temperature in Fahrenheit = " + f);

}

}

input

Enter temperature in Celsius 0

output

Temperature in Celsius = 0.0

Temperature in Fahrenheit = 32.0

input

Enter temperature in Celsius 100

output

Temperature in Celsius = 100.0

Temperature in Fahrenheit = 212.0

input

Enter temperature in Celsius 34

output

Temperature in Celsius = 34.0

Temperature in Fahrenheit = 93.2

input

Enter temperature in Celsius -40

output

Temperature in Celsius = -40.0

Temperature in Fahrenheit = -40.0

2. (b).AIM:-Write a simple Java program to find if a number from the command line is even or not.

Decription:This java program finds if a number is odd or even. If the number is divisible by 2 then it is even, otherwise it is odd. We use modulus operator to find remainder in our program.

// finds the given number is either odd or even

import java.util.Scanner;

classOddOrEven

{

public static void main(String args[])

{

int x;

System.out.println("Enter an integer to check either odd or even ");

Scanner in = new Scanner(System.in);

x = in.nextInt();

if ( x % 2 == 0 )

System.out.println(x+" is an even number.");

else

System.out.println(x+" is an odd number.");

}

}

input

Enter an integer to check either odd or even 3

output

3 is an odd number.

input

Enter an integer to check either odd or even 4

output

4 is an even number.

3. (a).AIM:-Write a Java program to print numbers from 1 to n using the do … whilestructure.

import java.util.Scanner;

class numbers

{

public static void main(String args[])

{

intn,i;

System.out.println("enter an integer");

Scanner in = new Scanner(System.in);

n= in.nextInt();

i=1;

do

{

System.out.println(i);

i=i+1;

}while(i<=n);

}

}

Input

enter an integer 6

output

1

2

3

4

5

6

3. (b). AIM:-Program to print sum of n natural Number

import java.io.*;

classSumNnum

{

public static void main(String[] args) throws IOException

{

BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));

int s=0, n, m, k=0;

System.out.println ("Enter the value :");

n=Integer.parseInt(br.readLine());

for(int i=0;i<=n;i++)

{

s = s+i;

}

System.out.println("The sum of N numbers is :"+s);

}

}

Input

enter an integer :5

output

Sum of N natural numbers is : 15

4. (a). AIM:-Write a Java program to print the multiplication table of a given numbern using the while structure.

// prints multiplication table of n

import java.util.Scanner;

classMultiplicationTable

{

public static void main(String args[])

{

int n, i;

System.out.println("Enter an integer to print it's multiplication table");

Scanner in = new Scanner(System.in);

n = in.nextInt();

System.out.println("Multiplication table of "+n+" is :");

for ( i = 1 ; i <= 10 ; i++ )

System.out.println( n+"*"+i+" = "+(n*i));

}

}

input

Enter an integer to print it's multiplication table 5

output

Multiplication table of 5 is :

5*1 = 5

5*2 = 10

5*3 = 15

5*4 = 20

5*5 = 25

5*6 = 30

5*7 = 35

5*8 = 40

5*9 = 45

5*10 = 50

4. (b). AIM:-Find if a given number is prime or not using the ‘ for’ statement.

// Prime number: A number which is only divisible by one and itself is called a // prime number. Divisible means remainder is zero. 7 is a prime number because // it is only divisible by one and seven. 8 is not a prime number because it is // divisible by one, two, four and eight.

// program finds a number is prime or not

import java.util.Scanner;

classPrimeNumber

{

public static void main(String args[])

{

int n, j ,status = 1 ;

Scanner in = new Scanner(System.in);

System.out.println("Enter a number to find whether prime or not");

n = in.nextInt();

for ( j = 2 ; j <= Math.sqrt(n) ; j++ )

{

if ( n % j == 0 )

{

status = 0;

System.out.println(n+" is not a prime number");

break;

}

}

if ( status == 1 )

{

System.out.println(n+" is a prime number ");

}

}

}

input

Enter a number to find whether prime or not 7

output

7 is a prime number

input

Enter a number to find whether prime or not 8

output

8 is not a prime number

5. (a).AIM:-Write a program that prints the first 20 Fibonacci numbers.

DECRIPTION:- First few numbers of Fibonacci series are 0, 1, 1, 2, 3, 5, 8 ,etc. Except first two terms in sequence every other term is the sum of two previous, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.

prints first 20 Fibonacci numbers

class Fibonacci

{

public static void main(String args[])

{

int f1, f2, f3, c;

f1 = 0;

f2 = 1 ;

System.out.println("First 20 Fibonacci numbers:");

for(c=1;c<=20;c++)

{

System.out.println(f1);

f3=f1+f2;

f1=f2;

f2=f3;

}

}

}

output

First 20 Fibonacci numbers:

0

1

1

2

3

5

8

13

21

34

55

89

144

233

377

610

987

1597

2584

4181

5. (b).AIM:-Create an array of integers and print its values, sum of values andaverage of the values.

// creates an array of integers and print its values, sum of values and

// average of the values

import java.util.Scanner;

classArrayExample

{

public static void main(String args[])

{

Inti,n,s=0,a[] ;

float average;

System.out.println("How many values in an array");

Scanner in = new Scanner(System.in);

n = in.nextInt();

a = new int[n];

System.out.println("enter"+n+"values line by line");

for(i=0;i<n;++i)

{

System.out.print("enter a value");

a[i] = in.nextInt();

s+=a[i];

}

average = (float)s/n ;

System.out.println("Given Array");

for(i=0;i<n;++i)

{

System.out.println(a[i]);

}

System.out.println("sum="+s);

System.out.println("Average="+average);

}

}

input

How many values in an array 5

Enter 5 values line by line

enter a value 12

enter a value 3

enter a value 24

enter a value 32

enter a value 5

output

Given Array

12

3

24

32

5

sum=76

Average=15.2

6. (a).AIM:-Find the maximum value from the given array of numbers.

// finds maximum value in a given array of integers

import java.util.Scanner;

classArrayMax

{

public static void main(String args[])

{

Inti,n,max,a[] ;

System.out.println("How many values in an array");

Scanner in = new Scanner(System.in);

n = in.nextInt();

a = new int[n];

System.out.println("enter "+n+" values line by line");

for(i=0;i<n;++i)

{

System.out.print("enter a value: ");

a[i] = in.nextInt();

}

//process to find maximum value

max = a[0];

for(i=1;i<n;++i)

{

if(max < a[i]) max = a[i];

}

System.out.println("Given Array");

for(i=0;i<n;++i)

{

System.out.println(a[i]);

}

System.out.println("Maximum value = "+max);

}

}

input

How many values in an array 5

enter 5 values line by line

enter a value: 12

enter a value: 23

enter a value: 45

enter a value: 32

enter a value: 18

output

Given Array

12

23

45

32

18

Maximum value = 45

6. (b).AIM:-Arrange the given array into ascending order.

// Sorting an Array of integers

import java.util.*;

classArraySort

{

public static void main(String args[])

{

Inti,n,a[] ;

System.out.print("How many values in an array: ");

Scanner in = new Scanner(System.in);

n = in.nextInt();

a = new int[n]; //specifies array size

System.out.println("enter "+n+" values line by line");

for(i=0;i<n;++i)

{

System.out.print("enter a value: ");

a[i] = in.nextInt();

}

System.out.println("Given Array");

for(i=0;i<n;++i)

{

System.out.println(a[i]);

}

// for array sorting

Arrays.sort(a);

System.out.println("Sorted Array in ascending order");

for(i=0;i<n;++i)

{

System.out.println(a[i]);

}

}

}

input

How many values in an array: 5

enter 5 values line by line

enter a value: 24

enter a value: 13

enter a value: 6

enter a value: 43

enter a value: 11

output

Sorted Array in ascending order

6

11

13

24

43

7. (a).AIM:-Create a string array, write values into it and print its values.

// Array with strings

import java.util.*;

classArrayString

{

public static void main(String args[])

{

Inti,n;

String[] a = new String[]{"Govardhan","Bheemeswar","Ramarao",

"Rajareddy", "Basavraj"};

n = a.length;

for(i=0;i<n;++i)

{

System.out.println(a[i]);

}

}

}

Output

Govardhan

Bheemeswar

Ramarao

Rajareddy

Basavraj

7. (b).AIM:-Write a program for addition of two matrices.

// Matrix Addition

import java.util.Scanner;

classMatrixAddition

{

public static void main(String args[])

{

Intm,n,c,d;

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of a 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 "+m*n+" 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 "+m*n+" 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("First Matrix is:-");

for ( c = 0 ; c < m ; c++ )

{

for ( d = 0 ; d < n ; d++ )

System.out.print(first[c][d]+"\t");

System.out.println();

}

System.out.println("Second Matrix is:-");

for ( c = 0 ; c < m ; c++ )

{

for ( d = 0 ; d < n ; d++ )

System.out.print(second[c][d]+"\t");

System.out.println();

}

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

}

}

}

input

Enter the number of rows and columns of a matrix : 22

Enter 4 elements of first matrix 1234

Enter 4 elements of second matrix5678

output

First Matrix is:-

12

34

Second Matrix is:-

56

78

Sum of entered matrices:-

68

1012

8.(a).AIM:-Write a program to Multiplication two matrix

import java.io.*;

classMatrixMultiplication

{

public static void main(String args[]) throws IOException

{

BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));

inti,j,k;

int a[][] = new int[10][10];

int b[][] = new int[10][10];

int c[][] = new int[10][10];

System.out.println("Enter the A matrix :");

for(i=0;i<2;i++)

for(j=0;j<2;j++)

a[i][j]=Integer.parseInt(br.readLine());

System.out.println("Enter the B matrix :");

for(i=0;i<2;i++)

for(j=0;j<2;j++)

b[i][j]=Integer.parseInt(br.readLine());

for(i=0;i<2;i++)

for(j=0;j<2;j++)

{

c[i][j]=0;

for(k=0;k<2;k++)

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

System.out.println("Multiplication of A & B matrix is :");

for(i=0;i<2;i++)

for(j=0;j<2;j++)

System.out.println(c[i][j]);

}}

input

Enter the number of rows and columns of a matrix22

Enter 4 elements of first matrix1234

Enter 4 elements of second matrix

5

6

7

8

output

First Matrix is:-

12

34

Second Matrix is:-

10

01

Sum of entered matrices:-

12

34

8. (b). AIM:-Write a program to find whether given number is perfect or not.

/*DECRIPTION :- A perfect number is a positive integer that is equal to the sumof its proper positive divisors, that is, the sum of its positivedivisors excluding the number itself. Equivalently, a perfect numberis a number that is half the sum of all of its positive divisors.The first perfect number is 6, because 1, 2 and 3 are its properpositive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6is equal to half the sum of all its positive divisors:

( 1 + 2 + 3 + 6 ) / 2 = 6. */

import java.util.Scanner;

public class PerfectNumber

{

Voidcal(int number)

{

int temp = 0;

for(int i=1;i<=number/2;i++)

{

if(number%i == 0){

temp += i;

}

}

if(temp == number)

{

System.out.println("It is a perfect number");

} else {

System.out.println("It is not a perfect number");

}

}

public static void main(String args[]) throws Exception

{

int n;

PerfectNumberpn = new PerfectNumber();

Scanner in = new Scanner(System.in);

System.out.println("Enter the Number :");

n = in.nextInt();

pn.cal(n);

}

}

Input

Enter the Number: 28

output

It is a perfect number

9. (a).AIM:-Write a program to find the maximum value among three values.

import java.util.Scanner;

classBigNumber

{

public static void main(String[] args)

{

inta,b,c,big;

Scanner in = new Scanner(System.in);

System.out.println("Enter the three Numbers :");

a = in.nextInt();

b = in.nextInt();

c = in.nextInt();

if(a<b)

{

if(b<c)

System.out.println("c is Big Number :"+c);

else

System.out.println("b is Big Number :"+b);

}

else

{

if(a<c)

System.out.println("c is Big Number :"+c);

else

System.out.println("a is Big Number :"+a);

}

}

}

Input:-

Enter the three numbers : 1 2 3

Output :-

c is Big Number : 3

9.(b). AIM:-Write a program to find roots of a quadratic equation in all cases.

Import java.util.Scanner;

Import java.lang.Math;

public class Qequation

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

System.out.println("Enter a,b,c pressing ENTER after each... ");

double a = input.nextDouble();

double b = input.nextDouble();

double c = input.nextDouble();

double root = ((b*b)-(4*a*c));

if(root == 0)

{

double r1 = -(((-b)+ Math.sqrt(root))/(2*a) );

System.out.println(" The Equation has the single root: " +r1);

}

if(root > 0)

{

double r1 = -(((-b)+ Math.sqrt(root))/(2*a) );

double r2 = -(((-b)- Math.sqrt(root))/(2*a) );

System.out.println("The Equation has the two roots: " +r1 + " and " +r2);

}

if(root < 0)

{

System.out.println("\n The Equation is an identity - any value is a root.");

}

}

}

Input:-

Enter a,b,c pressing ENTER after each...: 1 5 6

Output :-

The Equation has the two roots: 2.0 and 3.0

10.(a).AIM:-Write a program to find Given number is palindrome or not

import java.io.*;

class Palindrome

{

public static void main(String[] args) throws IOException

{

BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));

intn,m,s=0;

System.out.println ("Enter the value :");

n=Integer.parseInt(br.readLine());

m=n;

while (n!=0)

{

s=s*10+n%10;

n=n/10;

}

if(s==m)

System.out.println("palindrome");

else

System.out.println("Not pliandrome");

}

}

Input

Enter the Value:151

output

palindrome

10. (b) AIM:- Write a Program to find the factorial of given number by using recursion method

import java.io.*;

class Factorial

{

Staticint fact(int n)

{

int f;

if(n==1)

return n;

f=n*fact(n-1);

return f;

}

public static void main(String[] args) throws IOException

{

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));

int n, f;

System.out.println("enter the range of factorial of number :");

n=Integer.parseInt(br.readLine());

f = fact(n);

System.out.println("Factorial value:"+f );

}

}

Input

Enter the range of factorial of number:5

output

Factorial value: 120

11. (a) AIM:-Create a class called test with private data x and y

class Test

{

privateintx,y;

void display()

{

Scanner in = new Scanner(System.in);

System.out.println("Enter two Numbers :");

x = in.nextInt();

y = in.nextInt();

System.out.println("Sum of two Numbers :"+(x+y));

}

public static void main(String args[])

{

Test t=new Test();

t.display();

}

}

Input

Enter two Numbers :10 20

output

Sum of two Numbers: 30

11. (b).AIM:- Write all necessary constructors and methods to accept data from user, display user data and return the sum and product of these variable to theuser.

ClassMathcal

{

inta,b;

Mathcal(int x, int y)

{

System.out.println("calling constructor");

a=x;b=y;

}

float multi()

{

return a*b;

}

float sum()

{

returna+b;

}

}

ClassConstructorDemo

{

public static void main(String args[])

{

Mathcalobj= new Mathcal(10,20);

float c= obj.sum();

float d= obj.multi();

System.out.println("Sum of Two Numbers is: "+c);

System.out.println("product of Two Numbers is: "+d);

}

}

OUTPUT:-

Sum of Two Numbers is: 30

Product of Two Numbers is: 200

12.(a)Aim:- Create a class called “complex” in Java containing data by name” r” and “img” to represent the real and imaginary parts of a complex number

class Complex

{

int r, img;

Complex(int i, int j)

{

r=i; img=j;

}

void display()

{

System.out.println("Complex Number is : "+r+"+ "+img+"i");

System.out.println("Real Part is :"+r+"\nImaginary Part is :"+img);

}

}

class ComplexNumber

{

public static void main(String[] args)

{

Complex c=new Complex(10,20);

c.display();

}

}

Output:-

Complex Number is : 10+I 20

Real Part is 10

Imaginary Part is 20

12. (b)Write all necessary constructors and destructor. Write methods that “accept” two objects of complex class and “return” the sum , difference and product of these two complex objects.

class Complex

{

int r, img;

Complex(int i, int j)

{

r=i; img=j;

}

void display()

{

System.out.println("("+r+")+ ("+img+")i");

}

}

class TwoComplexNumbers

{

public static void main(String[] args) throws Exception

{

Complex c1=new Complex(1,2);

Complex c2=new Complex(3,4);

Complex s=new Complex();

s.r=c1.r+c2.r;

s.img=c1.img+c2.img;

System.out.println("\n Sum of Two Numbers is: ");

s.display();

Complex d=new Complex();

d.r=c1.r-c2.r;