0% found this document useful (0 votes)
6 views50 pages

Computer.11 Final

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

Computer.11 Final

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

NAME: rAihAN Abdul

bAsEEt
ClAss: Xi sEC:C2
rOll NO : 24
subJECt : COMPutEr
dAtE PrOGrAM:
[1]:- Write a program in java to accept a day no. between 1 to 366 and year
and the display the corresponding date. Also accept n from user. n is between 1
to 100. Compute and display the future day n days after the given date. Display
the error message if value of day no. n is not within the limit.
Input : Day No. : 50
Year : 2023
n = 25
Output : February 19, 2023
25 Days Later,
March 16, 2023
Input : Day No. : 321
Year : 2023
n = 77
Output : November 17, 2023
77 Days Later,
February 2, 2024
Input : Day No. : 367
Year : 2020
n = 101
Output : Invalid Input
Algorithm:

Class: Next_Date
1. Initialize Variables:
• dt: Initialize an array to store the number of days in each month.
• mn: Initialize an array to store the names of months.
• dy: Initialize an integer variable to represent the day number.
• yr: Initialize an integer variable to represent the year.
• n: Initialize an integer variable to represent the number of days to add.
2. Constructor (Next_Date):
• Initialize dy, yr, and n to zero.
3. Method (accept):
• Accept user input for dy, yr, and n using the Scanner class.
4. Method (check):
• Check the validity of input values:
• Ensure dy is between 1 and 366, yr is between 1900 and 2500, and n
is between 0 and 100.
• Return a boolean value indicating the validity.
5. Method (compute):
• Check the validity of input using the check method.
• If input is valid:
• Initialize ftdy, ftmn, and ftyr to the values of dy, 1, and yr, respectively.
• Use a loop to adjust ftdy, ftmn, and ftyr based on the number of days
and months.
• Display the entered date using the names of months (mn array).
• Calculate and display the next date after adding the specified number
of days.
• If input is not valid, display an error message.
6. Method (isLeapYear):
• Check if a given year is a leap year:
• Use the leap year calculation logic.
• Return a boolean value indicating whether it's a leap year.
7. Main Method (main):
• Create an instance of the Next_Date class.
• Call the accept method to get user input.
• Call the compute method to calculate and display the next date.
8. Execute Program:
• When the program is executed, the main method is called, initiating the
process.
• User input is accepted, and the validity is checked.
• If valid, the program calculates and displays the next date based on the input.

Variable Listing Table:


Variable Type Scope Purpose
dt int[] Class (Array) Stores the number of
days in each month.
mn String[] Class (Array) Stores the names of
months.
dy int Instance Represents the day
number.
yr int Instance Represents the year.
n int Instance Represents the
number of days to
add.
Next_Date() Constructor Instance Initializes dy, yr, and n
to zero.
accept() Method Instance Accepts user input for
dy, yr, and n.
check() Method Instance Checks the validity of
input values.
compute() Method Instance Computes the next
date based on input
values.
isLeapYear() Method Instance Checks if a year is a
leap year.
main() Method Static Executes the program
by creating an
instance of Next_Date
and calling methods.
Method Listing Table:

Method Return Type Purpose


accept void Accepts user input for day (dy), year (yr),
and number of days (n).
check boolean Checks the validity of input values (dy, yr,
and n). Returns true if valid, false
otherwise.
compute void Computes and displays the next date
based on user input and the specified
number of days.
isLeapYear(int boolean Checks if a given year is a leap year.
year) Returns true if it's a leap year, false
otherwise.
main(String[] args) void The main method that initializes the
program, accepts user input, and calls
necessary methods.
OUTPUT:
[2]:- A class called Date has been defined to handle the date related functions , i.e
,finding the no. of days and show the date when no. of days is given. Another method is
required of some job where you put input the day no. and it will show you the date in the
current year 2023. You may perform your job by using following functions / methods :
Class Name : Date
Data Member : dd : For day
mm : For month
Member Function :
Date (int , int) : Parameterized constructor
int dateTodateno (date dt) : Return the date no. equivalent to date object dt
int datenotodate (int dn) : Return the date equivalent to the given date no. dn in year
2023
void display () : To display the date in dd-mm format
void accept() : To accept the date in day no. and valid month no. from the user
Ex : Sample Inputs : Date Day No.
01-01-2023 01
20-01-2023 20
31-12-2023 365
Output : Day No. Date
365 31-12
20 20-11
01 01-01
Algorithm:

AlGOrithM: dAtE hANdliNG PrOGrAM

1. stArt

2. dEfiNE A ClAss NAMEd dAtE with thE fOllOwiNG AttributEs


ANd MEthOds:
A. AttributEs:
- iNt dd: dAy
- iNt MM: MONth
- iNt MONth[]: ArrAy tO stOrE thE NuMbEr Of dAys iN EACh
MONth

b. MEthOds:
- dAtE(iNt d, iNt M): CONstruCtOr tO iNitiAlizE dAy ANd
MONth
- vOid ACCEPt(): MEthOd tO ACCEPt dAy NO. ANd MONth frOM
thE usEr
- bOOlEAN isvAliddAtE(): MEthOd tO ChECk if thE ENtErEd
dAtE is vAlid
- iNt dAtEtOdAyNO(dAtE dt): MEthOd tO CONvErt dAtE
ObJECt tO dAtE NuMbEr
- vOid disPlAy(): MEthOd tO disPlAy thE dAtE iN dd-MM
fOrMAt
- bOOlEAN islEAPyEAr(iNt yEAr): MEthOd tO ChECk if A
yEAr is A lEAP yEAr
- MAiN(striNG ArGs[]): MAiN MEthOd whErE thE PrOGrAM
EXECutiON stArts

3. iNsidE thE MAiN MEthOd:


A. CrEAtE AN iNstANCE Of thE dAtE ClAss NAMEd Ob with
iNitiAl dAy ANd MONth As 0.
b. CAll thE disPlAy() MEthOd Of thE Ob ObJECt.
4. iNsidE thE disPlAy() MEthOd:
A. CrEAtE A NEw dAtE ObJECt Ob1 with iNitiAl dAy ANd MONth
As 0.
b. CAll thE ACCEPt() MEthOd Of Ob1 tO GEt iNPut frOM thE
usEr.
C. CAlCulAtE thE dAy NuMbEr usiNG thE dAtEtOdAyNO()
MEthOd fOr thE ENtErEd dAtE.
d. PriNt thE tAblE hEAdErs: "dAy NO." ANd "dAtE".
E. PriNt thE CAlCulAtEd dAy NuMbEr ANd thE dAtE iN dd-MM
fOrMAt.

5. iNsidE thE ACCEPt() MEthOd:


A. CrEAtE A sCANNEr ObJECt sC tO rEAd iNPut frOM thE usEr.
b. usE A whilE lOOP tO CONtiNuOusly PrOMPt thE usEr fOr
dAy ANd MONth uNtil A vAlid dAtE is ENtErEd.
i. PrOMPt thE usEr tO ENtEr thE dAy NuMbEr.
ii. PrOMPt thE usEr tO ENtEr thE MONth NuMbEr.
iii. ChECk if thE ENtErEd dAtE is vAlid usiNG thE
isvAliddAtE() MEthOd.
iv. if vAlid, brEAk Out Of thE lOOP; OthErwisE, disPlAy AN
ErrOr MEssAGE.

6. iNsidE thE isvAliddAtE() MEthOd:


A. dEtErMiNE thE MAXiMuM NuMbEr Of dAys fOr thE ENtErEd
MONth bAsEd ON thE MONth ArrAy.
b. AdJust thE MAXiMuM dAys fOr fEbruAry iN A lEAP yEAr.
C. ChECk if thE ENtErEd dAy is withiN thE vAlid rANGE fOr
thE GivEN MONth.
d. rEturN truE if thE dAtE is vAlid; OthErwisE, rEturN
fAlsE.

7. iNsidE thE dAtEtOdAyNO(dAtE dt) MEthOd:


A. iNitiAlizE vAriAblEs s ANd i tO 0.
b. usE A whilE lOOP tO itErAtE thrOuGh MONths uNtil
rEAChiNG thE ENtErEd MONth.
C. Add thE NuMbEr Of dAys iN EACh MONth tO thE tOtAl suM.
d. Add thE ENtErEd dAy tO thE suM.
E. rEturN thE CAlCulAtEd dAy NuMbEr.

8. iNsidE thE islEAPyEAr(iNt yEAr) MEthOd:


A. ChECk if thE GivEN yEAr is A lEAP yEAr usiNG lEAP yEAr
rulEs.
b. rEturN truE if it is A lEAP yEAr; OthErwisE, rEturN fAlsE.

9. ENd
vAriAblE listiNG tAblE:

vAriAblE sCOPE tyPE PurPOsE


dd iNt iNstANCE dAy Of thE dAtE.
MM iNt iNstANCE MONth Of thE
dAtE.
MONth iNt[] iNstANCE ArrAy
rEPrEsENtiNG thE
NuMbEr Of dAys iN
EACh MONth.
sC sCANNEr lOCAl usEd fOr tAkiNG
(ACCEPt) iNPut frOM thE
usEr.
Ob1 dAtE lOCAl iNstANCE Of thE
(disPlAy) dAtE ClAss fOr
iNPut ANd
disPlAy.
dAy iNt lOCAl rEPrEsENts thE
(disPlAy) dAy NuMbEr
CAlCulAtEd fOr
disPlAy.
i iNt lOCAl lOOP COuNtEr fOr
(vAriOus) vAriOus lOOPs.
MAXdAys iNt lOCAl MAXiMuM dAys iN
(isvAlid) thE MONth
CONsidEriNG lEAP
yEAr.
yEAr iNt lOCAl (islEAP) yEAr usEd tO
dEtErMiNE if it's A
lEAP yEAr Or NOt.
MEthOd listiNG tAblE:

MEthOd rEturN PArAMEtE PurPOsE


tyPE r
dAtE(iNt, iNt) CONstruCtO iNt d, iNt M iNitiAlizEs
r thE dd ANd MM
iNstANCE
vAriAblEs.
ACCEPt() vOid NONE ACCEPts thE
dAy NO. ANd
MONth frOM
thE usEr.
isvAliddAtE() bOOlEAN NONE ChECks if thE
ENtErEd dAtE
is vAlid,
CONsidEriNG
lEAP yEArs.
dAtEtOdAyNO(dAt iNt dAtE dt CONvErts A
E) dAtE ObJECt
tO thE
COrrEsPONdiN
G dAy
NuMbEr.
disPlAy() vOid NONE ACCEPts thE
dAtE ANd
disPlAys thE
dAy NuMbEr
ANd dAtE iN
dd-MM
fOrMAt.
islEAPyEAr(iNt) bOOlEAN iNt yEAr ChECks if A
GivEN yEAr is
A lEAP yEAr.
MAiN(striNG[]) vOid striNG thE ENtry
ArGs[] POiNt Of thE
PrOGrAM.
iNvOkEs thE
disPlAy
MEthOd.

OutPut:
MAtriX PrOGrAM:
[1] Write a program of 2D array of size m x n. Change the column in circular manner.
Input : m = 3, n = 4
1 2 4 5

9 7 2 1

3 3 4 9

Output :
5 1 2 4

1 9 7 2
9 3 3 4
Algorithm:

1. Class Definition:
• Class Name: Shift
• Data Members:
• int[][] ar: 2D array to store matrix elements.
• int n: Number of rows in the matrix.
• int m: Number of columns in the matrix.
• int i, j: Loop control variables.
2. Constructor:
• Method Signature: Shift(int n1, int m1)
• Parameters:
• n1: Number of rows.
• m1: Number of columns.
• Functionality:
• Initializes the size of the matrix.
3. accept Method:
• Method Signature: void accept()
• Input:
• Takes matrix elements from the user.
• Functionality:
• Uses nested loops to accept matrix elements from the
user.
4. display Method:
• Method Signature: void display()
• Output:
• Displays the original matrix.
• Functionality:
• Uses nested loops to print each element of the matrix.
5. shift Method:
• Method Signature: void shift()
• Functionality:
• Uses nested loops to shift each row's elements to the
right.
• The rightmost element of each row becomes the
leftmost.
6. Main Method:
• Method Signature: public static void main(String[] args)
• Functionality:
• Creates an instance of the Shift class.
• Calls accept to input matrix elements.
• Calls display to show the original matrix.
• Calls shift to perform the shifting operation.
• Calls display again to show the shifted matrix.

vAriAblE listiNG tAblE:

Variable Type Scope Purpose


ar int[][] Class 2D array to store matrix
elements.
n int Class Size of the square
matrix.
m int Class-wide Size of the square
matrix.
i int Method (accept, Loop variable for rows.
display, shift)
j int Method (accept, Loop variable for
display, shift) columns.
k int Method (shift) Temporary variable for
element swapping.

MEthOd listiNG tAblE:

Method Return Type Purpose


accept void Accepts user input for the elements of the
matrix.
display void Displays the original matrix.
shift void Performs shifting of elements in each row of
the matrix.
main void Main method to instantiate the class and
execute the program.
OutPut:
[2] Accept an array of m x n such that value of m must be greater than 0 and
less than 10 and value of n greater than 2 and less than 6. Allow the user to
input digits from 0 to F. Only at each location. Such that each row represent a
hexadecimal no., perform the following task on matrix :
i) Display original matrix
ii) Calculate the binary equivalent for each row and display as per
format given below :

Input : m = 4, n = 3
1 7 B
2 9 9

A B 8

F 6 2

Output :
0001 0111 1011

0010 1001 1001

1010 1011 1000

1111 0110 0010


Algorithm:

1. Prompt User for Matrix Dimensions:


• Prompt the user to input the number of rows (r) and columns (c) for
the matrix.
2. Validate Matrix Dimensions:
• Check if the entered dimensions are within the specified range (rows:
1-9, columns: 3-5).
• If not, display an error message and exit the program.
3. Initialize Matrix:
• Create a 2D array (ar) with dimensions r x c to store hexadecimal
elements.
4. Input Matrix Elements:
• Use nested loops to input each matrix element.
• Ensure that each entered character is a valid hexadecimal digit (0-9,
A-F, a-f).
• If not, prompt the user until a valid input is provided.
5. Display Original Matrix:
• Call the Matrix method to display the original matrix.
6. Display Binary Equivalent:
• Call the BinaryMatrix method to display the binary equivalent of each
row.
7. Convert Hex to Binary:
• Implement the hex_Binary method to convert a single hexadecimal
digit to binary.
8. Validate Hex Input:
• Implement the isValidHex method to check if a string represents a
valid hexadecimal digit.
9. Output Results:
• Display the original matrix and its binary equivalent.
10.End of Program.
vAriAblE listiNG tAblE:

Variable Scope Type Purpose


r main int Number of rows in
the matrix
c main int Number of columns in
the matrix
ar main, Matrix, char[][] 2D array to store
BinaryMatrix, matrix elements
hex_Binary
input main String Temporary storage for
user input
sc main Scanner Scanner object for
input
hex BinaryMatrix, char Individual matrix
hex_Binary element
decimal hex_Binary int Decimal equivalent of
a hexadecimal digit

MEthOd listiNG tAblE:

Method Return Type Purpose


main void Main method to drive the program and
handle user input
Matrix void Display the original matrix
BinaryMatrix void Display the binary equivalent of each
row in the matrix
hex_Binary String Convert a hexadecimal digit to its 4-digit
binary equivalent
isValidHex boolean Check if a string is a valid hexadecimal
digit
OutPut:
striNG PrOGrAM:-

[1] writE A PrOGrAM tO iNPut A sENtENCE frOM thE usEr


iN lOwErCAsE ANd rEMOvE thE first ANd thE lAst
ChArACtEr Of EvEry wOrd iN it.
sAMPlE iNPut : this bOOk is AN iNtErEstiNG bOOk.
sAMPlE OutPut : hi OO NtErEstiN OO.
ClAss NAME : wOrds
iNstANCE vAriAblE : striNG sEN
MEthOds :
wOrds (striNG s) : PArAMEtErizEd CONstruCtOr
vOid shOw() : disPlAy thE OriGiNAl ANd uPdAtEd striNG
striNG EXACtwOrd(striNG) : EXtrACt EACh wOrd usiNG
rECursivE tEChNiquE
vOid dEl(striNG) : dElEtE thE 1st ANd lAst ChArACtEr
Of EACh wOrd
source code :
Algorithm:

ClAss: wOrds
1. AttributEs:
• sEN (striNG): rEPrEsENts thE iNPut sENtENCE.
2. CONstruCtOr:
• wOrds(striNG s): iNitiAlizEs thE sENtENCE by
triMMiNG lEAdiNG ANd trAiliNG whitEsPACEs.
3. MEthOd: shOw
• iNPuts: NONE
• OutPuts: NONE
• EXECutiON stEPs:
• disPlAy thE OriGiNAl sENtENCE.
• ChECk fOr ".", "?", Or "!" At thE ENd Of thE
sENtENCE, triM thEM, ANd stOrE thEM iN thE
PuNCtuAtiON vAriAblE.
• triM ANy rEMAiNiNG whitEsPACEs frOM thE
sENtENCE.
• CrEAtE A COPy Of thE sENtENCE (k).
• EXtrACt ANd dElEtE wOrds frOM thE COPy (k)
usiNG thE EXtrACtwOrd MEthOd.
• disPlAy thE MOdifiEd sENtENCE with APPENdEd
PuNCtuAtiON.
4. MEthOd: EXtrACtwOrd
• iNPuts: striNG s (CurrENt POrtiON Of thE sENtENCE)
• OutPuts: NONE
• EXECutiON stEPs:
• ChECk if thE sENtENCE (s) CONtAiNs ONly ONE
wOrd (NO sPACE).
• if ONly ONE wOrd, dElEtE thE wOrd usiNG thE
dEl MEthOd.
• if MOrE thAN ONE wOrd, dElEtE thE first wOrd
ANd rECursivEly CAll EXtrACtwOrd ON thE
rEMAiNiNG POrtiON Of thE sENtENCE.
5. MEthOd: dEl
• iNPuts: striNG s (wOrd tO bE dElEtEd)
• OutPuts: NONE
• EXECutiON stEPs:
• APPENd thE wOrd (EXCludiNG thE first ANd
lAst ChArACtEr) tO thE MOdifiEd sENtENCE
(sEN).
6. MEthOd: ChECkENdPuNCtuAtiON
• iNPuts: NONE
• OutPuts: striNG PuNCtuAtiON (PuNCtuAtiON
ChArACtErs At thE ENd Of thE sENtENCE)
• EXECutiON stEPs:
• ChECk fOr ".", "?", Or "!" At thE ENd Of thE
sENtENCE ANd triM thEM.
• stOrE thE triMMEd PuNCtuAtiON ChArACtErs iN
thE PuNCtuAtiON vAriAblE.
7. MAiN MEthOd:
• PrOMPt thE usEr tO ENtEr A sENtENCE.
• CrEAtE AN iNstANCE Of thE wOrds ClAss (Ob) with
thE PrOvidEd sENtENCE.
• CAll thE shOw MEthOd tO disPlAy thE OriGiNAl ANd
MOdifiEd sENtENCEs.
vAriAblE listiNG tAblE:

vAriAblE tyPE sCOPE PurPOsE


sEN striNG iNstANCE (ClAss) rEPrEsENts
thE iNPut
sENtENCE
k striNG MEthOd (shOw) COPy Of thE
sENtENCE fOr
wOrd
EXtrACtiON
PuNCtuAtiON striNG MEthOd (shOw, stOrEs ENd
ChECkENdPuNCtuAtiON) PuNCtuAtiON
ANd APPENdEd
tO thE OutPut

Method Listing Table:

MEthOd rEturN dEsCriPtiON


tyPE
CONstruCtOr tO
iNitiAlizE thE sEN
wOrds(striNG s) NONE AttributE with thE
triMMEd iNPut
sENtENCE.
disPlAys thE OriGiNAl
ANd MOdifiEd
sENtENCEs, iNCludiNG
thE EXtrACtiON ANd
dElEtiON Of wOrds.
AlsO, ChECks ANd
triMs ENd
shOw() NONE PuNCtuAtiON.
rECursivE MEthOd tO
EXtrACt ANd dElEtE
wOrds frOM thE
EXtrACtwOrd(striNG s) NONE sENtENCE.
dElEtEs thE first ANd
lAst ChArACtEr Of A
wOrd ANd APPENds it
tO thE MOdifiEd
dEl(striNG s) NONE sENtENCE.
ChECks fOr ".", "?", Or "!"
At thE ENd Of thE
sENtENCE, triMs thEM,
ANd rEturNs thE
triMMEd PuNCtuAtiON.
usEd iN shOw MEthOd
fOr APPENdiNG tO thE
ChECkENdPuNCtuAtiON() striNG OutPut.
OutPut:
NuMbEr PrOGrAM:-

KAPREKAR NUMBER :

sOurCE COdE :-
AlGOrithM:-
Here's the algorithm of the provided Java program for identifying Kaprekar numbers
within a given range [p, q] and calculating their frequency:

1. Define a method to check if a number is a Kaprekar number:


• Define a method kaprekar(int n) that takes an integer n as input and
returns a boolean value indicating whether n is a Kaprekar number or
not.
• Check if n is equal to 1. If so, return true (1 is considered a Kaprekar
number).
• Calculate the square of n and count the number of digits in the square.
• Iterate through each possible partition point (number of digits in the
left part) except n itself:
• Calculate the left and right parts of the square based on the
partition point.
• Check if the sum of the left and right parts equals n. If so, return
true.
• If no valid partition point is found, return false.
2. Main method:
• Initialize variables p, q, and c (counter for Kaprekar numbers) to zero.
• Prompt the user to enter the values of p and q.
• Read the values of p and q from the user.
• Check if p and q are within the valid range (both less than or equal to
5000).
• If they are within the range, proceed; otherwise, print an error
message and terminate the program.
• Iterate through each integer i in the range [p, q]:
• Check if i is a Kaprekar number using the kaprekar method.
• If i is a Kaprekar number, print it and increment the counter c.
• Print the frequency of Kaprekar numbers ( c).
3. Correcting input validation:
• Ensure that both p and q are less than or equal to 5000, as specified in
the code.
• If either p or q exceeds 5000, print an error message and terminate the
program.

This algorithm provides the basic logic flow of the program for identifying Kaprekar
numbers within the specified range and calculating their frequency.
vdt:-
OutPut:-

You might also like