COMPUTER
PROJECT
NAME. ABHIGYAN DAS
CLASS. XI
SEC. SCI-C
ROLL NO. 1
INDEX
SL TOPIC P. REMARKS
NO. NO.
1 INTRODUCTION 1
2 Question 1 3
3 Question 2 8
4 Question 3 13
5 Question 4 18
6 Question 5 25
7 Question 6 31
8 Question 7 37
9 Question 8 45
10 Question 9 50
11 Question 10 56
12 Question 11 61
13 Question 12 66
14 Question 13 71
15 Question 14 76
16 Question 15 81
17 CONCLUSION 87
18 ACKNOWLEDGEMENT 88
INTRODUCTION
What is Java?
Java is a high-level, object-oriented programming language developed by Sun
Microsystems in 1995 (now owned by Oracle). It is designed to be platform-
independent, meaning Java programs can run on any device that has a Java Virtual
Machine (JVM). The main features of Java include:
Simple and easy to learn
Object-Oriented Programming (OOP)
Platform-independent (Write once, run anywhere)
Secure and robust
Multithreaded and high performance
Java is widely used for developing:
Desktop applications
Web applications
Mobile apps (especially Android)
Enterprise software
Games
Embedded systems
What is BlueJ?
BlueJ is an Integrated Development Environment (IDE) specifically designed for
beginners learning Java. It was developed by the University of Kent and Deakin
University. BlueJ is widely used in schools and colleges because of its simple
interface and features that help students understand basic concepts of object-
oriented programming.
Key features of BlueJ:
Easy-to-use interface
Visual representation of classes and objects
Built-in editor, compiler, and terminal
Supports direct interaction with objects
Ideal for educational purposes
In short:
Java is the programming language.
BlueJ is the tool (IDE) used to write, compile, and run Java programs, especially
for learning purposes.
1
PROGRAMS
2
Question 1
A class Series has been defined to generate sum of the following series:
Some of the members of the class are given below:
Class name : Series
Data member/instance variable:
X :integer to store the value of x
N :integer to store the value of n(term)
sum :double to store the final sum
Member functions/methods:
Series() :default constructor
voidaccept() : to accept the value of x and n.
int sum(int a) :return the sum of natural numbers up to a using recursion.
voiddisplay() : call the sum() method and compute the sum of the series.
Specify the class Series, giving details of the Constructor, void accept( ), int sum(int),
and voiddisplay( ). Define the main( ) function to create an object and call the
functions accordingly to enable the task..
3
Question 1
STEP 1 : Start the program
STEP 2 : Declare three variables: X (integer), N (integer), and sum (real number)
STEP 3 : Initialize X = 0, N = 0, and sum = 0.0
STEP 4 : Accept input for X (the number to be used in the series) from the user
STEP 5 : Accept input for N (the number of terms in the series) from the user
STEP 6 : Repeat steps 7 to 9 for each value of i from 1 to N
STEP 7 : Call a recursive function to calculate the sum of the first i natural numbers
(i.e., 1 + 2 + 3 + ... + i)
STEP 8 : Multiply X by i and divide the result by the sum calculated in step 7
STEP 9 : Add the result of step 8 to the variable 'sum'
STEP 10 : After the loop ends, display the final value of 'sum'
STEP 11 : End of the program
4
5
6
Question 1
Data
Variable Name Description
Type
X int Stores the base integer value entered by the user.
N int Stores the number of terms in the series.
sum double Accumulates the total sum of the computed series.
scanner Scanner Used to take input from the user via keyboard.
Loop counter used to iterate from 1 to N while
i int
computing the series.
Stores the sum of first i natural numbers, computed
sumOfFirstINumbers int
using recursion.
Represents the term for which sum of natural
a int
numbers is being calculated.
Object of the Series class used to call methods
seriesObject Series
accept() and display().
7
Question 2
A class Perfect has been defined to check the number is a perfect number or not. A
perfect number is a number whose sum of the factors excluding itself is equal to the
number.
For example, 6 = 1+2+3, 28 = 1+2+4+7+14 etc.
Some of the members of the class are given below:
Class name : Perfect
Data member/instance variable:
num : integer to store the number
sum : integer to store the sum of the factors
Member functions/methods:
Perfect( ) : default constructor
void accept( ) : to accept a positive number.
int factorSum(int a) : return the sum of the factors of the given number using
recursive technique.
void display( ) : call the factor() method and check and print the number is
palindrome or not and print appropriate message.
Specify the class Perfect giving details of the Constructor, void accept( ), int
factorSum(int), and void display( ).Define the main( )function to create an object and
call the functions accordingly to enable the task.
8
Question 2
STEP 1 : Start the program
STEP 2 : Declare two integer variables: num (to store input number), sum (to store
sum of factors)
STEP 3 : Initialize num = 0 and sum = 0
STEP 4 : Accept a positive integer input from the user and store it in num
STEP 5 : If the input number is less than or equal to 0, display an error message and
terminate the program
STEP 6 : Call a recursive function to find the sum of all factors of num (excluding
num itself), starting with divisor = 1
STEP 7 : Store the result of the recursive function in the variable sum
STEP 8 : Display the value of sum
STEP 9 : If sum equals num, display that the number is a Perfect Number
STEP 10 : Otherwise, display that the number is NOT a Perfect Number
STEP 11 : End of the program
9
10
11
Question 2
Variable Data
Description
Name Type
num int Stores the input number entered by the user.
Stores the sum of the proper divisors (factors) of the input
sum int
number.
scanner Scanner Used to take user input from the console.
Represents the number whose factors are being
number int
calculated (used in recursion).
Tracks the current divisor being tested in the recursive
divisor int
method.
Object of the Perfect class used to call accept() and
perfectNumber Perfect
display() methods.
12
Question 3
Design a class NumDude to check if a given number is a Dudency number or not.
(A Dudency number is a positive integer is a perfect cube , such that the sum of its
equal to the cube root of the number. )
Example : 5832 = (5+8+3+2)3 = (18)3 = 5832 , thus 5832 is a Dudency number.
The details of the class are given below:
Class name : NumDude
Data member/instance/variable:
num : to store a positive integer number
Methods/Member functions:
NumDude() : default constructor to initialize the data member with legal initial
value
void input() : to accept a positive integer number
int sumDigits(int x) : returns the sum of the digits of number
void is Dude() : checks whether the given number is a Dudeney number by
invoking the function sumDigits() and displays the result with an appropriate
message
Specify the class NumDude giving details of the constructor(), void input(), int
sumDigits(int) and void is Dude(). Define a main() function to create an object and
call the functions accordingly to enable the task.
13
Question 3
STEP 1 : Start the program
STEP 2 : Declare an integer variable num to store the input number
STEP 3 : Initialize num = 0
STEP 4 : Accept a positive integer input from the user and store it in num
STEP 5 : If the input number is less than or equal to 0, display an error message and
terminate the program
STEP 6 : Define a recursive function to calculate the sum of digits of a number:
If the number is 0, return 0
Else, return (last digit) + sum of digits of the remaining number
STEP 7 : Calculate the cube root of num and round it to the nearest integer
STEP 8 : Calculate the sum of the digits of num using the recursive function
STEP 9 : Display the cube root and the sum of digits
STEP 10 : If the cube of the cube root equals num and it is also equal to the digit
sum, then display that num is a Dudency Number
STEP 11 : Otherwise, display that num is NOT a Dudency Number
STEP 12 : End of the program
14
15
16
Question 3
Data
Variable Name Description
Type
num int Stores the input positive integer entered by the user.
Object of the Scanner class used to read input from the
scanner Scanner
user.
Parameter used in the sumDigits method to calculate
x int
the digit sum recursively.
cubeRoot int Stores the cube root (rounded) of the input number.
digitSum int Stores the sum of digits of the input number.
Object of the NumDude class to invoke input() and
dudencyChecker NumDude
isDude() methods.
17
Question 4
A class Trans is defined to find the transpose of a square matrix. A transpose of a
matrix is obtained by interchanging the elements of the rows and columns.
Example: If size of the matrix = 3, then
ORIGINAL TRANSPOSE
9 6 7 9 12 10
12 4 1 6 4 8
10 8 2 7 1 2
Some of the members of the class are given below:
Class name : Trans
Data members/instance variable:
arr[ ][ ] : to store integers in the matrix
m : integer to store the size of the matrix
Methods/Member functions:
Trans(int mm) : parameterised constructor to initialise the data member m = mm
void fillarray() : to enter integer elements in the matrix void
transpose() : to create the transpose of the given matrix
void display() : displays the original matrix and the transport matrix by invoking
the method transpose()
Specify the class Trans giving details of the constructor(), void fillarray(), void
transpose() and void display(). Define a main() function to create an object and call
the functions accordingly to enable the task.
18
Question 4
STEP 1 : Start the program
STEP 2 : Declare a 2D array to store matrix elements and an integer variable m to
store the size of the square matrix
STEP 3 : Accept the size of the square matrix (m) from the user
STEP 4 : Initialize the matrix with size m × m
STEP 5 : Accept m × m elements from the user and store them in the matrix
STEP 6 : Display the original matrix in proper format
STEP 7 : Create a new m × m matrix to store the transpose
STEP 8 : For each element at position [row][col] in the original matrix, place it at
position [col][row] in the transposed matrix
STEP 9 : Display the transposed matrix
STEP 10 : End of the program
19
20
21
22
23
Question 4
Data
Variable Name Description
Type
2D array to store the original square matrix entered by
arr int[][]
the user.
Stores the size (number of rows/columns) of the
m int
square matrix.
mm int Parameter used in the constructor to initialize m.
scanner Scanner Object of the Scanner class to take input from the user.
row int Loop control variable to iterate over matrix rows.
col int Loop control variable to iterate over matrix columns.
2D array used to store the transposed version of the
transposed int[][]
matrix.
transposedMatrix int[][] Stores the result returned by the transpose() method.
Stores the matrix size entered by the user in main()
size int
method.
matrixObject Trans Object of the Trans class used to call methods.
24
Question 5
A class SortAlpha has been defined to sort the words in the sentence in alphabetical
order.
Example:
Input: THE SKY IS BLUE
Output: BLUE IS SKY THE
Some of the members of the class are given below:
Class name : SortAlpha
Data members/instance variable:
sent : to store a sentence
n : integer to store the number of words in a sentence
Methods/Member functions:
SortAlpha() : default constructor to initialise data members with legal initial value
void acceptsent() : to accept a sentence in UPPER CASE
void sort(SortAlpha P) : sorts the words of the sentence of object P in
alphabetical order and stores the sorted sentence in the current object
void display() : display the original sentence along with the sorted sentence by
invoking the method sort()
Specify the class SortAlpha giving details of the constructor(), void acceptsent(), void
sort(SortAlpha) and void display(). Define a main() function to create an object and
call the functions accordingly to enable the task.
25
Question 5
STEP 1 : Start the program
STEP 2 : Declare a string variable to store the sentence and an integer variable to
store the number of words
STEP 3 : Accept a sentence in UPPER CASE from the user and remove
leading/trailing spaces
STEP 4 : Validate each character of the sentence to ensure it contains only
uppercase letters and spaces;
if invalid, display "INVALID INPUT" and terminate the program
STEP 5 : Split the sentence into words based on spaces and count the number of
words
STEP 6 : Create a new object to sort the sentence separately, preserving the original
STEP 7 : In the new object, split the sentence into words again
STEP 8 : Sort the words alphabetically using the bubble sort technique:
- Compare adjacent words
- Swap them if they are not in alphabetical order
- Repeat until all words are sorted
STEP 9 : Join the sorted words back into a sentence
STEP 10 : Display the original sentence
STEP 11 : Display the sorted sentence
STEP 12 : End of the program
26
27
28
29
Question 5
Variable Data
Description
Name Type
Stores the sentence entered by the user (in UPPER
sent String
CASE).
n int Holds the number of words present in the sentence.
sc Scanner Scanner object used to read input from the user.
Temporarily stores each character during validation of
ch char
uppercase and space.
words String[] Array of words obtained by splitting the sentence.
i, j int Loop control variables used in the bubble sort algorithm.
Used as a temporary storage during word swapping in
temp String
sorting.
P SortAlpha Object passed to the sort() method to be sorted.
Main object created in the main() method to run the
obj SortAlpha
program.
temp (in Temporary object created to preserve the original
SortAlpha
display) sentence and sort a copy.
30
Question 6
A Fascinating number is one which when multiplied by 2 and 3 and then after the
results are concatenated with the original number , the new number contain all the
digits from 1 to 9 exactly once. There can be any number of zeros and are to be
ignored.
Example : 273
273 X 1= 273
273 X 2= 546
273 X 3 = 819
Concatenating the results we get , 273546819 which contaings all the digits from 1 to
9 exactly once. Thus, 273 is a Fascinating number. Accept two positive integers m
and n, where m must be less then n and the values of both m and n must be greater
than 99 and less than 10000. Display all the Fascinating number that are in the
range between m and n (both inclusive) and output them along with the frequency, in
the format given below:
Test your program with the following data and some random data:
Example 1:
INPUT :
m = 100 n = 500
OUTPUT: THE FASCINATING NUMBERS ARE:
192 219 273 327
FREQUENCY OF FASCINATING NUMBERS IS : 4
Example 2:
INPUT : m = 900
n = 5000
OUTPUT: THE FASCINATING NUMBERS ARE:
1902 1920 2019 2190 2703 2730 3027 3270
FREQUENCY OF FASCINATING NUMBERS IS : 8
Example 3:
INPUT : m = 400
n = 900
OUTPUT: THE FASCINATING NUMBERS ARE: NIL
FREQUENCY OF FASCINATING NUMBERS IS : 0
Example 4:
INPUT : m = 70 n = 450
OUTPUT: INVALID INPUT
31
Question 6
STEP 1 : Start the program
STEP 2 : Declare two integer variables m and n for the lower and upper bounds of
the range
STEP 3 : Accept values for m and n from the user
STEP 4 : Validate that:
- m is at least 3 digits (i.e., m ≥ 100)
- n is at most 4 digits (i.e., n ≤ 10000)
- m is less than n
If invalid, display "INVALID INPUT" and terminate the program
STEP 5 : Initialize a frequency counter to 0 to count the fascinating numbers found
STEP 6 : Loop through all numbers i from m to n
STEP 7 : For each number i, do the following:
- Convert i, 2 × i, and 3 × i into strings
- Concatenate these three strings into a single strin
STEP 8 : Check whether the concatenated string contains **each digit from 1 to 9
exactly once**
STEP 9 : If it does, then:
- Print the number
- Increment the frequency counter
STEP 10 : After checking all numbers, if frequency is 0, print "NIL"
STEP 11 : Display the total frequency of fascinating numbers found
STEP 12 : End of the program
32
33
34
35
Question 6
Variable
Data Type Description
Name
Holds the reference string "123456789" used to
check String
verify if all digits 1–9 are present.
Lower bound of the number range to check for
m int
fascinating numbers.
Upper bound of the number range to check for
n int
fascinating numbers.
Keeps track of the total number of fascinating
frequency int
numbers found.
Used to determine whether a number satisfies
flag boolean
the conditions of a fascinating number.
in Scanner Scanner object used to take input from the user.
Object of the class used to invoke the
obj FascinatingNumber
generateFascinatingNumbers() method.
Loop control variable for traversing numbers
i int
from m to n.
Holds the current number being checked for
numb int
fascinating properties.
Counts how many times a digit appears in the
digitFrequency int
concatenated string.
Concatenated result of numb, numb*2, and
temp String
numb*3.
Loop control variable for checking each digit
j int
from '1' to '9'.
36
Question 7
Write a program to accept a paragraph containing TWO sentences
only. The sentence may be terminated by either ‘.’ , ‘ ? ’ or ‘ ! ’ only. Any
other character may be ignored. The words are to be separated by a
single blank space and must be in UPPER CASE. Perform the following
tasks:
a. Check for the validity of the accepted paragraph for the number of sentences
and for terminating character.
b. Separate the two sentences from the paragraph and fid the common words in
the two sentences with their frequency of occurrence in the paragraph.
c. Display both the sentences separately along with the common words and their
frequency, In the format given below,
Test your program with the sample data and some random data:
Example 1:
INPUT : IS IT RAINING ? YOU MAY GET WET IF IT IS RAINING
OUTPUT: IS IT RAINING ? YOU MAY GET WET IF IT IS RAINING
COMMON WORDS FREQUENCY
IS 2
IT 2
RAINING 2
Example 2:
INPUT : INDIA IS MY MOTHERLAND AND I AM PROUD OF MY
MONTHERLAND.
ALL INDIANS ARE MY BROTHERS AND SYSTER.
OUTPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY
MONTHERLAND. ALL INDIANS ARE MY BROTHERS AND SYSTER
COMMON WORDS FREQUENCY
MY 3
AND 2
Example 4:
INPUT : AT LAST , THE TIGER WAS SAVED.
OUTPUT: INVALID INPUT
37
Question 7
STEP 1 : Start the program
STEP 2 : Accept a paragraph input from the user that contains exactly two sentences
in UPPER CASE
STEP 3 : Check if the input contains any lowercase letters
- If yes, display "INVALID INPUT" and terminate the program
STEP 4 : Count the number of sentence-ending punctuation marks (., ?, !)
- If the count is not equal to 2, display "INVALID INPUT" and terminate the
program
STEP 5 : Split the paragraph into two separate sentences using the punctuation
marks as delimiters
STEP 6 : Remove punctuation from the paragraph and store all words in an array
STEP 7 : Remove punctuation from each of the two sentences and split them into
words using space as the separator
STEP 8 : Initialize two arrays: one to store common words and another to store their
frequencies
STEP 9 : For each word in the first sentence, check if it also exists in the second
sentence
- If yes, check if it has already been recorded as a common word
- If not recorded, add the word to the common words array
STEP 10 : For each common word found, count how many times it appears in the
entire paragraph and store that in the frequency array
STEP 11 : If any common words are found, display them along with their frequencies
STEP 12 : If no common words are found, display "NO COMMON WORDS
FOUND."
STEP 13 : End of the program
38
39
40
41
42
Question 7
Data
Variable Name Description
Type
sc Scanner Scanner object used to accept input from the user.
Stores the entire paragraph entered by the user (should
paragraph String
contain exactly two sentences in UPPER CASE).
Flag used to check if the paragraph contains any
hasLowerCase boolean
lowercase letters.
Counts the number of sentence-ending punctuations (., ?,
sentenceCount int
!) to ensure exactly 2 sentences.
Array to store the two individual sentences extracted from
tempSentences String[]
the paragraph.
Index used to track the sentence being stored in the
idx int
tempSentences array.
Temporary string used to accumulate characters while
sentence String
separating sentences.
Stores the paragraph after removing punctuation, used
cleanPara String
for word frequency calculation.
Array of all words in the cleaned paragraph (used to
allWords String[]
calculate word frequencies).
Cleaned version of the first sentence (without
sentence1 String
punctuation).
Cleaned version of the second sentence (without
sentence2 String
punctuation).
words1 String[] Array of words from the first sentence.
words2 String[] Array of words from the second sentence.
Array to store the common words found in both
commonWords String[]
sentences.
Array to store frequency of each common word found in
freq int[]
the paragraph.
commonCount int Counter for the number of common words found.
43
Data
Variable Name Description
Type
i, j, k, p int Loop control variables used for traversal and comparison.
Flag used to check if a word already exists in the
exists boolean
commonWords array.
Temporary counter for frequency of a specific word in
count int
allWords.
44
Question 8
Circular Prime is a prime number that remains prime under cyclic shifts of its digits.
When the leftmost digit is removed and replaced at the end of the remaining string of
digits, the generated number is still prime. The process is repeated until the original
number is reached again. A number is said to be prime if it has only two factors 1
and itself.
Example:
131
311
113
Hence, 131 is a circular prime.
Accept a positive number N and check whether it is a circular prime or not. The new
numbers formed after the shifting of the digits should also be displayed. Test your
program with the following data and some random data:
Example 1
INPUT:
N = 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME.
Example 2
INPUT:
N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME.
Example 3
INPUT:
N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME.
45
Question 8
STEP 1 : Start the program
STEP 2 : Accept an integer value N from the user
STEP 3 : Convert the number N into a string and find the number of digits (length)
STEP 4 : Initialize a boolean variable to assume N is a circular prime
STEP 5 : Repeat the following steps for the number of digits in N:
a) Convert the current string representation into an integer
b) Display this rotated number
c) Check if the number is prime using the following logic:
- If the number is ≤ 1, it is not prime
- Check divisibility from 2 to square root of the number
d) If any rotation is not prime, mark the circular prime flag as false
e) Perform a left rotation on the string by shifting the first character to the end
STEP 6 : If the circular prime flag is still true, display that N is a Circular Prime
STEP 7 : Otherwise, display that N is not a Circular Prime
STEP 8 : End of the program
46
47
48
Question 8
Variable Data
Description
Name Type
sc Scanner Scanner object used to accept user input.
The original number entered by the user to check for
n int
circular prime.
str String String representation of the number n.
Length of the number in digits (used to control number of
len int
rotations).
Flag that remains true if all rotations of the number are
isCircularPrime boolean
prime.
Temporarily stores each rotated version of the number
temp String
string.
Loop control variable for rotating the number and
i int
checking each version.
Stores the current rotated version of the number
num int
(converted from temp).
The number passed to the isPrime method to check for
x (in isPrime()) int
primality.
49
Question 9
A company manufactures packing cartons in four sizes, i.e. cartons to accommodate
6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number
of boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the
break-up of the cartons used in descending order of capacity (i.e. preference should
be given to the highest capacity available, and if boxes left are less than 6, an extra
carton of capacity 6 should be used.) Test your program with the following data and
some random data:
Example 1
INPUT:
N = 726
OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726 Total number of
cartons = 16
Example 2
INPUT:
N = 140
OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6*1=6
Remaining boxes = 2 * 1 = 2 Total
number of boxes = 140 Total number
of cartons = 6
Example 3
INPUT:
N = 4296
OUTPUT:
INVALID INPUT
50
Question 9
STEP 1 : Start the program
STEP 2 : Accept the number of boxes N from the user
STEP 3 : Check if N is within the valid range (1 to 1000)
- If not, display "INVALID INPUT" and terminate the program
STEP 4 : Store the original value of N for final reporting
STEP 5 : Initialize a counter to track the total number of cartons used
STEP 6 : Divide N by 48 to get the number of 48-capacity cartons
- Print the count and total boxes packed in 48s (if any)
- Add to the total carton counter
- Update N to the remaining boxes using modulus
STEP 7 : Repeat the same logic for 24-capacity cartons:
- Divide N by 24, print result if count > 0, and update N
STEP 8 : Repeat the same for 12-capacity cartons
STEP 9 : Repeat the same for 6-capacity cartons
STEP 10 : If any boxes are left (N > 0), print them as remaining boxes
- Count them as 1 additional carton
STEP 11 : If no boxes are left, display "Remaining boxes = 0"
STEP 12 : Display the original number of boxes and total number of cartons used
STEP 13 : End of the program
51
52
53
54
Question 9
Variable Data
Description
Name Type
sc Scanner Scanner object used to take input from the user.
Stores the number of boxes to be packed. It is updated
N int
during packing.
Stores the original value of N for displaying the total
originalN int
number of boxes.
cartons int Stores the total number of cartons used.
c48 int Number of cartons with capacity 48 used.
c24 int Number of cartons with capacity 24 used.
c12 int Number of cartons with capacity 12 used.
c6 int Number of cartons with capacity 6 used.
55
Question 10
A class Merger concatenates two positive integers that are greater than 0 and
produces a newly merged integer.
Example: If the first number is 23 and the second is 764, then the concatenated
number will be 23764.
Some of the members of the class are given below:
Class name : Merger
Data members/instance variables:
n1 : long integer to store the first number
n2 : long integer to store the second number
mergNum : long integer to store the merged number
Member functions:
Merger() : constructor to initialize the data members
void readNum() : to accept the values of the data members n1 and n2
void joinNum() : to concatenate the numbers n1 and n2 and store it in mergNum
void show() :to display the original numbers and the merged number with
appropriate messages
Specify the class Merger giving the details of the constructor, void readNum(), void
joinNum() and void show(). Define the main() function to create an object and call the
functions accordingly to enable the task.
56
Question 10
STEP 1 : Start the program
STEP 2 : Declare three long variables: n1, n2 (to store input numbers), and
mergNum (to store the merged result)
STEP 3 : Accept the first number (n1) from the user
STEP 4 : Accept the second number (n2) from the user
STEP 5 : Validate that both numbers are greater than 0
- If not, display "INVALID INPUT!" and terminate the program
STEP 6 : Convert both numbers to strings
STEP 7 : Concatenate the two strings to form one combined string
STEP 8 : Convert the concatenated string back to a long integer and store it in
mergNum
STEP 9 : Display the original values of n1 and n2
STEP 10 : Display the merged number
STEP 11 : End of the program
57
58
59
Question 10
Variable Data
Description
Name Type
n1 long Stores the first positive number input by the user.
n2 long Stores the second positive number input by the user.
Stores the result after merging n1 and n2 as a single
mergNum long
number.
sc Scanner Scanner object used to read input from the user.
str1 String Holds the string representation of the first number (n1).
Holds the string representation of the second number
str2 String
(n2).
Concatenated string of str1 and str2, which is converted to
mergedStr String
mergNum.
60
Question 11
Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words are to be separated by a single blank space and are in UPPER
CASE. Perform the following tasks:
[Link] for the validity of the accepted sentence only for the terminating
character.
[Link] the words in ascending order of their length. If two or more words have
the same length, then sort them alphabetically.
[Link] the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1:
INPUT:
AS YOU SOW SO SHALL YOU REAP.
OUTPUT:
AS YOU SOW SO SHALL YOU REAP. AS SO
SOW YOU YOU REAP SHALL
Example 2:
INPUT:
SELF HELP IS THE BEST HELP.
OUTPUT:
SELF HELP IS THE BEST HELP. IS THE BEST
HELP HELP SELF
Example 3:
INPUT:
BE KIND TO OTHERS.
OUTPUT:
BE KIND TO OTHERS. BE TO KIND
OTHERS
Example 4:
INPUT:
NOTHING IS IMPOSSIBLE#
OUTPUT:
INVALID INPUT
61
Question 11
STEP 1 : Start the program
STEP 2 : Accept a sentence from the user in UPPER CASE that ends with '.', '?' or '!'
STEP 3 : Check if the last character of the sentence is one of the valid punctuation
marks (., ?, !)
- If not, display "INVALID INPUT" and terminate the program
STEP 4 : Check that all characters (excluding the last punctuation mark) are in
uppercase
- If any lowercase character is found, display "INVALID INPUT" and terminate
the program
STEP 5 : Display the original sentence
STEP 6 : Remove the ending punctuation from the sentence
STEP 7 : Split the sentence into words using space as the delimiter
STEP 8 : Apply bubble sort to the array of words using the following rules:
- Sort by increasing word length
- If two words have the same length, sort them alphabetically
STEP 9 : Display the sorted words in a single line, separated by spaces
STEP 10 : End of the program
62
63
64
Question 11
Variable Data
Description
Name Type
sc Scanner Scanner object used to take input from the user.
Stores the user input sentence, which should be in
sentence String
uppercase and end with ., ?, or !.
Stores the last character of the sentence to check if it ends
lastChar char
with valid punctuation.
Array that holds the individual words of the sentence after
words String[]
splitting.
Stores the number of words in the sentence. Used for
len int
controlling the sort loop.
Temporary variable used for swapping strings during
temp String
bubble sort.
i, j int Loop control variables used in sorting.
Temporarily holds characters while validating if all are
ch char
uppercase.
word String Used in enhanced for-loop to print sorted words.
65
Question 12
Write a program to convert decimal to hexadecimal.
66
Question 12
STEP 1 : Start the program
STEP 2 : Accept a decimal number from the user and store it in a variable
STEP 3 : Initialize an empty string to store the hexadecimal result
STEP 4 : If the input number is 0, set hexadecimal result to "0"
STEP 5 : Otherwise, repeat the following steps until the number becomes 0:
a) Find the remainder when the number is divided by 16
b) Convert the remainder to its hexadecimal digit:
- If remainder is less than 10, convert to characters '0' to '9'
- If remainder is between 10 and 15, convert to characters 'A' to 'F'
c) Add the hex digit to the front of the result string
d) Divide the number by 16 to continue processing
STEP 6 : Display the final hexadecimal result along with the original decimal number
STEP 7 : End of the program
67
68
69
Question 12
Data
Variable Name Description
Type
sc Scanner Scanner object used to take input from the user.
decimalNumber int The original decimal number input by the user.
Stores the final hexadecimal representation of the
hex String
decimal number.
Stores the remainder when the decimal number is
rem int
divided by 16.
Stores the hexadecimal character corresponding to the
hexChar char
remainder.
Copy of decimalNumber used for calculations to
num int
preserve original input.
70
Question 13
Write a Program in Java to input a number and check whether it is a Harshad
Number Or NivenNumber or not.. Harshad Number: A Harshad number (or Niven
number), is an integer (in base 10) that isdivisible by the sum of its digits.
71
Question 13
STEP 1 : Start the program
STEP 2 : Accept a positive integer from the user
STEP 3 : If the number is less than or equal to 0, display "INVALID INPUT" and
terminate the program
STEP 4 : Store the number in a temporary variable for processing
STEP 5 : Initialize a variable to store the sum of digits
STEP 6 : Repeat the following steps until the temporary number becomes 0:
a) Extract the last digit using modulus 10
b) Add the digit to the sum
c) Remove the last digit by dividing by 10
STEP 7 : Check if the original number is divisible by the sum of its digits
STEP 8 : If divisible, display that the number is a Harshad Number
STEP 9 : If not divisible, display that the number is not a Harshad Number
STEP 10 : End of the program
72
73
74
Question 13
Variable Data
Description
Name Type
sc Scanner Scanner object used to accept user input.
num int Stores the original number entered by the user.
Temporary variable used to extract and sum digits of
temp int
num.
sumOfDigits int Holds the sum of all digits of the number.
75
Question 14
An emirp number is a number which is prime backwards and forwards. Example: 13
and 31 are both prime numbers. Thus, 13 is an emirp number. Design a class Emirp
to check if a given number is Emirp number or not.
Some of the members of the class are given below:
Class name : Emirp
Data members/instance variables:
N : stores the number
Rev : stores the reverse of the
number f : stores the divisor
Member functions:
Emirp(int nn) : to assign n = nn, rev = 0 and f = 2
int isprime(int x) : check if the number is prime using the recursive technique and
return 1 if prime otherwise return 0
void isEmirp() : reverse the given number and check if both the original number
and the reverse number are prime, by invoking the function isprime(int) and
display the result with an appropriate message
Specify the class Emirp giving details of the constructor(int), int isprime (int) and void
isEmirp(). Define the main function to create an object and call the methods to check
for Emirp number.
76
Question 14
STEP 1 : Start the program
STEP 2 : Accept a number from the user and store it in a variable
STEP 3 : Initialize variables to store the reverse of the number and a divisor (f = 2)
STEP 4 : Define a recursive method to check if a number is prime:
a) If the number is less than 2, return not prime
b) If the divisor is greater than half the number, return prime
c) If the number is divisible by the current divisor, return not prime
d) Increment the divisor and recursively check again
STEP 5 : Reverse the original number using a loop:
a) Extract the last digit
b) Add it to the reverse number after shifting digits left
c) Remove the last digit from the original number
STEP 6 : Check if the original number is prime using the recursive method
STEP 7 : Reset the divisor to 2 and check if the reversed number is also prime
STEP 8 : If both original and reversed numbers are prime, and they are not the
same:
- Display that the number is an Emirp number
STEP 9 : Otherwise, display that the number is not an Emirp number
STEP 10 : End of the program
77
78
79
Question 14
Variable Data
Description
Name Type
N int Stores the input number provided by the user.
Rev int Stores the reversed form of the number N.
Used as a divisor in the recursive prime-checking method
f int
isprime().
temp int Temporary variable used for reversing the number.
Stores the result (1 or 0) indicating if the original number
originalPrime int
is prime.
Stores the result (1 or 0) indicating if the reversed
reversedPrime int
number is prime.
Local variable in main() method to store the number
num int
entered by the user.
sc Scanner Scanner object used to read input from the user.
80
Question 15
A class SeriesSum is designed to calculate the sum of the following series:
Some of the members of the class are given below:
Class name : SeriesSum
Data members/instance variables:
x : to store an integer number
n : to store the number of terms
Sum : double variable to store the sum of the series
Member functions:
SeriesSum(int xx, int nn) : constructor to assign x=xx and n=nn
double find_fact(int m) : to return the factorial of m using the recursive technique.
double find_power(int x, int y) : to return x raised to the power of y using the
recursive technique.
void calculate() : to calculate the sum of the series by invoking the recursive
functions respectively
void display() : to display the sum of the series
Specify the class SeriesSum, giving details of the constructor(int, int), double find
fact(int), double find power(int, int), void calculate() and void display(). Define the
main() function to create an object and call the functions accordingly to enable the
task.
81
Question 15
STEP 1 : Start the program
STEP 2 : Accept the base value X and the number of terms N from the user
STEP 3 : Initialize the sum of the series as 0
STEP 4 : Define a recursive method to calculate factorial:
- If the input is less than or equal to 1, return 1
- Else return input * factorial of (input - 1)
STEP 5 : Define a recursive method to calculate power:
- If exponent is 0, return 1
- Else return base * power(base, exponent - 1)
STEP 6 : Initialize variables for the starting power (2) and factorial index (1)
STEP 7 : Loop from 1 to N (number of terms):
a) Calculate x raised to the current power using the power method
b) Calculate the factorial of the current index using the factorial method
c) Divide power result by factorial result and add to the sum
d) Increment power by 2 and factorial index by 2
STEP 8 : Display the final sum of the series
STEP 9 : End of the program
82
83
84
85
Question 15
Variable
Data Type Description
Name
x int Stores the base value for calculating powers in the series.
n int Stores the number of terms in the series.
Sum double Stores the final result (sum) of the computed series.
xx int Constructor parameter to initialize x.
nn int Constructor parameter to initialize n.
m int Parameter used in recursive factorial method find_fact().
Tracks the current power to raise x to in each term of the
power int
series (starts from 2).
Tracks the current factorial denominator for each term
fact int
(starts from 1).
p double Stores the result of x raised to the power power.
f double Stores the result of the factorial of fact.
sc Scanner Scanner object used for reading user input.
Object of the class SeriesSum used to call methods on
obj SeriesSum
input values.
86
CONCLUSION
Java is a powerful, versatile, and platform-independent programming language that
forms the backbone of many modern applications. It promotes object-oriented
programming and is widely used in various fields like web development, mobile
apps, and enterprise software. BlueJ, on the other hand, is a beginner-friendly
Integrated Development Environment (IDE) specially designed to help students learn
Java easily. With its simple interface and visual features, BlueJ makes understanding
Java concepts like classes, objects, and methods more interactive and effective.
Together, Java and BlueJ provide an excellent foundation for anyone starting their
programming journey.
87
ACKNOWLEDGEMENT
I would like to express my sincere gratitude to my Computer Science teacher for
introducing me to the fundamentals of Java programming and guiding me throughout
the learning process. I am also thankful to the developers of BlueJ for creating
such a user-friendly environment that made learning Java more interactive and
enjoyable. Lastly, I would like to thank my school and classmates for their constant
support and encouragement during this project.
88