Java Lab Record
Java Lab Record
int bs=12000;
double da,hra,pf,gp,np;
da=1.10*bs; hra=0.15*bs;
pf=0.12*bs; gp=bs+da+hra;
np=gp-pf;
System.out.println("Gross salary=Rs." +gp);
System.out.println("Net salary=Rs." +np);
}
}
Q2. Define a class CalcInt. Declare variables required to
calculate simple interest. Also define methods:
a) to set principal, time and rate of interest.
b) to calculate simple interest.
c) to display the loan details.
Try to modify the above program by making the method
private that is used to calculate simple interest.
Source Code:
import java.util.Scanner; class
CalcInt
{
int j; j=age%10;
m=age+j;
}
n=n/10; f=n;
}
h=t-f;
}
void circle(int r)
{
System.out.println("Area of circle=" +(3.14*r*r));
}
void triangle(int b,int h)
{
System.out.println("Area of triangle=" +(0.5*b*h));
}
void rectangle(int l,int w)
{
System.out.println("Area of rectangle=" +(l*w));
}
public static void main(String k[])
{
int r,b,h,l,w;
Scanner s=new Scanner(System.in); System.out.println("Enter
the radius of circle="); r=s.nextInt();
System.out.println("Enter the Base and Height of triangle=");
b=s.nextInt(); h=s.nextInt();
System.out.println("Enter the length and width of
rectangle=");
l=s.nextInt(); w=s.nextInt();
Areaofshapes obj=new Areaofshapes();
obj.circle(r);
obj.triangle(b,h);
obj.rectangle(l,w);
}
}
ASSIGNMNET-3(OPERATOR)
Source Code:
double f,c;
temperature="); f=s.nextDouble();
c=((5/9.0)*(f-32.0));
}
Q2.Calculate the volume and surface area of a sphere using the
following formula: V= 4/3 πr3 A = 4 πr2 π=3.14159 Test the program using
CMD for the given radius: 1, 6, 12.2,0.2.
Source Code:
pi=3.14159;
v=(4/3)*pi*r*r*r;
a=4*pi*r*r;
}
Q3. WAP in JAVA to find the smaller and greater number among
two numbers read from CMD using ternary operator.
Source Code:
c=a>b?a:b;
}
Q4.Write a program to show the use of ++, -- and different
assignment operators.
Source Code:
int a,var,b,c,k,l;
var=a;
k=++b;
l=--c;
}
Q5.WAP to observe the difference between – and ~
operators.
Source Code:
int a,b,c;
c=a-b;
}
ASSIGNMENT_4(BITWISE OPERATOR)
>>>.
Source Code:
input()
number="); v=s.nextInt();
void leftshift()
void rightshift()
{
a obj=new a();
obj.input();
obj.leftshift();
obj.rightshift();
}
Q2. Write a program to explain the use of (&, |, ^, ~)
bitwise operators in Java. Define two numbers num1 and num2. Then store the
result in num3 after using the operators given above. Print the value of num1,
num2 and num3 to check the result.
Source Code:
input()
num3=num1&num2;
num3=(~num1);
a obj=new a();
obj.input();
obj.output();
}
Q3. Define two numbers and swap them without using
third variable.
a. Use the bitwise ^ operator and show the numbers after swapping.
Source Code:
input()
void output()
num1=num1^num2; num2=num1^num2;
num1=num1^num2; System.out.println("After
Swapping:");
a obj=new a();
obj.input();
obj.output();
}
b. Restore the numbers by using (+, -) operator and
show the numbers.
Source Code:
input()
void output()
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
}
public static void main(String k[])
a obj=new a();
obj.input();
obj.output();
}
ASSIGNMENT-5(IfElse-SwitchCase)
Source Code:
import java.util.Scanner; class a
{
public static void main(String k[])
{
Scanner s=new Scanner(System.in); double b,hra,da,g;
System.out.println("Enter basic salary of an employee=");
b=s.nextDouble(); if(b<12000)
{
hra=0.2*b;
da=1.15*b;
g=b+hra+da;
System.out.println("Gross Salary of an employee=Rs."
+g);
}
else
{
hra=0.15*b;
da=0.9*b;
g=b+hra+da;
System.out.println("Gross Salary of an employee=Rs."
+g);
}
}
}
Q2.WAP in JAVA to input an amount in Rs through
command line argument and find the number of 2000, 500, 200, 100, 50, 20, 10, 5, 2 and 1
Rs denominations will be needed to have that amount.
Source Code:
import java.util.Scanner; class a
{
public static void main(String k[])
{
Scanner s=new Scanner(System.in); int n,a,b,c,d,e,f,g,h,i,j;
a=b=c=d=e=f=g=h=i=j=0;
System.out.print("Enter amount in Rs."); n=s.nextInt();
if(n>=2000)
{
a=n/2000; System.out.println("Rs.2000=" +a); n-=a*2000;
} if(n>=500)
{
b=n/500; System.out.println("Rs.500=" +b);
n-=b*500;
} if(n>=200)
{
c=n/200; System.out.println("Rs.200=" +c); n-=c*200;
} if(n>=100)
{
d=n/100; System.out.println("Rs.100=" +d); n-=d*100;
}
if(n>=50)
{
e=n/50; System.out.println("Rs.50=" +e); n-=e*50;
}
if(n>=20)
{
f=n/20; System.out.println("Rs.20=" +f);
n-=f*20;
}
if(n>=10)
{
g=n/10; System.out.println("Rs.10=" +g); n-=g*10;
}
if(n>=5)
{
h=n/5; System.out.println("Rs.5=" +h); n-=h*5;
}
if(n>=2)
{
i=n/2; System.out.println("Rs.2=" +i); n-=i*2;
}
if(n>=1)
{
j=n/1; System.out.println("Rs.1=" +j);
n-=j*1;
}
}
}
Q3. Test the nature of the root of a quadraticequation
ax2+bx+c=0. The nature can be tested from the discriminant d=b2-4ac. The result can
be displayed from the Table-2 after finding d from the values given in Table-1.
Table-1
a B c Result(d)
2 6 1
2 -4 3
3 3 0
1 3 1
0 12 -3
Table-2
Source Code:
import java.lang.*; class a
{
public static void main(String k[])
{
double a,b,c,d; a=2;b=6;c=1;d=0;
d=Math.pow(b,2);
d=(d-(4*a*c)); if(d>0)
{
else
{
System.out.println("Rational and squared or Rational but not squared" +d);
} a=3;b=3;c=0;d=0;
d=Math.pow(b,2);
d=(d-(4*a*c)); if(d>0)
{
System.out.println("Roots are squared!" +d);
}
else if(d==0)
{
System.out.println("Roots are equal so only one root!"
+d);
}
else
{
System.out.println("Rational and squared or Rational but not squared" +d);
} a=1;b=3;c=1;d=0;
d=Math.pow(b,2);
d=(d-(4*a*c)); if(d>0)
{
System.out.println("Roots are squared!" +d);
}
else if(d==0)
{
System.out.println("Roots are equal so only one root!"
+d);
}
else
{
System.out.println("Rational and squared or Rational but not squared" +d);
}
a=0;b=12;c=-3;d=0;
d=Math.pow(b,2);
d=(d-(4*a*c)); if(d>0)
{
System.out.println("Roots are squared!" +d);
}
else if(d==0)
{
{
System.out.println("Scalene traingle!");
}
}
}
Q5. Input Roll No, Name, and marks in five subjects.
Calculate total and percentage of marks using if-else-if. Calculate grade as follows :
>= 90% Grade O
>=80% Grade E
>=70% Grade A
>=60% Grade B
>=50% Grade C
>=40% Grade D
<40% Fail
Generate a Mark Sheet.
Source Code:
import java.util.Scanner; class a
{
public static void main(String l[])
{
Scanner s=new Scanner(System.in); int r,m,ss,p,h,e,t=0;
double per;
String k;
System.out.println("Enter roll no="); r=s.nextInt();
System.out.println("Enter your name:"); k=s.next();
}
else if(per>=70)
{
System.out.println("Grade A");
}
else if(per>=60)
{
System.out.println("Grade B");
}
else if(per>=50)
{
System.out.println("Grade C");
}
else if(per>=40)
{
System.out.println("Grade D");
}
else
{
System.out.println("FAIL");
}
}
}
Q6. Input two numbers and an operator(+, -, *, /) then
calculate the result according to the operator selected.
Source Code:
import java.util.Scanner; class a
{
public static void main(String k[])
{
Scanner s=new Scanner(System.in); double a,b,d=0;
char c;
System.out.println("Enter 1st number="); a=s.nextDouble();
System.out.println("Enter 2nd number="); b=s.nextDouble();
System.out.println("Enter an operator from(+,-,*,/):"); c=s.next().charAt(0);
switch(c)
{
case '+':
d=a+b;
break; case '-
': d=a-b;
break;
case '*':
d=a*b; break;
case '/':
d=a/b;
default:
System.out.println("Invalid Operator!");
}
System.out.println(a+" " +c +" " +b+ "=" +d);
}
}
Q7.Input a number and display it in words. Ex-
Source Code:
import java.util.Scanner;
class a
int q=0,l=0,n,i;
System.out.println("Enter 10 number=");
for(i=1;i<=10;i++)
n=s.nextInt();
if(n>l)
l=n;
}
if(n<l)
q=n;
}
Q2. Print the factorials of a given
number.
Source Code:
import
java.util.Scanner; class
Scanner s=new
System.out.println("Enter a
number="); n=s.nextInt();
for(i=1;i<=n;i++)
f=f*i;
}
}
Q3. Generate a Fibonacci series up to
nth term.
Source Code:
import java.util.Scanner;
class a
int c=0,n,a=0,b=1;
n=s.nextInt();
while(c<=n)
a=b;
b=c;
System.out.println(c);
c=a+b;
}
}
Q4. Input the starting range (m) and
the end range (n). Then find all prime numbers present in
this input range.
Source Code:
import java.util.Scanner;
class a
int f,l,i,j;
f=s.nextInt();
l=s.nextInt();
for(i=f;i<=l;i++)
for(j=2;j<i;j++)
{
if(i%j==0)
break;
if(i==j)
System.out.println(i);
}
Q5. Find the sum of all digits of a given
number. Then count and print those digits from the result
number which are also present in the original number.
Source Code:
import java.util.Scanner;
class a
int a,b=0,n,g=0,r,t=0,rem=0,c;
System.out.println("Enter a number=");
n=a=s.nextInt();
do{
b+=a%10;
a=a/10;
}
while(a>0);
while(b!=0)
r=b%10;
t=n;
g=0;
while(t!=0)
rem=t%10;
c=rem;
if(r==rem)
g++;
t/=10;
if(g!=0)
b=b/10;
}
}
Q6. Input a number and check whether
it is palindrome or not.
Source Code:
import java.util.Scanner;
class a
int n,rev=0,r,q;
System.out.println("Enter a number=");
q=n=s.nextInt();
while(n>0)
r=n%10;
rev=(rev*10 )+r;
n=n/10;
}
if(rev==q)
System.out.println("Palindrome!");
else
System.out.println("Not Palindrome!");
}
Q7. Check whether an input number is
an Armstrong number or not.
Source Code:
import java.util.Scanner;
import java.lang.Math;
class a
int n,d=0,d1,q=0,i;
System.out.println("Enter a number=");
n=s.nextInt();
for(i=n;i>0;i=i/10)
d++;
for(i=n;i>0;i=i/10)
{
d1=i%10;
q+=Math.pow(d1,d);
if(q==n)
System.out.println("Armstrong!");
else
System.out.println("Not Armstrong!");
}
ASSIGNMENT-7(SERIES)
a.) 1 0 0 0 1
01010
00100
01010
10001
Source Code:
class a
{
public static void main(String k[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if((j==i)||(i+j==5+1))
{
System.out.print("1"+" ");
}
else
{
System.out.print("0"+" ");
}
}
System.out.println();
}
}
}
b.) 5 4 3 2 1
5 4 3 2
5 4 3
5 4
Source Code:
class a
{
public static void main(String k[])
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=5;j>i;j--)
{
System.out.print(j+" ");
}
System.out.println("");
}
}
}
ASSIGNMENT-8(CONSTRUCTOR)
Define methods such as show(), add(), subtract(), multiply(), div(), compare(), mixed()
and reduce().
Create different objects using different constructors given above. Perform the above operations using
appropriate methods. Show the result in reduced form of the result fraction after each operation.
After reducing if the fraction is an improper fraction (Ex- 5/2) then represent it in the form of mixed
fraction(2 Full and ½).
Source Code:
import java.util.Scanner; class
Fraction
{
int num;
final int num_backup; int
denum;
final int denum_backup;
Fraction()
{
this(1);
}
Fraction(int num)
{
this(num, 1);
}
Fraction(Fraction object)
{
this(object.num_backup, object.denum_backup);
}
Fraction(int num, int denum)
{
this.num= num_backup = num; this.denum =
denum_backup = denum;
}
int greatestCommonDevisor(int first, int second)
{
if (first > second)
{
first = first + second; second =
first - second; first = first -
second;
if(i == first)
return commonDivisor;
}
return -1;
}
int reduce()
{
int quotient=num/denum;
num=num % denum;
int hcf=greatestCommonDevisor(num, denum); num=num/hcf;
denum=denum/ hcf; return
quotient;
}
public void show()
{
int quotient = reduce();
if(quotient == 0)
System.out.println(num + "/" + denum); else if
(num%denum== 0) System.out.println(quotient);
else
System.out.println(quotient + " full " + num + "/" + denum);
}
public static void main(String[] args)
{
System.out.println("Enter the numerator and denominator"); Scanner s=new
Scanner(System.in);
int firstNumber=s.nextInt(); int
secondNumber=s.nextInt();
Fraction test = new Fraction(firstNumber, secondNumber); Fraction
object=new Fraction();
Fraction obj=new Fraction(object);
test.show();
}
}
Q2. Create a class Complex having member variables
real and imag. Also create constructors and methods as follows:
a. Complex()
b. Complex(int,int)
c. Complex(Complex)
d. void showComplex()
e. Complex addComplex(Complex)
f. Complex substractComplex(Complex)
g. Complex multiplyComplex(Complex)
Write a java program to create objects of above class and perform operations as the methods
specified above.
Source Code:
import java.util.Scanner; import
java.lang.Math; class Complex
{
double real, img; Complex(double r,
double i)
{
this.real = r;
this.img = i;
}
public static Complex sum(Complex c1,Complex c2)
{
Complex temp = new Complex(0, 0); temp.real =
c1.real + c2.real; temp.img = c1.img + c2.img;
return temp;
}
public static Complex sub(Complex c1,Complex c2)
{
Complex temp1 = new Complex(0, 0); temp1.real
= c1.real - c2.real; temp1.img = c1.img - c2.img;
return temp1;
}
public static Complex mul(Complex c1,Complex c2)
{
Double i,m,n,j;
Complex temp2 = new Complex(0, 0); m=((c1.real *
c2.real) - (c1.img * c2.img)); n=((c1.real * c2.img) +
(c1.img * c2.real)); temp2.real = m;
temp2.img = n;
return temp2;
}
public static void main(String k[])
{
Scanner s=new Scanner(System.in); Double
r1,r2,i1,i2;
System.out.println("Enter real and imag part of 1st Complex Number="); r1=s.nextDouble();
i1=s.nextDouble();
System.out.println("Enter real and imag part of 2nd Complex Number="); r2=s.nextDouble();
i2=s.nextDouble();
Source Code:
import java.util.Scanner; class
ArrayOperation
{
int array[]=new int[100]; int i,n;
ArrayOperation()
{
this.input();
this.output();
this.sumavg();
this.swap();
this.ocrnce();
this.concat();
}
void input()
{
array[i]=s.nextInt();
}
}
void output()
{
System.out.println(array[i]);
}
}
void sumavg()
{
int sum=0,avg;
for(i=0;i<n;i++)
sum+=array[i];
}
avg=sum/n;
System.out.println("Sum of array elements=" +sum);
System.out.println("Average of array elements=" +avg);
}
void swap()
{
int max,min,mxpos=0,mnpos=0,l;
max=array[0];
min=array[0];
for(i=0;i<n;i++)
if(array[i]>max)
{
max=array[i];
mxpos=i;
if(array[i]<min)
{
min=array[i];
mnpos=i;
}
}
l=array[mxpos];
array[mxpos]=array[mnpos];
array[mnpos]=l;
System.out.println("After swapping max and min value="); for(i=0;i<n;i++)
System.out.println(array[i]);
}
}
void ocrnce()
{
int c=0,j;
int b[]=new int[n];
for(i=0;i<n;i++)
c=1;
if(array[i]!=-1)
{
for(j=i+1;j<n;j++)
{
if(array[i]==array[j])
{
c=c+1;
array[j]=-1;
}
}
b[i]=c;
}
}
if(array[i]!=-1)
{
System.out.println(array[i] + ":" +b[i]);
}
}
}
void concat()
{
int m,c=0,g=0,t,b;
int q[]=new int[100]; System.out.println("Concatenation after Occurence
="); for(int i=0,j=0;i<=array.length-1;i+=2,j++)
m=0;
m=array[i+1];
c=0;
while(m>0)
{
m=m/10;
c++;
if(i==(array.length-1))
{
q[j]=array[i+1]*(int)(Math.pow(10,c));
System.out.println(q[j]);
}
else
{
q[j]=array[i]*(int)(Math.pow(10,c)) + array[i+1];
if(q[j]>0)
{
System.out.println(q[j]); g++;
}
}
for(i=0;i<g;i++)
{
for(b=i+1;b<g;b++)
{
if(q[i]>q[b])
{
t=q[i];
q[i]=q[b];
q[b]=t;
}
}
}
d) Bubble sort
e) Selection sort
f) Insertion sort
Source Code:
import java.util.Scanner; class
SortArray
{
Scanner s=new Scanner(System.in); public
int n,i,j;
int[] array = new int[10]; void
allocate()
{
void input()
{
void show()
{
void bubblesort()
{
int temp = 0;
for(i=0; i < n; i++)
{
for(j=1; j < (n-i); j++)
{
if(array[j-1] > array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}
System.out.println(array[i]);
}
System.out.println("Array After Bubble Sort(descending order):-"); for(i=n-1;i>=0 ;
i--)
{
System.out.println(array[i]);
}
}
void selectionsort()
{
for(i=0;i<n-1;i++)
{
int min_indx=i;
for(j=i+1;j<n;j++)
{
if(array[j]<array[min_indx])
{
min_indx=j;
}
}
int temp=array[min_indx];
array[min_indx]=array[i];
array[i]=temp;
}
System.out.println(array[i]);
}
void insertionsort()
{
for(i=1;i<n;++i)
{
array[j+1]=array[j]; j=j-
1;
}
array[j+1]=key;
}
System.out.println(array[i]);
}
a) One 2D array.
b) Constructor to allocate memory of size (2 X n) for the array.
c) Input data into the Array.
a) One 1D array.
b) Constructor to allocate memory of size (n) as the column size of TwoDArray
class.
c) Show the array.
Source Code:
import java.util.Scanner; class
TwoDArray
{
twodarr=new int[2][n];
}
for(int i=0;i<2;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(twodarr[i][j]+"\t");
}
System.out.print("\n");
}
}
for(int i=0;i<twodarr[j].length;i++)
od.array[i]=twodarr[j][i];
}
twodarr[i][j]=0; for(int
k=0;k<n;k++)
twodarr[i][j]+=td1.twodarr[i][k]*td2.twodarr[k][j];
}
}
}
class OneDArray
{
array=new int[n];
}
for(int i=0;i<n;i++)
System.out.print(array[i]+"\t");
System.out.print("\n");
}
twodarr=new int[m][n];
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(twodarr[i][j]+"\t");
}
System.out.print("\n");
}
}
for(int i=0;i<twodarr[j].length;i++)
od.array[i]=twodarr[j][i];
}
}
class OneDArray
{
class m_d_array
{
od[i]=new OneDArray(n);
}
td1.getinput(m,n);
System.out.println("Array element row wise");
td1.display(m,n);
for(int i=0;i<m;i++)
td1.split(od[i],i);
System.out.println("OneDArray splitting "); for(int
i=0;i<m;i++)
od[i].print(n);
}
}
ASSIGNMENT-12(JAGGED ARRAY)
marks[0]=new int[3];
marks[1]=new int[5];
marks[2]=new int[2];
marks[3]=new int[6];
marks[4]=new int[4];
}
void input()
{
for(int i=0;i<marks.length;i++)
{
void show()
{
for(int i=0;i<marks.length;i++)
{
System.out.println("");
}
}
void total()
{
int sum=0;
for(int i=0;i<marks.length;i++)
{
for(int j=0;j<marks[i].length;j++)
{
sum+=marks[i][j];
}
void more()
{
int count=0;
for(int i=0;i<marks.length;i++)
{
for(int j=0;j<marks[i].length;j++)
{
if(marks[i][j]>80)
{
count++;
}
}
System.out.println("CHILD " +(i+1) +"have " +count +" subjects in which his/her
marks is more than 80!");
count=0;
}
}
void less()
{
for(int i=0;i<marks.length;i++)
{
for(int j=0;j<marks[i].length;j++)
{
if(marks[i][j]<30)
{
System.out.println("CHILD" +(i+1) +"needs better preparation in Subject" +(j+1));
}
}
}
Source Code:
import java.io.*;
class Cricket
String name;
int inning,tofnotout,totalruns;
float batavg;
Cricket()
name=null;
inning=0;
tofnotout=0;
totalruns=0;
batavg=0;
name=br.readLine();
inning=Integer.parseInt(br.readLine());
tofnotout=Integer.parseInt(br.readLine());
totalruns=Integer.parseInt(br.readLine());
System.out.println("Name="+name);
System.out.println("no of innings="+inning);
System.out.println("total runs="+totalruns);
System.out.println("bat avg="+batavg);
System.out.println(" ");
try
for(int i=0;i<n;i++)
c[i].batavg=c[i].totalruns/c[i].inning;
catch(ArithmeticException e)
System.out.println("Invalid arg");
}
static void sort(int n, Cricket c[])
String temp1;
int temp2,temp3,temp4;
float temp5;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(c[i].batavg<c[j].batavg)
temp1=c[i].name;
c[i].name=c[j].name;
c[j].name=temp1;
temp2=c[i].inning;
c[i].inning=c[j].inning;
c[j].inning=temp2;
temp3=c[i].tofnotout;
c[i].tofnotout=c[j].tofnotout;
c[j].tofnotout=temp3;
temp4=c[i].totalruns;
c[i].totalruns=c[j].totalruns;
c[j].totalruns=temp4;
temp5=c[i].batavg;
c[i].batavg=c[j].batavg;
c[j].batavg=temp5;
}
}
}
class a
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
c[i]=new Cricket();
c[i].get();
Cricket.avg(n,c);
Cricket.sort(n, c);
for(int i=0;i<n;i++)
c[i].put();
}
Q2. Create a class called Account having data members
acct_no, acct_type, customer_name and acct_balance. Write a java program to
input data for five customers and print the details of customer having maximum
balance in the account.
Source Code:
import java.io.*;
class Account
String acct_type,custmr_name;
acct_no=0;
acct_type=null;
custmr_name=null;
acct_balnc=0;
acct_no=Integer.parseInt(br.readLine());
acct_type=br.readLine(); System.out.print("Enter
acct_balnc=Integer.parseInt(br.readLine());
System.out.println("");
}
System.out.println(" ");
for(int i=0;i<5;i++)
a[i]=new Account();
a[i].get();
for(int i=0;i<5;i++)
array[i]=a[i].acct_balnc;
int largest=array[0];
for(int i=0;i<5;i++)
{
if(largest<array[i])
largest=array[i];
}
System.out.println("Maximum account balance among the customer="
+largest);
if(a[i].acct_balnc==largest)
a[i].put();
}
ASSIGNMENT-14(STRING)
Source Code:
import java.util.*;
import java.lang.*; class
UserString
{
void countchar(String s,String s1)
{
int c=1,d=1;
for(int i=0;i<s.length();i++)
{
c++;
}
}
System.out.println("Number of words in a sentence=" +c); for(int
i=0,j=0;i<s.length() && j<s1.length();i++,j++)
c++;
}
d++;
}
}
else
{
int i=0,t=0;
while(i!=l1)
if(s.charAt(i)!=s1.charAt(i))
{
t=1;
break;
}
i++;
}
if(t==0)
System.out.println("Strings are equal");
else
System.out.println("Strings are not equal");
}
}
ch[i]=(char)((int)ch[i] - 32);
}
}
System.out.print(ch[a]);
}
System.out.println(""); for(int
i=0;i<ch1.length;i++)
ch1[i]=(char)((int)ch1[i] - 32);
}
}
System.out.print(ch1[b]);
}
System.out.println("");
}
ch[a]=(char)((int)ch[a] + 32);
}
ch1[b]=(char)((int)ch1[b] + 32);
}
}
System.out.print(ch[a]);
}
System.out.println("");
System.out.println("String1 in lowercase is: "); for(int
b=0;b<ch1.length;b++)
System.out.print(ch1[b]);
}
System.out.println("");
}
char ch=s.charAt(i);
if(ch==' ')
for(j=0,k=str1.length()-1;j<(str1.length()/2);j++,k--)
{
if(str1.charAt(j)==str1.charAt(k)) c++;
if(c==(str1.length()/2))
System.out.print(str1 +" " );
str1="";
c=0;
}
else
str1=str1+ ch;
}
System.out.println("");
System.out.print("Palindrome word in another Sentence are : " );
for(i=0;i<s.length();i++)
char ch=s.charAt(i);
if(ch==' ')
for(j=0,k=str1.length()-1;j<(str1.length()/2);j++,k--)
{
if(str1.charAt(j)==str1.charAt(k)) c++;
if(c==(str1.length()/2))
System.out.print(str1 +" " );
str1="";
c=0;
}
else
str1=str1+ ch;
}
System.out.println("");
}
void position(String s,String s1)
{
int k,m;
System.out.print("Enter a character to check it's position in a sentence:"); str=sc.next();
k=s.indexOf(str);
while(k!=-1)
System.out.println((k+1));
k=s.indexOf(str, k+1);
System.out.println((m+1));
m=s1.indexOf(str1, m+1);
}
}
obj.replc(s,s1);
obj.swap(s,s1);
}
}
ASSIGNMENT-15(INHERITANCE)
Source Code:
import java.util.Scanner;
class Vehicle
brand=s.next();
Base_price=s.nextInt();
return Base_price;
+Country_of_Origin);
model=s.next();
speed=s.nextFloat();
System.out.println("");
void show()
{
a=getBase_price();
if(speed>80)
Market_price=(a +(0.15f*a));
else
Market_price=( a-(0.05f*a));
obj.input();
obj.read();
obj.display();
obj.show();
}
Q2. Create two classes such as Teacher (basic, da, hra,
epf, sub_taught) and Student (fees_per_sem, course, duration) which are inherited
from class Person(name, Id, year_of_join). Design appropriate methods to input the
data as given above whenever required.
Prepare an annual report for a Teacher showing the details such as Name, Id,
Subject Taught, Joining year, Basic salary per month, Total net salary received
per year, Total Epf deposited per year. If [Basic=15500/-, da=110%, hra=15%,
and epf=12%]
Inform the student by showing details such as Name, Id, Course Offered, Joining
year and total fees to be paid if the course duration is 4 years and fees per
semester is 18000/-.
Source Code:
import java.util.Scanner;
class Person
void input()
name=s.next();
id=s.next();
yr_of_join=s.nextInt();
}
class Teacher extends Person
float basic,da,hra,epf,total,tpef;
String sub_taught;
void input()
super.input();
sub_taught=s.next();
basic=15500;
da=1.10f*basic;
hra=0.15f*basic;
epf=0.12f*basic;
total=12*((basic+da+hra)-epf);
tpef=0.12f*basic*12;
void showteacher()
+basic);
System.out.println("Total net salary of "+name +" received per year is
" +total);
}
}
class Student extends Person
course;
void input()
super.input();
course=sc.next(); total_fees=18000*(2*duration);
void showstudent()
+total_fees);
t.input(); t.showteacher();
stud.input();
stud.showstudent();
}
}
ASSIGNMENT-16(INHERITANCE)
Source Code:
import java.util.Scanner;
class Number
public Number(int b)
m=b;
for(int i=0;i<m;i++)
arr[i]=s.nextInt();
display(m);
void display(int t)
for(int i=0;i<t;i++)
System.out.println(arr[i]);
public OddNum(int p)
super(p);
m=p;
for(int i=0;i<m;i++)
if(arr[i]%2!=0)
count++;
arr1=new int[count];
copyodd();
displayodd();
void copyodd()
int j=0;
for(int i=0;i<arr.length;i++)
if(arr[i]%2!=0)
arr1[j++]=arr[i];
void displayodd()
for(int i=0;i<arr1.length;i++)
System.out.println(arr1[i]);
System.out.println();
PrimeNum(int u)
super(u);
m=arr1.length;
for(int i=0;i<m;i++)
int j=2;
int q=1;
while(j<arr1[i])
if(arr1[i]%j==0)
q=0;
break;
j++;
if(q==1)
count++;
arr2=new int[count];
copyprime();
displayprime();
void copyprime()
int k=0;
for(int i=0;i<arr1.length;i++)
int j=2;
int q=1;
while(j<arr1[i])
if(arr1[i]%j==0)
{
q=0;
break;
}
j++;
if(q==1)
arr2[k]=arr1[i]
; k++;
void displayprime()
for(int i=0;i<arr2.length;i++)
System.out.println(arr2[i]);
System.out.println();
int a;
a=sc.nextInt();
}
Q2. Define a class Employee having private members –
id, name, department, salary. Define default and parameterized constructors.
Create a subclass called “Manager” with private member bonus. Define methods
accept() and display() in both the classes.
Create n objects of the Manager class and display the details of the manager
having the maximum total salary (salary+bonus).
Source Code:
import java.util.Scanner;
class Employee
Employee()
id=0;
depart_salary=0;
name="";
dept="";
id=i;
depart_salary=s;
name=n; dept=d;
void accept()
{
Scanner s=new Scanner(System.in);
name=s.next();
id=s.nextInt();
depart_salary=s.nextInt();
dept=s.next();
void display()
int getid()
return(id);
int getdepart_salary()
return(depart_salary);
String getname()
{
return(name);
String getdept()
{
return(dept);
Manager()
super();
super(i,s,n,d);
bonus=b;
void accept()
super.accept();
bonus=sc.nextInt();
void display()
super.display();
}
static void max(Manager p[])
int max=0,total=0,id=0,i;
for(i=0;i<p.length;i++)
{
total=(p[i].getdepart_salary()) + p[i].bonus;
if(max<total)
max=total;
id=i;
p[id].display();
int n,i;
n=s.nextInt();
for(i=0;i<n;i++)
m[i]=new Manager();
m[i].accept();
Manager.max(m);
}
ASSIGNMENT-17(ABSTRACT)
void input()
{
void volume()
{
void surfaceArea()
{
void inputcylndr()
{
void volume()
{
void surfaceArea()
{
}
class Cuboid extends Shape
{
void input()
{
void input()
{
void show()
{
System.out.println("Fruit is Apple!");
}
void display()
{
System.out.println("The colour of fruit Apple is " +colour +" and taste is " +taste);
}
}
class Banana extends fruit
{
void input()
{
void show()
{
System.out.println("Fruit is Banana!");
}
void display()
{
System.out.println("The colour of fruit Banana is " +colour +" and taste is " +taste);
}
}
class Orange extends fruit
{
void input()
{
System.out.println("Fruit is Orange!");
}
void display()
{
System.out.println("The colour of fruit Orange is " +colour +" and taste is " +taste);
}
}
class Strawberry extends fruit
{
void input()
{
void show()
{
System.out.println("Fruit is Strawberry!");
}
void display()
{
System.out.println("The colour of fruit Strawberry is " +colour +" and taste is "
+taste);
}
public static void main(String k[])
{
Apple obj=new Apple();
obj.show();
obj.input();
obj.display();
Source Code:
import
java.util.Scanner;
interface IntOperation
void positive(int
n);
private int
num;
MyNumber()
num=0;
MyNumber(int num)
{
this.num=num;
}
public void positive(int num)
if(num>0)
else
if(num%2==0)
else
int fact=1,i;
for(i=1;i<num+1;i++)
fact=fact*i;
System.out.println("Factorial is "+fact);
{
int sum=0;
while(num!=0
sum=sum+(num%10)
; num=num/10;
int i,j=0;
for(i=2;i<num;i++
if(num%i==0
) j=1;
if(j==0 || num==1)
System.out.println(num +" is
Prime");
else
System.out.println(num +" is not Prime");
Scanner s=new
Scanner(System.in); IntOperation
m;
MyNumber n1=new
MyNumber(); m=n1;
System.out.println("Enter a
n1.positive(n)
; n1.even(n);
n1.prime(n);
n1.fact(n);
n1.sum(n);
}
}
Q2. Define an interface “StackOperations” which
declares methods for a static stack. Define a class “MyStack” which contains an
array and top as data members and implements the above interface.
Initialize the stack using a constructor. Write a menu driven program to
perform all operations(Push, POP, Peak) on a MyStack object.
Source Code:
import java.util.Scanner;
interface StackOperations
int max=10;
void pop();
int isempty();
int isfull();
Mystack()
top=-1;
arr[++top]=data;
top--;
if(top==-1)
return 1;
else
return 0;
if(top==9)
return 1;
else
return 0;
int ch,data;
do
System.out.println("\n1:Push");
System.out.println("2:Pop");
System.out.println("3:Peak.");
ch=sc.nextInt();
switch(ch)
{
case 1:if(s.isfull()==1)
System.out.println("Stack is full");
}
else
s.push(data);
break;
case 2:if(s.isempty()==1)
System.out.println("Stack is empty");
}
else
s.pop();
break;
case 3:System.exit(0);
break;
default:System.out.println("\nInvalid choice:");
}while(ch!=4);
}
ASSIGNMENT-19(PACKAGE)
}
Statop Package-
package statop;
}
Main class-
import
mathop.MathOperations;
import
statop.StatsOperations;
class array{
public static void main(String args[])
{
double arr[]={1,2,3,4,5,6};
MathOperations ob=new
MathOperations(); StatsOperations
ob1=new StatsOperations();
System.out.println("Maximum value in array: "+
ob.max(arr)); System.out.println("Minimum value in array:
"+ ob.min(arr)); System.out.println("Average value in array:
"+ ob1.avg(arr)); System.out.println("Median value of
array: "+ ob1.median(arr));
}
}
2. Create a package called nodepack which contains the class “Node”.
Create
another
package called listpack which contains the class “LinkedList” representing
methods
to create a Single Linked list, Add a node to the list and traverse the list.
Write a menu
driven program in main to create a Single Linked list, Add nodes and
display the List.
The elements are passed as user input.
Source Code:
Node Pack:
package
nodepack; public
class Node{
List Pack:
package listpack;
import
nodepack.Node;
public class LinkedList{
Node head;
public LinkedList insert(LinkedList list, int d)
{
}
else{
Node last=list.head;
while(last.next!=null
){
last=last.next;
}
last.next=n;
}
return list;
}
public void display(LinkedList
list){ Node
c_node=list.head;
System.out.println("Linked List:
"); while(c_node!=null){
System.out.print(c_node.data+" -> ");
c_node=c_node.next;
}
}
Main Class:
import nodepack.Node;
import
listpack.LinkedList;
import
java.util.Scanner; class
list{
public static void main(String arg[])
{
int ch,data;
Scanner sc=new
Scanner(System.in); LinkedList
ll=new LinkedList();
do
{
System.out.println("\n1:Insert");
System.out.println("2:Display");
System.out.println("3:Exit.");
System.out.println("\nEnter your
choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the data
:"); data=sc.nextInt();
ll=
ll.insert(ll,data);
break;
case
2:ll.display(
ll); break;
case
3:System.exit(
0); break;
default:System.out.println("\nInvalid choice:");
}
}while(ch!=4);
}
}
ASSIGNMENT-20(BUILT IN EXCEPTION)
}
class ExitException extends
Exception{ public String
toString(){
return "Exit Exception";
}
}
class TestException{
static void display(char c)
throws
VowelException,BlankException,Exi
tException{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=
='u'){ throw new
VowelException();
}
else if(c==' '){
throw new BlankException();
}
else if(c=='X'){
throw new ExitException();
}
else
{
System.out.println("Valid Character");
}
}
}
class Exceptions{
public static void main(String args[])
throws Exception{ try{
char c=args[0].charAt(0);
TestException t=new
TestException(); t.display(c);
}
catch(Exceptio
n e){
System.out.println("Exception: "+ e);
}
}
1. Write a program which accepts two integers and an arithmetic operator
from the command line and performs the operation. Check the following
user defined exceptions:
i.
If the number of arguments are less than 3 then
throw “FewArgumentsException”.
ii. If the operator is not an Arithmetic operator,
throw “InvalidOperatorException”.
iii. If result is –ve, then throw “NegativeResult” exception.
Source Code:
class
FewArgumentsExc
eption{ public
String toString(){
class InvalidOperatorException
toString(){
}
}
System.out.println("Negative Result
Exception");
class Operation{
static void calc(int a,char o,int b)throws
FewArgumentsException,InvalidOperatorException
,NegativeResult{
switch(o){
case '+':
System.out.println("Addition
: "+(a+b)); break;
case '-':
System.out.println("subtractio
n: "+(a-b)); break;
case '*':
System.out.println("subtraction
: "+(a*b)); break;
case '/':
{
System.out.println("Division:
"+(a/b)); break;
case '%':
System.out.println("subtraction
: "+(a%b)); break;
}
class exceptions2{
main(String args[]){
try{
int
a=Integer.parseInt(args
[0]); char
o=args[1].charAt(0);
int
b=Integer.parseInt(args[
Operation();
op.calc(a,o,b);
}
catch(Exception e){
System.out.println("Exception: "+e);
}
2.Create a class Student with attributes roll no, name, age and
course. Initialize values through parameterized constructor. If age of
student is not between 15 and 21 then generate user-defined
exception
“InvalidAgeException”. If name contains numbers or special
characters raise exception “InvalidNameException”. Define the two
exception classes. Source Code:
import java.util.*;
toString(){
class InvalidNameException
String toString(){
class Student{
static int rollno,age;
name=snam
e; age=sage;
course=scou
rse;
else if(false){
else
{
System.out.println("Name:
"+name);
System.out.println("Roll No:
"+rollno);
System.out.println("Course:
}
"+course);
}
System.out.println("Agee:
}
"+age);
class Students{
main(String args[]){
int rn,sage;
String sname,scourse;
try{
Scanner sc=new
Scanner(System.in);
rn=sc.nextInt();
sage=sc.nextI
nt();
sname=sc.nex
t();
scourse=sc.ne
xt();
Student s=new
Student(rn,sname,sage,scourse);
s.check();
}
catch(Exception e){
System.out.println("Excepti
}
3.Define class MyDate with members day, month and year. Define
default and parameterized constructors. Accept values from the
command line and create a date object. Throw user defined
exceptions
–
“InvalidDayException” or “InvalidMonthException” if the day or
month are invalid. If the date is valid, display message “Valid Date”.
Source Code:
toString(){
class InvalidMonthException
String toString(){
class MyDate{
static int
day,month;
month=month;
static void
datecheck()throws
InvalidMonthException,InvalidDa
yException{
if(day<0||day>31){
else if(month<0||month>12){
else
{
System.out.println("Valid Date");
class MyDates{
int
day=Integer.parseInt(args[
0]); int
month=Integer.parseInt(ar
gs[1]);
MyDate md=new
MyDate(day,month);
md.datecheck();
catch(Exception e){
System.out.println("Exception: "+e);
}
ASSIGNMENT-22(FILE READ & WRITE)