0% found this document useful (0 votes)
72 views

Java Programs PDF

The document discusses Java programs to perform temperature conversion, complex number addition and subtraction, volume calculation using method overloading, checking if a string is a palindrome using command line arguments, and manipulating strings using various string functions.

Uploaded by

sachinsaaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Java Programs PDF

The document discusses Java programs to perform temperature conversion, complex number addition and subtraction, volume calculation using method overloading, checking if a string is a palindrome using command line arguments, and manipulating strings using various string functions.

Uploaded by

sachinsaaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

1

EX.NO : 1

TEMPERATURE CONVERSION
DATE :

AIM:
Write a java program to input Celsius value from user and convert temperature from
user and convert temperature from Celsius to Fahrenheit.

FORMULA:

C =(f-32.0)*5/9.0

F=((9*c)/5.0)+32.0

PROCEDURE:

 Start the program.

 Open the notepad and type the following program

 Create a class with class name prg1 and declare needed variables with their data types.

 Calculate Celsius value for given Fahrenheit value using the formula

 C=(f-32.0)*5/9.0

 Calculate Fahrenheit value for given Celsius value using the formula.

 F=((9*c)/5.0)+32.0

 Save the program as prg1.java.

 Compile and run the program using command prompt,as javac prg1.java and

java prg1.

 Display the result.

 Stop the program.

2
PROGRAM:

//prg1.java

import java.util.Scanner;

class prg1

public static void main(String arg[])

double f,c;

Scanner sc=new Scanner (System.in);

System.out.println("Choose type of Conversion\n 1.Fahrenheit to Celsius\n

2.Celsius to Fahrenheit\n");

int ch=sc.nextInt();

switch(ch)

case 1:

System.out.println("Enter Fahrenheit Temperature");

f=sc.nextDouble();

c=(f-32.0)*5/9.0;

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

break;

case 2:

System.out.println("Enter Celsius Temperature");

c=sc.nextDouble();

f=((9*c)/5.0)+32.0;

3
System.out.println("Fahrenheit Temperature is="+c);

break;

default:

System.out.println("Please choose valid choice");

4
OUTPUT:

5
RESULT:

Thus the program was executed successfully.

6
EX.NO : 2

COMPLEX NUMBER ADDITION & SUBTRACTION


DATE :

AIM:
To write a java program to perform complex number addition and subtraction using class
and object.

PROCEDURE:

 Start the program.

 Create a class with class name Complexno and its number function

addComp(),subtractComp(),printComplexNumber().

 Define the complex class member functions associated with its task.

 Create another class with name complex create fine objects(C1,C2,C3) for class comp.

 Invoke complex class member function with the help of objects.

 Save the program as Complexno.java.

 Compile and run the program using command prompt,as javac Complexno.java and

java Complexno.

 Display the result.

 Stop the program.

7
PROGRAM:

//complexno.java

import java.util.Scanner;

public class Complexno

public static void main(String[]args)

Scanner input=new Scanner(System.in);

System.out.println("*************************************\n");

System.out.println("COMPLEX NUMBER ADDITION & SUBTRACTION\n");

System.out.println("*************************************\n");

Complex C1 =new Complex(3,2);

C1.printComplexNumber();

Complex C2 =new Complex(9,5);

C2.printComplexNumber();

Complex C3 =new Complex();

C3 =C3.addComp(C1,C2);

System.out.println("Sum of");

C3.printComplexNumber();

C3 =C3.subtractComp(C1,C2);

System.out.println("Difference of");

C3.printComplexNumber();

class Complex

8
{

int real,imaginary;

Complex()

Complex(int tempReal,int tempImaginary)

real=tempReal;

imaginary=tempImaginary;

Complex addComp(Complex C1,Complex C2)

Complex temp=new Complex();

temp.real= C1.real+C2.real;

temp.imaginary= C1.imaginary+ C2.imaginary;

return temp;

Complex subtractComp(Complex C1,Complex C2)

Complex temp=new Complex();

temp.real= C1.real-C2.real;

temp.imaginary= C1.imaginary- C2.imaginary;

return temp;

void printComplexNumber()

9
{

System.out.println("Complex number:"

+ real+"+"

+imaginary+"i");

10
OUTPUT:

11
RESULT:

Thus the program was executed successfully.

12
EX.NO : 3
TO PERFORM VOLUME CALCULATION USING
METHOD OVERLOADING
DATE :

AIM:

Write a java program to perform volume calculation using method overloading.

FORMULA:

Cube volume=x3(x*x*x)

Cylinder volume=3.14xr*r*h(πr2h)

Rectangle volume=lbh(l*b*h)

PROCEDURE:

 Start the program.


 Create a class with class name volume and its number function area.
 Define the volume class member function associated with its task.
 Create another class with class name Overload(calculate the cube, rectangle and cylinder
using of formula).
 Create an object with name overload and invoke ‘Overload’ class members.
 Save the program as volume.java
 Compile and run the program using command prompt,as javac volume.java and
java volume.
 Display the result.
 Stop the program.

13
PROGRAM:

//volume.java

public class volume

public static void main(String[]args)

Overload overload= new Overload();

System.out.println("*****************************************************");

System.out.println("VOLUME CALCULATION USING METHOD OVERLOADING");

System.out.println("****************************************************");

double rectangleBox=overload.area_rectangle(5,8,9);

System.out.println("Area of rectangular box is"+rectangleBox);

System.out.println("");

double cube=overload.area_cube(5);

System.out.println("Area of cube is"+cube);

System.out.println("");

double cylinder=overload.area_cylinder(6,12);

System.out.println("Area of Cylinder is "+cylinder);

class Overload

double area_rectangle(float l, float w, float h)

14
{

return l*w*h;

double area_cube(float l)

return l*l*l;

double area_cylinder(float r, float h)

return 3.1416*r*r*h;

15
OUTPUT:

16
RESULT:

Thus the program was executed successfully.

17
EX.NO : 4
USING COMMAND LINE ARGUMENT TEXT IF THE
DATE : GIVEN STRING IS POLYNDROME OR NOT

AIM:

Write a java program using command line argument text if the given string palindrome or not.

PROCEDURE:

 Start the program.

 Create a class with class name palindrome and declare needed variable, with their data

type.

 Get the values of string a, b; from user.

 Calculate the palindrome or not using the for loop and equalsIgnoreCase() string

function.

 Save the program as polyndrome.java.

 Compile and run the program using command prompt,as javac palindrome.java and

java palindrome.

 Display the result.

 Stop the program.

18
PROGRAM:

//palindrome.java

import java.util.Scanner;

public class palindrome

public static void main(String[]args)

String a,b="";

Scanner s=new Scanner(System.in);

System.out.println("************************************");

System.out.println("STRING POLYNDROME CHECKING");

System.out.println("********************************");

System.out.println("Enter the String you want to check:");

a=s.nextLine();

int n=a.length();

for(int i=n-1;i>=0;i--)

b=b+a.charAt(i);

if(a.equalsIgnoreCase(b)) {

System.out.println("The string is Polyndrome.");

else{

System.out.println("The String is Not Polyndrome.");

19
OUTPUT:

20
RESULT:

Thus the program was executed successfully.

21
EX.NO : 5
MANIPULATING STRING FUNCTIONS
DATE :

AIM:

Write a java program to perform the string functions length, string concatentation, lower case,

upper case, replace a character.

PROCEDURE:

 Start the program.

 Create a class name String and declare needed variables with their data types.

 Get the value of string s1, s2; from user.

 Manipulating the string by using string methods like length(0, concat(), LowerCase(),

UpperCase(), replace().

 Save the program as string.java.

 Compile and run the program using command prompt,as javac string.java and

java string.

 Display the result.

 Stop the program.

22
PROGRAM:

//stringfunctions.java

import java.util.Scanner;

public class stringfunctions

public static void main(String[]args)

String s1,s2;

Scanner s= new Scanner(System.in);

System.out.println("***********************************************");

System.out.println("\t STRING FUNCTIONS");

System.out.println("***********************************************");

System.out.println("Enter the String you Want to perform String Functions:");

s1=s.nextLine();

System.out.println("/n");

System.out.println("Length of Given String is:"+s1.length());

System.out.println("\n");

System.out.println("Concatenation Operation of Given String is:"+ s1.concat

("HOW ARE YOU"));

System.out.println("\n");

System.out.println("Lower Case Converion Operation of Given

String is:"+s1.toLowerCase());

System.out.println("\n");

System.out.println("Upper case Conversion operation of Given

String is:"+s1.toUpperCase());

23
System.out.println("/n");

System.out.println("Replace operation (Replace the Letter 'h' as 't')of Given

String is:"+s1.replace('h','t'));

System.out.println("\n");

24
OUTPUT:

25
RESULT:

Thus the program was executed successfully.

26
EX.NO : 6
NAME SORTING
DATE :

AIM:

Write a java program to perform name sorting the names in reverse order. If the name contains

any numeric value throw an exception Invalid Name

PROCEDURE:

 Start the program.

 Declare a class name with class name Sorting_names and declare needed variable with

their data types.

 Sort the given set of array using bubble sort technique and string comparison function

compareTo().

 Check the string validation technique using the matches (“[a-zA-Z}*$+”]. If the name

contains any numeric value then display the given name is Invalid Name.

 Save the program as multilevel sorting_names.java.

 Compile and run the program using command prompt,as javac sorting_names.java and

java sorting_names.

 Display the result.

 Stop the program.

27
PROGRAM:

//sorting.java

import java.util.Scanner;

public class sorting

public static void main(String[]args)

String temp;

Scanner s=new Scanner(System.in);


System.out.println("****************************************************\n");

System.out.println("\t NAME SORTING WITH STRING VALIDATION");

System.out.println("****************************************************\n");

System.out.println("Enter the Limit of Array:");

int i,j;

int n=s.nextInt();

s.nextLine();

String names[]=new String[n];

System.out.println("Enter Strings(Names)for Array:");

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

System.out.println("Enter Name["+(i+1)+"]:");

names[i]=s.nextLine();

System.out.println("\n");

System.out.println("String Validation Checking:");

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

if(names[i].matches("^[a-z A-Z]*$+"))

System.out.println("String["+(i+1)+"]is valid!");

System.out.println("\n");

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

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

if(names[j-1].compareTo(names[j])<0)

temp=names[j-1];

names[j-1]=names[j];

names[j]=temp;

System.out.println("\nSorted names are in Descending Order:");

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

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

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

}}

29
OUTPUT:

30
RESULT:

Thus the program was executed successfully.

31
EX.NO : 7
BUILT-IN EXCEPTION
DATE :

AIM:

Write a java program to demonstrate the use of any two built in exception in java.

PROCEDURE:

 Start the program.

 Create a class with class name built.

 Initialize values for variables a[]={5, 10} and b=5.

 Create an exception for finding the error and its type.

 Use two built in exception like,

1.Arithmetic exception

2.Array Index of Bounds Exception

 Based on our exception result any one exception & finally block will be executed.

 Save the program as multilevel built.java.

 Compile and run the program using command prompt,as javac built.java and

java built.

 Display the result.

 Stop the program.

32
PROGRAM: //built.java

public class built

public static void main(String[]args)

int a[]={5,10};

int b=5;

System.out.println("*************************************");

System.out.println("BUILT-IN EXCEPTION");

System.out.println("*************************************");

try

int x=a[1]/b-a[1];

int y=a[1]/(5-5);

catch(ArithmeticException e)

System.out.println("Division by Zero");

catch(ArrayIndexOutOfBoundsException e)

System.out.println("Array Index error!");

33
OUTPUT:

34
RESULT:

Thus the program was executed successfully.

35
EX.NO : 8
MATRIX MULTIPLICATION
DATE :

AIM:

Write a java program to perform matrix multiplication using class and object.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Declare needed array variable with this data type.

 Read an inputs from keyboard for 3*3 A matrix and display it.

 Read an inputs from keyboard for 3*3 B matrix and display it.

 Perform multiplication operation between matrix A and B and Store multiplication result

of matrix C.

 Display the C matrix.

 Save the program as Matrix.java.

 Compile and run the program using command prompt,as javac Matrix.java and

java Matrix.

 Display the result.

 Stop the program.

36
PROGRAM:

//matrix.java

import java.util.Scanner;

public class matrix

public static void main(String[]args)

mat m1,m2,m3;

m1=new mat();

m2=new mat();

m3=new mat();

System.out.println("***********************************");

System.out.println(" MULTIPLICATION MATRIX");

System.out.println(" **********************************");

System.out.println(" Enter the First Matrix Values(3*3 Matrix)");

m1.get();

System.out.println(" First Matrix is");

m1.print();

System.out.println(" Enter the Second Matrix Values(3*3 Matrix)");

m2.get();

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

m2.print();

m3.mul(m1,m2);

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

System.out.println(" Multiplication Matrix");

37
m3.print();

class mat

int a[][]=new int[3][3];

int i,j;

void get()

Scanner s=new Scanner(System.in);

//i=s.nextInt();

//j=s.nextInt();

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

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

a[i][j]=s.nextInt();

void print()

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

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

38
{

System.out.print(" \t"+a[i][j]);

System.out.println(" \n");

void mul(mat m1,mat m2)

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

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

a[i][j]=a[i][j]+m1.a[i][j]*m2.a[i][j];

39
OUTPUT:

40
RESULT:

Thus the program was executed successfully.

41
EX.NO : 9
MULTILEVEL INHERITANCE FOR STUDENT DETAILS
DATE :

AIM:

Write a java program to perform Student details using multilevel inheritance.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Declare needed array variable with this data type.

 Read an inputs(Student number, student name from keyboard.

 Calculate two subject marks and store the result inm total variable.

 Display total marks.

 Save the program as Multilevel_student.java.

 Compile and run the program using command prompt,as javac Multilevel_student.java

and java Multilevel_student.

 Display the result.

 Stop the program.

42
PROGRAM:

//Multilevel_student.java

import java.util.Scanner;

public class Multilevel_student extends marks

private int total;

public void calc()

total=mark1+mark2;

public void puttotal()

System.out.println("Total:"+total);

public static void main(String[]args)

System.out.println("*****************************************************");

System.out.println("MULTILEVEL INHERITANCE FOR STUDENT DETAILS");

System.out.println("****************************************************");

Multilevel_student f= new Multilevel_student();

f.setstud();

f.setmarks();

f.calc();

f.putstud();

43
f.putmarks();

f.puttotal();

class students

private int sno;

private String sname;

public void setstud()

Scanner s=new Scanner(System.in);

System.out.println("Enter Student Number");

sno=s.nextInt();

System.out.println("Enter Student Name");

s.nextLine();

sname=s.nextLine();

public void putstud()

System.out.println("Student Number is:" +sno);

System.out.println("Student Name is:" +sname);

class marks extends students

44
{

int mark1,mark2;

public void setmarks()

Scanner s=new Scanner(System.in);

System.out.println("Enter Student Mark1");

mark1= s.nextInt();

System.out.println("Enter Student Mark2");

mark2=s.nextInt();

public void putmarks()

System.out.println("Mark1 : "+mark1);

System.out.println("Mark2 : "+mark2);

45
OUTPUT:

46
RESULT:

47
EX.NO : 10
MULTIPLE INHERITANCE (Interface) FOR PAYROLL
DATE : PROCESSING

Thus the program was executed successfully.

AIM:

Write a java program to perform payroll processing using Multiple Inheritance.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Declare needed array variable with this data type.

 Read an inputs(employee name, salary, hra from keyboard.

 Calculate gross salary of employee.

 Display the results.

 Save the program as payroll.java.

 Compile and run the program using command prompt,as javac payroll.java and

Java payroll

 Stop the program.

48
PROGRAM:

//payroll.java

import java.util.Scanner;

import java.lang.*;//import language package

public class payroll

public static void main(String[]args)

System.out.println("****************************************************");

System.out.println("MULTIPLE INHERITANCE (interface) FOR PAYROLL


PROCESSING");

System.out.println("*****************************************************");

Salary obj=new Salary();

obj.get();

obj.display();

obj.get_hra();

obj.disp_hra();

obj.gross_sal();

interface Gross//interface declaration

double ta=800.0;

double da=1500.0;//final variable

49
void gross_sal();

class Employee//super class declaration

String name;//data member declaration

float basic_sal;

void get()

Scanner s=new Scanner(System.in);

System.out.println("Enter Empoyee Name");

name=s.nextLine();

//s.nextLine();

System.out.println("Enter Employee Basic Salary");

basic_sal=s.nextFloat();

void display()//method to display value of data member

System.out.println("Name of Employee:"+name);

System.out.println("Basic Salary of Employee:"+basic_sal);

class Salary extends Employee implements Gross //sub class constructor

float hra;

void get_hra()

50
{

Scanner s=new Scanner(System.in);

System.out.println("Enter Employee HRA (House rentallowence");

hra=s.nextFloat();

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

void disp_hra()

display();

System.out.println("HRA (House rent allowence)of Employee:"+hra);

public void gross_sal()//method to calculate gross salary

double gross_sal=basic_sal+ta+da+hra;

System.out.println("TA of Employee:"+ta);

System.out.println("DA(Dearness Allowance)of Employee:"+da);

System.out.println("Gross Salary of Employee:"+gross_sal);

51
OUTPUT:

52
RESULT:

Thus the program was executed successfully.

53
EX.NO : 11
AREA CALCULATION OF SHAPES (Circle, Rectangle)
DATE : USING INTERFACE

AIM:

Write a java program to perform area calculation of different shapes using interface.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Declare needed array variable with this data type.

 Read an inputs(radius, length, breadth values from keyboard.

 Calculate area of circle and area of rectangle values.

 Display the results.

 Save the program as AreaCalculation.java.

 Compile and run the program using command prompt,as javac area.java and

java area .

 Display the results.

 Stop the program.

54
PROGRAM:

//area.java

import java.util.Scanner;

import java.lang.*;

public class area

public static void main(String[]args)

System.out.println("***************************************************");

System.out.println("AREA CALCULATION OF SHAPES(Circle,Rectangle)USING


INTERFACE");

System.out.println("*****************************************************");

Rectangle obj=new Rectangle();

obj.input();

obj.area();

interface Shape

void input();

void area();

class Circle implements Shape

55
int r=0;

double pi=3.14,ar=0;

public void input()

Scanner s=new Scanner(System.in);

System.out.println("Enter Radious Value for Circle");

r=s.nextInt();

public void area()

ar=pi*r*r;

System.out.println("Area of Circle:"+ar);

class Rectangle extends Circle

int length=0,breadth=0;

double ar;

public void input()

super.input();

Scanner s=new Scanner(System.in);

System.out.println("Enter Length Value for Rectangle");

length=s.nextInt();

System.out.println("Enter Breadth value for Rectangle");

56
breadth=s.nextInt();

public void area()

super.area();

ar=length*breadth;

System.out.println("Area of Rectangle:"+ar);

57
OUTPUT:

58
RESULT:

Thus the program was executed successfully.

59
EX.NO : 12
ARITHMETIC OPERATIONS USING PACKAGE
DATE :

AIM:

Write a java program to perform arithmetic operations using package with package name

arithmetic.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Declare needed array variable with this data type.

 Read an inputs(a, b values) from keyboard.

 Create a package with package name arithmetic and create a class with class name

operation.

 Define the operation class for Calculate addition, subtraction, multiplication, division,

modulodivision operation with the values of variables a and b.

 Save the 1st program as operation.java & 2nd program as myclass.java.

 Compile and run the program using command prompt,as javac –d . operation.java &

javac myclass.java and java myclass.

 Display the results.

 Stop the program.

60
PROGRAM:

FILE 1:

//add.java

package mypack;

import java.util.Scanner;

public class add

int a,b,c,d,e,f,g,n,q;

Scanner s=new Scanner(System.in);

public void process()

System.out.println("Enter the value of A");

a=s.nextInt();

System.out.println("Enter the value of B");

b=s.nextInt();

c=a+b;

System.out.println("ADDITION RESULT:"+c);

d=a-b;

System.out.println("SUBTRACTION RESULT:"+d);

e=a*b;

System.out.println("MULTIPLICATION RESULT:"+e);

f=a/b;

System.out.println("DIVISION RESULT:"+f);

g=a%b;

61
System.out.println("MODULO DIVISION RESULT:"+g);

FILE 2:

//myclass.java

import mypack.add;

public class myclass

public static void main(String[]args)

System.out.println("******************************");

System.out.println("ARITHMETIC OPERATION USING PACKAGE");

add obj=new add();

obj.process();

62
OUTPUT:

63
RESULT:

Thus the program was executed successfully.

64
EX.NO : 13
GENERATE FIBONACCI SERIES & PERFECT NUMBERS
DATE : BETWEEN LIMIT USING EXCEPTION

AIM:

Write a java program to create atwo threads such that one of the thread generate Fibonacci

series and another generate perfect numbers between two given limits.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Create a main class with class name fibo_perfect for generate Fibonacci series.

 Create another class with class name perfect for generate perfect number.

 Read an input from keyboard for getting limit.

 Create an object t1, t2 for invoke fib, perfect class members with the help of start() and

sleep() Functions.

 Save the program as fibo_perfect.java.

 Compile and run the program using command prompt,as javac fibo_perfect.java and

java fibo_perfect.

 Display the results.

 Stop the program.

65
PROGRAM:

//fibo.java

import java.util.Scanner;

public class fibo

public static void main(String[]args)

try{

fib t1=new fib();

t1.start();

t1.sleep(5000);

perfect t2=new perfect();

t2.start();

catch(Exception e){}

class fib extends Thread

public void run()

try

int a=0,b=1,c=0;

Scanner s=new Scanner(System.in);

66
System.out.println("Enter the limit");

int n=s.nextInt();

System.out.println("Fibonacci series:");

while(n>0)

System.out.println(c);

a=b;

b=c;

c=a+b;

n=n-1;

catch(Exception e){}

class perfect extends Thread

public void run()

try

//System.out.println("Perfect no is");

System.out.println("Enter the limit");

Scanner s=new Scanner(System.in);

int n=s.nextInt();

67
for(int j=1;j<n;j++)

int counter=0;

for(int i=1;i<j;i++)

if(j%i==0)

counter=counter+i;

if(counter==j)

System.out.println(j);//here you can print j or counter

catch(Exception e){}

68
OUTPUT:

69
RESULT:

Thus the program was executed successfully.

70
EX.NO : 14
MARKS OUT OF BOUND EXCEPTION
DATE :

AIM:

Write a java program to define an exception called: Marks out of bound exception that is
thrown if the entered marks are greater than 100.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Create a class with class name Marks.

 Read an input from keyboard for student name and mark.

 Using ArrayIndexOutOfBoundsException and check whether the given mark is within

100 or not.

 If the mark is lower than 100 then display ‘Congrats: The Mark you gave Correct’

message.

 Otherwise display ‘Your Mark is Out Of Bounds’ message.

 Save the program as Marks.java.

 Compile and run the program using command prompt,as javac Marks.java and

java Marks.

 Display the results.

 Stop the program.

71
PROGRAM:

//Marks.java

import java.util.Scanner;

import java.lang.Exception;

public class Marks

public static void main(String[]args)

String name;

int mark;

Scanner s=new Scanner(System.in);

System.out.println("\n");

System.out.println("******************************************************");

System.out.println("\n EXCEPTION HANDLING[ArrayIndexOutOfBoundsException]\n");

System.out.println("******************************************************");

System.out.println("Enter Student Name:");

name=s.nextLine();

System.out.println("Enter Student Mark:");

mark=s.nextInt();

if(mark>100)

try

System.out.println("\n");

System.out.println("Your Mark is Out Of Bounds");

72
catch(ArrayIndexOutOfBoundsException e){}

if(mark<=100)

System.out.println("\n");

System.out.println("Congrats:The Mark you gave Correct");

73
OUTPUT:

74
RESULT:

Thus the program was executed successfully.

75
EX.NO : 15
WRAPPER CLASS
DATE :

AIM:

Write a java program to convert one data type value into other type using Wrapper class

method, here we convert given integer value into binary value.

PROCEDURE:

 Start the program.

 Open notepad and type the following program.

 Create a class with class name Wrapper.

 Read an integer input fron keyboard for convert into binary value.

 Using Integer to BinaryString() method for conversion.

 Save the program as wrapper.java.

 Compile and run the program using command prompt,as javac Wrapper.java and

java Wrapper.

 Display the results.

 Stop the program.

76
PROGRAM:

//Wrapper.java

import java.util.Scanner;

public class Wrapper

public static void main(String[]args)

Scanner s=new Scanner(System.in);

System.out.println("********************");

System.out.println("\t WRAPPER CLASS");

System.out.println("********************");

int a;

System.out.println("Enter a value for Convert Integer type to Binary Type");

a=s.nextInt();

String binary=Integer.toBinaryString(a);

System.out.println("Binary value:"+binary);

77
OUTPUT:

78
RESULT:

Thus the program was executed successfully.

79
EX.NO : 16
BYTE STREAM
DATE :

AIM:

Write a java program to perform file processing using byte stream.

PROCEDURE:

 Start the program.

 Create a class with class name Byte and declare needed variables with their data types.

 Initialize the array values as {1,2,3,4} for variable ‘array’ as byte type.

 Using ByteArrayInputStream() method to read an array of inputs from file.

 Save the program as byte_stream.java.

 Compile and run the program using command prompt,as javac Byte.java and

java Byte.

 Display the result.

 Stop the program.

80
PROGRAM: //Byte.java

import java.io.ByteArrayInputStream;

public class Byte

public static void main(String[]args)

byte[]array={1,2,3,4};

System.out.println("************************");

System.out.println("\t BYTE STREAM");

System.out.println("**********************");

try

ByteArrayInputStream input=new ByteArrayInputStream(array);

System.out.println("The bytes read from the input stream:");

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

{//reads the bytes

int data=input.read();

System.out.println(data +",");

input.close();

catch(Exception e)

e.getStackTrace();

}}}

81
OUTPUT:

82
RESULT:

Thus the program was executed successfully.

83
EX.NO : 17
CHARACTER STTREAM
DATE :

AIM:

Write a java program to perform file processing using character stream.

PROCEDURE:

 Start the program.

 Open Notepad and Type the following program.

 Create a class with class name Char and declare needed variables with their data types.

 Initialize the array values as {‘w’,’e’,’l’,’c’,’o’,’m’,’e’} for variable ‘charArray’ as byte

type.

 Using Writer writer = new PrintWriter(System.out) method.

 Save the program as char_stream.java.

 Compile and run the program using command prompt,as javac Char.java and

java Char.

 Display the result.

 Stop the program.

84
PROGRAM:

//Char.java

import java.io.*;

public class Char

public static void main(String[]args)

System.out.println("*******************************");

System.out.println("\t CHARACTER STREM");

System.out.println("********************************");

try

Writer writer=new PrintWriter(System.out);

char[]charArray={'W','E','L','C','O','M','E'};

writer.write(charArray);

writer.flush();

catch(Exception e)

System.out.println(e);

85
OUTPUT:

86
RESULT:

Thus the program was executed successfully.

87
EX.NO : 18
DRAW SHAPES USING APPLET
DATE :

AIM:

Write a Java Applet program to draw the following shapes: Cone, Cylinder, Square Inside a

Circle, Circle Inside a Square.

PROCEDURE:

 Start the program.

 Create a class with class name draw_shapes.

 Draw the given shapes by using paint() method.

 Save the program file1 as draw_shapes.java & File2 as draw_shapes.html.

 Compile and run the program using command prompt,as javac draw_shapes.java and

appletviewer draw_shapes.java.

 Display the result.

 Stop the program.

88
PROGRAM:

FILE 1:

//draw_shapes.java

import java.applet.Applet;

import java.awt.*;

import java.applet.*;

public class draw_shapes extends Applet

public void paint(Graphics g)

/*Cylinder*/

g.drawString("(a).Cylinder",10,110);

g.drawOval(10,10,50,10);

g.drawOval(10,80,50,10);

g.drawLine(10,15,10,85);

g.drawLine(60,15,60,85);

/*Cube*/

g.drawString("(b).Cube",95,110);

g.drawRect(80,10,50,50);

g.drawRect(95,25,50,50);

g.drawLine(80,10,95,25);

g.drawLine(130,10,145,25);

g.drawLine(80,60,95,75);

g.drawLine(130,60,145,75);

89
/*Square inside a Circle*/

g.drawString("(c).Square inside a Circle",150,110);

g.drawOval(180,10,80,80);

g.drawRect(192,22,55,55);

/*Circle inside A Square*/

g.drawString("(d).Circle inside A Square",290,110);

g.drawRect(290,10,80,80);

g.drawOval(290,10,80,80);

/*<html>

<applet code="draw_shapes.class"width=500 height=500></applet>

</html>*/

FILE 2:

//draw_shapes.html

<html>

<applet code="draw_shapes.class"width=500 height=500></applet>

</html>

90
OUTPUT:

91
RESULT:

Thus the program was executed successfully.

92
EX.NO : 19
DESIGN A SIMPLE CALCULATOR
DATE :

AIM:

Write a Java Applet program to design a simple calculator.

PROCEDURE:

 Start the program.

 Create a class with class name Calculator.

 Design the calculator by using Button,Textbox, frame properties with the help of init()

method.

 Save the program file1 as calculator.java & File2 as Calculator.html.

 Compile and run the program using command prompt,as javac Calculator.java and

appletviewer Calculator.java.

 Display the result.

 Stop the program.

93
PROGRAM:

FILE 1:

//calculator.java

import java.util.Scanner;

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class Calculator extends Applet implements ActionListener

Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;

String s1="",s2;

Frame f;

Panel p2;

TextField t;

int n1,n2;

public void init()

setLayout(new BorderLayout());

t=new TextField();

p2=new Panel();

p2.setLayout(new GridLayout(4,4));

b1 =new Button("1");

b1.addActionListener(this);

b2= new Button("2");

94
b2.addActionListener(this);

b3= new Button("3");

b3.addActionListener(this);

b4=new Button("+");

b4.addActionListener(this);

b5=new Button("4");

b5.addActionListener(this);

b6=new Button("5");

b6.addActionListener(this);

b7=new Button("6");

b7.addActionListener(this);

b8=new Button("-");

b8.addActionListener(this);

b9=new Button("7");

b9.addActionListener(this);

b10=new Button("8");

b10.addActionListener(this);

b11=new Button("9");

b11.addActionListener(this);

b12=new Button("*");

b12.addActionListener(this);

b13=new Button("c");

b13.addActionListener(this);

b14=new Button("0");

b14.addActionListener(this);

95
b15=new Button("/");

b15.addActionListener(this);

b16=new Button("=");

b16.addActionListener(this);

add(t,"North");

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.add(b7);

p2.add(b8);

p2.add(b9);

p2.add(b10);

p2.add(b11);

p2.add(b12);

p2.add(b13);

p2.add(b14);

p2.add(b15);

p2.add(b16);

add(p2);

public void actionPerformed(ActionEvent e1)

96
String str=e1.getActionCommand();

if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/"))

String str1=t.getText();

s2=str;

n1=Integer.parseInt(str1);

s1="";

else if(str.equals("="))

String str2=t.getText();

n2=Integer.parseInt(str2);

int sum=0;

if(s2=="+")

sum=n1+n2;

else if(s2=="-")

sum=n1-n2;

else if(s2=="*")

sum=n1*n2;

else if(s2=="/")

sum=n1/n2;

String str1=String.valueOf(sum);

t.setText(""+str1);

s1="";

97
else if(str.equals("c"))

t.setText("");

else

s1+=str;

t.setText(""+s1);

/*<html>

<applet code="Calculator.class" width=500 height=500></applet>

</html>*/

FILE 2:

//calculator.html

<html>

<applet code="draw_shapes.class"width=500 height=500></applet>

</html>

98
OUTPUT:

99
RESULT:

Thus the program was executed successfully.

100
EX.NO : 20
BALL BOUNCING USING ANIMATION
DATE :

AIM:

Write a Java Applet program to animate a ball across the screen.

PROCEDURE:

 Start the program.

 Create a class with class name ball_bouncing.java.

 Design a ball (oval shape) with the help of paint() method.

 Using run() method to move ythe shape across the screen.

 Using start(),stop() methods for animation.

 Save the program file1 as ball bouncing.java & File2 as ball bouncing.html.

 Compile and run the program using command prompt,as javac ball_bouncing.java and

appletviewer ball_bouncing.java.

 Display the result.

 Stop the program.

101
PROGRAM:

FILE 1:

//ball_bouncing.java

import java.applet.Applet;

import java.applet.*;

import java.awt.*;

public class ball_bouncing extends Applet implements Runnable

int x= 150,y= 50,r= 20;

int dx= 11,dy= 7;

// create thread.

Thread t;

boolean stopFlag;

public void start()

t= new Thread(this);

stopFlag=false;

t.start();

public void paint(Graphics g)

g.setColor(Color.green);

g.fillOval(x-r, y-r, r*2, r*2);

102
public void run()

while(true)

if(stopFlag)

break;

//Bounce if we've hit an edge

if((x - r + dx < 0) || (x + r + dx > bounds().width)) dx = -dx;

if((y - r + dy < 0) || (y + r + dy > bounds().height)) dy = -dy;

x += dx; y += dy;

try

Thread.sleep(100);

catch(Exception e)

System.out.println(e);

//print circle again n again

repaint();

// function to stop printing

public void stop()

103
stopFlag=true;

t=null;

/*<html>

<applet code="ball_bouncing.class" width=500 height=500></applet>

</html>*/

FILE 2:

//ball_bouncing.html

<html>

<applet code="draw_shapes.class"width=500 height=500></applet>

</html>

104
OUTPUT:

105
RESULT:

Thus the program was executed successfully.

106
107

You might also like