Computer Science Project Term 1 (Xii)
Computer Science Project Term 1 (Xii)
Page 1 of 57
QUESTION
The coordinates of a point P on a two dimensional plane can be represented by P(x,y) with x as
the x-coordinate and y as the y-coordinate.The coordinates of midpoint of two points P1(x1,yI)
and P2(x2,y2) can be calculated as P(x,y) where:
x=(xl+x2)/2,y=(y1+y2)/2
Classname : Point
DataMembers / instancevariables:
Member functions:
Specify the class Point giving details of the constructor() , member functions void readpoint()
Point midpoint(Point,Point) and void displaypoint() along with the main() function to create an
object and call the functions accordingly to calculate the midpoint between any two given points.
Page 2 of 57
ALGORITHM
Step 1: Start.
Step 5: In the readpoint() method, take the values of x and y as input from the user.
Step 7: In the midpoint() method, calculate and return the mid-point of the two points A and B.
Step 8: In the main() method, create necessary objects and print the required co-ordinates.
Step 9: Stop.
Page 3 of 57
SOURCE CODE
import java.util.Scanner;
double x,y;
public Point()
x=0.0;
y=0.0;
System.out.println("enter x");
x=sc.nextDouble();
System.out.println("enter y");
y=sc.nextDouble();
System.out.println(x);
System.out.println(y);
Page 4 of 57
{
C.x=(A.x+B.x)/2;
C.y =(A.y+B.y)/2;
return C;
p.readpoint();
q.readpoint();
r=r.midpoint(p,q);
p.displaypoint();
q.displaypoint();
r.displaypoint();
Page 5 of 57
VARIABLE DESCRIPTION
TABLE
Variable Data Type Purpose
x int Value of x-ordinate taken as
input from the user.
y int Value of y-ordinate taken as
input from the user.
Page 6 of 57
OUTPUTS
Page 7 of 57
PROGRAM 2
Page 8 of 57
QUESTION
A unique-digit integer is a positive integer (without leading zeros) with no duplicates digits. For
example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not. Given two
positive integers m and n, where m < n, write a program to determine how many unique-digit
integers are there in the range between m and n (both inclusive) and output them. The input
contains two positive integers m and n. Assume m < 30000 and n < 30000. You are to output the
number of unique-digit integers in the specified range along with their values in the format
specified below:
Sample Input:
m = 100
n = 120
Sample Output: The Unique-Digit integers are: 102, 103, 104, 105, 106, 107, 108, 109, 120.
Frequency of unique-digit integers is: 9
Sample Input:
m = 2500
n = 2550
Sample Output: The Unique-Digit integers are: 2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510,
2513, 2514, 2516, 2517, 2518, 2517, 2530, 2519, 2530, 2531, 2534, 2536, 2537, 2538, 2539,
2540, 2541, 2543, 2546, 2547, 2548, 2549.
Frequency of unique-digit integers is: 28.
Page 9 of 57
ALGORITHM
Step 1: Start.
Step 3: If m (or n) < 1, m (or n) > 30000 or m > n, print an error message and terminate the
program accordingly.
Step 4: Declare a variable of int data type to count the number of unique numbers. (count)
Step 6: Declare an array of boolean data type (visited[]), a variable (isUnique) to check if a
number is unique and create a copy variable of the looping variable (num).
Step 9: Divide the number by 10 inside the while loop for every iteration.
Step 10: If isUnique returns true i.e. the number is unique, then add 1 to count and print the
unique number.
Step 11: Outside the loops, print the frequency of unique-digit integers in the next line.
Page 10 of 57
SOURCE CODE
import java.util.Scanner;
System.out.print("Enter m: ");
int m = in.nextInt();
return;
System.out.print("Enter n: ");
int n = in.nextInt();
return;
if (m > n)
Page 11 of 57
return;
int count = 0;
int num = i;
while (num != 0)
if (visited[d])
isUnique = false;
break;
visited[d] = true;
num /= 10;
if (isUnique)
count++;
Page 12 of 57
}
System.out.println();
VARIABLE DESCRIPTION
TABLE
Variable Data Type Purpose
m int User input
n int User input
i int Looping variable
num int Copy variable
count int Counts the number of unique-
digit numbers
d int Stores modulus of num
visited[] boolean Array to check if a number is
unique-digit or not
isUnique boolean Returns true if the number is
unique-digit
Page 13 of 57
OUTPUTS
Page 14 of 57
PROGRAM 3
Page 15 of 57
QUESTION
An Evil number is a positive whole number which has even number of 1's in its binary
equivalent. Example: Binary equivalent of 9 is 1001, which contains even number of 1's. A few
evil numbers are 3, 5, 6, 9…. Design a program to accept a positive whole number and find the
binary equivalent of the number and count the number of 1's in it and display whether it is a Evil
number or not with an appropriate message. Output the result in format given below:
Example 1
Input: 15
Binary Equivalent: 1111
No. of 1's: 4
Output: Evil Number
Example 2
Input: 26
Binary Equivalent: 11010
No. of 1's: 3
Output: Not an Evil Number
Page 16 of 57
ALGORITHM
Step 1: Start.
Step 3: If the integer is negative, display an error message and terminate the program.
Step 4: Declare variables of int data type to count the number of 1’s (count), store the exponent
of cardinal number 10 (p) and store the binary equivalent of the input (binNum).
Step 5: Inside a while loop, provided that the input is positive, calculate the binary equivalent of
the integer using the declared variables.
Step 6: Print the binary equivalent and the number of 1’s in it.
Step 7: If the number of 1’s is even (count%2==0), print “Evil Number”, otherwise print “Not an
Evil Number”.
Step 8: Stop.
Page 17 of 57
SOURCE CODE
import java.util.Scanner;
int n = in.nextInt();
if (n < 0)
System.out.println("Invalid Input");
return;
int count = 0;
int p = 0;
int binNum = 0;
while (n > 0)
int d = n % 2;
if (d == 1)
count++;
p++;
Page 18 of 57
n /= 2;
if (count % 2 == 0)
else
VARIABLE DESCRIPTION
TABLE
Variable Data Type Purpose
n int User input
count int Counts the number of 1’s in
the input’s binary equivalent
p int Stores the exponent of
cardinal number 10
binNum int Stores the binary equivalent
d int Stores remainder when input
is divided by 2
Page 19 of 57
OUTPUTS
Page 20 of 57
PROGRAM 4
Page 21 of 57
QUESTION
The result of a quiz competition is to be prepared as follows: The quiz has five questions with
four multiple choices (A, B, C, D), with each question carrying 1 mark for the correct answer.
Design a program to accept the number of participants N such that N must be greater than 3 and
less than 11. Create a double-dimensional array of size (Nx5) to store the answers of each
participant row-wise. Calculate the marks for each participant by matching the correct answer
stored in a single-dimensional array of size 5. Display the scores for each participant and also the
participant(s) having the highest score. Example: If the value of N = 4, then the array would be:
Q1 Q2 Q3 Q4 Q5
Participant 1 A B B C A
Participant 2 D A D C B
Participant 3 A A B A C
Participant 4 D C C A B
Key to the D C C B A
question:
Note: Array entries are line fed (i.e. one entry per line)
Test your program for the following data and some random data.
Example 1
INPUT:
N=5
Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D
Key: B C D A A
OUTPUT:
Scores:
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2
Page 22 of 57
Highest Score:
Participant 5
Example 2
INPUT:
N=4
Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B
Key: A C D B B
OUTPUT:
Scores:
Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3
Highest Score:
Participant 1
Participant 4
Example 3
INPUT:
N = 12
OUTPUT:
INPUT SIZE OUT OF RANGE.
Page 23 of 57
ALGORITHM
Step 1: Start.
Step 3: If n <= 3 or n >= 11, print an error message and terminate the program accordingly.
Step 5: Using a nested for loop, take the answers of the participants as user input.
Step 6: Using another for loop, take the answer key as user input.
Step 7: Declare a variable of int type to store highest score achieved by a participant (hScore).
Step 8: Declare an array to store and print the scores of each participant (score[]).
Step 9: Using a nested for loop, find the highest score and print the scores.
Step 10: Using a for loop, print the number of the participant with the highest score.
Page 24 of 57
SOURCE CODE
import java.util.Scanner;
int n = in.nextInt();
return;
answers[i][j] = in.next().charAt(0);
Page 25 of 57
}
key[i] = in.next().charAt(0);
int hScore = 0;
System.out.println("Scores:");
if (answers[i][j] == key[j])
score[i]++;
hScore = score[i];
System.out.println("Highest Score:");
Page 26 of 57
for (int i = 0; i < n; i++)
if (score[i] == hScore)
VARIABLE DESCRIPTION
TABLE
Variable Data Type Purpose
n int Number of participants taken
as user input
i int Looping variable in for loop
j int Looping variable in for loop
hScore int Stores highest score
score[] int Stores score of the participants
answers[][] char Answers of participants taken
as user input
key[] char Answer key taken as user
input
Page 27 of 57
OUTPUTS
Page 28 of 57
Page 29 of 57
PROGRAM 5
Page 30 of 57
QUESTION
Caesar Cipher is an encryption technique which is implemented as ROT13 ('rotate by 13 places').
It is a simple letter substitution cipher that replaces a letter with the letter 13 places after it in the
alphabets, with the other characters remaining unchanged.
ROT13
A/a B/b C/c D/d E/e F/f G/g H/h I/i J/j K/k L/l M/m
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/m O/o P/p Q/q R/r S/s T/t U/u V/v W/w X/x Y/y Z/z
Write a program to accept a plain text of length L, where L must be greater than 3 and less than
100.
Test your program with the sample data and some random data.
Example 1
INPUT:
Hello! How are you?
OUTPUT:
The cipher text is: Uryyb! Ubj ner lbh?
Example 2
INPUT:
Encryption helps to secure data.
OUTPUT:
The cipher text is:
Rapelcgvba urycf gb frpher qngn.
Example 3
INPUT:
You
OUTPUT:
INVALID LENGTH
Page 31 of 57
ALGORITHM
Step 1: Start.
Step 3: If the length of the string is <=3 or >=100, display an error message and terminate the
program accordingly.
Step 6: Substitute the characters with their corresponding characters as provided in the question.
Step 9: Stop.
Page 32 of 57
SOURCE CODE
import java.util.Scanner;
System.out.println("INVALID LENGTH");
return;
char ch = str.charAt(i);
if ((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))
sb.append((char)(ch + 13));
else if ((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))
Page 33 of 57
{
sb.append((char)(ch - 13));
else
sb.append(ch);
System.out.println(cipher);
VARIABLE DESCRIPTION
TABLE
Variable Data Type Purpose
len int Stores length of the input
String
i int Looping variable in for loop
ch char Stores each character of the
input String
str String String taken as user input
cipher String Stores the encoded String
Page 34 of 57
OUTPUTS
Page 35 of 57
PROGRAM 6
Page 36 of 57
QUESTION
Write a program which first inputs two integers, the first between 1 to 12 (inclusive) and the
second between 0 to 59 to print the time in words.
Input:
Time = 3, 0
Output = 3 o’ clock
Input:
Time = 3, 1
Output = one minute past 3
Input:
Time = 3, 15
Output = quarter past 3
Input:
Time = 7, 29
Output = 29 minutes past 7
Input:
Time = 8, 30
Output = half past 8
Input:
Time=6, 34
Output = 26 minutes to 7
Input:
Time = 12, 45
Output = quarter to 1
Input:
Time = 12, 59
Output = one minute to 1
Page 37 of 57
Input:
Time = 14, 16
Output = Invalid Input!
ALGORITHM
Step 1: Start.
Step 2: Take the hours and minutes as input from the user.
Step 3: If the hours are not between 1 and 12 (inclusive) or the minutes are not between 0 and 59
(inclusive), print an error message and terminate the program accordingly.
Step 4: Declare a String array would store the numbers in words (zero to twenty-nine).
Step 5: Using the String array, print the time in words using different print statements for
different values or range of values of minutes taken as input.
Step 6: Stop.
SOURCE CODE
// Java program to convert time into words
import java.util.*;
if (m == 0)
else if (m == 1)
nums[h]);
else if (m == 59)
else if (m == 15)
else if (m == 30)
else if (m == 45)
System.out.println("quarter to " +
nums[h]);
Page 39 of 57
}
// Driven code
int h = in.nextInt();
int m = in.nextInt();
if(h<1||h>12||m<0||m>59)
System.out.println("Invalid Input!");
return;
printWords(h, m);
VARIABLE DESCRIPTION
TABLE
Variable Data Type Purpose
h int Stores hour(s) taken as input
m int Stores minute(s) taken as input
nums[] String Stores numbers in words
Page 40 of 57
OUTPUTS
Page 41 of 57
Page 42 of 57
PROGRAM 7
Page 43 of 57
QUESTION
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:
Member functions:
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.
Page 44 of 57
ALGORITHM
Step 1: Start.
Step 5: In isprime(int x), using recursion, return 1 if the number is prime otherwise return 0.
Step 7: If the number is an Emirp number, print “Emirp Number” otherwise print “Not an Emirp
Number”.
Step 8: In main() method, take a number as input from the user and invoke the above methods.
Step 9: Stop.
Page 45 of 57
SOURCE CODE
import java.util.Scanner;
int n,rev,f;
Emirp(int nn)
n=nn;
rev=0;
f=2;
int isprime(int x)
if(n==x)
return 1;
return 0;
else
return isprime(x+1);
Page 46 of 57
}
void isEmirp()
int x=n;
while(x!=0)
rev=(rev*10)+x%10;
x=x/10;
int ansl=isprime(f);
x=n;
n=rev;
f=2;
int ans2=isprime(f);
else
Page 47 of 57
{
System.out.println("Enter a number");
int x=sc.nextInt();
obj1.isEmirp();
VARIABLE DESCRIPTION
TABLE
Variable Data Type Purpose
N int Static variable; stores user
input
Rev int Stores reverse of the input
F int Stores the divisor which
divides the input
Nn int Formal parameter in Emirp()
X int Number taken as user input
ans1 int Stores returned number when
the number is passed to
isprime()
ans2 int Stores returned number when
the reverse of the number is
passed to isprime()
Page 48 of 57
OUTPUTS
Page 49 of 57
PROGRAM 8
Page 50 of 57
QUESTION
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 uppercase. Perform the following
tasks:
(b) Convert the non-palindrome words of the sentence into palindrome words by concatenating
the word by its reverse (excluding the last character).
Example:
The reverse of the word HELP would be LEH (omitting the last alphabet) and by concatenating
both, the new palindrome word is HELPLEH. Thus, the word HELP becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would become ABBA
and not ABBBA and XAZZZ becomes XAZZZAX.
[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]
(c) Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
INPUT:
THE BIRD IS FLYING.
OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF
Example 2
INPUT:
IS THE WATER LEVEL RISING?
OUTPUT:
IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR
Example 3
Page 51 of 57
INPUT:
THIS MOBILE APP LOOKS FINE.
OUTPUT:
THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF
Example 4
INPUT:
YOU MUST BE CRAZY#
OUTPUT:
INVALID INPUT
ALGORITHM
Step 1: Start.
Step 4: Declare a method isPalindrome() of boolean return type which would return true if the
parameter is a palindrome.
Step 5: Declare another method makePalindrome() of String return type which would convert the
non-palindrome words into palindrome words by concatenating each word with its reverse.
Step 8: Take a sentence as input from the user and store it in the variable ipStr of String type.
Step 9: If the sentence is not terminated with ‘.’, ‘?’ or ‘!’, display an error message and
terminate the program accordingly.
Step 10: Using the above methods, construct the output and display it.
Page 52 of 57
SOURCE CODE
import java.util.*;
palin = false;
break;
return palin;
Page 53 of 57
int i = len - 1;
i--;
sb.append(word.charAt(j));
return sb.toString();
if (lastChar != '.'
Page 54 of 57
System.out.println("INVALID INPUT");
return;
while (st.hasMoreTokens())
if (isPalinWord)
sb.append(word);
else
sb.append(palinWord);
sb.append(" ");
System.out.println();
Page 55 of 57
System.out.println(ipStr);
System.out.println(convertedStr);
VARIABLE DESCRIPTION
TABLE
boolean isPalindrome():
String makePalindrome():
main():
Page 56 of 57
OUTPUTS
Page 57 of 57