Java in BlueJ Project
Java in BlueJ Project
QUESTION 1
Design a programme to accept a day number (between 1 and 366), year (in 4 digit) from the
user to generate and display the corresponding date. Also accept 'N' (1<=N<=100) from the
user to compute and display the future date corresponding to 'N' days after the generated
date. Display an error message if the value of the day number, year and N are not within the
limit or not according to the conditions specified.
Test your programme for the following data and some random data.
Example 1:
InputDAY NUMBER
YEAR
DATE AFTER (N)
Output20 TH AUGUST 2008
DATE AFTER 17 DAYS
Example 2:
InputDAY NUMBER
YEAR
DATE AFTER (N)
Output25 TH DECEMBER 2008
DATE AFTER 45 DAYS
233
2008
17
360
2008
45
Start
Take day as input from console and store it in int variable d1
Take year as input from console and store it in int variable y1
Take N as input from console and store it in int variable n
Call the function Date as: obj.Date(d1,y1)
Check if n>=1 and n<=100, if yes then,
Assign d1=d1+n
Check if (y1%400==0) or (y%4==0) and (y%100!=0), if yes then,
Assign y1=y1+1
Assign d1=d1-366
Call the function Date as: obj.Date(d1,y1)
Else check if (y1%400!=0) and (y1%4!==0), if yes then,
Assign y1=y1+1
Assign d1=d1-365
Call the function Date as: obj.Date(d1,y1)
Else
Call the function Date as: obj.Date(d1,y1)
7. Else
Print "OUT OF RANGE"
8. End of main() method
Algorithm for function Date(int d, int y)
1. Initialise an int array as:
a[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
2. Check if (y1%400==0) or (y%4==0) and (y%100!=0), if yes then,
Assign a[1]=29
3. Initialise a String array as:
m[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AU
GUST", "SEPTEMBER","OCTEBER","NOVEMBER","DECEMBER"}
4. Initialise two int variables as i=0 and j=0
5. Initialise a String variable as month=""
6. Repeat steps 7 to 9 till d>a[i]
7. Assign d=da[i]
8. Increase value of i by 1
9. Assign j=i
10. Repeat step 11 for i=0 till i<11
11. Check if j==I, if yes then
Assign month=m[i]
12. Print d, month and y
13. End of function
NAME
d
y
a[]
m[]
i
j
d1
y1
n
DATA TYPE
int
Int
int array
String array
int
int
int
Int
int
DESCRIPTION
As an argument for the function Date()
As an argument for the function Date()
An array to store the nos. of days of the months of a year
An array to store the name of the months of a year
To run a loop
To store the month number
To store the inputted day number
To store the inputted year
To store the inputted N
Rearranged matrix:
1
8
6
7
2
3
4
5
9
1
9
8
3
2
7
4
5
6
Sum=43
Start
Initialise an int variable m with inputted number of rows
Initialise an int variable n with inputted number of columns
If m<=2 or m>=20 or n<=2 or n>=20 then print "OUT OF RANGE" and exit from
program
Declare an int array A[][] with dimensions (m X n) as A[][]=new int[m][n]
Initialise two int variable i=0 and j=0
Repeat steps 7 to 10 till i<m
Repeat steps 8 and 9 till j<n
Take input from console and store it in A[i][j]
Increase value of j by 1
Increase value of I by 1
Assign j=0
Repeat steps 14 to 17 till i<m
Repeat step 15 and 16 till j<n
Print A[i][j]+"\t"
Increase value of j by 1
Increase value of I by 1
Assign j=0
Initialise an int variable with number of boundary elements of matrix A[][] as:
no=(m*2)+(n*2)4
Declare an int array e[] with dimension 'no' as e[]=new int[no]
Initialise two int variable k=0 and t=0
Assign i=0
Repeat steps24 to 26 from j=0 till j<n
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of j by 1
Assign i=m1
Repeat steps 29 to 31 from j=0 till j<n
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of j by 1
Assign j=0
Repeat steps 34 to from i=1 till i<m1
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of i by 1
Assign j=n1
Repeat steps 39 to 41 from i=1 till i<m1
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of i by 1
Repeat steps 43 to 46 from i=0 till i<k
Repeat steps 44 to 45 from j=0 till j<k
10
10
11
11
12
12
NAME
m
n
i
j
A[][]
no
e[]
k
t
sum
DATA TYPE
int
int
int
int
int array
int
int array
int
int
int
DESCRIPTION
To store the number of rows
To store the number of columns
To run loops
To run loops
To store the elements inputted by user
To store the number of boundary elements
To store the boundary elements
To store the number of boundary elements
To use as a temporary variable during sorting
To store the sum of the boundary element
13
13
14
14
15
15
11.
12.
13.
14.
15.
16.
17.
18.
Start
Take input from console
Convert it into lower case
Eliminate last character of the inputted string
Split the string using split() function and store it in a String array a[]
Initialise int l with the size of a[] as l=a.length
Declare String variable temp, int variable i and j
Repeat steps 9 to 12 from i=0 till i<l
Repeat steps 10 and 11 from j=0 till j<(li1)
If length of a[i] > length of a[i+1]
Assign temp=a[i]
Assign a[i]=a[i+1]
Assign a[i+1]=temp
Increase value of j
Increase value of i
Print first word of rearranged string with its first character changed to upper case
Repeat steps 15 and 16 from i=0 till i<(l1)
Print a[i]+" "
Increase value of i by 1
Print last word of rearranged string with full stop (.) at end as a[l1]+ "."
End
16
16
17
17
DATA TYPE
String
String array
int
int
int
int
DESCRIPTION
To store the inputted string
To store the words of the string
To store the number of words
Temporary variable to be used during sorting
To run a loop
To run a loop
18
18
19
19
20
20
21
21
22
22
23
23
NAME
rev
amount
amt
a
ch
den[]
i
tot
DATA TYPE
int
int
int
String
char
int array
int
int
DESCRIPTION
To store the number of notes of a particular denomination
To store the inputted amount
Dummy variable
To store the inputted amount
To store the extracted digits of the inputted amount
To store the available denominations
To run a loop
To store the total number of notes
24
24
25
25
26
26
6.
7.
8.
9.
10.
11.
12.
13.
14.
Start
Take p as input from console and store bit in int variable p
Take q as input from console and store bit in int variable q
Initialise two int variable as i=0 and c=0
Check if p>5000 or q>5000 or p>q, if yes then
Print "OUT OF RANGE"
Else
Goto step 6
Repeat steps 7 to 12 for i=p till i=q
Assign int len with the length of i as len=(""+i).length()
Assign long sq=i*i
Assign long k=sq%(10^len)
Assign long s=sq/(10^len)
Assign long newn=k+s
Check if newn=I, if yes then
Print i
Increase value of c by 1
Print c
End
27
27
28
28
NAME
p
q
i
c
len
sq
r
l
newn
DATA TYPE
int
int
int
int
long
long
long
long
long
DESCRIPTION
To store the lower limit
To store the upper limit
To run a loop
Counter to count the number of Kaprekar numbers
To store the length of each number
To store the square of each number
To store the right hand piece of the square of the number
To store the left hand piece of the square of the number
To store the new number created by adding r and l
29
29
30
30
OutputInvalid entry
31
31
32
32
33
33
34
34
NAME
n
s
i
j
t
temp
a[]
l
c[]
DATA TYPE
int
String
int
int
int
String
String array
int
int array
DESCRIPTION
To store the number of sentences
To store the sentences
To run a loop
To run a loop
Temporary variable used during sorting
Temporary variable used during sorting
Array to store the words
To store the number of words
Array to store the length of each word
35
35
36
36
37
37
Start
Create an object of the class
Call the method inputNumber()
Call the method extract()
End of main() method
Algorithm for inputNumber() method
38
38
39
39
import java.io.*;
public class QUESTION7
{
int n;
void inputNumber()throws IOException
{
BufferedReader inp=new BufferedReader(new InputStreamReader(System.in));
System.out.println("INPUT:\t");
n=Integer.parseInt(inp.readLine());
}
public void extract()
{
String str="",str1="",str2="";
int hund=0,tens=0,units=0,i=0;
if(n<1 || n>999)
System.out.println("\nOUT OF RANGE ");
else
{
while(n!=0)
{
if(i==0)
units=n%10;
else if(i==1)
tens=n%10;
else if(i==2)
hund=n%10;
i++;
n=n/10;
}
if(hund>0)
str=w1(hund)+ " HUNDRED ";
if(tens>1)
str1= w2(tens);
if(tens==1)
str2= w3(units);
else
str2=w1(units);
if((!str.equals(""))&&(!str1.equals("") || !str2.equals("")))
str=str+ "AND ";
if(!str1.equals(""))
str=str+ str1+ " ";
40
40
41
41
42
42
43
43
NAME
n
str
str1
str2
hund
tens
units
i
x
DATA TYPE
int
String
String
String
int
int
int
int
int
DESCRIPTION
To store the input
To store the word of the number
To store the word of the number
To store the word of the number
To store the number of 100s
To store the number of 10s
To store the number of 1s
To run a loop
As a parameter to the method
44
44
45
45
QUESTION 8
Encryption is a technique of coding messages to maintain their secrecy. A String array of size
'n' where 'n' is the greater than 1 and less than 10, stores single sentences (each sentence
ends with a full stop) in each row of the array.
Write a program to accept the size of the array. Display an appropriate message if the size is
not satisfying the given condition. Define a string array of the inputted size and fill it with
sentences row-wise. Change the sentence of the odd rows with an encryption of two
characters ahead of the original character. Also change the sentence of the even rows by
storing the sentence in reverse order. Display the encrypted sentences as per the sample data
given below.
Test your program on the sample data and some random data.
Sample Data:
Example 1:
Inputn=4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OutputKV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
Example 2:
Inputn=13
OutputINVALID INPUT
46
46
ALGORITHM
1. Start
2. Take the number of sentence as input from the console and store it in n
3. Check if n<=1 or n>=10, if yes then
Print ''INVALID INPUT''
Else go to step 4
4. Declare a String array with the dimension n
5. Repeat steps 5 to 7 from i=0 till i=n-1
6. Take input from the console and store it in S[i]
7. Increase value of i by 1
8. Repeat steps 8 to 15 from i=0 till i=n-1
9. Check if i is even or odd
10. If i is even, then
Assign w=S[i]
(a) Repeat steps (a) to (h) from j=0 till j=w.length()
(b) Assign ch =w.charAt(j)
(c) Check if ch>=65 and ch <=88
Assign ch=ch+2
(d) Check if ch=89 or ch=90
Assign ch=ch-24
(e) Check if ch>=97 and ch<121
Assign ch=ch+2
(f) Check if ch=121 or ch=122
Assign ch=ch-24
(g) Assign c=c+ch
(h) Increase value of j by 1
11. Print c
12. If i is odd, then go to step 13
13. Extract the words of the sentence at S[i] by split() function
14. Print the words of the sentence in the reverse order
15. Increase the value of i by 1
16. Stop
47
47
48
48
}
}
else
{
System.out.println("INVALID INPUT");
}
}
}
49
49
NAME
n
i
j
ch
w
c
S[]
l
DATA TYPE
int
int
int
int
String
String
String array
int
DESCRIPTION
To store the number of sentences
To run a loop
To run a loop
Used in the encryption operation
Used in the encryption operation
Used in the encryption operation
To store the inputted sentences
To store the length of the sentences
50
50
51
51
52
52
7.
8.
9.
10.
11.
12.
Start
Initialise days[]={31,28,31,30,31,30,31,31,30,31,30,31}
Assign dateToDays =0
Assign l=checkLeap(y)
Assign days[1]=days[1]+l
Check if m<= or m>12 or d<=0 or d>days[m-1] or y<=0, then
Print "INVALID INPUT" and stop
Else go to step 7
Repeat steps 8 and 9 from x=0 till x=m-2
Assign dateToDays= dateToDays+days[x]
Increase value of x by 1
Assign dateToDays= dateToDays+d
Print dateToDays
End of checkValidity() method
Algorithm for checkLeap(int year) method
1. Start
2. Check if year%100 0, then return 1
Check if year%400 = 0, then return 1
Else return 0
3. End of checkLeap(int year) method
Algorithm for main() method
1.
2.
3.
4.
5.
Start
Create an object of the class
Call the function takeInput()
Call the function checkValidity()
End of the main() method
53
53
import java.io.*;
class QUESTION9
{
int d,m,y;
void takeInput() throws IOException
{
BufferedReader inp=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your date of birth in dd mm yyyy format:");
d=Integer.parseInt(inp.readLine());
m=Integer.parseInt(inp.readLine());
y=Integer.parseInt(inp.readLine());
}
void checkValidity()
{
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int dateToDays=0;
int l=checkLeap(y);
days[1]+=l;
if(m<=0 || m>12 || d<=0 || d>days[m-1] || y<=0 )
System.out.println("INVALID DATE");
else
{
for(int x=0;x< m-1;x++)
{
dateToDays+=days[x];
}
dateToDays+=d;
System.out.println("\nVALID DATE\n"+ dateToDays);
}
}
int checkLeap(int year)
{
if (year% 4 == 0)
{
if (year % 100 != 0)
return 1;
else if (year % 400 == 0)
return 1;
else
return 0;
}
54
54
55
55
NAME
d
m
y
days[]
dateToDays
year
DATA TYPE
int
int
int
int
int
int
DESCRIPTION
To store the inputted date
To store the inputted month
To store the inputted year
To store the days of the months
Used in the check of the validity
As a parameter
56
56
57
57
58
58
59
59
60
60
NAME
m
n
i
j
f
c
DATA TYPE
int
int
int
int
int
int
DESCRIPTION
To store the lower limit
To store the upper limit
To run a loop
To run a nested loop
To find the nos. of factors of a number
To find the number of prime palindrome integers
61
61
62
62
63
63
64
64
65
65
NAME
s1
ls
i
a[]
i
temp
j
DATA TYPE
String
int
int
String array
int
String
int
DESCRIPTION
To store the inputted sentence
To store the length of the sentence
To run loop
To store the words of the sentence
To store the length of the sentence in words
Temporary variable to be used in the sorting
To run loop
66
66
67
67
12
14
OutputORIGINAL MATRIX
4
16
12
14
16
14
68
68
69
69
70
70
NAME
m
i
j
M[][]
A[][]
DATA TYPE
int
int
int
int array
int array
DESCRIPTION
To store the matrix size
To run a loop
To run a loop
To store the original matrix
To store the mirror image matrix
71
71
72
72
0201530821
SUM=99
LEAVES NO REMAINDER VALID ISBN CODE
Example 2:
Input
Output
035680324
INVALID INPUT
Example 1:
Input
Output
0231428031
SUM=122
LEAVES REMAINDER INVALID ISBN CODE
73
73
74
74
75
75
NAME
m
i
j
M[][]
A[][]
DATA TYPE
int
int
int
int array
int array
DESCRIPTION
To store the matrix size
To run a loop
To run a loop
To store the original matrix
To store the mirror image matrix
76
76
77
77
Example 2:
Input
Output
Example 3:
Input
Output
78
78
79
79
80
80
NAME
s1
l
i
c
sr
a[]
DATA TYPE
String
int
int
int
String
String array
DESCRIPTION
To store the inputted sentence
To store the length of the sentence
To run a loop
To store the number of palindrome words
To store the reversed words
To store the words of the sentence
81
81
82
82
Magic number:
Accept two integers m and n, where m is less than n, as user input. Display the number of
Composite magic integers that are in the range between m and n (both inclusive) and output
them along with the frequency, in the format specified below.
Test your program with the sample data and some random data:
Example 1:
Input -
m=10
n=100
m=1200
n=1300
m=120
n=99
83
83
15.
16.
17.
18.
Start
Taker the lower limit m and upper limit n as input from the console
Check if m>n, then print ''INVALID INPUT'' and stop
Repeat steps 5 to 17 from i=m till i=n
Initialise a=i
Repeat steps 6 to 8 from j=2 till j=a/2
Check if a%j=0, then increase value of f by 1
Increase value of j by 1
Check if f 0, thengo to step 10 else go to step 17
Repeat steps 10 to 15 when a>0
Assign r=a%10
Assign a=a/10
Assign s=s+r
Check if a=0 and s>9, then
Assign a=s
Assign s=0
Goto step 10
If s=1, print i
Increase value of I by 1
Stop
84
84
85
85
NAME
m
n
i
f
j
r
a
s
c
DATA TYPE
int
int
int
int
int
int
int
int
int
DESCRIPTION
To store the lower limit
To store the upper limit
To run loop
To store the nos. of factors of a number
To run a loop
To extract the digit
Dummy variable
To store the eventual sum of the digits
To count the nos. of composite magic numbers
86
86
87
87
3
5
OutputORIGINAL MATRIX
1
2
3
5
6
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal=11
The sum of the right diagonal=10
Example 2:
Input- M= 4
7
8
9
2
4
OutputORIGINAL MATRIX
7
8
7
6
4
2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal=17
The sum of the right diagonal=20
88
88
89
89
90
90
NAME
m
i
j
M[][]
c
ls
rs
DATA TYPE
int
int
int
int array
int
int
int
DESCRIPTION
To store the size of the matrix
To run a loop
To run a loop
To store the inputted elements
Used as a counter
To store the sum of the left diagonal elements of the matrix
To store the sum of right diagonal elements of the matrix
91
91
92
92
M= 100
N=11
The required number= 119
Total number of digits= 3
M= 1500
N= 25
The required number= 1699
Total number of digits= 4
M= 99
N=11
INVALID INPUT
M= 112
N=130
INVALID INPUT
93
93
Start
Take M and N as user input
Check if m<100 or m>10000 or n>=100, then print ''INVALID INPUT''
Initialise s=0
Repeat steps 6 to 12 when s n
Assign a=m, s=0
Repeat steps 8 to 11 when a>0
Assign r=a%10
Assign a=a/10
Assign s=s+r
Go to step 7
Increase the value of i by 1
Assign k=m-1
Print k
Stop
94
94
95
95
NAME
m
n
a
s
r
DATA TYPE
int
int
int
int
int
DESCRIPTION
To store M
To store N
Dummy variable
Used in the operation
To be used in the extraction of digits of M
96
96
97
97
M= 3
3
1
6
MATRIX AFTER ROTATION
1
2
OutputORIGINAL MATRIX
3
2
3
4
7
8
9
Sum of the corner elements= 20
Example 2:
InputOutput-
M= 14
SIZE OUT OF RANGE
98
98
99
99
100
10
NAME
m
i
j
M[][]
s
DATA TYPE
int
int
int
int array
int
DESCRIPTION
To store the size of the matrix
To run a loop
To run a loop
To store the inputted elements
To store the sum of the corner elements
101
10
102
10