0% found this document useful (0 votes)
13 views

Class 12 Computer File

The document describes computer programs written in Java for various topics like arrays, strings, recursion, inheritance, and data structures. Code snippets with sample outputs are provided for programs related to arrays, string manipulation, and searching/sorting algorithms.

Uploaded by

Taj Herbals
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Class 12 Computer File

The document describes computer programs written in Java for various topics like arrays, strings, recursion, inheritance, and data structures. Code snippets with sample outputs are provided for programs related to arrays, string manipulation, and searching/sorting algorithms.

Uploaded by

Taj Herbals
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 78

Computer Project File

Name: Mehfooz Ali

Class :XII(B).
Roll NO :27.
Topic: java programs.
INDEX
S.NO TOPIC DATE
1. 1D and 2D Array 26/6/22
2. String Programs 26/6/22
3. Recursion 26/6/22
programs
4. Object Passing 26/6/22
Programs
5. Inheritance 29/11/22
programs
6. Stack, Queue & 29/11/22
Linked list
programs
ACKNOWLEDGEMENT
I would like to express my special thanks of gratitude to my Computer teacher
Mr. Monis Naqvi for his able guidance and support in completing my project
by giving me an interesting topic and providing me with all facility which was
required.

Secondly, i would like to thanks my parents and friends who helped me to


finish this project within alotted time.

Thank You

Mehfooz Ali
(1).1D And 2D Array Programs.
(1).Description:- To Arrange Elements In An 2D Array
Using Bubble Sort.
import java.util.*;

class bubblesort

public static void main(String args[])

Scanner sc= new Scanner(System.in);

System.out.println("Enter Size");

int n=sc.nextInt(); \\ number of rows

int list[]=new int[n]; \\ array creation

System.out.println("Enter"+n+"Roll Number");

for (int i=0; i<n;i++)

list[i]=sc.nextInt();\\ filling values in an array.

for(int i=0;i<n-1;i++)

for(int j=0;j<n-1-i;j++)

{
if(list[j]>list[j+1])

int t=list[j];

list[j]=list[j+1];

list[j+1]=t;

System.out.println("Sorted array in ascending order is below:");

for(int i=0;i<n;i++)

System.out.println(list[i]);

Output-
(2). Description:- To Find The Sum Of Boundary
Elements Of A 2D Array.
import java.util.*;

class sum_boundary

public static void main(String args[])

Scanner sc= new Scanner(System.in);

int n=0,m=0;

System.out.println("Enter row"); \\ for rows

n=sc.nextInt();

System.out.println("Enter Column"); \\ for column

m=sc.nextInt();

int [][]mat=new int[n][m];


System.out.println("Enter elements of Array");

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

mat[i][j]=sc.nextInt(); \\ filling values.

}}

int r= mat.length;

int c=mat[0].length;

int sum=0;

for(int i=0;i<r;i++)

for(int j=0;j<c;j++)

if(i==0|| i==r-1||j==0||j==c-1)

sum=sum+mat[i][j];

}}

System.out.println("Sum of bondary elements"+sum);

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

{
System.out.print("row["+i+"]: Column["+j+"]:"+mat[i][j]+" ");

System.out.println();

Output-

(3). Description:- To Search A Number In An Array Using


Linear Search Method.
import java.util.*;

class LinearSearch

{
public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter Size Of Array");

int n=sc.nextInt();\\ for array size.

int Roll[]= new int[n]; \\ for array creation.

System.out.println("Enter"+n+"Numbers");

for(int i=0;i<n;i++)

Roll[i]=sc.nextInt();\\ for input of Numbers.

System.out.println("Enter A To Number search");

int src=sc.nextInt();

boolean found=false;

for( int j=0; j<n; j++)

if(Roll[j]==src)

found=true;

System.out.println("Numbers found");

break;

}
}

if(found==false)

System.out.println("No Not in list");

Output:-

4. Description:- To Find The Sum Of Non Boundary


Elements In A 2D Array.
import java.util.*;

class sumofnonbond

public static void main(String args[])


{

Scanner sc= new Scanner(System.in);

System.out.println("Enter row");

int n=sc.nextInt();\\ size of rows.

System.out.println("Enter column");

int m=sc.nextInt(); \\size of column.

int mat[][]=new int[n][m]; \\ Creation of Array.

System.out.println("Enter "+(m*n)+"Elements");

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

mat[i][j]=sc.nextInt(); \\ filling values.

System.out.println("Array is Given Below");

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

System.out.print(mat[i][j]+" ");

}
System.out.println();

int sum=0;

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

if(i!=0&& i!=n-1&&j!=0&&j!=m-1)

sum=sum+mat[i][j];

System.out.println("Sum of non bondary of an elements is--"+sum);

Output:-
(5). Description:- To Find The Sum Of Corner Elements.
import java.util.*;

class corner

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Ente Size Of Array");


int m=sc.nextInt();\\ size of rows

int n=sc.nextInt();\\ size of column

int arr[][]=new int[m][n]; \\ Creation Of Array.

System.out.println("Enter"+m*n+"Elements");

for(int i=0; i<m; i++)

for(int j=0;j<n; j++)

arr[i][j]=sc.nextInt();\\ filling values.

int sum;

sum=arr[0][0]+arr[0][n-1]+arr[m-1][0]+arr[m-1][n-1];

for(int i=0; i<m; i++)

for(int j=0;j<n; j++)

System.out.print(arr[i][j]+" ");

System.out.println();

System.out.println("Sum Of Corner Elements is"+sum);


}

Output:-

(2). Program Based On String.


(1). Description:- To Reverse A String.
import java.util.*;

class revstring

void joystring(String s)

int len=s.length();\\ To store length of String.


char rev[]=new char[len]; \\ Array Creation.

for(int i=0;i<len;i++)

rev[len-1-i]=s.charAt(i);

System.out.println("Reversed String Is Given Below");

for(int i=0;i<len;i++)

System.out.print(rev[i]);

System.out.println();

public static void main(String args[])

revstring obj=new revstring();\\ Creation Of Object.

Scanner sc=new Scanner(System.in);

System.out.println("Enter a string");

String s=sc.nextLine();

obj.joystring(s); \\ Calling of Function with Help object.

Output:-
(2). Description:- To Check Weather The Given String Is
A Palindrome Or Not.
import java.util.*;

class palinstr

void vol(String str)

int len=str.length(); \\ To store Length.

char ch[]=str.toCharArray(); \\ Creation Of Array.

String rev="";

for(int i=len-1;i>=0;i--)

rev+=ch[i];

if(str.equals(rev))

System.out.println("Is a Palaindrome");

else

System.out.println("Is not a Palaindrome");

public static void main(String args[])


{

Scanner sc=new

Scanner(System.in);

System.out.println("Enter a string");

String str=sc.nextLine();

palinstr obj=new palinstr();\\ Creation Of Object.

obj.vol(str);

Output:-

(3). Description:- To Rearrange A String In Ascending


Order.
import java.util.*;

class arrange

int len;

String str;

Sort()

{
str="";

len=0;

void read()

Scanner sc= new Scanner(System.in);

System.out.println("Enter a String");

str=sc.nextLine(); \\ To store String.

void arrange()

len=str.length(); \\ To store Length.

char spr[]= new char[len]; \\ Creation Of Array.

char ch[]=new char[len]; \\ Creation Of Array.

for(int i=0; i<len; i++)

ch[i]=str.charAt(i);

for(int j=0;j<len; j++)

if(ch[i]<ch[j])

spr[i]=ch[i];
ch[i]=ch[j];

ch[j]=spr[i];

System.out.println("Rearrange is Given below");

for(int i=0; i<len; i++)

System.out.print(ch[i]+" ");

System.out.println();

void display()

System.out.println("Original is-"+str);

arrange();

public static void main(String args[])

arrange obj= new arrange();\\ Creation Of Object.

obj.read();

obj.display();
}

Output:-

(4). Description:- To Accept A String And Print Each


Character Of The String Along With Its ASCII Value.
import java.util.*;

class ASCII

void word(String s)

int len=s.length();\\ To Store Length.

int a;char a1;

for(int i=0; i<len; i++)

a1=s.charAt(i);

a=a1;

System.out.println(a1+"->"+a);
}

public static void main(String args[])

ASCII obj=new ASCII();\\ Creation Of Object.

Scanner sc=new Scanner(System.in);

System.out.println("Enter A String Or Word");

String S=sc.nextLine();\\ To Take String.

obj.word(S);

Output:-
(5). Description:- To Remove The Repeating Letter From
The Given String(Repeating Letter Display Only Once).
import java.util.*;

class common

void vol(String str)

int len=str.length();\\ To Store String.

int c=0;

char ch[]=str.toCharArray();

for(int i=0;i<len;i++)

int j;

for(j=0;j<i;j++)

ch[i]=Character.toUpperCase(ch[i]);

ch[j]=Character.toUpperCase(ch[j]);

if(ch[i]==ch[j])

break;

if(j==i)
{

ch[c++]=ch[i];

for(int i=0;i<c;i++)

System.out.print(ch[i]+"");

System.out.println();

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter a string");

String str=sc.nextLine();\\ Input Of A String.

common obj=new common();\\ Creation Of Object.

obj.vol(str);

Output-

(3). Recursion Programs.


(1). Description:- To Change The Character Of A String
Using Recursion Method.
import java.util.*;

class Change

int len;

String str,newstr;

Change()

len=0;

str=newstr="";

void inputword()

System.out.println("Enter a word");

Scanner sc=new Scanner(System.in);

str=sc.nextLine();\\ Input Of String.

len=str.length();\\ Store Length Of String.

char caseconvert(char ch)

char ch1=Character.toUpperCase(ch);

if(ch==ch1)

ch1=Character.toLowerCase(ch);
else

ch1=Character.toUpperCase(ch);

return ch1;

void recchange(int x)

if(x<len)

char ch=caseconvert(str.charAt(x));

newstr=newstr+ch;

recchange(x+1);

void display()

System.out.println("Original"+str);

System.out.println("Changed word "+newstr);

public static void main(String args[])

Change obj=new Change();\\ Creation Of Object.

obj.inputword();

obj.recchange(0);

obj.display();
}

Output:-

(2). Description:- To Convert The Decimal Number To


Octal Number By Using Recursion Method.
import java.util.*;

class DeciOct

int n,oct,p;

DeciOct()

n=0;

void getnum(int nn)

n=nn;

p=n;

void deci_oct()

{
if(n==0)

oct=oct+0;

else

int d=(n%8);

n=n/8;

deci_oct();

oct=oct*10+d;

void show()

deci_oct();

System.out.println("Decimal number is"+p);

System.out.println("Octal number is"+oct);

public static void main(String args[])

DeciOct obj=new DeciOct();\\ Creation Of Object.

Scanner sc=new Scanner(System.in);

System.out.println("Enter a decimal number");

int x=sc.nextInt();\\ Input Of Number.

obj.getnum(x); \\ Calling Of Function.

obj.show();\\ Calling Of function.


}

Output:-

(3). Description:- To Convert A Number To Word Using


Recursion Method.
import java.util.*;

class convert

int n;

convert()

n=0;

void inpnum()

Scanner sc=new Scanner(System.in);

System.out.println("Enter A Number");

n=sc.nextInt();\\ Input Of Number


}

void extdigit(int x)

if(x!=0)

int d=x%10;

extdigit(x/10);

num_to_words(d);

void num_to_words(int z)

String
Digit[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight"

,"Nine"};

System.out.print(Digit[z]+" ");

public static void main(String args[])

convert obj=new convert();\\ Creation Of Object.

obj.inpnum();\\ Calling Of Object.

obj.extdigit(obj.n); \\ Calling Of Object.


}

Output:-

(4). Description:- To Print Fibonacci Series Till A Limit


Using Recursion Method.
import java.util.*;

class recursion

int a,b,c,limit;

recursion()

c=a=0;

b=1;

void input()

Scanner sc=new Scanner(System.in);

System.out.println("Enter A Limit For Series");

limit=sc.nextInt(); \\ Input Of A Number.


}

int fib(int n)

if(n==1)

return a;

else if(n==2)

return b;

else

c=fib(n-2)+fib(n-1);

return c;

void generate_fibseries()

int x;

for(int i=1; i<=limit; i++)

x=fib(i);

System.out.print(x+" ");

System.out.println();

public static void main(String args[])


{

recursion obj= new recursion();\\ Creation Of Object.

obj.input();

obj.generate_fibseries();

Output:-

(5). Description:- To Reverse A Given String Using


Recursion Method.
import java.util.*;

class Revstr

String str,revstr;

void getStr()

revstr="";

str="";

System.out.println("Enter a String");

Scanner sc=new Scanner(System.in);


str=sc.nextLine();\\ Input Of String.

void recReverse(int x)

int len=str.length(); \\ Store Length.

if(x<len)

char ch=str.charAt(len-1-x);

revstr+=ch;

recReverse(x+1);

void check()

if(str .equals(revstr))

System.out.println("Is A Palindrome");

else

System.out.println("Not A Palindrome");

}
System.out.println("Original "+str);

System.out.println("Chage "+revstr);

public static void main(String args[])

Revstr obj= new Revstr();\\ Creation Of Object.

obj.getStr();

obj.recReverse(0);

obj.check();

Output:-

(4). Object Passing Programs.


(1). Description:- To Reverse All The Elements Of A 2D
Array.
import java.util.*;

class MatRev
{

int arr[][],m,n;

MatRev(int mm, int nn)

m=mm;n=nn;

arr=new int[m][n]; \\ Creation Of Array.

void fillarray()

Scanner sc=new Scanner(System.in);

System.out.println("Enter"+m*n+"Elements");

for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

arr[i][j]=sc.nextInt();\\ Filling Values.

int rev(int x)

int revu=0;
int d=0;

while(x!=0)

{ d=x

%10;

revu=revu*10+d;

x=x/10;

return revu;

void rev(MatRev P)

for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

this.arr[i][j]= rev(P.arr[i][j]);

void show()

System.out.println("Elemennts of arrays are");


for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

System.out.print(arr[i][j]+" ");

System.out.println();

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter numbers of row and column");

int r=sc.nextInt();

int c=sc.nextInt();

MatRev obj1=new MatRev(r,c); \\ Creation Of Object.

obj1.fillarray();

MatRev obj2=new MatRev(r,c); \\ Creation Of 2nd Object.

obj2.rev(obj1);

obj1.show();

obj2.show();

}
}

Output:-

(2). Description:- To Shift An Array In Which Last Row


Become First Row, And Other Shift Down By One Row.
import java.util.*;

class shiftt

int m,n,mat[][];

shiftt(int mm, int nn)


{

m=mm;

n=nn;

mat=new int[m][n]; \\ Creation Of Array.

void input()

Scanner sc=new Scanner(System.in);

System.out.println("Enter"+m*n+"number");

for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

mat[i][j]=sc.nextInt(); \\ Input Of Elements.

void cyclic(shiftt P)

shiftt tmp=new shiftt(P.m,P.n);

int k=m-1;

for(int j=0;j<P.n;j++)
tmp.mat[0][j]=P.mat[k][j];

for(int i=0; i<P.m-1; i++)

for(int j=0; j<P.n; j++)

tmp.mat[i+1][j]=P.mat[i][j];

for(int i=0; i<P.m; i++)

for(int j=0; j<P.n; j++)

this.mat[i][j]=tmp.mat[i][j];

void display()

for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

{
System.out.print(mat[i][j]+" ");

System.out.println();

public static void main(String args[])

Scanner sc1=new Scanner(System.in);

System.out.println("Enter row and column");

int r=sc1.nextInt();

int c=sc1.nextInt();

shiftt obj1=new shiftt(r,c); \\ Creation Of Object.

obj1.input();

shiftt obj2=new shiftt(r,c); \\ Creation Of 2nd Object.

obj2.cyclic(obj1);

System.out.println("Contents of Original Arrays are");

obj1.display();

System.out.println("Contents of Rearranged Arrays are");

obj2.display();

Output:-
(3). Description:- To Add Two String.
import java.util.*;

class Join

String wrd;

int len;
Join()

wrd=" ";

len=0;

void feedword()

System.out.println("Enter a word");

Scanner sc=new Scanner(System.in);

wrd=sc.nextLine();\\ Input Of A Sentence.

len=wrd.length();\\ Input Of Length.

void mixWord(Join P, Join Q)

Join tmp=new Join();

tmp.wrd=P.wrd+Q.wrd;

int c=P.len+Q.len;

char arr[]=new char[c];

for(int i=0; i<c; i++)

arr[i]=tmp.wrd.charAt(i);

}
for(int i=0; i<c; i++)

this.wrd=this.wrd+arr[i];

void show()

System.out.println("Word Mix "+wrd);

public static void main(String args[])

Join obj1=new Join();\\ Creation Of Object.

obj1.feedword();

Join obj2=new Join();\\ Creation Of 2nd Object.

obj2.feedword();

Join obj3=new Join();\\ Creation Of 3rd Object.

obj3.mixWord(obj1,obj2);

obj1.show();

obj2.show();

obj3.show();
}

Output:-

(4). Description:- To Take Two Values Of Time In Hours


And Minutes And Then Add Both The Time And Display
It, In Hours And Minutes.
import java.util.*;

class Adder1

int a[]=new int[2];

Adder1()

for(int i=0;i<2;i++)

a[i]=0;

}
}

void readtime()

Scanner sc=new Scanner(System.in);

System.out.println("Enter time in hour and minute");

for(int i=0;i<2;i++)

a[i]=sc.nextInt();

void addtime(Adder1 X, Adder1 Y)

for(int i=0; i<2;i++)

this.a[i]=X.a[i]+Y.a[i];

int p=this.a[0]*60;

int z= p+this.a[1];

this.a[0]=z/60;

this.a[1]=z%60;

void disptime()
{

System.out.println("Hours="+a[0]+"Minutes="+a[1]);

public static void main(String args[])

Adder1 obj1=new Adder1();\\ Creation Of Object.

obj1.readtime();

Adder1 obj2=new Adder1();\\ Creation Of 2nd Object.

obj2.readtime();

Adder1 obj3=new Adder1();\\ Creation Of 3rd Object.

obj3.addtime(obj1,obj2);

obj1.disptime();

obj2.disptime();

obj3.disptime();

Output:-
(5). Description:- To Sum Two Arrays And Store It In An
Other Array And Display The Array.
import java.util.*;

class Matrix

int arr[][];

int m,n;

Matrix(int mm, int nn)

m=mm;

n=nn;

arr=new int [m][n]; \\ Creation Of Array.

void fillarray()

Scanner sc=new Scanner(System.in);


System.out.println("Enter"+(m*n)+"Elements");

for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

arr[i][j]=sc.nextInt();

Matrix AddMat(Matrix A)

for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

this.arr[i][j]=this.arr[i][j]+A.arr[i][j];

return A;

void display()

{
System.out.println("Content of array is given below");

for(int i=0; i<m; i++)

for(int j=0; j<n; j++)

System.out.print(arr[i][j]+" ");

System.out.println();

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter number of row and column");

int r=sc.nextInt();

int c=sc.nextInt();

Matrix obj=new Matrix(r,c); \\ Creation Of Object.

obj.fillarray();

Matrix obj1=new Matrix(r,c); \\ Creation Of 2nd Object.

obj1.fillarray();

obj1.AddMat(obj);

obj1.display();
}

Output:-
(5) Inheritance Programs;-

1.Write a program to calculate interest on the price of a


book following below table.
If Number of days:- a.

a>7&&a<=12.

a>12&&a<=17.
a>17.

Parent class: Library.

Data members/Instance variables.

String : name,author;

double :p;

Member Function/Methods.

void libarary():- To accept name of book and author and to


accept price of book.

void show():- To display given details with appropriate


messages.

Child class: compute.


Data members/Instance
variables. int :a;
double :r;
Member Function/Methods.

void compute():- To accept the number of days.


void fine():- To compute the interest on books as per
the numbers of days.
void display():- To display all details with appropriate message.
Also write the main function and call the
appropriate methods.
import java.util.*;

class libarary

String name,author; double

p;

void libarary()

Scanner sc=new Scanner(System.in);

System.out.println("Enter Name and price");

name=sc.nextLine();

author=sc.nextLine();

p=sc.nextDouble();

void show()

System.out.println("Name"+name+"author"+author+"total price"+p);

}
}

class compute extends libarary

int a;

double r;

void compute()

Scanner sc=new Scanner(System.in);

System.out.println("Enter Number Of Days");

a=sc.nextInt();

void fine()

if(a>7&&a<=12)

r=2*a;

else if(a>12&&a<=17)

r=3*(a-5)+2*5;

else if(a>17)

r=5*(a-10)+3*5+2*5;

else
r=0;

void display()

show();

System.out.println("the fine"+r);

public static void main(String args[])

compute obj=new compute();

obj.libarary();

obj.compute();

obj.fine();

obj.display();

}
Output:-
Enter Story and Author Name and price of book
The j o u r n e y .
Mehfooz ali
15 B6B
Enter Number Of
Days 90
Name= The journey.author= mehfooz ali.total price= 15B00.0 the
fine= 425.0
2.Write a program to calculate compound interest on
the principal value invested in bank using
formula:-
C=1+(rate*0.01);

K=Math.pow(C,Time);

C.I= (P.V*k)-P.V;

Parent class: BANK.

Data members/Instance variables.

String : name;

double :principal;

int :acc.no Member

Function/Methods.

BANK(String, int, double):- Parameterized constructors to


initialized all the data members.

void display():- To display given details with appropriate messages.

Child class: Interest.

Data members/Instance variables. int

:rate, time;

Interest(String, int, double, int, int):-Parameterized


constructors to call the constructor of parent class and to
initialized data members.
Member Function/Methods.

double calculate ():- To calculate and return the compound


interest.

void display():- To display all details with appropriate message.


Also write the main function and call the
appropriate methods.
import java.util.*;

class BANK

String name; int

acc_no;

double principal;

BANK(String N, int A, double P)

name=N;

acc_no=A;

principal=P;

void display()

System.out.println("Name"+name+"Account Number"+acc_no+"Principal
Value"+principal);;

import java.util.*;
class Interest extends BANK

int rate,time;

Interest(String N2,int A2,double P2,int R,int T)

super(N2,A2,P2);

rate=R;

time=T;

double calculate()

double CI=0.0; double

b=rate*0.01; double

c=1+b;

double k=Math.pow(c,time);

CI=(principal*k)-principal;

return CI;

void display()

{
super.display(); double

a=calculate();

System.out.println(time+"Rate"+rate+"CI"+a);

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter All Details");

String n=sc.nextLine();

int a=sc.nextInt();

double p=sc.nextDouble(); int

r=sc.nextInt();

int t=sc.nextInt();

Interest obj=new Interest(n,a,p,r,t);

obj.display();

Output:-
Name and account number principal value and the rate and time
mehfooz ali
12345983
209817

12
Name= mehfooz ali Account Numbe r= 12345983 P r Inc spaI VaIue=2B9817 0
T1me= 12Rat e= 2CI= 56281.6886B972958
3.Write a program to calculate interest on the given product if
the number of days=A.

A>0&&0<=100.

Amount=Rate.

A>100&&A<=200.

Amount=(60%*A)+rate;

A>200&&A<=300.

Amount=(80%*A)+rate;

A>300.

Amount=1*A+rate;

Parent class: Detail.

Data members/Instance variables.

String : name, address;

int :tel;

double :rate;

Member Function/Methods.

Detail(String, String, int, double):-Parameterized constructors to


initialized all the data members.

void display():- To display given details with appropriate messages.

Child class: Rate.


Data members/Instance

variables. int :n;

double :amt;

Member Function/Methods.

Rate(String, String, int, double, int):-Parameterized constructors to call the


constructor of parent class and to initialized data members.

void cal():- To calculate the interest of the product, using above formula.

void display():- To display all details with appropriate message. Also

write the main function and call the appropriate methods.


import java.util.*;

class Detail

String name,address; int

tel;

double rate;

Detail(String n, String a,int t,double r)

name=n;

address=a;

tel=t; rate=r;

void display()

System.out.println(name+"\t"+address+"\t"+tel+"\t"+rate);

class Rate extends Detail

{
int n; double

amt;

Rate(String n1,String a1,int t1,double r1,int num)

super (n1,a1,t1,r1);

n=num;

amt=0.0;

void cal()

if(n>0&&n<=100)

amt=rate;

else if(n>100&&n<201)

amt=(60*0.01)*n+rate;

else if(n>200&&n<301)

amt=(80*0.01)*n+rate;

else

amt=1*n+rate;

void display()
{

super.display();

System.out.println(amt+"\t"+n);

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter name,address,telephone number,rate and


number of day");

String n=sc.nextLine();

String a=sc.nextLine(); int

t=sc.nextInt(); double

r=sc.nextInt(); int

n1=sc.nextInt();

Rate obj=new Rate(n,a,t,r,n1); obj.cal();

obj.display();

}
Output:-
3.Write a program to calculate perimeter of a rectangle. Parent

class: Perimeter.

Data members/Instance variables. int

:a,b;

Member Function/Methods.

Perimeter(int, int):-Parameterized constructors to initialized all the data


members.

double calculate():- To return the perimeter of rectangle calculated.

void show():- To display given details with appropriate messages. Child

class: Area.

Data members/Instance

variables. int :h;

double :b;

Member Function/Methods.

Area(int, int, int):-Parameterized constructors to call the constructor of


parent class and to initialized data members.

void doarea():- To calculate area of rectangle.

void show():- To display all details with appropriate message.


Also write the main function and call the appropriate methods. import

java.util.*;

class Perimeter

int a,b;

Perimeter(int A,int B)

a=A;

b=B;

double calculate()

int c=2*(a+b);

return c;

void show()

double C=calculate();

System.out.println(a+"and"+b+"Area"+C);

}
}

class Area extends Perimeter

int h;

double b;

Area(int A, int B, int H)

super(A,B);

h=H; b=0.0;

void doarea()

b=(super.b*h);

void show()

super.show();

System.out.println(b+"and"+h);

}
public static void main(String args[])

Area obj=new Area(10,6,4);

obj.doarea();

obj.show();
}
}
Output:-

4. A super class Worker has been defined to store the details


of a worker. Define a sub class Wages to compute the
monthly wages for the worker. The details of the classes are
given below:-
class name :- Worker

Data Members:-
Name :- to store the name of the worker
Basic :- to store pay in decimal

Member functions:-
Worker(…) :- parametrized constructor to assign values to
the instance variables
void display() :- display worker details

class name;- Wages

Data Members:-

You might also like