0% found this document useful (0 votes)
55 views54 pages

Define A Class 'Stack' With Following Specification:-Data Members

The document defines a Complex class with: - Data members to store the real and imaginary parts of a complex number - Constructors to initialize the values - Member functions to add, subtract, multiply and divide two complex numbers and display the results It then provides a main method to test the class by performing operations on sample complex numbers and displaying the outputs.

Uploaded by

Aryaman Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views54 pages

Define A Class 'Stack' With Following Specification:-Data Members

The document defines a Complex class with: - Data members to store the real and imaginary parts of a complex number - Constructors to initialize the values - Member functions to add, subtract, multiply and divide two complex numbers and display the results It then provides a main method to test the class by performing operations on sample complex numbers and displaying the outputs.

Uploaded by

Aryaman Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 54

/*

Define a class 'Stack' with following specification :-

Data Members :-
a[] : array to store elements
size : to store size of array
top : to store and delete element at top position

Member functions :-
Stack(int s) : to initiallise array with size & top=-1
boolean isEmpty() : to return true if array is empty
boolean isFull() : to return true if array is full
void Push(int item) : to push item at top position
void Pop() : to pop element at top position
void display() : to display elements from top to 0
*/

PROGRAM

import java.io.*;
class Stack
{
int size,top,a[];
Stack(int s)
{
size = s;
a = new int[s];
top = -1;
}
boolean isEmpty()
{
if(top==-1)
return true;
return false;
}
boolean isFull()
{
if(top==size-1)
return true;
return false;
}
void Push(int item)
{
if(isFull())
System.out.println("Filled Upto The Brim");
else
a[++top] = item;
}
void Pop()
{
if(isEmpty())
System.out.println("Asking From Beggar");
else
top--;
}
void display()
{
System.out.print("Stack is :----> ");
for(int i=top;i>=0;i--)
System.out.print(a[i]+"\t");
System.out.println();
}
}
class teststack
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter size : ");
Stack a = new Stack(Integer.parseInt(in.readLine()));int n;
System.out.println("1. Insert\n2. Delete\n3. Display\n4. Exit");
do
{
System.out.print("Enter choice : ");
n=Integer.parseInt(in.readLine());
switch(n)
{
case 1 :
System.out.print("Enter item : ");
a.Push(Integer.parseInt(in.readLine()));
break;
case 2 :
a.Pop();
break;
case 3 :
a.display();
break;
case 4 :
System.exit(0);
}
}while(n!=4);
}
}

Output Of Program

/*
Enter size : 1
1. Insert
2. Delete
3. Display
4. Exit
Enter choice : 1
Enter item : 2
Enter choice : 1
Enter item : 3
Filled Upto The Brim
Enter choice : 3
Stack is :----> 2
Enter choice : 2
Enter choice : 2
Asking From Beggar
/*Define a class 'Queue' with following specification :-

Data Members :-
a[] : array to store elements
size : to store size of array
rear : store position of push
front : store position of pop

Member functions :-
Stack(int s) : Parameterised Constructor
boolean isEmpty() : to return true if array is empty
boolean isFull() : to return true if array is full
void Push(int item) : to push item at rear position
void Pop() : to pop element at front position
void display() : to display elements from front to rear
*/
PROGRAM

import java.io.*;
class Queue
{
int size,front,rear,a[];
Queue(int s)
{
size = s;a = new int[s];front = rear = -1;
}
boolean isEmpty()
{
if(front==-1 || front==rear+1)
return true;
return false;
}
boolean isFull()
{
if(rear==size-1)
return true;
return false;
}
void Push(int item)
{
if(isFull())
System.out.println("Filled Upto The Brim");
else
{
if(rear==-1)
rear = front = 0;
else
++rear;
a[rear] = item;
}
}
void Pop()
{
if(isEmpty())
{
front = rear= -1;
System.out.println("Asking From Beggar");
}

else
front++;
}
void display()
{
System.out.print("Queue is :----> ");
if(front>-1)
{
for(int i=front;i<=rear;i++)
System.out.print(a[i]+"\t");
}
System.out.println();
}
}
class testqueue
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter size : ");
Queue a=new Queue(Integer.parseInt(in.readLine()));int n;
System.out.println("1. Insert\n2. Delete\n3. Display\n4. Exit");
do
{
System.out.print("Enter choice : ");
n = Integer.parseInt(in.readLine());
switch(n)
{
case 1 :
System.out.print("Enter item : ");
a.Push(Integer.parseInt(in.readLine()));
break;
case 2 :
a.Pop();
break;
case 3 :
a.display();
break;
case 4 :
System.exit(0);
}
}while(n!=4);
}
}
Output Of Program

/*
Enter size : 1
1. Insert
2. Delete
3. Display
4. Exit
Enter choice : 1
Enter item : 2
Enter choice : 1
Enter item : 3
Filled Upto The Brim
Enter choice : 3
Queue is :----> 2
Enter choice : 2
Enter choice : 2
Asking From Beggar
Enter choice : 1
Enter item : 3
Enter choice : 4 */
Define a class 'Angle' with following classification :-

Data Member :-
deg : an integer to degrees of angle
min : an integer to minutes of angle
sec : an integer to seconds of angle

Member Function :-
Angle() : Default constructor
Angle(int d,int m,int s) : Parameterised constructor
void readAngle() : to input angle
void showAngle() : to display angle
int isValidAngle : to return 1 if angle is valid else 0
Angle add(Angle a) : to return an Angle with sum of 'a' and
calling object
Angle Diff(Angle a) : to return an Angle with difference of 'a'
and calling object
int compare(Angle a) : to return 1 if calling angle is greater or
else -1 if 'a' greater otherwise 0
*/

PROGRAM

import java.io.*;
class Angle
{
int deg,min,sec;
Angle()
{
deg = min = sec = 0;
}
Angle(int d,int m,int s)
{
deg = d;
min = m;
sec = s;
}
void readAngle() throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\n\tEnter Data About An Angle");
System.out.print("\n\tEnter Degrees :");
deg = Integer.parseInt(in.readLine());
System.out.print("\tEnter Minutes :");
min = Integer.parseInt(in.readLine());
System.out.print("\tEnter Seconds :");
sec = Integer.parseInt(in.readLine());
}
void showAngle()
{
System.out.print("\n\n\tAngle Is :"+deg+
(char)248+min+"'"+sec+"\"");
}
int isValidAngle()
{
if(deg<0 || min>59 || min<0 || sec>59 || sec<0)
return 0;
return 1;
}
int compare(Angle a)
{
long s1 = deg*3600+min*60+sec,s2=a.deg*3600+a.min*60+a.sec;
if(s1>s2)
return 1;
if(s1<s2)
return -1;
return 0;
}
Angle add(Angle a)
{
Angle ret = new Angle();
ret.sec = sec+a.sec;
ret.min = min+a.min+ret.sec/60;
ret.deg = deg+a.deg+ret.min/60;
ret.sec %= 60;
ret.min %= 60;
return ret;
}
Angle Diff(Angle a)
{
int s = (deg*3600+min*60+sec)-(a.deg*3600+a.min*60+a.sec);
if(s<0)
s = s*-1;
return new Angle(s/3600,(s%3600)/60,(s%3600)%60);
}
}
class Angl
{
public static void main(String args[])throws Exception
{
Angle a = new Angle(),b = new Angle();
a.readAngle();
b.readAngle();
a.showAngle();
b.showAngle();
a.Diff(b).showAngle();
}
}

Output Of Program

/*
Enter Data About An Angle
Enter Degrees :10
Enter Minutes :20
Enter Seconds :30

Enter Data About An Angle


Enter Degrees :20
Enter Minutes :30
Enter Seconds :40

Angle Is :10ø20'30"

Angle Is :20ø30'40"

Angle Is :10ø10'10"
*/

Define a 'Binomial' class with following specification :-


Data Members :-
x : to store value of x in (x + a)^n
a : to store value of a in (x + a)^n
n : to store limit
Member Functions :-
void readdata() : To input x,a,n
long factorial(int) : To return factorial of n
long nCr(int,int) : To return nCr
void sumseries() : To return value of (x+a)^n
*/

PROGRAM
import java.io.*;
class Binomial
{
int x,a,n;
void readdata()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter limit : ");
n = Integer.parseInt(in.readLine());
System.out.print("Enter value of x :");
x = Integer.parseInt(in.readLine());
System.out.print("Enter value of a :");
a = Integer.parseInt(in.readLine());
}
long factorial(int b)
{
if(b==0 || b==1)
return 1;
else return b*factorial(b-1);
}
long nCr(int l,int r)
{
return factorial(l)/(factorial(r)*factorial(l-r));
}
void sumseries()
{
long sum=0;
for(int i=0;i<=n;i++)
{
sum += nCr(n,i)*(long)Math.pow(x,n-i)*(int)Math.pow(a,i);
}
System.out.println("Sum of "+"("+x+"+"+a+")"+"^"+n+" is : "+sum);
}
}
class Bin
{
public static void main(String args[])throws Exception
{
Binomial a=new Binomial();
a.readdata();
a.sumseries();
}
} Output Of Program

/*
Enter limit : 6
Enter value of x :2
Enter value of a :1 Sum of (2+1)^6 is : 729 */
/*
Define a class 'complex' with following specification :-

Data Member :-
real : to store real part
imag : to store imaginary part

Member Function :-
complex(double x,double y) : Parameterised constructor
complex add(complex z) : to return 'this' plus z as complex
complex sub(complex z) : to return 'this' minus z as complex
complex multiply(complex z) : to return 'this' product z as complex
complex divide(complex z) : to return 'this' division z as complex
void display() : to display 'this' in complex number
format
*/

PROGRAM

class complex
{
double real,imag;
complex()
{
real = imag = 0;
}
complex(double x,double y)
{
real = x;
imag = y;
}
void display()
{
if(imag>0)
System.out.println(real+"+"+imag+"i\n");
else
System.out.println(real+""+imag+"i\n");
}
complex add(complex z)
{
return new complex(real+z.real,imag+z.imag);
}
complex sub(complex z)
{
return new complex(real-z.real,imag-z.imag);
}
complex multiply(complex z)
{
return new complex(real*z.real-imag*z.imag,
real*z.imag+imag*z.real);
}
complex divide(complex z)
{
complex temp = new complex(real,imag);
complex conjugate = new complex(z.real,-(z.imag));
z = z.multiply(conjugate);
temp=temp.multiply(conjugate);
temp.real /= z.real;
temp.imag /= z.real;
return temp;
}
}
class compl
{
public static void main(String args[])
{
complex a = new complex(3,2),b = new complex(2,-3);
a.display();
b.display();
System.out.print("Sum is : ");
a.add(b).display();
System.out.print("Difference is : ");
a.sub(b).display();
System.out.print("Product is : ");
a.multiply(b).display();
System.out.print("Division is : ");
a.divide(b).display();

}
}

Output Of Program

/*
3.0+2.0i

2.0-3.0i

Sum is : 5.0-1.0i

Difference is : 1.0+5.0i

Product is : 12.0-5.0i

Division is : 0.0+1.0i

Define Class 'Conversion' with following specifications :-


Member Functions :-
void deci2ne() : to convert decimal no into base equivalent
void ne2deci() : to convert base no. to decimal equivalent
void ne2ne() : to convert any base no. to any base
void hexa2binary() : to convert hexadecimal no. to binary equivalent
void octal2binary() : to convert octal no. to binary equivalent
void binary2hexa() : to convert binary no. to hexadecimal equivalent
void binary2octal() : to convert binary no. to octal equivalent
*/

PROGRAM

class Conversion
{
void deci2ne(int n,int b)
{
String sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s = "";
for(;n>0;s=sym.charAt(n%b)+s,n/=b);
System.out.println(s);
}
void ne2deci(String s,int b)
{
String sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int l=0,n=0;
for(;l<s.length();n=n*b+sym.indexOf(s.charAt(l)),l++);
System.out.println(n);
}
void ne2ne(String s,int b1,int b)
{
String sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String ans = "";int n=0,l=0;
for(;l<s.length();n=n*b1+sym.indexOf(s.charAt(l)),l++);
for(;n>0;ans=sym.charAt(n%b)+ans,n/=b);
System.out.println(ans);
}
void hexa2binary(String s)
{
String sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String hexa[]={"0000","0001","0010","0011","0100","0101","0110",
"0111","1000","1001","1010","1011","1100","1101","1110","1111"};
int l=0;
String ans="";
for(;l<s.length();ans=ans+hexa[sym.indexOf(s.charAt(l))],l++);
System.out.println(ans);
}
void octal2binary(String s)
{
String sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String octal[] = {"000","001","010","011","100","101","110","111"};
int l=0;
String ans="";
for(;l<s.length();ans=ans+octal[sym.indexOf(s.charAt(l))],l++);
System.out.println(ans);
}
void binary2octal(String s)
{
String oct[] = {"000","001","010","011","100","101","110","111"};
String ans="",s1="";int i=0,j=0;
while(s.length()%3!=0)
s='0'+s;
while(s.length()!=0)
{
s1 = s.substring(0,3);
for(j=0;j<8;j++)
{
if(s1.equals(oct[j]))
ans = ans+j;
}
s = s.substring(3);
}
System.out.println(ans);
}
void binary2hexa(String s)
{
String sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String hexa[]={"0000","0001","0010","0011","0100","0101","0110",
"0111","1000","1001","1010","1011","1100","1101","1110","1111"};
String ans="",s1="";int i=0,j=0;
while(s.length()%4!=0)
s='0'+s;
while(s.length()!=0)
{
s1 = s.substring(0,4);
for(j=0;j<16;j++)
{
if(s1.equals(hexa[j]))
ans = ans+sym.charAt(j);
}
s = s.substring(4);
}
System.out.println(ans);
}
}
class aconversion
{
public static void main(String args[])
{
Conversion a = new Conversion();
System.out.print("(41567359,35):deci2ne :");a.deci2ne(41567359,35);
System.out.print("(ROHIT,35) : ne2deci : ");a.ne2deci("ROHIT",35);
System.out.print("(ROHIT,35,10) :ne2ne : ");a.ne2ne("ROHIT",35,10);
System.out.print("(ABC) : hexa2binary : ");a.hexa2binary("ABC");
System.out.print("(345) : octal2binary : ");a.octal2binary("345");
System.out.print("(1010011) : binary2octal : ");
a.binary2octal("1010011");
System.out.print("(101010111100) : binary2hexa : ");
a.binary2hexa("101010111100");
}
}

Output Of Program

/*
(41567359,35) : deci2ne : ROHIT
(ROHIT,35) : ne2deci : 41567359
(ROHIT,35,10) : ne2ne : 41567359
(ABC) : hexa2binary : 101010111100
(345) : octal2binary : 011100101
(1010011) : binary2octal : 123
(101010111100) : binary2hexa : ABC
*/
Define a class 'Dec_Bin' with following specifications :-

Data Members :-
n : to store decimal no.
s : to store equivalent binary no
i : incremental value of power

Member Functions :-
void getdata() : to input decimal no
void recursive() : recursive method to compute binary of no
void display() : to display decimal no and its binary equivalent
*/

PROGRAM

import java.io.*;
class Dec_Bin
{
int n,s,i;
void getdata()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a no. : ");
n = Integer.parseInt(in.readLine());
recursive(n);
display();
}
void recursive(int n)
{
if(n>0)
{
s = s+(n%2)*(int)Math.pow(10,i++);
recursive(n/2);
}
}
void display()
{
System.out.print("Decimal no. : "+n+" "+"
\nBinary equaivalent is : "+s);
}
}
class dec_bin
{
public static void main(String args[])throws Exception
{
Dec_Bin a=new Dec_Bin();
a.getdata();
}
}

Output Of Program

/*
Enter a no. : 14
Decimal no. : 14
Binary equaivalent is : 1110
*/

Define class 'duplicate' with following specifiations :-


Data Members :-
arr[] : to store sorted elements
size : to store size of array

Member Functions :-
void readList() : to enter elements
void packList() : to remove duplicates
void dispList() : to display array the array
*/

PROGRAM

import java.io.*;
class duplicate
{
int arr[],size;
void readList()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter size : ");
size=Integer.parseInt(in.readLine());
System.out.println("Enter elements : ");
arr = new int[size];
for(int i=0;i<size;i++)
arr[i] = Integer.parseInt(in.readLine());
}
void packList()
{
int i,j=0;
for(i=0;i<size-1;i++)
{
if(arr[i]!=arr[i+1])
arr[j++] = arr[i];
}
arr[j++] = arr[i];
size = j;
}
void dispList()
{
System.out.print("Array is : ");
for(int i=0;i<size;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
class dupli
{
public static void main(String args[])throws Exception
{
duplicate b=new duplicate();
b.readList();
b.dispList();
b.packList();
b.dispList();
}
}

Output Of Program
/*
Enter size : 4
Enter elements :
4
3
2
2
Array is : 4 3 2 2
Array is : 4 3 2
*/

Define a class 'Fraction' with following classification :-

Data Member :-
num : an integer to store numerator
den : an integer to store denominator

Member Function :-
void readFraction() : to input Fraction
void showFraction() : to display Fraction
int hcf(int a,int b) : to return HCF of a & b
int lcm(int a,int b) : to return LCM of a & b
void reduce() : to reduce the fraction to co-prime
factor
Fraction addFraction(Fraction f) : to return sum of 'f' and calling
object as a Fraction
int compare(Fraction f) : to return 1 if calling fraction is
greater or else -1 if 'f' greater otherwise 0
*/

PROGRAM

import java.io.*;
class Fraction
{
int num,den=0;
void readFraction() throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\n\tEnter Numerator :");
num = Integer.parseInt(in.readLine());
while(den==0)
{
System.out.print("\n\tEnter Denominator(<>0) :");
den = Integer.parseInt(in.readLine());
}
if(den<0)
{
num = num*-1;
den = den*-1;
}
}
void showFraction()
{
if(den==0)
System.out.print("\n\tFraction Is Undefined");
else
{
System.out.print("\n\tFraction Is :"+num);
if(den!=1)
System.out.print("/"+den);
}
}
int lcm(int a,int b)
{
int c=a,d=b;
while(a!=b)
if(a<b)
a = a+c;
else
b = b+d;
return a;
}
int hcf(int a,int b)
{
while(a!=b)
if(a<b)
b = b - a;
else
a = a - b;
return a;
}
void reduce()
{
int cut = hcf((int)Math.abs(num),den);
num = num/cut;
den = den/cut;
}
Fraction addFraction(Fraction f)
{
Fraction ret = new Fraction();
ret.num = num*(lcm(den,f.den)/den)
+ f.num*(lcm(den,f.den)/f.den);
ret.den = lcm(den,f.den);
ret.reduce();
return ret;
}
int compare(Fraction f)
{
if(num*f.den>f.num*den)
return 1;
if(num*f.den<f.num*den)
return -1;
return 0;
}
}
class Fract
{
public static void main(String args[])throws Exception
{
Fraction a=new Fraction(),b=new Fraction();
a.readFraction();b.readFraction();
a.reduce();a.showFraction();
b.reduce();b.showFraction();
a.addFraction(b).showFraction();
}
}

Output Of Program

/*
Enter Numerator :1000
Enter Denominator(<>0) :3000

Enter Numerator :2000


Enter Denominator(<>0) :3000

Fraction Is :1/3
Fraction Is :2/3
Fraction Is :1
*/

Define class 'GPseries' with following specifications :-

Date Members :-
a : to store first term
r : to store common ratio
n : to store limit

Member Functions :-
void readdata() : to input a,d,n
long nThterm(int) : to return nTh term of the series
long sum() : to return sum of the series
void showseries() : to display the series and sum
*/

PROGRAM

import java.io.*;
class GPseries
{
int a,r,n;
void readdata()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter first term : ");
a = Integer.parseInt(in.readLine());
System.out.print("Enter common ratio : ");
r = Integer.parseInt(in.readLine());
System.out.print("Enter limit : ");
n = Integer.parseInt(in.readLine());
}
long nThterm(int b)
{
return a*(int)Math.pow(r,b-1);
}
long sum()
{
return a*(int)(Math.pow(r,n)-1)/(r-1);
}
void showseries()
{
for(int i=1;i<=n;i++)
System.out.print(nThterm(i)+" ");
System.out.println("\nSum : "+sum());
}
}
class gp
{
public static void main(String args[])throws Exception
{
GPseries a=new GPseries();
a.readdata();
a.showseries();
}
}

Output Of Program

/*
Enter first term : 2
Enter common ratio : 2
Enter limit : 5
2 4 8 16 32
Sum : 62
*/

Define class 'lucas' with following specifications :-

Date Members :-
n : to store no

Member Functions :-
void readdata() : to input n
int nThterm(int) : to return nTh term of the series
int sum() : to return sum of the series
void showseries(): to display the series
*/

PROGRAM

import java.io.*;
class lucas
{
int n;
void readdata()throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter limit : ");
n = Integer.parseInt(in.readLine());
}
int nThterm(int m)
{
if(m>3)
return nThterm(m-1)+nThterm(m-2)+nThterm(m-3);
return m;
}
void showseries()
{
System.out.println("Series : ");
for(int i=1;i<=n;i++)
System.out.print(nThterm(i)+" ");
}
}
class luca
{
public static void main(String args[])throws Exception
{
lucas a = new lucas();
a.readdata();
a.showseries();
}
}

Output Of Program

/*
Enter limit : 10
Series :
1 2 3 6 11 20 37 68 125 230
*/

/*
Define a class with ' MyMath ' with following specifications :-

Data Members :-
x : to store value of theta

Member Functions :-
MyMath(double nx) : Parameterised constructor
double abs(double y) : to return modulus of x
double cos() : to return cos x if x lies in 0 and PI/2
double sec() : to return sec x if x lies in 0 and PI/2
*/

PROGRAM

import java.io.*;
class MyMath
{
double x;
MyMath(double nx)
{
x = nx;
}
double abs(double y)
{
if(y<0)
return -1*y;
return y;
}
double cos()
{
if(x<0 || x>Math.PI/2)
return 999999.9;
int i,j,s=-1,f;double cosv=1,term=1;
for(i=2;i<=10;i+=2)
{
f = 1;
for(j=1;j<=i;f*=j,j++);
term = (Math.pow(x,i)/f)*s;
cosv = cosv+term;s*=-1;
}
return 1-cosv;
}
double sec()
{
if(cos()==999999.9)
return cos();
return 1/cos();
}
}
class math
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter value of theta in radians : ");
double x = Double.parseDouble(in.readLine());
MyMath a = new MyMath(x);
System.out.println("cos x : "+a.cos());
System.out.println("sec x : "+a.sec());
System.out.println("Absolute value of x : "+a.abs(x));
}
}
Output Of Program

/*
Enter value of theta in radians : 0
cos x : 0.0
sec x : Infinity
Absolute value of x : 0.0
*/

/*
Define a class ' Matrix ' with following specifications :-

Data Members :-
mat[][] : an array to store matrix
row : to store no of rows
col : to store no of columns

Member Functions :-
Matrix(int r,int c) : Parameterised constructor
readMatrix() : to input matrix
showMatrix() : to display the matrix
Matrix Transpose() : to return the transpose matrix
Matrix Add(Matrix m) : to add matrix and return resultant
Matrix Subtract(Matrix m) : to subtract two matrices and return resultant
Matrix Multiply(Matrix m) : to multiply matrices and return resultant
*/

PROGRAM
import java.io.*;
class Matrix
{
int mat[][],row,col;
Matrix(int r,int c)
{
row = r;
col = c;
mat = new int[row][col];
}
void readMatrix()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Start Entering elements : ");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
mat[i][j] = Integer.parseInt(in.readLine());
}
}
void showMatrix()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
System.out.print(mat[i][j]+"\t");
System.out.println();
}
}
Matrix Transpose()
{
Matrix t = new Matrix(col,row);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
t.mat[j][i] = mat[i][j];
}
return t;
}
Matrix Add(Matrix m)
{
Matrix t = new Matrix(row,col);
if(row!=m.row || col!=m.col)
return t;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
t.mat[i][j] = mat[i][j]+m.mat[i][j];
}
return t;
}
Matrix Subtract(Matrix m)
{
Matrix t = new Matrix(row,col);
if(row!=m.row || col!=m.col)
return t;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
t.mat[i][j] = mat[i][j]-m.mat[i][j];
}
return t;
}
Matrix Multiply(Matrix m)
{
Matrix t = new Matrix(row,m.col);
if(col!=m.row)
return t;
for(int i=0;i<row;i++)
{
for(int j=0;j<m.col;j++)
{
t.mat[i][j] = 0;
for(int k=0;k<col;k++)
t.mat[i][j] += mat[i][k]*m.mat[k][j];
}
}
return t;
}
}
class matrix
{
public static void main(String args[])throws Exception
{
Matrix a = new Matrix(2,2);
a.readMatrix();a.showMatrix();
System.out.println("Transpose : ");a.Transpose().showMatrix();
System.out.println("Sum : ");a.Add(a.Transpose()).showMatrix();
System.out.println("Subtraction : ");
a.Subtract(a.Transpose()).showMatrix();
System.out.println("Multiplication : ");
a.Multiply(a.Transpose()).showMatrix();
}
}

Output Of Program

/*
Start Entering elements :
1
2
3
4
1 2
3 4
Transpose :
1 3
2 4
Sum :
2 5
5 8
Subtraction :
0 -1
1 0
Multiplication :
5 11
11 25

PROGRAM ON INHERIT //Define a class 'Account' with two extended classes ‘Simple’ &
‘Compound’ for calculating interest :-

PROGRAM

import java.io.*;
class Account
{
protected int accountNumber;
protected double principal;
Account()
{
accountNumber = 0;
principal = 0;
}
Account(int acn,double np)
{
accountNumber=acn;
principal=np;
}
void display()
{
System.out.println("Account Number : "+accountNumber);
System.out.println("Principal : "+principal);
}
}
class Simple extends Account
{
protected double time,rate;
Simple()
{
time = 0;
rate = 0;
}
Simple(int acn,double np,double nt,double nr)
{
super(acn,np);
time = nt;
rate = nr;
}
void display()
{
super.display();
System.out.println("Rate : "+rate);
System.out.println("Time : "+time);
System.out.println("Interest : "+interest());
}
double interest()
{
return principal*rate*time/100;
}
}
class Compound extends Account
{
protected double time,rate;
Compound()
{
time = 0;
rate = 0;
}

Compound(int acn,double np,double nt,double nr)


{
super(acn,np);
time = nt;
rate = nr;
}
void display()
{
super.display();
System.out.println("Rate : "+rate);
System.out.println("Time : "+time);
System.out.println("Interest : "+interest());
}
double interest()
{
return principal*(Math.pow(1+rate/100,time)-1);
}
}
class testaccount
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Account a=new Account();
System.out.print("Enter Acc# : ");
int acn = Integer.parseInt(in.readLine());
System.out.print("Enter principal : ");
double p = Double.parseDouble(in.readLine());
System.out.print("Enter rate : ");
int r = Integer.parseInt(in.readLine());
System.out.print("Enter time : ");
int t = Integer.parseInt(in.readLine());
System.out.print("Enter type of account (S/C) : ");
if(in.readLine().charAt(0)=='S')
a = new Simple(acn,p,t,r);
else
a = new Compound(acn,p,t,r);
a.display();
}
}

Output Of Program

/*
Enter Acc# : 4
Enter principal : 10
Enter rate : 1
Enter time : 1
Enter type of account (S/C) : S
Account Number : 4
Principal : 10.0
Rate : 1.0
Time : 1.0
Interest : 0.1
*/

//to display diamond pattern of string

PROGRAM

import java.io.*;
class diamondstr
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//inputting string
System.out.print("Enter a string : ");
String s = in.readLine();int i,j,k;
System.out.println("Pattern is : ");
//displaying diamond pattern
for(i=-s.length();i<s.length();i++)
{
k = 0;
for(j=-s.length();j<s.length();j++)
{
if(Math.abs(i)+Math.abs(j)<s.length())
{
System.out.print(s.charAt(k));
k = j<0?++k:--k;
}
else System.out.print(" ");
}
System.out.println();
}
}
}

Output Of Program

/*
Enter a string : abc
Pattern is :
a
aba
abcba
aba
a

//to display odd length string in different pattern

PROGRAM

import java.io.*;
class oddlength
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting odd length string
System.out.print("Enter a string : ");
String s = in.readLine();int i,j;
//displaying pattern
for(i=0;i<s.length();i++)
{
for(j=0;j<s.length();j++)
{
if(i==s.length()/2)
{
System.out.print(s);
break;
}
else if(i==j || j==s.length()/2 || i+j==s.length()-1)
System.out.print(s.charAt(i));
else
System.out.print(" ");
}
System.out.println();
}
}
}

Output Of Program

/*
Enter a string : rohit
rrr
ooo
rohit
iii
ttt

//to show pascal reverse triangle

PROGRAM

import java.io.*;
class pascalrev
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args)throws Exception
{
//inputting limit
System.out.print("\n\n\tEnter limit : ");
int n = Integer.parseInt(in.readLine());
int arr[][] = new int[n*2][],i,j,k;
//evaluating Pascal Triangle
System.out.print("\n\tPascal Triangle :\n");
for(i=0;i<n*2;i++)
{
k = i<n?i+1:n*2-i-1;
arr[i] = new int[k];
for(j=0;j<k;j++)
{
if(j==0 || j==i)
arr[i][j] = 1;
else if(i<n)
arr[i][j] = arr[i-1][j-1]+arr[i-1][j];
else
arr[i][j] = arr[i-1][j]-arr[i][j-1];
System.out.print("\t"+arr[i][j]);
}
System.out.print("\n");
}
}
}

Output Of Program

/*
Enter limit : 3
Pascal Triangle :
1
1 1
1 2 1
1 1
1
*/

//to find second highest no. amongst the list

PROGRAM

import java.io.*;
class secondhigh
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputtng size and nos.
System.out.print("Enter size : ");
int n = Integer.parseInt(in.readLine()),h=0,sh=0,i,k;
System.out.println("Start Entering Elements : ");
//calculating second highest
for(i=0;i<n;i++)
{
k = Integer.parseInt(in.readLine());
if(k>h)
{
sh = h;
h = k;
}
else if(k<h&&k>sh)
sh = k;
}
//displaying second highest no
System.out.print("Second Highest Is : "+sh);
}
}

Output of Program

/*
Enter size : 3
Start Entering Elements :
1
3
2
Second Highest Is : 2
*/
//to find intersection of two arrays

PROGRAM

import java.io.*;
class intersection
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//inpuuting size
System.out.print("Enter size of array 1(R1) : ");
int r1 = Integer.parseInt(in.readLine());
System.out.print("Enter size of array 2(R2) : ");
int r2 = Integer.parseInt(in.readLine());
int i,j,p=0,t;
int a1[] = new int[r1],a2[] = new int[r2],a[] = new int[r2];
System.out.println("Enter elements of array 1 : ");
//inputing nos.
for(i=0;i<r1;i++)
{
//a1[i] = Integer.parseInt(in.readLine());
a1[i] = (int)(Math.random()*10);
}
System.out.println("Enter elements of array 2 : ");
for(i=0;i<r2;i++)
{
//a1[i] = Integer.parseInt(in.readLine());
a2[i] = (int)(Math.random()*10);
}
if(r1<r2)
a = new int[r1];
//processing
for(i=0;i<r1;i++)
{
t = 0;
for(j=0;j<r2;j++)
{
if(a1[i]==a2[j])
t = 1;
}
if(t==1)
a[p++] = a1[i];
}
//displaying arrays
System.out.println("ARRAY 1 : ");
for(i=0;i<r1;i++)
System.out.print(a1[i]+"\t");
System.out.println("\nARRAY 2 :");
for(i=0;i<r2;i++)
System.out.print(a2[i]+"\t");
System.out.println("\nINTERSECTION ARRAY : ");
for(i=0;i<p;i++)
System.out.print(a[i]+"\t");
}
}

Output Of Program
/*
Enter size of array 1(R1) : 4
Enter size of array 2(R2) : 5
Enter elements of array 1 :
Enter elements of array 2 :
ARRAY 1 :
7 5 9 8
ARRAY 2 :
9 8 7 3 5
INTERSECTION ARRAY :
7 5 9 8
*/

/ / RECURSIVE METHOD to interchange even with odd and vice-versa

PROGRAM

import java.io.*;
class eve_odd
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting size
System.out.print("Enter size : ");
int i,size = Integer.parseInt(in.readLine());
int arr[] = new int[size];
//inputting nos
System.out.println("Start Entering elements : ");
for(i=0;i<size;i++)
arr[i] = Integer.parseInt(in.readLine());
//displaying initial array
System.out.println("Initial Array : ");
for(i=0;i<size;i++)
System.out.print(arr[i]+"\t");
//caliing function
arrange(arr,size,0,0);
//displaying sorted array
System.out.println("\nSorted Array : ");
for(i=0;i<size;i++)
System.out.print(arr[i]+"\t");
System.out.println();
}
//changing position of even with odd and vice-versa
public static void arrange(int arr[],int size,int eve,int odd)
{
while(eve<size && arr[eve]%2!=0)
eve++;
while(odd<size && arr[odd]%2==0)
odd++;
if(eve<size && odd<size)
{
arrange(arr,size,eve+1,odd+1);
swap(arr,eve,odd);
}
}
//swapping nos
public static void swap(int arr[],int i,int j)
{
int t = arr[i];
arr[i] = arr[j];arr[j] = t;
}
}

Output Of Program

/*
Enter size : 2
Start Entering elements :
1
2
Initial Array :
1 2
Sorted Array :
2 1
*/

//to search given element arranged in array in ascending order

PROGRAM

import java.io.*;
class binarysearch
{
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting size & elements
System.out.print("\n\n\tEnter size of array : ");
int a[] = new int[Integer.parseInt(in.readLine())];
int i,st = 0,la = a.length-1,mid = 0,d = 1,key;
System.out.println("\n\n\tStart Entering elements :");
for(i=0;i<a.length;i++)
a[i] = Integer.parseInt(in.readLine());
//inputting key
System.out.print("\n\n\tEnter Key :");
key = Integer.parseInt(in.readLine());
//searching for elements
while(st<=la)
{
mid = (st+la)/2;
if(a[mid]==key)
{
d = 0;break;
}
if(a[mid]>key)
la = mid-1;
if(a[mid]<key)
st = mid+1;
}
//displaying whether key is present or not
if(d==0)
System.out.print("\n\n\tKey Occur at : "+(mid+1));
else
System.out.print("\n\n\tKey doesn't occur : ");
}
}

Output Of Program

/*
Enter size of array : 3
Start Entering elements :
1
2
3
Enter Key :1
Key Occur at : 1
*/

//to search an element using linear searching

PROGRAM

import java.io.*;
class linearsearch
{
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting size & elements
System.out.print("\n\n\tEnter size : ");
int i,a[] = new int[Integer.parseInt(in.readLine())];
System.out.print("\n\tStart Entering elements :\n");
for(i=0;i<a.length;i++)
a[i] = Integer.parseInt(in.readLine());
//inputting key
System.out.print("\n\tEnter Key :");
int key = Integer.parseInt(in.readLine());
//searching for elements
for(i=0;i<a.length;i++)
if(a[i]==key)
{
System.out.println("\n\tKey at position : "+(i+1));
System.exit(0);
}
System.out.print("\n\tKey doesn't occur");
}
}

Output Of Program

/*
Enter size : 3
Start Entering elements :
3
2
1
Enter Key :4
Key doesn't occur
*/

//to display magical matrix of given size

PROGRAM

import java.io.*;
class magicalodd
{
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting size
System.out.print("\n\tEnter size : ");
int n = Integer.parseInt(in.readLine());
int a[][] = new int[n][n],i,r=0,c=n/2,r1,c1;
//genarating magical array
for(i=1;i<=n*n;i++)
{
a[r][c] = i;
r1=r;c1 = c;
r--;
c++;
if(r==-1)
r = n-1;
if(c>n-1)
c = 0;
if(a[r][c]!=0)
{
r = r1;
c = c1;
r++;
if(r>n-1)
r = 0;
}
}
//displaying magical matrix
System.out.println("\n\tMAGICAL MATRIX : ");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
System.out.print("\t"+a[r][c]);
System.out.println();
}
}
}

Output Of Program

/*
Enter size : 3
MAGICAL MATRIX :
8 1 6
3 5 7
4 9 2
*/

//to arrange elements so that largest in centre and so on.

PROGRAM

import java.io.*;
class middlelargest
{
public static void main(String args[])throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
//inputting size & elements
System.out.print("Enter size : ");
int n = Integer.parseInt(in.readLine());
System.out.println("Start Entering Elements :");
int arr[] = new int[n],i,j,k,mid,pos=0,max,kmax;
for(i=0;i<n;i++)
{
//arr[i] = Integer.parseInt(in.readLine());
arr[i] = (int)(Math.random()*100);
}
//displaying array
System.out.print("Initial list : ");
for(i=0;i<n;i++)
System.out.print(arr[i]+"\t");
mid=n/2;
if(n%2==1)
mid++;
mid--;
//sorting array
for(i=0;i<n-1;i++)
{
max = arr[0];
kmax = 0;
for(j=0;j<n;j++)
if(j<=mid-pos-i%2 || j>mid+pos)
if(arr[j]>max)
{
max = arr[j];
kmax = j;
}
if(i%2==1)
{
pos++;
k = arr[mid+pos];
arr[mid+pos] = max;
arr[kmax] = k;
}
else
{
k = arr[mid-pos];
arr[mid-pos] = max;
arr[kmax] = k;
}
}

//displaying array
System.out.print("\nArranged List is : ");
for(i=0;i<n;i++)
System.out.print(arr[i]+"\t");
}
}

Output Of Program

/*
Enter size : 3
Start Entering Elements :
Initial list : 6 24 42
Arranged List is : 6 42 24
*/

//break the amount into minimum posssible denominations

PROGRAM
import java.io.*;
class ATM
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting amount
System.out.print("Enter amount to change : ");
int n = Integer.parseInt(in.readLine()),d=0,k=0,i=0;
int a[] = {1000,500,100,50,20,10,5,2,1};
//breaking in denominations
while(n>0)
{
while(n>a[i]||d!=0&&n>=a[i])
{
n -= a[i];
k++;
d++;
}
if(k>0)
System.out.println("Rs. "+a[i]+"--->"+k+" notes");
k=0;
i++;
}
}
}

Output Of Program

/*
Enter amount to change : 5
Rs. 2--->2 notes
Rs. 1--->1 notes
*/

//to convert binary no. into its hexadecimal equivalent

PROGRAM

import java.io.*;
class binary2hexa
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting no
System.out.print("\n\n\tEnter binary no. : ");
String s = in.readLine(),ans="",sym="0123456789ABCDEF";
String hexa[] = {"0000","0001","0010","0011","0100","0101","0110"
,"0111","1000","1001","1010","1011","1100","1101","1110","1111"};
while(s.length()%4!=0)
s = '0'+s;
//converting binary to hexadecimal
while(s.length()!=0)
{
for(int j=0;j<16;j++)
{
if(s.substring(0,4).equals(hexa[j]))
ans = ans+sym.charAt(j);
}
s = s.substring(4);
}
//displaying answer
System.out.print("\n\n\tHexa equivalent : "+ans);
}
}

Output Of Program

/*
Enter binary no. : 1010
Hexa equivalent : A
*/

//to add two different base nos.

PROGRAM

import java.io.*;
class neaddne
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
String sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//inputting no
System.out.print("Enter a no. : ");
String s1 = in.readLine();
//inputting base
System.out.print("Enter the base of the no. : ");
int b1 = Integer.parseInt(in.readLine());
//inputting no
System.out.print("Enter a no. : ");
String s2 = in.readLine();
//inputting base
System.out.print("Enter the base of the no. : ");
int b2 = Integer.parseInt(in.readLine());
String ans="";
int n1=0,n2=0,n,l=0;
//converting in decimal
for(;l<s1.length();n1=n1*b1+sym.indexOf(s1.charAt(l)),l++);
for(l=0;l<s2.length();n2=n2*b2+sym.indexOf(s2.charAt(l)),l++);
n = n1+n2;
System.out.println("Answer in Decimal : "+n);
System.out.print("Enter the base to convert Answer : ");
int b = Integer.parseInt(in.readLine());
//converting into inputted base
for(;n>0;ans=sym.charAt(n%b)+ans,n/=b);
System.out.print(ans);
}
}

Output Of Program

/*
Enter a no. : ROHIT
Enter the base of the no. : 35
Enter a no. : 1111001
Enter the base of the no. : 2
Answer in Decimal : 41567480
Enter the base in which u want to convert Answer : 2
10011110100100010011111000
*/

//to add n days to particular date and display new date

PROGRAM

import java.io.*;
class addndays
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int w[] = new int[3];
int mon[] ={31,31,28,31,30,31,30,31,31,30,31,30,31};
//inputting details of date
System.out.print("Enter date : ");
w[0] = Integer.parseInt(in.readLine());
System.out.print("Enter month : ");
w[1] = Integer.parseInt(in.readLine());
System.out.print("Enter year : ");
w[2] = Integer.parseInt(in.readLine());
//inputting days to add
System.out.print("Enter no. of days to add : ");
int n = Integer.parseInt(in.readLine());
if(w[2]%4==0)
mon[2] = 29;
//checking whether date is valid
if(valid(w[0],w[1],w[2])==false)
{
System.out.println("Invalid Date");
System.exit(0);
}
else
{
for(;n>0;n--)
date(w);
}
//displaying result
System.out.println("Next Date is : "+w[0]+":"+w[1]+":"+w[2]) ;
}
//returning whether date is valid or not
public static boolean valid(int dd,int mm,int yy)
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%400==0 || yy%4==0 && yy%100!=0)
mon[2] = 29;
if(dd>0 && dd<=mon[mm] && mm>0 && mm<13 && yy>0)
return true;
else
return false;
}
//adding n days to date
public static void date(int w[])
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
w[0]++;
if(w[0]>mon[w[1]])
{
w[0] = 1;
w[1]++;
}
if(w[1]>12)
{
w[1] = 1;
w[2]++;
}
}
}

Output Of Program

/*
Enter date : 31
Enter month : 7
Enter year : 2007
Enter no. of days to add : 14
Next Date is : 14:8:2007
*/

//to display day on given dates

PROGRAM

import java.io.*;
class dayondate
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int mon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//inputting details about date
System.out.print("Enter date : ");
int dd = Integer.parseInt(in.readLine());
System.out.print("Enter month : ");
int mm = Integer.parseInt(in.readLine());
System.out.print("Enter year : ");
int yy = Integer.parseInt(in.readLine()),odd=0,i;
//checking for validity
if(yy%4==0 && yy%100!=0 || yy%400==0)
mon[2] = 29;
if(valid(dd,mm,yy)==false)
{
System.out.print("Invalid date");
System.exit(0);
}
String list[] = {"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
//calculating no of odd days
for(i=1;i<yy;i++)
odd += i%4==0 && i%100!=0 || i%400==0?366:365;
for(i=1;i<mm;i++)
odd += mon[i];
odd = (dd+odd)%7;
//displaying day
System.out.println("Day is : "+list[odd]);
}
//returning whether date is valid or not
public static boolean valid(int dd,int mm,int yy)
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%400==0 && yy%100==0 || yy%4==0)
mon[2] = 29;
if(mm>12)
{
System.out.print("Date is Invalid");
System.exit(0);
}
if(dd>0 && dd<=mon[mm] && mm>0 && mm<13 && yy>0)
return true;
else
return false;
}
}

Output Of Program

/*
Enter date : 14
Enter month : 8
Enter year : 1989
Day is : Monday
*/

//to display calendar of inputed month and year

PROGRAM

import java.io.*;
class calen
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int mon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//inputting details about date
System.out.print("Enter month : ");
int mm = Integer.parseInt(in.readLine());
System.out.print("Enter year : ");
int yy = Integer.parseInt(in.readLine());
if(yy%4==0 && yy%100!=0 || yy%400==0)
mon[2]=29;
if(valid(mm,yy)==false)
{
System.out.print("Invalid date");
System.exit(0);
}
int odd=1,i;
//calculating no of odd days
for(i=1;i<yy;i++)
odd += i%4==0 && i%100!=0 || i%400==0?366:365;
for(i=1;i<mm;i++)
odd += mon[i];
odd = odd%7;
System.out.print("\n\nCALENDAR : \n\n");
System.out.print("SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n\n");
for(i=0;i<odd;i++)
System.out.print("\t");
//displaying calendar
for(i=1;i<=mon[mm];i++)
{
System.out.print(i+"\t");
if((i+odd)%7==0)
System.out.println();
}
}
//returning whether date is valid or not
public static boolean valid(int mm,int yy)
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%400==0 && yy%100==0 || yy%4==0)
mon[2]=29;
if(mm>0 && mm<13 && yy>0)
return true;
else
return false;
}
}

Output Of Program

/*
Enter month : 8
Enter year : 1989

CALENDAR :

SUN MON TUE WED THU FRI SAT

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
*/

/*
Define a 'Binomial' class with following specification :-

Data Members :-
x : to store value of x in (x + a)^n
a : to store value of a in (x + a)^n
n : to store limit

Member Functions :-
void readdata() : To input x,a,n
long factorial(int) : To return factorial of n
long nCr(int,int) : To return nCr
void sumseries() : To return value of (x+a)^n
*/

PROGRAM

import java.io.*;
class Binomial
{
int x,a,n;
void readdata()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter limit : ");
n = Integer.parseInt(in.readLine());
System.out.print("Enter value of x :");
x = Integer.parseInt(in.readLine());
System.out.print("Enter value of a :");
a = Integer.parseInt(in.readLine());
}
long factorial(int b)
{
if(b==0 || b==1)
return 1;
else return b*factorial(b-1);
}
long nCr(int l,int r)
{
return factorial(l)/(factorial(r)*factorial(l-r));
}
void sumseries()
{
long sum=0;
for(int i=0;i<=n;i++)
{
sum += nCr(n,i)*(long)Math.pow(x,n-i)*(int)Math.pow(a,i);
}
System.out.println("Sum of "+"("+x+"+"+a+")"+"^"+n+" is : "+sum);
}
}
class Bin
{
public static void main(String args[])throws Exception
{
Binomial a=new Binomial();
a.readdata();
a.sumseries();
}
}

Output Of Program

/*
Enter limit : 6
Enter value of x :2
Enter value of a :1
Sum of (2+1)^6 is : 729
*/
Define a class 'Student' with following classification :-

Data Member :-
rollno : an integer to store Roll# of student
name : a String to store Name of student
marks : an array of integers to store marks of Student

Member Function :-
void readStudent() : to input Data about Student
void showStudent() : to display Data of Student
double getPercentage() : to return percentage of a Student
Using above class write a program to input data about 3 Students
and display data of the student with highest percentage.
*/
PROGRAM

import java.io.*;
class Student
{
int rollno,marks[] = new int[5];String name = new String();
void showStudent()
{
System.out.println("Student Data");
System.out.println("Roll # :"+rollno+"\nName :"+name);
System.out.println("Marks :");
for(int i=0;i<5;i++)
{
System.out.print(marks[i]+"\t");
}
System.out.println("\nPercentage :"+getPercentage());
}
void readStudent() throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input Student Data");
System.out.print("Roll # :");
rollno = Integer.parseInt(in.readLine());
System.out.print("Name :");
name = in.readLine();
System.out.println("Marks : ");
for(int i=0;i<5;i++)
{
System.out.println("Enter Marks :");
marks[i] = Integer.parseInt(in.readLine());
}
}
double getPercentage()
{
double sum=0;
for(int i=0;i<5;i++)
sum+=marks[i];
return sum/5;
}
}
class a
{
public static void main(String args[]) throws Exception
{
Student s[] = new Student[3],max;
for(int i=0;i<3;i++)
{
s[i] = new Student();
s[i].readStudent();
}
max = s[0];
for(int i=1;i<3;i++)
{
if(max.getPercentage()<s[i].getPercentage())
` max = s[i];
}
max.showStudent();
}
}

Output Of Program

/*
Roll # :1
Name :ROHIT ANAND
Marks :
Enter Marks :
88
Enter Marks :
90
Enter Marks :
94
Enter Marks :
96
Enter Marks :
100
Input Student Data
Roll # :2
Name :ROHIT
Marks :
Enter Marks :
88
Enter Marks :
90
Enter Marks :
94
Enter Marks :
96
Enter Marks :
100
Input Student Data
Roll # :3
Name :ANAND
Marks :
Enter Marks :
88
Enter Marks :
90
Enter Marks :
94
Enter Marks :
96
Enter Marks :
100
Student Data
Roll # :1
Name :ROHIT ANAND
Marks :
88 90 94 96 100
Percentage :93.6
*/

Define a class ' taxpayer ' with following specifications :-

Data Members :-
pan : to store personal account no.
name : to store the name of a person
income : to store annual income
taxableinc : to store tha taxable income
tax : to store the tax is calculated

Member Functions :-
inputdata() : to store data
displaydata() : to display data
computetax() : to compute tax
*/

PROGRAM

import java.io.*;
class taxpayer
{
int pan;double taxableinc,tax=0;String name="";
void inputdata()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter name : ");
name = in.readLine();
System.out.print("Enter pan no. : ");
pan = Integer.parseInt(in.readLine());
System.out.print("Enter annual taxable income : ");
taxableinc = Double.parseDouble(in.readLine());
computetax();
displaydata();
}
void displaydata()
{
System.out.println("\nTaxpayer data");
System.out.println("Name : "+name);
System.out.println("Pan no. : "+pan);
System.out.println("Taxable Income : "+taxableinc);
System.out.println("Tax : "+tax);
}
void computetax()
{
if(taxableinc>60000&&taxableinc<=150000)
tax = taxableinc*0.05;
else if(taxableinc>150000&&taxableinc<=500000)
tax = taxableinc*0.10;
else if(taxableinc>500000)
tax = taxableinc*0.15;
}
}
class tax
{
public static void main(String args[])throws Exception
{
taxpayer a=new taxpayer();
a.inputdata();
}
}

Output Of Program

/*
Enter name : Rohit Anand
Enter pan no. : 4
Enter annual taxable income : 1481990

Taxpayer data
Name : Rohit Anand
Pan no. : 4
Taxable Income : 1481990.0
Tax : 222298.5
*/

/*
Define class 'Vector' with following specifications :-

Data Members :-
a : to store coefficient of i
b : to store coefficient of j
c : to store coefficient of k

Member Functions :-
Vector(int a,int b,int c) : Parameterised Constructor
void showVector() : to display vectors
Vector CrossProduct(Vector z) : to calculate and return cross product
int dotproduct(Vector z) : to calculate and return dot product
Vector add(Vector z) : to calculate and return sum of vectors
*/

PROGRAM

class Vector
{
int a,b,c;
Vector(int a,int b,int c)
{
this.a=a;this.b=b;this.c=c;
}
void showVector()
{
System.out.println(a+"i + "+b+"j + "+c+"k ");
}
int dotproduct(Vector z)
{
return(a+z.a+b+z.b+c+z.c);
}
Vector add(Vector z)
{
return new Vector(a+z.a,b+z.b,c+z.c);
}
Vector crossproduct(Vector z)
{
return new Vector(b*z.c-z.b*c,c*z.a-a*z.c,a*z.b-b*z.a);
}
}
class vect
{
public static void main(String args[])
{
Vector a=new Vector(1,2,3),b=new Vector(3,4,5);
a.showVector();b.showVector();
System.out.println("Dot Product : "+a.dotproduct(b));
System.out.print("ADD Vector : ");
Vector z=a.add(b);z.showVector();
System.out.print("Cross Product : ");
z=a.crossproduct(b);z.showVector();
}
}

Output Of Program
/*
1i + 2j + 3k
3i + 4j + 5k
Dot Product : 18
ADD Vector : 4i + 6j + 8k
Cross Product : -2i + 4j + -2k
*/

Define a class 'Set' with following specification :-

Data Member :-
int arr[] : to store elements
int size : to store size

Member Function :-
Set(int s) : parameterised constructor
void readset() : to input values in set
void displayset() : to display set elements
int isPresent(int n) : to return 1 if n is present in Set
int isProperSubset(Set a) : to return 1 if 'a' is proper subset
of calling set
Set Difference(Set a) : to return set containing difference
of set 'a' & calling object
Set SymmetricDifference(Set a) : to return set containing sym. diff of
set 'a' & calling object
*/

PROGRAM

import java.io.*;
class Set
{
int arr[],size;
Set(int s)
{
size = s;
arr = new int[s];
}
void readset()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Start Entering Elements : ");
for(int i=0;i<size;i++)
{
//arr[i] = Integer.parseInt(in.readLine());
arr[i] = (int)(Math.random()*10);
}
}
void displayset()
{
for(int i=0;i<size;i++)
System.out.print(arr[i]+"\t");
}
int isPresent(int n)
{
for(int i=0;i<size;i++)
if(arr[i]==n)
return 1;
return 0;
}
int isProperSubset(Set a)
{
int c=0,i;
for(i=0;i<size;i++)
if(isPresent(a.arr[i])==1)
c++;
if(c<size && c==a.size)
return 1;
return 0;
}
Set Difference(Set a)
{
int i,c=0;
Set t = new Set(size);
for(i=0;i<size;i++)
if(a.isPresent(arr[i])!=1)
t.arr[c++] = arr[i];
t.size=c;
return t;
}
Set SymmetricDifference(Set a)
{
int i,c=0;
Set t = new Set(size*size);
Set t1 = a.Difference(this),t2 = Difference(a);
for(i=0;i<t1.size;i++)
t.arr[c++] = t1.arr[i];
for(i=0;i<t2.size;i++)
t.arr[c++] = t2.arr[i];
t.size = c;
return t;
}
}
class settheory
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter size for Set A : ");
Set a = new Set(Integer.parseInt(in.readLine()));
System.out.print("Enter size for Set B : ");
Set b = new Set(Integer.parseInt(in.readLine()));
a.readset();
b.readset();
System.out.println("Set A :");a.displayset();
System.out.println("\nSet B :");b.displayset();
System.out.println("\nDifference of A & B :");
a.Difference(b).displayset();
System.out.println("\nSymmetric Difference of A & B :");
a.SymmetricDifference(b).displayset();
}
}

Output Of Program

/*
Enter size for Set A : 2
Enter size for Set B : 2
Start Entering Elements :
Start Entering Elements :
Set A :
6 3
Set B :
8 3
Difference of A & B :
6
Symmetric Difference of A & B :
8 6
*/

/*
Define a class 'Saddle' with following specification :-

Data Member :-
arr[][] : to store numbers
row,col : to store dimensions

Member Function :-
void readArray() : to input values in set
int highestInRow(int eleno) : to return 1 if eleno is highest in row
int lowInColumn(int eleno) : to return 1 if eleno is lowest in
column
int isSaddle(int eleno) : to return 1 if it is saddle point
void displaySaddle() : to display all saddle point
*/

PROGRAM

import java.io.*;
class Saddle
{
int row,col,arr[][];
void readArray() throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no. of rows : ");
row = Integer.parseInt(in.readLine());
System.out.print("Enter no. of columns : ");
col = Integer.parseInt(in.readLine());
System.out.println("Start Entering elements : ");
arr = new int[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
arr[i][j] = Integer.parseInt(in.readLine());
}
}
int highestInRow(int eleno)
{
int i,r=eleno/col,c=eleno%col,p=0;
for(i=0;i<col;i++)
{
if(arr[r][p]<arr[r][i])
p = i;
}
if(p==c)
return 1;
return 0;
}
int lowestInColumn(int eleno)
{
int i,r=eleno/col,c=eleno%col,p=0;
for(i=0;i<row;i++)
{
if(arr[p][c]>arr[i][c])
p = i;
}
if(p==r)
return 1;
return 0;
}

int isSaddle(int eleno)


{
if(highestInRow(eleno)+lowestInColumn(eleno)==2)
return 1;
return 0;
}
void displaySaddle()
{
int f=0,i;
for(i=0;i<row*col;i++)
{
if(isSaddle(i)==1)
{
System.out.print("Saddle Point :"+arr[i/col][i%col]);
f = 1;
}
}
if(f==0)
System.out.print("NO saddle point found");
}
}
class saddlepoint
{
public static void main(String args[])throws Exception
{
Saddle a = new Saddle();
a.readArray();
a.displaySaddle();
}
}

Output Of Program

/*
Enter no. of rows : 3
Enter no. of columns : 3
Start Entering elements :
9
8
7
6
5
4
3
2
1
Saddle Point :3
*/

//to compute matrix multiplication

PROGRAM

import java.io.*;
class matrixmulti
{
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
//inputting details about the array
System.out.println("ARRAY 1 :\nEnter no. of rows & columns : ");
int r = Integer.parseInt(in.readLine());
int c = Integer.parseInt(in.readLine());
System.out.println("\nARRAY 2 :\nEnter no. of rows & columns : ");
int m = Integer.parseInt(in.readLine());
int n = Integer.parseInt(in.readLine());
int i,j,k,a[][] = new int[r][c];
int b[][] = new int[m][n];
int arr[][] = new int[r][n];
//inputting elements in array
System.out.println("Start Entering elements of array 1 : ");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j] = Integer.parseInt(in.readLine());
System.out.println("Start Entering elements of array 2 : ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j] = Integer.parseInt(in.readLine());
//displaying array
System.out.println("\nARRAY 1 : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println("\nARRAY 2 : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
System.out.print(b[i][j]+"\t");
System.out.println();
}
//computing matrix multiplication
if(c==m)
{
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
{
arr[i][j] = 0;
for(k=0;k<c;k++)
arr[i][j] = arr[i][j]+a[i][k]*b[k][j];
}
}
//displaying array
System.out.println("\nRESULTANT ARRAY : ");
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
System.out.print(arr[i][j]+"\t");
System.out.println();
}
}
else
System.out.print("Matrix Mulitiplication not possible : ");
}
}

Output Of Program

/*
ARRAY 1 :
Enter no. of rows & columns :
2
3

ARRAY 2 :
Enter no. of rows & columns :
3
2

Start Entering elements of array 1 :


Start Entering elements of array 2 :

ARRAY 1 :
6 0 7
0 4 2

ARRAY 2 :
2 7
7 3
4 3

RESULTANT ARRAY :
40 63
36 18
*/

/*
Define a class 'Encrypt' wirh following specifications :-

Data Members :-
str : to store string

Member Functions :-
void readdata() : to input string
void encrypt() : to encrypt string by word length
void decrypt() : to decrypt string by word length
void display() : to display string
*/

PROGRAM

import java.io.*;
class Encrypt
{
String str="";
void readdata()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string : ");
str = in.readLine()+" ";
System.out.print("Original String : ");
display(str);
System.out.print("Encrypt String : ");
encrypt();
System.out.print("Decrypt String : ");
decrypt();
}
void encrypt()
{
String s="",s1="";int i=0,j=0,r,k=0;
for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf(" ",i+1))
{
s = str.substring(j,i);j=i+1;
for(k=0;k<s.length();k++)
{
r = s.charAt(k)+s.length();
if(r>90&&s.charAt(k)<='Z')
r = r-26;
if(r>122&&s.charAt(k)<='z')
r = r-26;
s1 = s1+(char)r;
}
s1 = s1+" ";
}
display(s1);
}
void decrypt()
{
String s="",s1="";int i=0,j=0,r,k=0;
for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf(" ",i+1))
{
s = str.substring(j,i);j=i+1;
for(k=0;k<s.length();k++)
{
r=s.charAt(k)-s.length();
if(r<65&&s.charAt(k)>='A')
r = r+26;
if(r<97&&s.charAt(k)>='a')
r = r+26;
s1 = s1+(char)r;
}
s1 = s1+" ";
}
display(s1);
}
void display(String st)
{
System.out.println(st);
}
}
class code
{
public static void main(String args[])throws Exception
{
Encrypt a=new Encrypt();
a.readdata();
}
}

Output Of Program

/*
Enter a string : A a Z z
Original String : A a Z z
Encrypt String : B b A a
Decrypt String : Z z Y y
*/

/*
Define a class 'Mean' with following specification :-

Data Member :-
int data[] : an array to store readings
int low : to store lower limit of lowest class
int high : to store upper limit of highest class
int n : to store number of readings
int size : to store class size

Member Function :-
void readData() : to input details
int getFrequency(int start,int end) : to give frequency of a class
double getMean() : to return mean of data
void showFrequencyTable() : to display data in tabular form
*/

PROGRAM

import java.io.*;
class Mean
{
private int data[] = new int[100],low,high,n,size;
void readData() throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\tEnter Number Of Entry :");
n = Integer.parseInt(in.readLine());
System.out.print("\n\tEnter Lower Limit :");
low = Integer.parseInt(in.readLine());
System.out.print("\n\tEnter Upper Limit :");
high = Integer.parseInt(in.readLine());
System.out.print("\n\tEnter Size :");
size = Integer.parseInt(in.readLine());
if((high-low)%size!=0)
System.exit(0);
for(int i=0;i<n;i++)
{
System.out.print("\n\tEnter Observation :");
data[i] = Integer.parseInt(in.readLine());
}
}
int getFrequency(int start,int end)
{
int f = 0,i;
for(i=0;i<n;i++)
if(data[i]>=start && data[i]<end)
f++;
return f;
}
double getMean()
{
int l = low,i;
double fx=0,f=0;
for(i=0;i<(high-low)/size;i++,l+=size)
{
fx += getFrequency(l,l+size)*(l+(double)size/2);
f += getFrequency(l,l+size);
}
return (fx/f);
}

void showFrequencyTable()
{
System.out.println("\nClass \tx\tf\tfx");
int l = low;
for(int i=0;i<(high-low)/size;i++,l+=size)
{
System.out.print(l+" - "+(l+size)+"\t"+
(double)(l+(double)size/2)+" \t");
System.out.print(getFrequency(l,l+size)+"\t");
System.out.print(getFrequency(l,l+size)*
(l+(double)size/2));
System.out.println();
}
System.out.print("Mean Is :"+getMean());
}
}
class stat
{
public static void main(String args[]) throws Exception
{
Mean a = new Mean();
a.readData();
a.showFrequencyTable();
System.out.print("\n");
}
}

Output Of Program

/*
Enter Number Of Entry :3

Enter Lower Limit :0

Enter Upper Limit :9


Enter Size :3

Enter Observation :1

Enter Observation :4

Enter Observation :7

Class x f fx
0-3 1.5 1 1.5
3-6 4.5 1 4.5
6-9 7.5 1 7.5
Mean Is : 4.5
*//*
Define class 'MyNumber' with following specifications :-

Data Members :-
n - to store no

Member Functions :-
readdata() : to input n
boolean palindrome() : to return true if no. is palindrome otherwise
false
boolean automorphic(): to return true if no. is automorphic otherwise
false
boolean armstrong() : to return true if no. is armstrong otherwise
false
boolean magical() : to return true if no. is magical otherwise
false
voidshowall() : to display whether the no. one of the above
*/

PROGRAM
import java.io.*;
class MyNumber
{
int n;
void readdata()throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no. : ");
n = Integer.parseInt(in.readLine());

}
boolean palindrome()
{
int a=n,b=0;
for(;a>0;b=b*10+a%10,a/=10);
if(n==b)
return true;
else
return false;
}
boolean magical()
{
int a=n;
for(;a>9;a=a%10+a/10);
if(a==1)
return true;
else
return false;
}
boolean automorphic()
{
int a=n,b=n,j=10,k=0;
for(;a>0;k=(b*b)%j,j*=10,a/=10);
if(k==b)
return true;
else
return false;
}
boolean armstrong()
{
int a=n,c=0;
for(;a>0;c+=(int)Math.pow(n%10,3),a/=10);

if(n==c)
return true;
else
return false;
}
void showall()
{
if(magical())
System.out.println("no. is magical : ");
if(palindrome())
System.out.println("no. is palindrome : ");
if(automorphic())
System.out.println("no. is automorphic : ");
if(armstrong())
System.out.println("no. is armstrong : ");
}
}
class number
{
public static void main(String args[])throws Exception
{
MyNumber a=new MyNumber();
a.readdata();
a.showall();
}
}

Output Of Program

/*
Enter no. : 1
no. is magical :
no. is palindrome :
no. is automorphic :
no. is armstrong :
*/

You might also like