Java Programs for Basic Operations
Java Programs for Basic Operations
Sample I/O
23
Addition of 2 and 3 is 5
*/
import [Link];
class Addition
int a,b,c;
a=[Link]();
b=[Link]();
c=a+b;
Sample I/O:
65
23
After Swap:
23
65
*/
//write code here
import [Link];
class SwapTest{
[Link]();
[Link]();
[Link]();
class Swap
int a,b,temp;
void input()
a=[Link]();
b=[Link]();
void process()
temp=a;
a=b;
b=temp;
void output()
{
[Link]("After Swap:\n"+a+"\n"+b);
3. /* Program to read amount in rupees. If amount is >=300 print idly, vada, dosa and puri followed by glass of water.
sample I/O
sample 1
output:
400
Idly
Vada
Dosa
Puri
glass of water
sample 2
30
glass of water
*/
import [Link];
class Tiffin
int amt;
void process()
{
if(amt>=300)
[Link]("Idly");
[Link]("Vada");
[Link]("Dosa");
[Link]("Puri");
[Link]("glass of water");
class TiffinTest{
[Link]=[Link]();
[Link]();
Sample I/O
15.50
Area of circle:754.7676350249478
Perimeter of circle:97.38937226128358
*/
import [Link];
//create a class
class Circle{
double r;
double getArea()
return([Link]*r*r);
double getPerimeter()
return([Link]*2*r);
c.r=[Link]();
};
5. /*Write a java program to read three subject marks and display pass or fail.
Sample I/O:
case 1:
60
20
50
Fail
case 2:
70
802
90
Pass
*/
class Result
p=a;
q=b;
r=c;
void process()
[Link]("Pass");
else
[Link]("Fail");
class ResultTest{
int m1,m2,m3;
m1=[Link]();
m2=[Link]();
m3=[Link]();
[Link]();
}
Sample Input
42
3.1415
true
Sample Output
Boolean: true
Double: 3.1415
Byte: 42
Sample I/O-2:
128
Value:"128" Radix:10
at [Link](Unknown Source)
at [Link](Unknown Source)
at [Link]([Link])
*/
import [Link];
class DataType
byte a;
double b;
boolean c;
a=[Link]();
b=[Link]();
c=[Link]();
[Link]("Boolean:"+" "+c);
[Link]("Double:"+" "+b);
[Link]("Byte:"+" "+a);
7. /*program to find the sum of the individual digits of the given number.
Sample I/O:
1234
Sum of digits: 10
*/
import [Link];
class Demo{
[Link]();
[Link]();
[Link]();
class SumOfDigits
void input()
{
n=[Link]();
void process()
while(n>=1)
m=n%10;
sum=sum+m;
n=n/10;
void output()
the properties customer name,account number & balance and a methods to deposit,
Charge a 5 penalty if an attempt is made to withdraw more money than available in the account.
Sample Output:
3000
1000
1000
2000
*/
import [Link];
class Account
String name;
long num;
double balance;
[Link]=name;
[Link]=num;
[Link]=balance;
}
public void withdraw(double z)
z=[Link]();
if(z>balance)
balance=balance-5;
else
balance=balance-z;
z=[Link]();
balance=balance+z;
return(balance);
class TestAccount
double x;
[Link](2500);
[Link](500);
56
*/
import [Link];
class Area
return(3.14*a*a);
return(a*b);
class Overloading
int a,b,c;
a=[Link]();
b=[Link]();
[Link] Programs
import [Link];
class Area
return(3.14*a*a);
return(a[0]*a[1]);
class Overloading
int a,b,c;
a=[Link]();
b=[Link]();
11. /*Write a Java program that prints the numbers from 1 to 50. But for multiples of three print "Fizz" instead of
the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and
SAMPLE OUTPUT:
Fizz
Buzz
Fizz
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
*/
class FizzBuzzTest
for(int i=1;i<=50;i++)
[Link]("FizzBuzz");
[Link]("Buzz");
[Link]("Fizz");
else
[Link](i);
12. /* Write a Java Program To Calculate Sum Of First And Last Digit Of A Number
*/
import [Link];
class Sum
int n=[Link]();
int last=n%10;
int first=0;
while(n>=1)
{
first=n;
n=n/10;
13. /* Write a Java Program To Calculate Perfect Squares Between Two Numbers
Sample I/O:
*/
import [Link].*;
class Squares
int a,b;
a=[Link]();
b=[Link]();
for(int i=a;i<=b;i++)
if(i%[Link](i)==0)
[Link](i);
}
14. /*Java Program To Find All Pairs Of Elements In An Array Whose Sum Is Equal To A Given Number :
Sample Output :
4 + 6 = 10
5 + 5 = 10
-10 + 20 = 10
12 + 38 = 50
23 + 27 = 50
125 + -75 = 50
*/
static void findThePairs(int inputArray[],int inputNumber)// start writing the code here
for(int i=0;i<[Link];i++)
for(int j=i+1;j<[Link];j++)
if(inputArray[i]+inputArray[j]==inputNumber)
}
public static void main(String[] args)
findThePairs(new int[] {12, 23, 125, 41, -75, 38, 27, 11}, 50);
15. /*
Write a java program to find the no. of times each charecter repeated in a given String.
Sample Input/Output:
Example 1:
java
Example 2:
hello
*/
class RepetitionsInString
String a,temp="";
int i,j,cnt=0;
a=[Link]();
for(i=0;i<[Link]();i++)
char c=[Link](i);
for(j=i;j<[Link]();j++)
char c2=[Link](j);
cnt++;
if([Link](c)==-1)
temp+=c;
cnt=0;
Sample Output:
*/
import [Link].*;
class CountTheWords
{
public static void main(String[] args)
String a;
int n;
a=[Link]();
n=[Link];
17. /* Write a Java Program To Calculate Sum Of First And Last Digit Of A Number
SAMPLE INPUT/OUTPUT:
456
*/
import [Link];
class Sum
int num;
num=[Link]();
int last=num%10;
int first=0;
while(num>=1)
first=num;
num=num/10;
}
a house number,
a street,
a city,
a state and a
postal code.
Supply a print function that prints the address with the street on one line and the city,
Sample output:
Street: Narayanguda
*/
import [Link].*;
class Address
int hno,oan;
String str;
String city;
String state;
int pc;
Address()
}
Address(int oan)
str="Narayanguda";
city="Hyd";
state="Telengana";
pc=500021;
[Link]("Street: "+str);
class Test
[Link]("Narayanguda","Hyd","Telengana",500021);
19. /*Write a Java Program to define a class,define instance methods for setting and Retrieving values of
SAMPLE OUTPUT:
Name:smith
ID:1234
Address:narayanguda
*/
import [Link].*;
class empdemo
[Link]("smith",1234,"narayanguda");
[Link]();
class Employee
n="smith";
num=1234;
add="narayanguda";
void putData()
String nam="smith";
int nu=1234;
String addre="narayanguda";
[Link]("Name:"+nam);
[Link]("ID:"+nu);
[Link]("Address:"+addre);
*/
import [Link].*;
class add_demo
[Link](10,20);
obj.display2(10.2,20.2);
class Sum
i=10;
j=20;
int add=i+j;
y=10.2;
z=20.2;
double add=y+z;
}
21. /*Program to read person's age and display eligible to vote or not.
SAMPLE I/O:
CASE 1:
23
CASE 2:
16
*/
import [Link];
class VoteTest{
int age;
age=[Link]();
[Link]();
class Vote
int a;
Vote(int i)
a=i;
}
void process()
if(a>=18)
else
create a sub class Result with total and average and result.
Sample I/O:
Case 1:
Enter id:
Enter name:
abc
40
50
60
Student id:1
Name :abc
Subject1 :40
Subject2 :50
Subject3 :60
Total is :150
Average is:50.0
CASE 2:
Student id:2
Name :xyz
Subject1 :70
Subject2 :0
Subject3 :90
Total is :160
Average is:53.0
result is :Fail
CASE 3:
Student id:3
Name :mno
Subject1 :88
Subject2 :99
Subject3 :100
Total is :287
Average is:95.0
result is :Distinction
CASE 4:
Student id:4
Name :pqr
Subject1 :66
Subject2 :59
Subject3 :70
Total is :195
Average is:65.0
*/
import [Link].*;
class StudentInheritanceTest
int id,m1,m2,m3;
String name;
[Link]("Enter id:");
id=[Link]();
[Link]("Enter name:");
name=[Link]();
m1=[Link]();
m2=[Link]();
m3=[Link]();
[Link]();
[Link]();
class Student
int id;
String name;
int m1,m2,m3;
[Link]=id;
[Link]=name;
this.m1=m1;
this.m2=m2;
this.m3=m3;
void output()
[Link]("Student id:"+id);
[Link]("Name :"+name);
[Link]("Subject1 :"+m1);
[Link]("Subject2 :"+m2);
[Link]("Subject3 :"+m3);
int total;
double avg;
String result;
super(id,name,m1,m2,m3);
void process()
total=m1+m2+m3;
avg=total/3;
if(avg>=70)
result="Distinction";
else if(avg>=60 && avg<70)
result="First Division";
result="Second Division";
result="Third Division";
else
result="Fail";
void output()
[Link]();
[Link]("Total is :"+total);
[Link]("Average is:"+avg);
[Link]("result is :"+result);
Sample I/O:
*/
Shapes s[];
s=new Shapes[10];
s[0]=new Rectangle();
s[1]=new Triangle();
s[2]=new Circle();
for(int i=0;i<3;i++)
s[i].printArea(10,20);}
int a,b;
double c=3.14;
}
class Triangle extends Shapes
24. /* You are given a class Test with a main method to demonstrate static modifier.
Sample I/O:
*/
}
}
class Student
static
cnt=0;
String na;
long b,l;
na=a;
this.b=b;
this.l=l;
cnt++;
return cnt;
25. /*Q2) Read the content of the file [Link], count and display number of characters,
younus shariff
tejaswitha)
Sample I/O:
*/
import [Link].*;
class CountCWL
int i=0,cnt1=0,cnt2=0,cnt=0;
while(true)
i=[Link]();
if(i==-1)
break;
cnt1++;
if((char)i=='\n')
cnt2++;
cnt++;
26. /*(Q)Copy the content of the file [Link] to [Link] a byte at a time.
shivaram
Sample Output:
shivaram
*/
import [Link].*;
class FileCopy1
int ch;
while(true)
ch=[Link]();
if(ch==-1)
break;
[Link]((char)ch);
// code for displaying the content of [Link] file. is already given below
//Read characters from fis2 into ch and write onto the monitor,repeat this till end of the file is reached
while(true)
ch=[Link]();
if(ch==-1)
break;
[Link]((char)ch);
[Link]();
Read all the numbers from [Link] and write all even numbers into the file '[Link]',
SAMPLE OUTPUT:
11 33 55
*/
import [Link].*;
import [Link].*;
class FileRW
int i,x;
[Link](22);
[Link](33);
[Link](44);
[Link](55);
[Link]();
while([Link]()>0)
x=[Link]();
if(x%2==0)
[Link](x);
else
[Link](x);
[Link]();
[Link]();
[Link]();
// Code For Displaying the contents of the file '[Link]' is givenm below.
while([Link]()>0)
x=[Link]();
[Link](x+" ");
[Link]();
28. /*
Display the name of the student for the given roll number.
SAMPLE OUTPUT:
CASE 1:
15BD1A0503
STUDENT NAME=kiran
CASE 2:
15BD1A0508
*/
import [Link].*;
import [Link].*;
String rno;
String name;
[Link]=rno;
[Link]=name;
class ObjectIO
// Writing the details of three students on to the file '[Link]' is given below
[Link](s1);
[Link](s2);
[Link](s3);
[Link]();
// write the code for Displaying the name of the student for the given roll number.
String r;
[Link]("enter the student roll number:");
r=[Link]();
if([Link]([Link]))
[Link]("STUDENT NAME="+[Link]);
else if([Link]([Link]))
[Link]("STUDENT NAME="+[Link]);
else if([Link]([Link]))
[Link]("STUDENT NAME="+[Link]);
else
29. /*create a thread that prints the multiplication table for number 5
Sample output:
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
*/
int f=5;
public void run()
for(int i=1;i<=10;i++)
class MyThread1
[Link]();
30. /*Write a program that displays 5th and 6th mathematical tables.
SAMPLE I/O:
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
6 *1 = 6
6 *2 = 12
6 *3 = 18
6 *4 = 24
6 *5 = 30
6 *6 = 36
6 *7 = 42
6 *8 = 48
6 *9 = 54
6 *10 = 60
*/
for(int i=1;i<=10;i++)
class MultipleTables2
[Link]();
[Link]();
for(int i=1;i<=10;i++)
31. /*
Write a Java program that implements a multi-thread application that has three threads.
if the value is even,second thread computes the square of the number and prints.
If the value is odd, the third thread will print the value of cube of the number.
input/output:
--------------
case 1:
Enter x value :
X=4 SQUARE=16
case 2:
Enter x value :
X=5 CUBE=125
*/
import [Link].*;
int x;
Thread2(int a)
{
x=a;
[Link]("X="+x+" SQUARE="+(x*x));
int x;
Thread3(int a)
x=a;
[Link]("X="+x+" CUBE="+(x*x*x));
class Thread1
int x=[Link]();
if(x%2==0)
[Link]();
else
{
[Link]();
32. /**
Producer is waiting...
Product3 is consumed.
Consumer is waiting...
Product4 is produced.
Producer is waiting...
Product4 is consumed.
Consumer is waiting...
Product5 is produced.
Producer is waiting...
Product5 is consumed.
Consumer is waiting...
Product6 is produced.
Producer is waiting...
Product6 is consumed.
Consumer is waiting...
Product7 is produced.
Producer is waiting...
Product7 is consumed.
Consumer is waiting...
Product8 is produced.
Producer is waiting...
Product8 is consumed.
Consumer is waiting...
Product9 is produced.
Producer is waiting...
Product9 is consumed.
Consumer is waiting...
Product10 is produced.
Product10 is consumed.
*/
class Buffer
int a;
if(produced)
[Link]("Producer is waiting...");
try{
wait();
}catch(Exception e){
[Link](e);
a=x;
produced = true;
notify();
if(!produced)
{
[Link]("Consumer is waiting...");
try
wait();
catch(Exception e){}
produced=false;
notify();
Buffer b;
public Producer(Buffer b)
this.b = b;
[Link](i);
Buffer b;
Consumer(Buffer b)
this.b=b;
for(int count=1;count<=10;count++)
[Link]();
//starting threads.
[Link]();
[Link]();
class Test
[Link](true);
//[Link](400,700);
//[Link](100,100);
[Link](500,100,300,400);
[Link](JFrame.DISPOSE_ON_CLOSE);//DO_NOTHING,EXIT,HIDE
[Link]("KMIT");
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public Test()
setVisible(true);
//setSize(400,700);
//setLocation(100,100);
setBounds(500,100,800,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("KMIT");
//setResizable(false);
Container c=getContentPane();
[Link]([Link]);
add(l);
setLayout(null);
[Link](100,100,400,300);
[Link]([Link]);
[Link](f);
[Link]("This is label");
}
public static void main(String args[])
new Test();
import [Link];
import [Link];
class Test
[Link](300,150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](20,20,120,30);
[Link](t1);
[Link](75,60,80,50);
[Link](b1);
[Link](true);
import [Link];
import [Link];
import [Link];
import [Link];
class Test
[Link](500,100,800,400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link]("Demo application");
[Link](null);
[Link](10,10,80,25);
[Link](10,30,80,25);
[Link](l1);
[Link](l2);
[Link](100,10,160,25);
[Link](100,10,160,25);
[Link](t1);
[Link](t2);
[Link](100,40,160,25);
[Link](100,40,160,25);
[Link](p1);
[Link](p2);
[Link](10,80,80,25);
[Link](180,80,150,25);
[Link](b1);
[Link](b2);
[Link](true);
import [Link];
import [Link];
import [Link];
import [Link];
class Test
[Link](500,150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout());
[Link](l);
[Link](t);
[Link](b);
[Link](true);
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Test()
setSize(300,150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(b);
setVisible(true);
@Override
new Test();
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Test()
{
setSize(300,150);
setTitle("Event");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
[Link](this);
[Link](this);
[Link](this);
setVisible(true);
@Override
setBackground([Link]);
new Test();
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b;
Test()
setSize(500,150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout([Link]));
l3=new JLabel("Result");
t1=new JTextField(8);
t2=new JTextField(8);
t3=new JTextField(8);
b=new JButton("Divide");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
[Link](this);
setVisible(true);
@Override
try
int a=[Link]([Link]());
int b=[Link]([Link]());
float c=a/b;
[Link](""+c);
catch(ArithmeticException e1)
[Link](null,[Link]());
catch(NumberFormatException e2)
[Link](null,[Link]());
new Test();
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
JLabel l;
JTextField t;
Container c;
Test()
[Link](700,150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout([Link]));
t=new JTextField(20);
[Link](l);
[Link](t);
c=[Link]();
[Link](true);
[Link](this);
@Override
String s=[Link]();
switch(s)
case "red":[Link]([Link]);
break;
case "yellow":[Link]([Link]);
break;
case "blue":[Link]([Link]);
break;
default:[Link](null,"Wrong Input");
break;
new Test();
42.//PRACTICE LAB
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
JButton b;
JTextField t;
Test()
[Link](500,150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout([Link]));
t=new JTextField(20);
b=new JButton("Click");
[Link](t);
[Link](b);
[Link](true);
[Link](this);
@Override
[Link](" ");
new Test();
}
43.//PRACTICE LAB
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
JTextField t;
JLabel l;
Test()
[Link](500,150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout([Link]));
t=new JTextField(20);
l=new JLabel();
[Link](t);
[Link](l);
[Link](true);
[Link](this);
@Override
String ch=[Link]();
String word[]=[Link]("\\s");
new Test();
44.//PRACTICE LAB
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
JLabel l;
Test()
[Link](500,150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout());
l=new JLabel();
[Link](l);
[Link](new Font("Arieal",[Link],25));
//[Link]([Link]);
[Link](true);
[Link](this);
[Link](this);
@Override
[Link]("Mouse Entered");
[Link]("Mouse Exited");
[Link]("Mouse Pressed");
[Link]("Mouse Released");
[Link]("Mouse Clicked");
[Link]("Mouse Dragging");
[Link]("Mouse Moving");
new Test();
}
}
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
//import [Link];
import [Link];
JLabel l;
Test()
[Link](500,150);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout());
l=new JLabel();
[Link](l);
[Link](new Font("Arieal",[Link],25));
//[Link]([Link]);
[Link](true);
[Link](this);
@Override
[Link]("Mouse Entered");
[Link]("Mouse Released");
}
[Link]("Mouse Clicked");
new Test();
46.