COMPLETE JAVA PROGRAM File
COMPLETE JAVA PROGRAM File
class HelloWorld
System.out.println("Hello, World!");
OUTPUT:
Hello, World!
1. Program to assign two integer values to X and Y. Using the ‘if’
statement the output of the program should display a message
whether X is greater than Y.
import java.util.Scanner;
class BiggestOfTwoNos
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Input Two Numbers ");
int x=s.nextInt();
int y=s.nextInt();
if(x>y)
System.out.println("X is Greater than Y");
else
System.out.println("Y is Greater than X");
}
}
Output:
Input Two Numbers
10
11
Y is Greater than X
2. Program to list the factorial of the numbers 1 to 10. To calculate the
factorial value, use while loop. (Hint: Fact of 4 = 4*3*2*1)
class Factorial
{
public static void main(String args[])
{
int n=1;
while(n<=10)
{
int fact=1;
int i=1;
while(i<=n)
{
fact=fact*i;
i++;
}
System.out.println("Factorial of "+n+" = "+fact);
n++;
}
}
}
OUTPUT:
Factorial of 1 = 1
Factorial of 2 = 2
Factorial of 3 = 6
Factorial of 4 = 24
Factorial of 5 = 120
Factorial of 6 = 720
Factorial of 7 = 5040
Factorial of 8 = 40320
Factorial of 9 = 362880
Factorial of 10 = 3628800
3. Program to find the area and circumference of the circle by accepting the
radius from the user.
import java.util.*;
Output:
Enter Radius of Circle:
5
Area of Circle : 78.53981633974483
Circumference of Circle : 31.41592653589793
4. Program to add two integers and two float numbers. When no arguments
are supplied, give a default value to calculate the sum. Use function
overloading.
import java.util.Scanner;
class FunOverload
{
int add(int a,int b)
{
int sum=a+b;
return(sum);
}
float add(float a,float b)
{
float sum=a+b;
return(sum);
}
double add()
{
double a=23.45,b=56.56;
double sum=a+b;
return(sum);
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
FunOverload obj=new FunOverload();
System.out.println("Input two integers ");
int i1=s.nextInt();
int i2=s.nextInt();
System.out.println("Sum of two integers = "+obj.add(i1,i2));
System.out.println("Input two float values ");
float f1=s.nextFloat();
float f2=s.nextFloat();
System.out.println("Sum of two float values = "+obj.add(f1,f2));
System.out.println("Sum of two Double values = "+obj.add());
}
}
OUTPUT:
Input two integers
10
20
Sum of two integers = 30
Input two float values
2.1
3.2
Sum of two float values = 5.3
Sum of two Double values = 80.01
5. Program to perform mathematical operations. Create a class called AddSub
with methods to add and subtract. Create another class called MulDiv that
extends from AddSub class to use the member data of the super class.
MulDiv should have methods to multiply and divide A main function should
access the methods and perform the mathematical operations.
class addsub
{ int num1;
int num2;
addsub(int n1, int n2)
{
num1 = n1;
num2 = n2;
} int add()
{ return num1+num2;
} int sub()
{
return num1-num2;
}
}
class multdiv extends addsub
{ public multdiv(int n1, int n2)
{
super(n1, n2);
}
int mul()
{return num1*num2;}
float div()
{return num2/num1;}
public void display()
{
System.out.println("Number 1 :" + num1);
System.out.println("Number 2 :" + num2);
}
}
public class adsb
{ public static void main(String arg[])
{
addsub r1=new addsub(50,20);
int ad = r1.add();
int sb = r1.sub();
System.out.println("Addition =" +ad);
System.out.println("Subtraction =" +sb);
multdiv r2 =new multdiv(4,20);
int ml = r2.mul();
float dv =r2.div();
System.out.println("Multiply =" +ml);
System.out.println("Division =" +dv);
}}
Save Your File as adsb.java
OUTPUT:
Addition =70
Subtraction =30
Multiply =80
Division =5.0
6.Write a program with class variable that is available for
all instances of a class. Use static variable declaration. Observe
the changes that occur in the object’s member variable values.
class Staticvar
{
public static int a,b;
public void display()
{
System.out.println(" A value ="+a+" B value ="+b);
}
}
class Demo
{
public static void main(String args[])
{
Staticvar sv=new Staticvar();
sv.a=10;
sv.b=20;
sv.display();
Staticvar sv1=new Staticvar();
sv.display();
}}
Save your file as Demo
Output:
A value =10 B value =20
import java.util.*;
class Student
String Enrollment_id;
String Name;
Student()
readStudentInfo();
System.out.println("EnrolmentNo: ");
Enrollment_id = sc.next();
System.out.println("Name: ");
Name = sc.next();
sub1 = sc.nextInt();
sub2 = sc.nextInt();
sub3 = sc.nextInt();
else
total = 0;
System.out.println(Enrollment_id+"\t\t"+Name+"\t"+total);
System.out.println("\t\tStudentDetails");
System.out.println("EnrollmentNo\tName\tTotal");
s[i].displayInfo();
}
Save Your File : StudentInfo
Output:
EnrolmentNo:1001
Name:Ram
EnrolmentNo:1002
Name:Anoop
EnrolmentNo:1003
Name:Manvith
StudentDetails
interface Circle
class Rectangle
int length,width;
length=l; width=w;
void areaRect()
int area=length*width;
double area=pi*r*r;
return(area);
int r=s.nextInt();
int l=s.nextInt();
int w=s.nextInt();
shapes.setValues(l,w);
shapes.areaRect();
Output:
Area of a Rectangle = 48
9. Illustrate creation of thread by a) Extending Thread class. b) Implementing
Runnable Interfaces .
a) Extending Thread class
class ThreadA extends Thread
System.out.print(" i = "+i);
System.out.print(" j = "+j);
System.out.print(" k = "+k);
}
}
class MainThread
th1.start();
th2.start();
th3.start();
Output:
k=1k=2j=1i=1k=3j=2j=3i=2k=4j=4i=3k=5j=5i=4k=6j=6i=5k=7k=8j=7i
= 6 k = 9 j = 8 i = 7 k = 10 j = 9 i = 8 k = 11 j = 10 i = 9 i = 10 k = 12 j = 11 i = 11 k = 13 j = 12 i = 12 k = 14
k = 15 j = 13 i = 13 k = 16 j = 14 i = 14 k = 17 j = 15 i = 15 k = 18 j = 16 i = 16 k = 19 k = 20 j = 17 i = 17 k
= 21 k = 22 j = 18 i = 18 k = 23 k = 24 j = 19 i = 19 k = 25 k = 26 j = 20 i = 20 k = 27 k = 28 j = 21 i = 21 k
= 29 k = 30 j = 22 j = 23 i = 22 k = 31 j = 24 i = 23 k = 32 j = 25 i = 24 i = 25 k = 33 j = 26 i = 26 k = 34 j =
27 i = 27 k = 35 j = 28 i = 28 k = 36 j = 29 i = 29 k = 37 j = 30 j = 31 i = 30 k = 38 j = 32 i = 31 k = 39 j = 33
i = 32 k = 40 j = 34 i = 33 i = 34 j = 35 i = 35 j = 36 i = 36 j = 37 i = 37 j = 38 i = 38 j = 39 i = 39 j = 40 i =
40 j = 41 j = 42 j = 43 j = 44 j = 45 j = 46 j = 47 j = 48 j = 49 j = 50
9.b) Implementing Runnable Interface
import java.lang.Thread;
System.out.print(" i = "+i);
System.out.print(" j = "+j);
System.out.print(" k = "+k);
}
}
class MainThreadRun
thread1.start();
thread2.start();
thread3.start();
Output:
k=1k=2k=3i=1j=1k=4i=2j=2k=5i=3j=3k=6i=4j=4j=5k=7k=8i=5j=6k=9i
= 6 i = 7 j = 7 k = 10 i = 8 j = 8 k = 11 i = 9 j = 9 k = 12 i = 10 j = 10 k = 13 i = 11 j = 11 k = 14 i = 12 j = 12
k = 15 i = 13 j = 13 k = 16 i = 14 i = 15 j = 14 k = 17 i = 16 j = 15 k = 18 i = 17 j = 16 k = 19 i = 18 j = 17 k
= 20 i = 19 j = 18 k = 21 i = 20 i = 21 j = 19 k = 22 i = 22 i = 23 j = 20 k = 23 i = 24 j = 21 k = 24 i = 25 j =
22 k = 25 i = 26 j = 23 k = 26 k = 27 i = 27 j = 24 k = 28 i = 28 j = 25 k = 29 i = 29 j = 26 k = 30 i = 30 j =
27 k = 31 i = 31 j = 28 k = 32 i = 32 i = 33 j = 29 k = 33 k = 34 i = 34 j = 30 k = 35 i = 35 i = 36 i = 37 i = 38
j = 31 j = 32 j = 33 j = 34 k = 36 i = 39 i = 40 j = 35 j = 36 j = 37 j = 38 j = 39 j = 40 j = 41 j = 42 k = 37 j =
43 k = 38 j = 44 k = 39 j = 45 k = 40 j = 46 j = 47 j = 48 j = 49 j = 50
/*10.Write a program to demonstrate multilevel inheritance using abstract
class*/
// Abstract class
// Regular method
System.out.println("Zzz");
class Main {
myPig.animalSound();
myPig.sleep();
OUTPUT
Zzz
PART-B
1. Program to catch Negative Array Size Exception. This exception is caused when the array size is
initialized to negative values.
import java.util.Scanner;
class DemoExcpt
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
try
{
System.out.println("Input array size ");
int n=s.nextInt();
int []A=new int[n];
System.out.println("Input Array Elements ");
for(int i=0; i<n; i++)
A[i]=s.nextInt();
System.out.println("Array Elements ");
for(int i=0; i<n; i++)
System.out.print(A[i]+" ");
}catch(NegativeArraySizeException e)
{
System.out.println("Error: Negative Array Size Exception is Raised .....");
}
catch(Exception e)
{
System.out.println("Error: Other Error has been occured ....");
}
}
}
SAVE YOUR FILE: DemoExcpt
OUTPUT
10
20
45
Array Elements
10 20 3 45 8
C:\Users\gpunh\Desktop>java DemoExcpt
-4
import java.util.Scanner;
class ErrorHandling
int a=0,b=0,result=0;
try
a=s.nextInt();
b=s.nextInt();
result=a/b;
}catch(ArithmeticException e)
finally
System.out.println("Result = "+result);
OUTPUT:
12
0
Divide By Zero Exception is Raised ...
Result = 0
C:\Users\gpunh\Desktop>java ErrorHandling
12
Result = 2
3.Write a program which create and displays a message on the window
import java.awt.*;
FrameDemo()
fm.add(lb);
fm.setSize(300,300);
fm.setVisible(true);
import java.awt.event.*;
TextFieldExample2() {
tf3.setEditable(false);
b1 = new Button("+");
b2 = new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);
add(tf2);
add(tf3);
add(b1);
add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
String s1 = tf1.getText();
String s2 = tf2.getText();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
int c = 0;
if (e.getSource() == b1){
c = a + b;
c = a - b;
tf3.setText(result);
// main method
new TextFieldExample2();
import java.awt.*;
import java.awt.event.*;
Label lbl;
KeysDemo()
addKeyListener(this);
requestFocus();
lbl=new Label();
lbl.setBounds(100,100,200,40);
lbl.setFont(new Font("Calibri",Font.BOLD,16));
add(lbl);setSize(400,300);
setLayout(null);
setVisible(true);
lbl.setText("Good morning");
else if
lbl.setText("Good afternoon");
else if
lbl.setText("Good night");
lbl.setText("good evening");
}
public void keyTyped (KeyEvent e)
new KeysDemo();
KeysDemo
8.Demonstrate the various mouse handling events using suitable example
import java.awt.*;
import java.awt.event.*;
Label l;
MouseListenerExample1(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
l.setText("Mouse Clicked");
l.setText("Mouse Entered");
l.setText("Mouse Exited");
l.setText("Mouse Pressed");
l.setText("Mouse Released");
new MouseListenerExample1();
class MenuExample
MenuExample()
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
new MenuExample();
OUTPUT: