Arjun Computer Project
Arjun Computer Project
“JAVA PROGRAMS”
The selection statements allowed to choose the set of instructions for execution depending upon
expressions, truth value. Java provides two types of selection statements and one of them is the if
statement.
1.if
An if statement tests a particular condition ; if the condition evaluates to true, a course of action is
followed i.e, A statement or set of statements is executed. Otherwise the course of action is
ignored.
Format:
if(expression)
Statement;
2.if- else
Another form of if that allows for this kind of either or condition by providing an else clause is known
as if-else statement. The if-else statement tests an expression and depending upon it’s truth value
one of the two sets of action is executed.
Format:
if(expression)
Statement 1;
Else
Statement 2;
3.if-else-if
A common programming construct in java is the if-else-if ladder, which is often also called the if-
else-if staircase because of it’s appearance.
Format:
if(expression1)
Statement 1;
else if(expression2)
Statement 2;
else if(expression3)
Statement 3;
:
:
else
Statement n;
Page 1 of 31
Program 1:
The Electricity board charges from their consumers according to the units consumed per month. The
amount is calculated as per the tariff given below.
Write a program to input consumer’s name, consumer’s number and the units consumed. The
program displays the following information after calculating the amount.
Money Receipt
Consumer’s Number:
Consumer’s Name:
Units Consumed:
Amount to be paid:
import java.util.*;
int n,u;
String name;
double amt=0,total=0;
name=in.nextLine();
n=in.nextInt();
u=in.nextInt();
if(u<=100)
Page 2 of 31
amt=u*5.50;
if((u>100)&&(u<=300))
amt=100*5.50+(u-100)*6.50;
if((u>300)&&(u<=600))
amt=100*5.50+200*6.50+(u-300)*7.50;
if((u>300)&&(u<=600))
amt=100*5.50+200*6.50+300*7.50+(u-600)*8.50;
System.out.println("Consumer's number:"+n);
System.out.println("Consumer's name:"+name);
System.out.println("Units consumed:"+u);
System.out.println("Amount to be paid:Rs."+amt);
Input:
Arjun
1235
150
Output:
Consumer's number:1235
Consumer's name:Arjun
Units consumed:150
Amount to be paid:Rs.875.0
Page 3 of 31
2.SWITCH
Java provides a multiple branch selection statement known as switch. This selection statement
successively tests the value of an expression against a list of integer or character constants, for
equality.
Format:
switch(expression)
break;
break;
break;
break;
Program 2:
import java.util.*;
int c,s,a,b,m,n,p;
Page 4 of 31
double k, area=0;
c=in.nextInt();
switch(c)
case 1:
s=in.nextInt();
area= (Math.sqrt(3)*s*s)/4.0;
System.out.println("Area="+area);
break;
case 2:
a=in.nextInt();
b=in.nextInt();
area= (Math.sqrt(4*a*a-b*b))/4.0;
System.out.println("Area="+area);
break;
case 3:
m=in.nextInt();
n=in.nextInt();
p=in.nextInt();
k=(m+n+p)/2.0;
area=Math.sqrt(k*(k-m)*(k-n)*(k-p));
System.out.println("Area="+area);
break;
default:
Page 5 of 31
System.out.println("Wrong choice!!");
Input:
23
40
Output:
Area=5.678908345800274
Program 3:
A) Tribonacci numbers are a sequence of numbers similar to Fibonacci numbers, except that a
number is formed by adding the three previous numbers. Write a program to display the first twenty
Tribonacci numbers.
For example;
1,1,2,4,7,13, …………………………………..
For example;
import java.util.*;
Page 6 of 31
int ch;
ch=sc.nextInt();
switch(ch)
case 1:
int a=1,b=1,c=2,d=0;
System.out.print(a+","+b+","+c);
d=a+b+c;
System.out.print(","+d);
a=b;
b=c;
c=d;
break;
case 2:
System.out.print("Enter n: ");
int n = sc.nextInt();
sqRoot= Math.sqrt(i+1);
if(temp==0)
Page 7 of 31
System.out.print(i+ " ");
break;
default:
System.out.println("Incorrect choice");
Input:
Output:
1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136,5768,10609,19513,35890,66012
Page 8 of 31
3.FUNCTION OVERLOADING
A function name having several definitions in the same scope that are differentiable by the number
or types of arguments , is set to be an overloading function. Process creating over loaded function is
called function over loading.
Program 4:
write a class with the name volume using function overloading that computes the volume of a cube,
a shape and a cuboid.
import java.util.*;
double vc=0.0D,vs=0.0D,vcd=0.0D;
void volume(int s)
vc=s*s*s;
void volume(float r)
vs=4/3*22/7*r*r*r;
vcd=l*b*h;
Page 9 of 31
Scanner in= new Scanner(System.in);
int s,l,b,h;
float r;
s=in.nextInt();
r=in.nextFloat();
l=in.nextInt();
b=in.nextInt();
h=in.nextInt();
ob.volume(s);
ob.volume(r);
ob.volume(l,b,h);
Input:
25
35
23
143
123
Output:
Page 10 of 31
4.Constructor
A member with the same name as it’s class is called constructor and it is used to initialize the
objects of the class type with a legal initial value
Type of constructors:
1. non-parameterized constructors
Format:
Class A
Int I;
Class B
A 01 = new A();
01.getval();
01.prnval();
2. parameterized constructors:
Parameterized constructors are ones that receive parameters and initialize objects with
received value.
Format:
Class ABC
Int I;
Float j;
Char k;
Page 11 of 31
Public ABC (int a ,float b ,char c)
I = a;
J = b;
K = c;
Program 5:
Member methods:
2) to compute the annual income tax at 5% of the annual salary exceeding rs.175000
Write a main method to create object of the class and call the above member methods.*/
import java.util.*;
class Salary
String name,add,sub;
int ph;
double sal,tax;
void accept()
System.out.println("Enter name");
name=in.nextLine();
System.out.println("Enter address");
add=in.next();
Page 12 of 31
ph=in.nextInt();
sub=in.next();
sal=in.nextDouble();
void calculate()
if(12*sal>175000)
tax=((sal*12)-175000)*5/100;
else tax=0;
void display()
System.out.println("Name :"+name);
System.out.println("Address :"+add);
ob.accept();
ob.calculate();
ob.display();
Page 13 of 31
Input:
Enter name
Arjun
Enter address
82084006
Computer
20000
Output:
Name : Arjun
Page 14 of 31
5.String functions
String replace(char old_char, char Used to replace a character by another character in the
new_char) given string.
boolean endsWith(String s) Checks whether the string ends with the specified string.
String valueOf(a_primitive_datatype Converts any primitive value (char, int, float, etc.) into
n) string.
Page 15 of 31
Program 6:
Data members :
Member methods:
Write a main method to create an object of s class and call the above member methods.
import java.util.*;
int basic;
double da,hra,pf,gs,net;
name=n;
empno=en;
basic=bs;
void compute()
da=basic*30.0/100.0;
hra=basic*15.0/100.0;
Page 16 of 31
pf=basic*12.0/100.0;
gs=basic+da+hra;
net=gs-pf;
void display()
System.out.println("Name :"+name);
int bsal;
nm=in.nextLine();
enm=in.next();
bsal=in.nextInt();
ob.compute();
ob.display();
Input:
Page 17 of 31
arjun
212
30000
Output:
Name :arjun
Program 7:
Write a program in java to accept a word and display the same in pig Latin form. A word is framed In
Pig Latin form by using the following steps:
1. Enter a word.
2. Shift all the letters which are available before the first vowel, towards the end of the word.
int x,y;
String str1,str2;
char b;b=0;
x=str.length();
for(y=0;y<x;y++)
b=(str.charAt(y));
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'||b=='A'||b=='E'||b=='I'||b=='O'||b=='U')
break;
Page 18 of 31
str1=str.substring(x,y);
str2=str.substring(0,y);
System.out.println(str1+str2+"ay");
Program 8:
Write a program in java to enter a String. Print the string in alphabetical order of its letters.
Sample output:CEMOPRTU
int i,j,p;
char ch;
p=str.length();
for(i=65;i<90;i++)
for(j=0;j<p;j++)
ch=str.charAt(j);
if(ch==(char)i||ch==(char)(i+32))
System.out.print(ch);
Output:
CEMOPRTU
Page 19 of 31
6.Array
An array is a collection of variables of the same type that are referenced by a common name.
Format:
Format:
Page 20 of 31
Type array-name [] [] = new type [row][columns];
Program 9:
Display the greatest and the smallest numbers of the array elements.
import java.util.*;
int i,min,max;
for(i=0;i<10;i++)
m[i]=in.nextInt();
max=m[0];min=m[0];
for(i=0;i<10;i++)
if(m[i]>max)
max=m[i];
if(m[i]<min)
min=m[i];
Input:
Page 21 of 31
Enter the no. in the cell :6
Output:
Searching:
Sometimes you need to search for an element in an array. To accomplish this task, you can use
different searching techniques. It consists of two very common search techniques viz., linear search
and binary search.
1) Linear search : Linear search refers to the searching technique in which each element of an
array is compared with the search item, one by one, until the search- item is found or all
elements have been compared.
2) Binary search : Binary search is a search technique that works for sorted arrays. Here search
item is compared with the middle element of the array. If the search item matches with the
element, search finishes. If the search item is less than the middle perform, perform binary
search in the first half of the array , otherwise perform binary search in the latter half of the
array.
Program 10:
Write a program to accept 10 different numbers in a single dimensional array (SDA). Now, enter a
number and by using binary search technique, check whether or not the number is present in the list
of array elements. If the number is present then display the message “Search successful” otherwise,
display “Search unsuccessful”.
import java.util.*;
Page 22 of 31
int i,k=0,p=0,ns,lb=0,ub=9;
for(i=0;i<10;i++)
m[i]=in.nextInt();
ns=in.nextInt();
while (lb<=ub)
p=(lb+ub)/2;
if(m[p]<ns)
lb=p+1;
if(m[p]>ns)
ub=p-1;
if(m[p]==ns)
k=1;
break;
if(k==1)
else
Input:
Page 23 of 31
Enter the number in ascending order :2
Output:
Sorting:
Sorting of an array means arranging the array elements in a specified order i.e, either ascending or
descending order. There are several sorting techniques available e.g., shell sort, shuttle sort, bubble
sort, selection sort, quick sort, heap sort, etc.
Program 11:
Write a program to accept 10 different numbers in a single dimensional array (SDA). Arrange the
numbers in ascending order by using ‘Selection Sort’ technique and display them.
import java.util.*;
int i,j,t,min;
for(i=0;i<10;i++)
m[i]=in.nextInt();
for(i=0;i<9;i++)
Page 24 of 31
{
min=i;
for(j=i+1;j<10;j++)
if(m[j]<m[min])
min=j;
t=m[i];
m[i]=m[min];
m[min]=t;
for(i=0;i<10;i++)
System.out.println(m[i]);
Input:
Output:
Page 25 of 31
2
Program 12:
Write a program in java to store the numbers in a 4*4 matrix in a double dimensional array. Find the
highest and the lowest numbers of the matrix by using an input statement.
import java.util.*;
int i,j,min,max;
min=0;max=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
m[i][j]=in.nextInt();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
Page 26 of 31
System.out.print(m[i][j]+" ");
System.out.println();
min=m[0][0];
max=m[0][0];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(min>m[i][j])
min=m[i][j];
if(max<m[i][j])
max=m[i][j];
Input:
23
18
14
10
11
Page 27 of 31
99
445
65
Output:
23 18 14 10
8 2 1 11
7 99 445 4
4 3 2 65
Page 28 of 31
7.Special number program
Program 13:
Write a program to input a number and print whether the number is a special number or not. A
number is said to be special if the sum of the factorial of the digits of the number is the same as the
original number.
import java.util.*;
int m,n,i,d,f=1,s=0;
n=in.nextInt();
m=n;
while(m!=0)
d=m%10;
for(i=1;i<=d;i++)
f=f*i;
s=s+f;
f=1;
m=m/10;
if(s==n)
else
Page 29 of 31
System.out.println(n+" is not a special number");
Input:
Enter a number :
23
Output:
Page 30 of 31
Bibliography
http://amanjava.blogspot.in/
http://www.guideforschool.com
http://www.icsej.com/
Page 31 of 31