Untitled Document
Untitled Document
PROJECT FILE
2024-25
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;
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:
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
Output:
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();
}
}
}
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();
}
}
}
}
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
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().
String name="";
long pan=0;
double basic_pay=0.0;
long acc_no=0;
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;
}
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).
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 {
class Node {
int data;
Node next;
this.data = data;
this.next = null;
// If the list is empty or the new node should be the new head
newNode.next = head;
head = newNode;
} else {
current = current.next;
newNode.next = current.next;
current.next = newNode;
}
if (head == null) {
System.out.println("List is empty");
return;
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
OUTPUT: