You are on page 1of 20

Program 1(a):

import java.io.*;

class Prog1
{
public static void main(String[] args)
{
int reg=360;
String name="Aseem garg";
char gender='m';
double cgpa=10;
String add="CHANDIGARH";

System.out.println("Register number: "+reg);


System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("CGPA: "+cgpa);
System.out.println("Address: "+add);
}
}
Program 1(b):

import java.io.*;
import java.math.*;

class Prog2
{
public static void main(String[] args)
{
int a=14,b=9,c=-2;
double d, r1,r2;

System.out.println("The quadratic eqaution is: "+a+"x^2+" +b +"x" +c);


d= (b*b)-(4*a*c);
if (d<0)
System.out.println("No real solution exists");

else
{
r1=(-b+Math.sqrt(d))/2;
r2=(-b-Math.sqrt(d))/2;
System.out.println("The roots are: "+r1+"and "+r2);
}

}
}
Program 1(c):

import java.io.*;

class Prog3
{
public static void main(String[] args)
{
char []a={'b','o','a','t'};
int i,k=6;
for(i=0;i<4;i++)
{
if(a[i]!=a[k])
{
System.out.println("Not a palindrome");
break;
}
k--;
}

if(i==4)
System.out.println("Palindrome");
}
}
Program 1(d):
import java.io.*;

class Prog4
{
public static void main(String[] args)
{
int n=10,a=0,b=1,c;
System.out.print("Fibonacci Series: " +a +" "+b);

for(int i=0;i<n;i++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
}
Program 1(e):

import java.io.*;

class Prog5
{
public static void main(String[] args)
{
int i,j,k; double f,sum=0;
for(i=1;i<=2;i++)
{
f=1;
for(j=1;j<=i;j++)
f*=j;
sum=sum+(1/f);
}
System.out.println("Value of e: "+sum);
}
}
Program 2(a):
import java.io.*;
class A
{ int a[][]=new int[3][3];
public void read1() throws IOException
{
DataInputStream d = new DataInputStream(System.in);
System.out.println("enter first matrix");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=Integer.parseInt(d.readLine());
}
}
class B
{ int b[][]=new int[3][3];
public void read2() throws IOException
{
DataInputStream d = new DataInputStream(System.in);
System.out.println("enter second matrix");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
b[i][j]=Integer.parseInt(d.readLine());
}
}
class C
{ int c[][]=new int[3][3];
int d[][]=new int[3][3];
public void add(A a , B b)
{ for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=a.a[i][j]+b.b[i][j];
}
public void mul(A a, B b)
{ for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
d[i][j]=d[i][j]+a.a[i][k]*b.b[k][j];
}
public void display()
{ System.out.println("Sum of the two matrices is:");
for(int i=0;i<3;i++)
{for(int j=0;j<3;j++)
System.out.print(c[i][j]+" ");
System.out.println();}
System.out.println("Product of the two matrices is:");
for(int i=0;i<3;i++)
{for(int j=0;j<3;j++)
System.out.print(d[i][j]+" ");
System.out.println();}
}
}
class p2a
{ public static void main(String[] args)throws IOException
{ A obj1 = new A();
B obj2 = new B();
obj1.read1();
obj2.read2();
C obj3 = new C();
obj3.add(obj1 , obj2);
obj3.mul(obj1 , obj2);
obj3.display();
}
}
Program 2(b):

import java.io.*;
class area
{ area(int s)
{ int area1;
area1 = s*s;
System.out.println("Area of square is :"+area1); }
area(int l , int b)
{ int area2 = l*b;
System.out.println("Area of rectangle is : "+area2); }
void aream(int r)
{ double area3;
area3=r*3.14*r;
System.out.println("Area of circle is : "+area3); }
void aream(int base , int h )
{ double area4;
area4 = 0.5*base*h;
System.out.println("Area of triangle is : "+area4); }
}
class p2b{
public static void main(String[] args)throws IOException
{ DataInputStream d = new DataInputStream(System.in);
System.out.print("Enter side of square:");
int s1=Integer.parseInt(d.readLine());
area a = new area(s1);
System.out.print("Enter length and breadth of rectangle:");
int l1= Integer.parseInt(d.readLine());
int b1= Integer.parseInt(d.readLine());
area a1 = new area(l1,b1);
System.out.print("Enter radius of circle:");
int r1= Integer.parseInt(d.readLine());
area.aream(r1);
System.out.print("Enter base and height of triangle:");
int base1= Integer.parseInt(d.readLine());
int h1= Integer.parseInt(d.readLine());
area.aream(base1,h1); }
}
Program 3(a):

import java.io.*;
class complex
{ int real,img;;
void get()throws IOException
{ DataInputStream d = new DataInputStream(System.in);
System.out.println("Enter real and imaginary part :");
real=Integer.parseInt(d.readLine());
img=Integer.parseInt(d.readLine());
}
complex add(complex a,complex b)
{ complex c = new complex();
c.real=a.real+b.real;
c.img=a.img+b.img;
return c; }
complex()
{ real=0;
img=0; }
complex(int a,int b)
{ real=a;
img=b; }
void display()
{ System.out.println("the sum is"+real+"+"+img+"i"); }
protected void finalize()
{ System.out.println("the end"); }
public static void main(String[] args)throws IOException
{ int x,y;
DataInputStream d = new DataInputStream(System.in);
System.out.println("enter real and imaginary part");
x=Integer.parseInt(d.readLine());
y=Integer.parseInt(d.readLine());
complex a = new complex(x,y);
complex b = new complex();
b.get();
complex c = new complex();
c=complex.add(a,b); c.display();
}
}
Program 3(b):

import java.io.*;
class outer
{
int a,sum;
public void read()throws IOException
{ System.out.print("Enter first number :");
DataInputstream d = new DataInputstream(System.in);
a=Integer.parseInt(d.readLine());
}
class inner
{ int b;
public void read1()throws IOException
{ System.out.print("Enter second number : ");
DataInputstream d1 = new DataInputstream(System.in);
b=Integer.parseInt(d1.readLine());
}
}
void add()throws IOException
{ inner i = new inner();
i.read1();
sum=a+i.b;
System.out.println("Sum is :"+sum);
}
}
class p3b
{
public static void main(String[] args)throws IOException
{ outer obj = new outer();
obj.read();
obj.add();
}
}
Program 4(a):

import java.io.*;
class p4a
{
public static void main(String[] args)throws IOException
{
int i,j=0,k=0,l=0;
char a[]=new char[100];
char b[]=new char[100];
char c[]=new char[100];
FileInputStream f = new FileInputStream("myfile.java");
while((i=f.read())!=-1)
{
if ((char)i>='1'&&(char)i<='9')
{ b[k]=(char)i; k++; }
else if((char)i>='a'&&(char)i<='z')
{ c[l]=(char)i; l++; }
else
{ a[j]=(char)i; j++; }
}
f.close();
System.out.println("Numbers are :");
for(int x=0;x<k;x++)
System.out.print(b[x]+" ");
System.out.println();
System.out.println("Letters are :");
for(int y=0;y<l;y++)
System.out.print(c[y]+" ");
System.out.println();
System.out.println("Special characters are :");
for(int z=0;z<j;z++)
System.out.print(a[z]+" ");
}
}
Program 4(b):

import java.io.*;
class p4b
{
public static void main(String[] args)throws IOException
{
int i,j;
FileInputStream fin = new FileInputStream("myfile.java");
FileOutputStream fout = new FileOutputStream("myfile1.java");
while((i=fin.read())!=-1)
{
fout.write(i);
}
fin.close();
FileInputStream f = new FileInputStream("myfile1.java");
System.out.println("Dsiplaying contents of the file: ");
while((j=f.read())!=-1)
{
System.out.print((char)j+" ");
}
f.close();
}
}
Program 5(a):

import java.io.*;
class p5a
{
public static void main(String[] args) throws IOException
{ FileInputStream f1= new FileInputStream("history.txt");
int size=f1.available();
System.out.print("1.Displaying contents of file: ");
byte b[]=new byte[size]; f1.read(b);
String s=new String(b);
System.out.println(s);
System.out.print("2.Without white spaces: ");
for(int i=0;i<size;i++)
if(((char)b[i])!=' ')
System.out.print((char)b[i]); System.out.println();
String s1= s.replace('a','e');
System.out.println("3.Replacing 'a' with 'e': "+s1);
int i=0; char ch[]=new char[4];
while(s.charAt(i)!=' ')
{ ch[i]=s.charAt(i); i++; }
i++; s1=new String(ch); int j=0; char c[]=new char[2];
while(s.charAt(i)!=' ')
{ c[j]=s.charAt(i);j++;i++; }
String s2=new String(c);
System.out.println("4.First two words:"+s1+" "+s2);
System.out.println("Comparing the words: "+s1.compareTo(s2)); i=0;
System.out.print("5.First Sentence: ");
while(s.charAt(i)!='.')
{ System.out.print(s.charAt(i)); i++; }
System.out.println();
System.out.println("Number of characters: "+i);
System.out.println("6. Upper Case: "+s.toUpperCase()+"\nLower Case:
"+s.toLowerCase());
System.out.println("7. Last character:. and Index value: "+s.lastIndexOf('.'));
f1.close();
}
}
Program 5(b):

import java.io.*;
class p5b
{
public static void main(String[] args) throws IOException
{
System.out.println("Using StringBuffer class.");
StringBuffer sb1= new StringBuffer("Computer");
StringBuffer sb2= new StringBuffer("Science and Engineering");
System.out.println("1.First string: "+sb1+" Length: "+sb1.length()+" Capacity:
"+sb1.capacity());
System.out.println("Second string: "+sb2+"\nLength: "+sb2.length()+" Capacity:
"+sb2.capacity());
sb1.append(sb2);
System.out.println("Appended string: "+sb1);
sb2.replace(12,23,"Technology");
System.out.println("2.After replacing: "+sb2);
sb2.replace(8,11,"&");
System.out.println("3.After replacing String2: "+sb2+"\n4.Length: "+sb2.length()
+" Capacity: "+sb2.capacity());
System.out.println("5.String1: Length: "+sb1.length()+" Capacity:
"+sb1.capacity());
sb1.reverse();
System.out.println("6.After Reversing: "+sb1);
System.out.println("7.Substring: "+sb2.substring(0,7));
System.out.println("8.Substring from a given position: "+sb2.substring(4));
System.out.println("9.Last char of string1: "+sb1.charAt(sb1.length()-1));
System.out.println("10.Last char of string2: "+sb2.charAt(sb2.length()-1));
}
}
Program 6(a):

import java.io.*
class Bank
{string name;
int accno;
int cbal, sbal;
public static void get()throws IOException
{DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter name and account number");
name=d.readLine();
accno=Integer.parseInt(d.readLine()); }
void disp()
{System.out.println("Name: "+name);
System.out.println("Account number: "+accno); }
}
class Savings extends Bank
{void disp1()
{disp();
System.out.println("Savings balance: "+sbal); }
void deposit()throws IOException
{DataInputStream d=new DataInputStream(System.in);
System.out.println("enter amount to be deposited:");
int amt=Integer.parseInt(d.readLine());
if(amt>250000)
System.out.println("Exceeds deposit limit");
else
{sbal+=amt;
System.out.println("Deposit successful"); } }
void withdrawal()throws IOException
{DataInputStream d=new DataInputStream(System.in);
if(sbal<500)
System.out.println("Not enough balance");
else
{System.out.println("Enter amount to withdraw: ");
int amt=Integer.parseInt(d.readLine());
if(amt<100||amt>4000||amt>sbal)
System.out.println("incorrect amount");
else
{sbal-=amt;
System.out.println("done!"); }
}
}
}
class Current extends Bank
{void disp2()
{disp();
System.out.println("Current balance: "+cbal);
}
void deposit2()throws IOException
{DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter amount to deposit");
int amt=Integer.parseInt(d.readLine());
if(amt>100000)
System.out.println("Exceeds deposit limit");
else
{cbal+=amt;
System.out.println("Deposit successful");
}
}
void withdrawal2()throws IOException
{DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter amount to withdraw: ");
int amt=Integer.parseInt(d.readLine());
if(amt<100||amt>100000||amt>cbal)
System.out.println("Incorrect amount");
else
{cbal-=amt;
System.out.println("Done!");
}
}
}
class p6a
{public static void main(String[] args)throws IOException
{Savings S=new Savings();
Current C=new Current();
Bank B=new Bank();
B.get();
DataInputStream d=new DataInputStream(System.in);
C.name=B.name;
S.name=B.name;
C.accno=B.accno;
S.accno=B.accno;
int c1,c2;
do
{System.out.println("Welcome to Bank ");
System.out.println("1.Savings Account\n2.Current Account\n3.Exit");
c1=Integer.parseInt(d.readLine());
if(c1==1)
{System.out.println("1.Deposit\n2.Withdrawal\n3.Display");
c2=Integer.parseInt(d.readLine());
switch(c2)
{case 1:S.deposit();break;
case 2:S.withdrawal();break;
case 3:S.disp1();break;
}
}
else if(c1==2)
{System.out.println("1.Deposit\n2.Withdrawal\n3.Display");
c2=Integer.parseInt(d.readLine());
switch(c2)
{case 1:C.deposit();break;
case 2:C.withdrawal();break;
case 3:C.disp2();break;
}
}
}while(c1!=3);
}
}
Program 6(b):

import java.io.*;
abstract class sum
{
abstract void get() throws IOException;
abstract void add();
void welcome()
{ System.out.println("Welcome!"); }
}

class isum extends sum


{ int a,b,isum;
void get() throws IOException
{ welcome();
DataInputStream d=new DataInputStream(System.in);
System.out.print("Enter two integers: ");
a=Integer.parseInt(d.readLine());
b=Integer.parseInt(d.readLine());
}
void add()
{ isum=a+b; }
void disp()
{ System.out.println("Sum of the integers is: "+isum); }
}

class fsum extends sum


{ float a,b,fsum;
void get() throws IOException
{ DataInputStream d=new DataInputStream(System.in);
System.out.print("Enter two floating point numbers: ");
a=Float.parseFloat(d.readLine());
b=Float.parseFloat(d.readLine());
}
void add()
{ fsum=a+b; }
void disp()
{ System.out.println("Sum of the numbers is: "+fsum); }
}
class p6b
{ public static void main(String args[])throws IOException
{
isum i=new isum();
fsum f=new fsum();
i.get();
i.add();
i.disp();
f.get();
f.add();
f.disp();
}
}
Program 6(c):

import java.io.*;
interface factorial
{ int fact(int n); }
class A implements factorial
{
public int fact(int n)
{ int f=1;
for(int i=1;i<=n;i++)
f*=i; return f; }
void disp() throws IOException
{ DataInputStream d=new DataInputStream(System.in);
System.out.print("Enter a number: ");
int x=Integer.parseInt(d.readLine());
System.out.println("Factorial w/o recursion: "+ fact(x)); }
}
class B implements factorial
{ public int fact(int n)
{ if(n==1) return 1;
else return n*fact(n-1);
}
void disp() throws IOException
{ DataInputStream d=new DataInputStream(System.in);
System.out.print("Enter a number: ");
int x=Integer.parseInt(d.readLine());
System.out.println("Factorial with recursion: "+ fact(x)); }
}
class p6c
{
public static void main(String[] args) throws IOException
{ A a=new A();
B b=new B();
a.disp();
b.disp(); }
}

You might also like