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

Untitled Document

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

Untitled Document

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

COMPUTER

PROJECT FILE

2024-25

Submitted to: Submitted by:


Mr. Sunil Bisht Aditi Khandelwal
XII A
R.No.-03
Question 1: Convert date

ALGORITHM:

Step1: START
Step 2: declare n,d,m an y
Step 3: default constructor Convert
Initialise n,d,m&y with default values
Step 4: method accept()
INPUT n and y
Step 5: method day_to_date()
IF Y%4EQUALS 0
a[2]EQUAL TO 29;
Integer s&c
S equal to 0 and c equals 0
WHILE s IS LESS THAN n while (s<n)
S equal to add s &a [c increment by]
S qual to(subtract a[decrement c]from s
D equal to (n minus s)
M equal to c
End method
Step 6: method display()
PRINT “Date=”a[m] “, ”d “ , ” y
Step 7: main method
Create object ob
Calls methods
Step 8 :END

Q. Design a class Convert to find the date and the month from a given day no. for a
particular year.
E.g. if day no.is 64 and the year is 2002,then the corresponding date would be:
March 4,2020 ie.(31+29+4=64)

import java.util.Scanner;
class Convert
{
int n,d,m,y;

Convert() //default constructor


{
n=0;
d=0;
m=0;
y=0;
}
void accept() //to input the variables
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter date no. and year:");
n=sc.nextInt();
y=sc.nextInt();
}
void day_to_date() //to convert day no. to date
{
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //no. Of Days in months
if(y%4==0) //leap year
a[2]=29;
int s=0,c=0;
while (s<n)
s=s+a[c++];
s=s-a[--c];
d=n-s;
m=c;
}

void display()
{

Stringx[]={"","January","February","March","April","May","June","
July","August","September","October","November","December"};
System.out.println("Day No.:"+n);
System.out.print("Date: "+x[m]+","+d+","+y);
}
public static void main(String[] args) {
Convert ob=new Convert(); //creates object of the class and
invokes default constructor
ob.accept(); //invokes functions
ob.day_to_date();
ob.display();
}
}

OUTPUT:

Question 2:Fibonacci String series


Q:A sequence of Fibonacci series is generated as follows: S0 = “a”, S1 = “b”, Sn =
S(n – 1) + S(n – 2) where ‘+’ denotes concatenation.
Thus, the sequence is: a, b, ba, bab, babba, babbabab, … n terms
Design a class FiboString to generate fibonacci strings.

ALGORITHM:

Step 1: START
Step 2:declare x,y,z
Step 3: fibostring()-constructor-initialize x=a,y=b,z=ba
Step 4: void accept()- print “Enter the no. of terms”
Input n
Step 5: void generate()-print x and y
s=ADD x&y
FOR I EQUALS 0 I UPTO (N-1)
INCREMENT VAL OF I BY 1
Print s
Y EQUAL Z
Z EQUALS Y
END FOR
Step 6: main() method
Step 7: Calling functions using obj
Step 8: STOP

import java.util.*;
class Fibostring
{
String x,y,z; // to store the terms of the series
int n; //no. Of terms of the series

Fibostring() // Default constructor to initialize x,y,z


{
x="a";
y="b";
z="ba";
}

void accept() //input the no. of terms


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of terms upto which the series is to be
generated: ");
n=sc.nextInt();
}
void generate() //to generate Fibonacci series
{
System.out.print(x+" "+y+" ");
int i;
for(i=0;i<=(n-1);i++)
{
String s=z+y;
System.out.print(s+" ");
//Calculating terms for fibonacci series
y=z;
z=s;
}
}

public static void main(String args[]) //main method


{
ob.accept();
ob.generate();
}
Fibostring ob=new Fibostring();
}

Output:

Question 3:Equality of Matrices


Q . Two matrices are said to be equal if thy have the same dimension and
thor corresponding elements are equal.
Design a class EqMat to chcek if the two matrices are equal or not. Assume
that the two atrices have the same dimensions.

ALGORITHM:
Step 1: START
Step 2: Declare integers m and n and an integer array a[][]
Step 3: create a constructor EqMat()
Initialise m to mm and n to nn and pass the mm and nn as the size of the
array.
Step 4: Create function readarray()
PRINT “ Enter array elements”
FOR(i equal to 0, i less than m,i increment by 1)
FOR(j equal to 0,j less than n,j increment by 1)
INPUT array elements
Step 5: Create function check(EqMat P,EqMat Q)
FOR(i=0;i<P.a.length,i increment by 1)
FOR(j=0,j<Q.a.length,j increment by 1)
IF(P.a.[i][j] equals Q.a[i][j])
RETURN 1
OTHERWISE
RETURN 0
RETURN 1
Step 6:Create function print()
PRINT “Array:”
FOR(iequal to 0, i less than a.length, i increments by 1)
FOR(jequals to0, j less than a.length, jincrements by 1)
PRINT a[i][j];
Step 7: Create main method
Step 8: Invoke constructor by creating an object
Step 9: Call the functions by passing the objects
Step 10:STOP
import java.util.Scanner;
public class EqMat {
int m,n;
int a[][];
EqMat(int mm,int nn)
{
m=mm;
n=nn;
a=new int[mm][nn];
}

void readarray()
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter array elements: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j]=sc.nextInt();
}
}
}

int check(EqMat P,EqMat Q)


{
for(int i=0;i<P.a.length;i++)
{
for(int j=0;j< Q.a.length;j++)
if(P.a[i][j]==Q.a[i][j])
{
return 1;
}
else
{
return 0;
}
}
return 1;
}

void print()
{
System.out.println("Array:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[] args) {


EqMat obj=new EqMat(3, 3);
EqMat obj1=new EqMat(3, 3);
obj.readarray();
obj1.readarray();
if(obj.check(obj, obj1)==1)
System.out.println("The two matrcices are equal");
else if(obj.check(obj, obj1)==0)
System.out.println("The matrices are not equal");
obj.print();
obj1.print();

}
}

OUTPUT:
Question 4:Transpose of matrix:
Q: A class Transarray contains a two dimensional integer array of order[m x n].
The maximum value possible for both ‘m’ and ‘n’ is 20. Design a class Transarray
to find the transpose of a givem matrix. Specify the class Transarray giving details
of the constructors and functions.
import java.util.*;
class Transarray{
int n,m;
int arr[][]; //array
public Transarray() /Default constructor
{
m=0;
n=0;
arr=new int[m][n];
}
Transarray (int mm,int nn) //Parameterised constructor
{
m=mm;
n=nn;
arr=new int[mm][nn];
}

void fillarray()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter array elements: ");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
arr[i][j]=sc.nextInt();
}
}
}

void transpose(Transarray A)
{
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-1; j++) {
temp=A.arr[i][j];
A.arr[i][j]=A.arr[j][i];
A.arr[j][i]=temp;
}
}
}

void disparray()
{
for (int i = 0; i < arr.length; i++) {
for ( int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Transarray ob1=new Transarray();
Transarray ob=new Transarray(3, 3);
ob.fillarray();
ob.transpose(ob);
ob.disparray();
}
} //end

OUTPUT:
Question 5: Armstrong No.
Q:Design a class ArmNum to check if a given number is an Armstrong number or
not. A number is said to be Armstrong if the sum of its digits raised to the power of
length of the number is equal to the number.
Example:
371 = 33 + 73 + 13

ALGORITHM:
Step 1: START
Step2 : declare variables n and l
Step 3: Constructor ArmNum(int n)
Initialize n equal to nn
Step 4: method isArmstrong()
Check for armstrong no. using recursion
Step 5: recursion
Step 6: IF n EQUALS sum_Pow(n)
Print ARMSTRONG NO.
OTHERWISE print NOT AN ARMSTRONG NO.
Step 7: main method
Create object obj
Step 8: input a no. in x
Step 9: pass the value of x to function isArmstrong()
Steo 10:STOP

import java.util.*;
class ArmNum
{
int n,l;
ArmNum(int nn) //parameterized
constructor
{
n=nn;
l=String.valueOf(n).length();
}
public int sum_pow(int i){
if(i < 10) //base case
return (int)Math.pow(i, l);
return (int)Math.pow(i % 10, l) + sum_pow(i / 10); //recursive case
}
public void isArmstrong(){ //to check if a no. is
armstrong by
implementing
sum_pow() method
if(n == sum_pow(n))
System.out.println("Armstrong No.");
else
System.out.println("It is not an Armstrong No.");
}
public static void main(String args[]) //main method
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. to be checked:");
int x=sc.nextInt();
ArmNum obj=new ArmNum(x); //invokes constructor
obj.isArmstrong(); //invokes method
} //end of method main
}

OUTPUT:
Question 7: Volume
Q:Using the concept of
inheritance, specify the class CalVol giving the details of the constructor(...), double
volume() and
void show().

ALGORITHM:
Step1: START
Step 2: Declare variable rad
Step 3:Create a parameterised constructor
Initialize rad equal to r
Step 4: create method show
PRINT “RADIUS”
Step 5: Create subclass CalVol extends Base
Step 6: Declare variable ht of type DOUBLE
Step 7: Create parameterized constructor
Initial ht to h and rad to r
Step 8: Create member method volume
DOUBLE V
V=pi*r*r*ht;
Calculate volume
Step 9: create function show
PRINT “ VOLUME”
Step 10: method main
Create object obj of class Base and obj1 of class CalVol
Call function of class Base using obj1 and functions of CalVol using obj
Step 11: STOP

class Base{
double rad; //radius

Base(Double r) //parameterised constructor


{
rad=r;
}
void show()
{
System.out.println("The radius is:"+rad);
}
}

class CalVol extends Base //subclass


{
double ht; //height
public CalVol(double h,double r) //parameterized contructor
{
super(r);
ht=h;
}

double volume() //calculates volume


{
double v; //stores volume
v=Math.PI*rad*(rad)*ht;
return v;
}
void show() //display the volume
{
System.out.println("Radius:"+rad);
System.out.println("Height:"+ht);
System.out.println("Volume:"+volume());
}
public static void main(String[] args) {
CalVol obj=new CalVol(2.5, 2.0); //invokes constructor of base class
Base obj1=new Base(2.0); //invokes constructor of sub class
obj1.show(); //calls function of super class
obj.show(); //calls function of sub class
}
}

OUTPUT:
Question 8:Provident fund
Q:A class Personal contains employee details and another class Retire calculates
the employee's Provident Fund
and Gratuity.
Displays the employee details along with the PF (Provident Fund) and
gratuity amount.
Specify the class Personal giving details of the constructor and member functions
void display( ). Using
the concept of inheritance, specify the class Retire giving details of constructor, and
the member
functions void provident(), void gratuity() and the void display1().

public class Personal {

String name="";
long pan=0;
double basic_pay=0.0;
long acc_no=0;

Personal(String n,long p,double pay,long ac)


{
name=n;
pan=p;
basic_pay=pay;
acc_no=ac;
}
void display()
{
System.out.println("Name of employee:"+name);
System.out.println("PAN No.:"+pan);
System.out.println("Basic Pay:"+basic_pay);
System.out.println("Account No.:"+acc_no);
}
}

class Retire extends Personal


{
int yrs;
double pf;
double grat;

Retire(int y,double prov,double g)


{
yrs=y;
pf=prov;
grat=g;
}

void provident()
{
pf=0.02*basic_pay;
}
void gratuity()
{
if(yrs>=10)
grat=12*basic_pay;
else
grat=0.0;
}

void display1()
{
super.display();
System.out.println("Provident Fund:"+pf);
System.out.println("Gratuity:"+grat);

}
public static void main(String[] args) {
Retire ob=new Retire(12, 0.0, 0.0);
Personal obj=new Personal("Ram",900, 10000.0, 990);
obj.display();
ob.provident();
ob.gratuity();
ob.display1();
}
}

OUTPUT:
Question 9:
Q: Holder is a kind of data structure which can store elements with the restriction
that an element can be added from the rear end and removed from the front end
only.
Specify the class Holder giving details of the functions.

ALGORITHM:
Step 1:START
Step 2:Declare an integer array Q[],integers cap,front and rear
Step 3: Create constructor Holder with parameter integer n
Initialize cap q=equal to n
Pass the value of cap to the size of array
Initialize front an drear
Step 4: Create function addint()
Step 5: IF(REAR EQUALS CAPminus 1
PRINT”HOLDER IS FULL
Otherwise IFfront equals -1
Front equal to rear equal to 0
Q[rear] equal to v
OTHWERWISE Q[increment rear] equal to v
Step 6: Create function removeint()
Step 7: Declare an integer variable n
Step 8:IF(front equals -1)
N equal to -999
PRINT”HOLDDER IS EMPTY”
OTHERWISE if (front equals rear)
N equal to Q[front]
OTHERWISE
N equal to Q[front increment by 1]
Step 9: return the value of n to the main method
Step 10: create main method
Step 11: Create an object of class holder and invoke the constructor
Step 12: Call the functions of the class using the object
Step 13: STOP
public class Holder {
int Q[]; // Array to hold the integers
int cap; // Capacity of the array
int front; // Index of the front element
int rear; // Index of the rear element

public Holder(int n) {
cap = n;
Q = new int[cap]; // Initializing the array with the given capacity
front = -1; // Front starts at -1 to indicate the queue is empty
rear = -1; // Rear starts at -1 to indicate the queue is empty
}

void addint(int v) {
if (rear == cap - 1) {
System.out.println("HOLDER IS FULL");
} else if (front == -1) {
front = rear = 0;
Q[rear] = v;
} else {
Q[++rear] = v;
}
}

int removeint() {
int n;
if (front == -1) {
n = -999; // Return -999 to indicate the holder is empty
System.out.println("HOLDER IS EMPTY");
} else if (front == rear) {
n = Q[front];
front = rear = -1; // Reset front and rear as the holder becomes empty
} else {
n = Q[front++];
}
return n;
}

public static void main(String[] args) {


Holder obj = new Holder(10);
obj.addint(5);
System.out.println("Removed: " + obj.removeint());
System.out.println("Removed: " + obj.removeint());
}
}

OUTPUT:
Question 10:Stack Operation
Q:A bookshelf is designed to store the bookmarks in a stack with LIFO(Last In
First Out) operation. Specify the class Book giving the details of ONLY the
functions void tell() and void add(String).

public class Book {


String[] name; // Array to hold book names
int point; // Points to the topmost book
int max; // Maximum capacity of the shelf

// Constructor to initialize the book shelf with a certain capacity


public Book(int cap) {
this.max = cap;
this.point = -1;
this.name = new String[max]; // Initialize the name array with the given
capacity
}

// Method to tell the topmost book or indicate if the shelf is empty


void tell() {
if (point == -1) {
System.out.println("SHELF EMPTY!");
} else {
System.out.println("The topmost book is " + name[point]);
}
}

// Method to add a book to the shelf


void add(String v) {
if (this.point == max - 1) {
System.out.println("SHELF FULL!");
} else {
this.name[++this.point] = v;
System.out.println("Added book: " + v);
}
}
// Method to display all the books on the shelf
void display() {
if (point == -1) {
System.out.println("SHELF IS EMPTY!");
} else {
System.out.println("Books on the shelf:");
for (int i = 0; i <= point; i++) {
System.out.println(name[i]);
}
}
}

public static void main(String[] args) {


Book shelf = new Book(3); // Creating a book shelf with capacity of 3
shelf.add("Harry Potter");
shelf.add("The Hobbit");
shelf.add("The Catcher in the Rye");
shelf.add("1984"); // This will trigger "SHELF FULL!" because capacity is
3
shelf.tell(); // Show the topmost book
shelf.display(); // Display all books on the shelf
}
}

OUTPUT:
Question 11:Linked List
Q:Program to insert a node in a sorted linked list
ALGORITHM:
Step 1: START
Step 2: Create class Node
Step 3:Declare an integer variable data
Step 4:Create a paramaeterized constructor Node()
Initialize the variables data and next
Step 5: create a private data member Head of type Node
Step 6: Create a function insert SortedOrder()
Create an object of class Node and inoke constructor
Step 7:IF(head equals null OR head.data Greaterthan equals newNode.data)
newNoDE.next equals to head
head equal to newNode
OTHERWISE Node current equal to head step 8: WHILE(current.next is not
equal to null AND current.next.data LESS THAN newNode.data
current equal to current.next
Step 8: create a function display()
INitialse NOde current to head
PRINT “LINKED LIST”
WHILE(current NOT equal to null)
Current equal to current.next
PRINT current.data
PRINT “”
Step 9: Create method main
Create an object list
Step 10:Call the functions using object list
Step 11:STOP
class LinkedList {

// Node class to represent a node in the linked list

class Node {

int data;

Node next;

// Constructor to create a new node

public Node(int data) {

this.data = data;

this.next = null;

// Head of the list

private Node head;

// Method to insert a node into a sorted linked list


public void insertInSortedOrder(int data) {

Node newNode = new Node(data);

// If the list is empty or the new node should be the new head

if (head == null || head.data >= newNode.data) {

newNode.next = head;

head = newNode;

} else {

// Traverse the list to find the proper position to insert

Node current = head;

while (current.next != null && current.next.data < newNode.data) {

current = current.next;

newNode.next = current.next;

current.next = newNode;
}

// Method to display the linked list

public void display() {

if (head == null) {

System.out.println("List is empty");

return;

public static void main(String[] args) {

LinkedList list = new LinkedList();

// Display the list

list.display();

}
}

OUTPUT:
Question 9:

Q: Holder is a kind of data structure which can store elements with the restriction
that an element can be added from the rear end and removed from the front end
only.
Specify the class Holder giving details of the functions void addint(int) and int
removeint(). Assume that the other functions have been defined.

ALGORITHM:
Step 1:START
Step 2: declare array Q,Integers cap,front and rear
Step 3: Create a constructor Holder
Initialise cap to n
Pass the value of cap in the size of array Q
Step 4: Create a function addint
IFrear equals cap minus 1
PRINT “HOLDER IS FULL’’
OTHERWISE IF reae equals 0
Front equal to rear equal to -1
Step 5: create function removeint
Declare integer variable v
Step 6: IF rear equals 0
N equal to -999
OTHERWISE IF front EQUALS rear,front equal to rear to 0
OTHERWISE n equal to Q[front increment by 1]
Step 7:Return the value of n to method main
Step 8: main metod
Create object obj
Step 9: call the function using obj
Step 10: STOP

public class Holder {


int Q[],cap,front,rear;

public Holder(int n){ //parameterised constructor


cap=n;
Q=new int[cap];
front=rear=0;
}
void addint(int v) //to add ‘v’ to the end
{
if(rear==cap-1)
{
System.out.println("HOLDER IS FULL");
}
else if(rear==0)
{
front=rear=1;
Q[rear]=v;
}
else
{
Q[++rear]=v;
}
}

int removeInt() //to remove ‘v’ from the end


{
int n;
if(rear==0)
{
n=-999;
}
else if(front==rear)
{
n=Q[front];
front=rear=0;
}
else
{
n=Q[front++];
}
return n;
}
public static void main(String[] args) { //method main
Holder obj=new Holder(10); //invokes constructor
obj.addint(5); //calls function
obj.removeInt();
}
} //end of class

OUTPUT:

(no print statement)

You might also like