Computer Project
Session 2024-25
NAME : Shajar Ali
CLASS : XII - B
ROLL NO. : 22
TOPIC :-
1. Inheritance Based 5 Programs
2. Arrays Based 5 Programs
3. Object Passing Based 5 Programs
4. String Based 5 Programs
5. Miscellaneous Programs
1
Submitted to: Mr. Monis Naqvi
Table of Content
1. Inheritance Based Programs
Program 1.1
Program 1.2
Program 1.3
Program 1.4
Program 1.5
2. Object Based Programs
Program 2.1
Program 2.3
1.Inheritance Based Programs
Program 1.1
A super class Record has been defined to store the names and ranks of 50 students. Define a sub
class Rank to find the highest rank along with the name. The details of both the classes are given
below:-
Class name : Record(Super Class)
Data Members/Instance Variables Description
name[] To store the names of the students
rnk[] To store the ranks of the students
Class name : Rank(Sub Class)
Data Members/Instance Variables Description
index Integer to store the index of the topmost rank
Member functions Description
2
Data Members/Instance Variables Description
Rank() Constructor to invoke the base class converter
and to initialize index=0
void highest() Finds the index/location of the topmost rank
and stores it in index without sorting the array
void display() Displays the names and ranks along with the
name having topmost rank.
Specify the class Record giving details of the constructor() , void readvalues() and void
display().Using the concept of inheritance, specify the class Rank giving details of constructor,
void highest() and void display().The main function and the algorithm need not be written.
import java.util.Scanner;
public class Record { //Super Class
String name[];
int rnk[];
Record(){
name = new String[50] ;
rnk = new int[50] ;
}
void readvalues(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter name and rank:");
for (int i = 0; i < 50; i++) {
name[i] = sc.nextLine() ;
rnk[i] = sc.nextInt() ;
}
}
void display(){
System.out.println("Names"+"\t"+"Ranks");
for (int i = 0; i < 50; i++) {
System.out.println(name[i]+"\t"+rnk[i]);
3
}
}
}
public class Rank extends Record{ //Sub Class
int index;
Rank(){
super() ;
index = 0 ;
}
void highest(){
int max = rnk[0] ; index = 0 ;
for(int i = 1; i<50 ; i++){
if(max<rnk[i])
index = i;
}
}
void display(){
super.display();
System.out.println("Names of the Top Rankers:");
}
}
Variable Description Table(VDT):-
Variable Name Data Type Description
name String (array) To store the names of the students
rnk int (array) To store the ranks of the students
index int To store the topmost index
—---------------------------------------------------------------------------------------------------------------------------------
4
Program 1.2
A superclass Worker has been defined to store the details of the worker. Define a subclass
Wages to compute the monthly wages for the worker. The details of the classes are given
below:-
Class name : Worker(Super Class)
Data members/Instance Variable Description
name To store the name of the worker
basic To store the basic pay in decimals
Member functions Description
Worker(...) Parameterized Constructor to assign values to
the instance variables
void display() Display the workers details.
Class name : Wages(Sub class)
Data members/instance variables Description
hrs Stores the hours worked
5
Data members/instance variables Description
rate Stores the rate per hour
wage Stores the overall wage of the worker
Member functions Description
Wages(...) Parameterized constructor to assign values to
the instance variables of both the classes
double overtime() Calculates and returns the overtime amount as
(hours*rate)
void display() Calculate the wage using the formula
wage = Overtime amount + Basic Pay and
display it along with the other details.
class Worker //Super Class
{
String name;
double basic;
Worker(String s,double x)
{
name=s;
basic=x;
}
void display()
{
System.out.println("\nName of the worker : "+name);
System.out.println("\nBasic pay of the worker : "+basic);
}
}
class Wages extends Worker //Sub Class
{
int hrs;
6
double rate,wage;
Wages(String s1,double x1,int h1,int m1,double r1)
{
super(s1,x1);
hrs=h1;
rate=r1;
mh=m1;
}
double overtime()
{
return hrs*rate ;
}
void display()
{
super.display();
double om=overtime();
wage=om+basic;
System.out.println("Total hrs. worked by the worker : "+hrs);
System.out.println("Extra hrs. worked by the worker : "+a);
System.out.println("Overtime amount of the worker : "+om);
System.out.println("Total amount of the worker : "+wage);
}
}
Variable Description Table(VDT):-
Variable Name Data Type Description
name String To store the name of the worker
basic double To store the basic pay in the decimal
hrs int To store the hours worked by the worker
rate double To store the rate per hour of the worker
wage double To calculate and store the overall wage of the worker
7
Variable Name Data Type Description
om double To calculate and store the overtime amount of the
worker
—-----------------------------------------------------------------------------------------------------------------
-
Program 1.3
A superclass Stock has been defined to store the details of the stock of a retail store.Define a
subclass Purchase to store the details of the items purchased with the new rate and update the
stock. Some of the members of the classes are given below:
Class name : Stock(Super class)
Data members/instance variables Description
item To store the name of the item
qty To store the quantity of item in stock
rate The unit price of an atom respectively
amt To store the net value of the item in stock
Member functions Description
Stock(...) Parameterized constructor to assign values to
the data members
8
Data members/instance variables Description
void display() To display the stock details
Class name : Purchase(Sub class)
Data members/instance variables Description
pqty To store the purchased quantity
prate To store the unit price of the purchased item
Member functions Description
Purchase(...) Parameterized constructor to assign values to
data members
void update() To update the stock by adding the previous
quantity by the purchased quantity and
replace the rate of the item if there is a
difference in the purchase rate.Also update
current stock value as: (quantity*unit price)
void display() To display the stock details before and after
updation
Specify the class Stock giving details of the constructor and void display().Using the concept
of the inheritance specify the class Purchase,giving details of the constructor(...) , void update
and void display().The main function need not be written.
class Stock
{
String item;
double qty,rate,amt;
Stock(String a,double b,double c)
{
item=a;
qty=b;
rate=c;
amt=qty * rate;
}
void display()
{
System.out.println("Name of the item : "+item);
System.out.println("Quantity:"+qty);
System.out.println("Rate per unit:"+rate);
9
System.out.println("Net value: "+amt);
}
}
class Purchase extends Stock
{
int pqty;
double prate;
Purchase(String a, double b, double c, int d, double e)
{
super(a,b,c);
pqty=d;
prate=e;
}
void update()
{
qty += pqty;
if(prate!=rate)
rate=prate;
amt = qty * rate;
}
void display()
{
super.display();
update();
super.display();
}
}
Variable Description Table(VDT):-
Variable Name Data type Description
item String To store the name of the item
qty double To store the quantity of the item
rate double To store the unit price of the item
amt double To store the net value of the of the item in stock
pqty int To store the purchased quantity
prate double To store the unit price of the purchased item
10
—-----------------------------------------------------------------------------------------------------------------
-
Program 1.4
A superclass Record contains names and marks of the students in two different single
dimensional arrays.Define a subclass Highest to display the names of the students obtaining the
highest marks. The details of the members of both the classes are given below:-
Class name : Record
Data members/instance variables Description
n[] Array to store the names
m[] Array to store the marks
size To store the number of students
Member functions Description
Record(int cap) Parameterized constructor to initialize the data
member size = cap
void readarray() To enter elements in both the array
void display() Display the array elements
Class name
11
Data members/instance variables Description
ind To store the index
Member functions Description
Highest(...) Parameterized constructor to initialize the data
members of both the classes
void find() Finds the index of the student obtaining the
highest marks and assign it to ‘ind’
void display() Displays the array elements along with the
names of the students who have obtained the
highest marks
Specify the class Record giving details of the Record() , void readarray() , void
display().Using the concept of inheritance, specify the class Highest , giving details of the
constructor(...), void find() and void display().The main function need NOT be written.
import java.util.Scanner;
class Record{ //Super class
String n[];
int m[];
int size;
Record(int cap){
size = cap;
n = new String[size];
m = new int[size];
}
void readarray(){
Scanner in = new Scanner(System.in);
for(int i = 0; i < size; i++){
System.out.print("Student name: ");
n[i] = in.nextLine();
System.out.print("Marks scored: ");
m[i] = in.nextInt();
}
}
void display(){
for(int i = 0; i < size; i++)
System.out.println(n[i] + " score = " + m[i]);
}
12
}
class Highest extends Record{ //Sub class
int ind;
Highest(int cap){
super(cap);
ind = 0;
}
void find(){
for(int i = 1; i < size; i++){
if(m[i] > m[ind])
ind = i;
}
}
void display(){
super.display();
System.out.println("Students with highest mark:");
for(int i = 0; i < size; i++){
if(m[i] == m[ind])
System.out.println(n[i] + " = " + m[i]);
}
}
}
Variable Description Table(VDT):-
Variable Name Data type Description
n String(array) To store the names of the students
m int(array) To store the marks of the students
size int To store the number of students
ind int To find and store the index of the topmost rankers
i int for loop variable
—-------—-------—-------—-------—-------—-------—-------—-------—-------—-------—-------—----
13
Program 1.5
A class Employee contains employee details and another class Salary calculates the employee;s
net salary.The details of the two classes are given below:-
Class name : Employee(Super Class)
Data members/instance variables Description
empNo Stores the employee number
empName Stores the employee name
empDesig Stores the employee designation
Member functions Description
Employee() Default constructor
Employee(...) Parameterized constructor to assign value to
employee number, name and designation
void display() Display the employee details
Class name : Salary(Sub Class)
Data members/instance variables Description
basic float variable to store the basic pay
Member functions Description
14
Data members/instance variables Description
Salary(...) Parameterized constructor to assign values to
data members
void calculate() Calculate the employee’s net salary as
following rule
DA = 10% of basic
HRA = 15% of basic
Salary = basic + DA + HRA
PF = 8% of Salary
Net salary = Salary - PF
Specify the class Employee giving details of the constructor and member functions void
display().Using the concept of inheritance specify the class Salary giving details of the
constructor and the member function void calculate().The main function need NOT be written.
class Employee{ //Super Class
int empNo;String empName,empDesig;
Employee()
{
empNo=0;
empName="";
empDesig="";
}
Employee(int a,String b,String c)
{
empNo=a;
empName=b;
empDesig=c;
}
void display()
{
System.out.println("Employee name:"+empName);
System.out.println("Employee number:"+empNo);
System.out.println("Employee designation:"+empDesig);
}
}
class Salary extends Employee{ //Sub Class
float basic;
15
Salary(float b,int n,String name,String d)
{
super(n,name,d);
basic=b;
}
void calculate()
{
double da=0.1*basic,hra=0.15*basic,gross=basic+da+hra,pf=0.08*gross;
double Net_Salary=gross-pf;
System.out.println("Employee Details");
super.display();
System.out.println("Net Salary:"+Net_Salary);
}
Variable Description Table(VDT):-
Variable Name Data type Description
empNo int To store employee number
empName String To store employee’s name
empDesig String To store employee’s designation
basic float To store employee’s basic pay
da double To calculate and store dearness allowance(DA)
hra double To calculate and store house renting
allowance(HRA)
gross double To calculate and store gross salary
pf double To calculate and store provident fund(PF)
Net_Salary double To calculate and store Net_Salary
—-------—-------—-------—-------—-------—-------—-------—-------—-------—-------—-------—-
16
2.Object Based Programs
Program 2.1
A class Collection contains an array of 100 integers.Using the following class description create
an array with common elements from two integer arrays.Some of the members of the class are
given below:-
Class name : Collection
Data members/instance variables Description
arr[] Integer array
len Length of the array
Member functions Description
Collection() Default constructor
Collection(int) Parameterized constructor to assign the length
of the array
void inparr() To accept the array elements
Collection common(Collection) Returns a Collection containing the common
elements of the current Collection object and
Collection object passed as a parameter.
void arrange() Sort the array elements of the object
containing common elements in ascending
17
Data members/instance variables Description
order using any sorting technique.
void display() Display the array elements
Specify the class Collection giving details of the constructor, void inparr() and void
arrange(),Collection common(Collection).You need to write the main function.
import java.util.Scanner;
class Collection {
int arr[], len ;
Collection(){
len = 100 ;
arr = new int[len] ;
}
Collection(int len){
this.len = len ;
arr = new int[len];
}
void inparr(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter "+len+" elements:");
for(int i = 0; i<len; i++)
arr[i] = sc.nextInt();
}
void arrange(){
for(int i = 0 ; i<len-1 ; i++){
for(int j = 0; j<len-1; j++){
if(arr[j]>arr[j+1]){
int t = arr[j];
arr[j] = arr[j+1] ;
arr[j+1] = t ;
}
}
}
}
Collection common(Collection collection){
Collection temp = new Collection(Math.min(collection.len ,
18
this.len)) ;
int index = 0;
for(int i = 0; i<collection.len ; i++){
for(int j = 0; j<len ; j++){
if(collection.arr[i] == this.arr[j]){
temp.arr[index++] = arr[j] ;
break ;
}
}
}
temp.len = index;
return temp ;
}
void display(){
System.out.println("Contents of the array:-");
for(int i = 0 ; i<len ; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
Variable Description Table(VDT):-
Variable Name Data type Description
arr int(array) To store the array
len int To store the length of the array
i int for loop
j int for loop
index int To store the index of the temporary variable temp
temp Collection(object) To store and return the Collection object containing
Common elements of the passed object and from the
current object.
—-------—-------—-------—-------—-------—-------—-------—-------—-------—-------—-------—-
19
Program 2.3
Define a class MatRev with the following details:-
Class name : MatRev
Data members/instance variables Description
arr[][] To store the integer elements
m To store the number of rows
n To store the number of columns
Member functions Description
MatRev(int nn , int mm) Parameterised constructor
void fillarray() To enter the elements in the array
int rev(int x) Returns the reverse of the number
void rev(MatRev P) Reverse each element of the array of the
parameterized object and store it in the array
of the current object
void show() Displays the array elements in matrix form.
Define the class MatRev giving the details of the constructor, void fillarray(), int reverse(int),
void MatRev(MatRev) , void show().Define the main function to call the functions and enable
the task accordingly.
import java.util.Scanner;
public class MatRev {
int arr[][] ;
int m , n ;
MatRev(int mm, int nn){
20
m = mm ;
n = nn ;
arr = new int[m][n];
}
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();
}
int rev(int n){
if(n<10)
return n ;
else{
int l = String.valueOf(n).length() ;
int d = n%10 ;
return (int)(d*Math.pow(10 , l-1)) + rev(n/10) ;
}
}
void rev(MatRev P){
for(int i = 0; i<m ; i++)
for(int j = 0 ;j<n ; j++)
arr[i][j] = rev(P.arr[i][j]) ;
}
void show(){
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String[] args) {
MatRev objMatRev = new MatRev(5, 5);
MatRev objMatRev2 = new MatRev(5, 5);
objMatRev.fillarray();
System.out.println("Original Matrix:");
objMatRev.show();
objMatRev2.rev(objMatRev);
System.out.println("Reversed Matrix:-");
objMatRev2.show();
21
}
}