Thursday, March 10, 2016

java- Data encapsulation

Data encapsulation is the most important concept to grasp when programming with objects. In object-oriented programming data encapsulation is concerned with:
  • Combining data and how it's manipulated in one place. This is achieved through the state (the private fields) and the behaviors (the public methods) of an object.
  • Only allowing the state of an object to be accessed and modified through behaviors. The values contained within an object's state can then be strictly controlled.
  • Hiding the details of how the object works. The only part of the object that is accessible to the outside world is its behaviors. What happens inside those behaviors and how the state is stored is hidden from view.

Enforcing Data Encapsulation

Firstly, we must design our objects so that they have state and behaviors. We create private fields that hold the state and public methods that are the behaviors.
[blockquote shade="no"]For example, if we design a person object we can create private fields to store a person's first name, last name, and address. The values of these three fields combine to make the object's state. We could also create a method called displayPersonDetails to display the values of the first name, last name and address to the screen.
Next, we must make behaviors that access and modify the state of the object. This can be accomplished in three ways:
  • Constructor methods: A new instance of an object is created by calling a constructor method. Values can be passed to a constructor method to set the initial state of an object. There are two interesting things to note; one, Java does not insist that every object has a constructor method. If no method exists then the state of the object uses the default values of the private fields; two, more than one constructor method can exist. The methods will differ in terms of the values that are passed to them and how they set the initial state of the object.
  • Related Article  of JAVA

Wednesday, March 9, 2016

Java – Question Bank

Related Article  of JAVA
‘Java’ Question Bank Ver. 2012SEQUENTIAL STRUCTURE PROGRAMS
1. WAP to store / Initialize 2 integer type variables and display them.
2. WAP to store your name in a variable and display.
3. WAP to input Name, Age and Date of Birth and Display using Input Method (Streams and Buffers)
4. WAP to initialize / input two numbers and display their sum.
5. WAP to find the sum, product, difference & quotient of two numbers (initialize / input)
6. WAP to input / Initialize 2 nos. and store their sum in ‘s’ and product in ‘p’ and display both of them.
7. WAP to Store the value of side of a square and display the Area & Perimeter of the Square.
8. WAP to store / input the length and breadth of a Rectangle, calculate and print the area and Perimeter of Rectangle
9. WAP to store / input the radius of a Circle, and calculate the Area and Circumference using the formula Area =π.R2 and Circumference = 2 π.R
10. WAP to input the marks obtained by a student in English, Hindi, Maths, Science, History, Geography, Computers and EVS and calculate the Total and Aggregate marks obtained by a student in the 8 subjects.
11. WAP to input the values of resistances R1 and R2 and find out the total equivalent resistance in a circuit in which the 2 resistors are connected in series
(Series = R1 + R2) and when they are connected in parallel,
(Parallel = (R1*R2) / (R1+R2))
12. WAP to input the two angles of a triangle and find the 3rd angle
13. WAP to find out the value of distance (s) covered by a body, where s = ut + ½ at2 , value of u, a and t are provided by the user.
14. WAP to input the distance in meter and convert it into to centimeters.
15. WAP to input the time in hours and convert it into minutes and seconds.
16. WAP to input the time in seconds and split it into hours, minutes and seconds.
17. WAP to find out the distance covered by light in One Year (Speed light = 3 x 108 m/s)
18. WAP to input the distance covered by a body along with time and calculate the Speed
19. WAP to input the speed in km/hr and convert it into m/s.
20. WAP to input distance in km and convert it into yards (1 yard = 0.9144 m) and vice versa.
21. WAP to input the temperature in Centigrade and convert it into to Fahrenheit and vice versa,
Formula : C/5 = (F – 32) /9
22. If ‘m’ and ‘v’ denote the mass and velocity of any moving object WAP to input the values of ‘m’ and ‘v’ and find out the Kinetic Energy of a body which is given by the formula (KE = ½ mv2)
23. WAP to find out the Potential Energy given by the formula (PE= m*g*h) where the values of m, g and h are entered by the user.
24. WAP to input the Base and Height of a Right angled Triangle and calculate the Area using the formula
Area = ½ Base X Height
25. WAP to the convert the amount entered by the user in denominations of Rs. 1000, Rs. 500, Rs. 100, Rs. 50, Rs. 20, Rs. 10, Rs. 5, Rs. 2 and Re.1 notes (Note : denominations should be least possible numbers)
26. Write a program to find all the remainders when a number ‘n is divided by any number from 1 to 9
27. Write a program that takes the three ‘sides’ of a cube as input and displays the TSA and Volume of the same.
(Hint TSA = 6 * side * side) (Vol. = side * side * side)
28. WAP to swap the values stored in two variables (using a third variable) Display the values of the two variables before and after swapping.
29. WAP to swap the values stored in two variables (Without using a third variable) Display the values of the two variables before and after swapping.
30. Write a program to input the price of 1 pen and 1 eraser and calculate the total cost of 15 pens and 17 erasers.
31. WAP to input the Basic Salary of an employee and Calculate his Gross and Net Salary based on the information given below
D.A. (Dearness Allowance) = 75% of Basic Salary
H.R.A. (House Rent Allowance)= 25% of Basic Salary
P.F. (Provident Fund) = 12% of (Basic + D.A.)
Gross Salary = Basic + D.A + H.R.A
Net Salary = Gross Salary – P.F.
32. WAP to input the Principal, Rate and Time and calculate the Simple Interest and Amount
33. WAP to input the Principal, Rate per-annum and Time along with the compounding frequency in a year and calculate the Compound Interest and Amount.
34. Dish TV allows a discount of 30% on a monthly recharge of a channel pack. For the same channel pack Tata Sky offers two successive discounts of 20% and 10%. WAP to input the recharge amount and display which company is providing a cheaper deal.
SELECTION STRUCTURE PROGRAMS
35. WAP to input two numbers and print the greater.
36. WAP to input two numbers and print the smaller.
37. WAP to input two numbers and print the square of the smaller and cube of the greater.
38. WAP to find out the smallest of 3 nos. entered by the user.
39. WAP to find out the largest of 3 nos. entered by the user.
40. WAP to input 3 numbers and display the second largest number.
41. WAP to input three nos. and print the 1st largest, 2nd largest and the 3rd largest of them do not use arrays / loops or any sorting techniques.
42. Input age of a person and check if he is eligible to vote or not
43. Input age of a person determine for how many years he has been voting or how many years after will he be eligible to vote.
44. Input the age of a person and check if he is a Senior Citizen or not.
45. Input the age of a person and check if he is a teenager or not.
46. Input the sales and find out if he is eligible to get a commission, The salesman is entitled to a commission of 5% if sales is greater than Rs.50,000
47. WAP to input a number and display its Square if it is Even and Cube if the number is Odd.
48. WAP to input marks obtained by a student in 10 subjects and display whether Pass Certificate is awarded or Pass Certificate is NOT awarded.
49. Input the Radius of a circle and check if it is less than zero if YES display ERROR message or else in case of Radius greater than zero print the Area and the Circumference of the circle (Area =  * r * r) &
Circum. = (2 *  * r)
50. WAP to input the marks in 7 subjects and find out the Total, Percentage and Grade based on the structure given below,
Below 50% – Grade C
50% to 75% – Grade B
76% to 90% – Grade A
above 90% – Distinction
51. WAP to input a character and check whether it is an alphabet, number or special character.
52. WAP to input a character and check that whether it is a vowel or consonant.
53. WAP to input a number and check whether it a single, double, triple or ‘n’ digit number.
54. Input any number and check if it is odd or even.
55. Input any number and check if it is divisible by 5 or not.
56. Input any number and check if it is Odd and divisible by 9.
57. Input a number and check whether it is divisible by 7 and not by 3.
58. Input any year and check if it a leap year or not.
59. WAP to input the cost price and selling price of an article and find out Profit % or Loss %.
60. Input the 3 sides of a triangle and check if it is Isosceles, Scalene or Equilateral
61. WAP to input the 3 angles of a triangle and display whether it is an Acute, Obtuse or Right Angled Triangle.
62. WAP to enter the co-efficient of Quadratic Equation ax2+bx+c=0 and find out whether the roots are Real, Equal or Imaginary using the discriminant of the equation
X = ( – b + √(b2 – 4ac) ) / 2a
63. Write a program to enter the marks obtained by a student in 13 subjects, find out the total and average and allot a distinction Grade according to the following –
10000 30%
> 5000 & 2500 & <=5000 15%
<2500 10%
69. JSEB charges their consumer according to the tariff chart given below,
Units Consumed Charges
Up to 50 units Free
51 – 100 0.75p / unit
101 – 250 1.25p / unit
252 – 500 2.50p / unit
Above 500 3.00p / unit
WAP in java to input the units consumed and display the electricity bill calculated as per following. A fixed rental of Rs. 150.00 is charged to all the consumers.
70. A cab driver charges Rs. 15.00 on the first meter (km). He charges an extra of Rs. 22.00 on the next 5 kms. Anything beyond that is charged at Rs. 2.50 per km. WAP to input the distance covered and calculate the cab charges.
71. WAP to enter number from 1 to 7 and display the corresponding day of the week using switch…case
1 – Monday,
2 – Tuesday,

7 – Sunday
72. WAP to enter number from 1 to 7 and display the corresponding day of the week using if …else.
73. WAP to enter number from 1 to 12 and display the corresponding month of the year using switch…case.
1 – January
2 – February
3 – March

12 – December
74. Write a menu driven program to input/assign/store 2 numbers and perform the following mathematical operations on the two variables as given below in the menu…
1. Addition (+)
2. Subtraction (-)
3. Multiplication (x)
4. Division (/)
5. Modulus (%)
75. Write a menu driven program to calculate the area of
a. Circle (Π.r2)
b. Square (side * side)
c. Rectangle (l * b)
76. Write a menu driven program to calculate the volume of
a. Sphere (4/3.Π.r3)
b. Cube (side * side * side)
c. Cuboid (l * b * h)
77. WAP to input the time and using a menu driven program convert it into
a. Hours
b. Minutes and
c. Seconds
78. Using a menu driven program give a choice to the users to convert the time from
a. Fahrenheit to Celsius
b. Celsius to Fahrenheit
79. WAP to input the monthly salary of a person and calculate his Annual Income Tax based on the table given below – Annual Salary Income Tax
Less than equal to Rs. 1,80,000 Nil
Greater than Rs. Rs. 1,80,000 to Rs. 3,00,000 Rs. 5000 + 13% of the amount exceeding Rs. 1,80,000
Greater than Rs. Rs. 3,00,000 Rs. 15,000 + 33% of the amount exceeding Rs. 3,00,000
80. WAP to input a number and determine whether it is a Buzz number or not. A Buzz number is one which either ends with 7 or is divisible by 7. E.g. 70, 17, 21 etc…
81. WAP to input a number and check whether it is a Perfect square or not.
82. WAP to input a double digited number and display the sum and product of its digits, e.g. if the number entered by the user is 23 then the output is Sum = 5, Prod = 6
83. WAP to input a Triple digit number and display whether the middle digit is Odd or Even also display its square and cube.
84. WAP to input a double digit number and reverse it (without using any loops) e.g. if input is 23 output is 32
LOOPING STRUCTURE PROGRAMS
85. WAP to Display the first 10 Natural numbers
86. WAP to Display the first ten Whole numbers in reverse order.
87. WAP to Print the natural numbers between x and y limits where ‘x’ and ‘y’ are entered by the user.
88. WAP to Print all the Even numbers between x and y limits
89. Print all the Odd numbers between x and y limits
90. Print all the Series of Prime numbers between x and y limits
91. Input any number and check if it is Prime or not.
92. Generate all the Multiples of 5 till n
93. Generate all the multiples of 5 between x and y limits
94. Generate the table of 8…
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24 till 10…
95. Sum of ‘n’ Natural numbers
96. Sum of ‘n’ Odd Numbers
97. Sum of ‘n’ Even Numbers
98. Sum of Natural Numbers between x and y limits
99. Sum of Odd Numbers between x and y limits
100. Sum of Even Numbers between x and y limits
101. Sum of Prime Numbers between x and y limits
102. Input a number and print its Factors
103. Input a number and count its factors.
104. Input a number and print its Odd/Even Factors
105. Input any 2 numbers and check if it is Twin Prime or not. (Hint: 3 & 5, 5 & 7 etc…)
106. Input 2 nos. and check whether they make up Brun’s Constant or not, Brun’s Constant is sum of
reciprocal of Twin Prime Nos. e.g (1/3) + (1/5) is a Brun’s Constant
107. Input any number and print its Factorial. (Hint: 5! = 5 x 4 x 3 x 2 x 1)
108. Input a number and count the number of digits.
109. Input any number and print the sum/prod. of its digits.
110. WAP to input a number and extracts the digits from reverse.
111. WAP to reverse and store a number in a different variable.
112. WAP to input 10 positive numbers and display the maximum and minimum amongst them.
113. Input ten numbers and count the number of positive nos., negative nos. and zeros
114. WAP to input a number and print the sum of all the digits that are even. E.g. 456, output is 10
115. WAP to input a number and print the sum of all the digits that are at even locations. E.g. 4567, output is 12.
116. WAP to input a number and display the sum of odd digits and even digits separately. E.g. 4567, output is Sum of Odds = 12 and Sum of Evens are 10.
117. Input a number and display all the prime digits and their sum. E.g. 347, output 3, 7 and sum = 10
118. 21, 19, 17 … series of odd in reverse order.
119. 50, 48, 46 … series of even in reverse order
120. 1, 4, 9, … series of squares
121. 1, 2, 4, 8, 16 … series of Decimal Equivalent Binary (Hint: 20, 21, 22 … etc)
122. S = 1 + 4 + 9 … sum of series of squares
123. S = 1 + 3 + 5 … sum of series of odd
124. S = 2 + 4 + 6 … sum of series of even
125. 1, 8, 27, … series of cubes
126. 1, 10, 2, 9, 3, 8, 4, … , 10, 1
127. 0, 3, 8, 15 … (one less than squares)
128. 2, 9, 28, … (one more than cubes)
129. 0, 1, 1, 2, 3, 5, 8, … Fibonacci Series (Hint: Every third element is a sum of 1st & 2nd)
130. 0, 7, 26, … one less than series of cubes
131. 1, 3, 6, 10 … differences increases by one.
132. 6, 2, 8, 4, 0, 6 … till 10 terms (last digit of multiples of 6)
133. 1, 10, 100, … till 10 terms
134. 1, 11, 111, 1111, … till 10 terms
135. 5, 55, 555 … till 10 terms
136. 1, 12, 123 … till 10 terms.
137. 1, 21, 321 … till 10 terms.
138. S = 1 + 1/2 + 1/3 + 1/4 + … till 10 terms.
139. S = 1 – 2 + 3 – 4 + 5 … till 10 terms.
140. S = 1 + 1/4 + 1/9 + … till 10 terms
141. S = 1 + 1/3 + 1/5 + … till 10 terms
142. S = y + y/8 + y/27 + … till ‘n’ terms
143. Sum of Products (SOP) S = 1 + (1×2) + (1x2x3) + … + (10 terms)
144. Product of Sums (POS) P = 1 * (1+2) * (1+2+3) * … * (n terms)
145. Sum of Sums (SOS) = 1 + (1+2) + (1+2+3) + (1+2+3+4) … till 10 terms.
146. Product of Products (POP) = 1 * (1*2) * (1*2*3) * (1*2*3*4) … till 10 terms.
147. 1 + 1/(1+2) + 1/(1+2+3) … (n terms)
148. (1)/(1) + (1+2)/(1*2) + (1+2+3)/(1*2*3) + … till ‘n’ terms
149. S = 1 + a2/1! + a3/2! + a4/3! … n terms
150. S = 1 + a2/2! + a4/4! + a6/6! … n terms
151. 1 – a2/3! + a4/5! – a6/7! … n terms
152. WAP to input a number and print the digits in ascending order. E.g. if the input is 5723 the output is 2357
153. Input any number and check if it is a Special (Krishnamurthy) number or not. (Hint: 145 = 1! + 4! + 5!)
154. Input any number and check if it is an Armstrong (Narcissistic) number or not. (Hint: 153 = 13 + 53 + 33)
155. Input any number and check if it is a Perfect number or not. (Hint: 6 = 1 + 2 + 3, sum of factors excluding itself)
156. Input any number and check if it is a Palindrome or not. (Hint: 121 = 121)
157. WAP to input two numbers and print the HCF (GCD) and LCM of the two numbers.
158. Convert a number from Decimal to Binary and vice versa.
159. Convert a number from Decimal to Octal and vice versa.
160. Convert a number from Decimal to Hexadecimal and vice versa.
161. WAP to input a number and count the number of Zeros
162. WAP to check whether a number entered by the user is a Automorphic number. E.g. 25 is an Automorphic number because 252 = 625 in which the number of digits in the original number (i.e. 25) before squaring is identical to that many number of digits from the LHS of the squared number (i.e. 625).
163. WAP to check whether a number entered by the user is an Evil number or not. An Evil number is one whose Binary counterpart has even number of ones. E.g. 10, as the binary of 10 is 1010 which has even number of ones.
164. WAP to check whether a number entered by the user is an Odious number or not. An Odious number is one whose Binary counterpart has odd number of ones. E.g. 7, as the binary of 7 is 111 which has odd number of ones.
165. WAP to generate the Tribonacci Series. E.g. 0, 1, 1, 2, 4, 7, 13… A Tribonacci series is one whose every 4th term is the sum of 1st, 2nd and 3rd terms.
166. WAP to check whether the number entered by the user is a Sunny number or not. A Sunny number is one which becomes a Perfect Square after adding one to it. E.g. 8, as 8+1=9 which is a perfect square.
167. WAP to check whether two numbers entered by the user belong to the set of Amicable Pair or not. Two numbers are said to be Amicable Pair if the sum of factors of the 1st excluding the number is equal to the 2nd number and the sum of factors of the 2nd number excluding itself is equal to the 1st E.g. 220 and 284
168. A Buzz number is one which is either divisible by 7 or has 7 as the last digit. E.g. 27 or 49.
169. Display all the Prime numbers between 1 and 100
170. WAP to input a number and print all the possible combination of consecutive natural nos. that adds up to the given number. E.g. Input is 15 then output is
1, 2, 3, 4, 5
4, 5, 6
7, 8
171. WAP to display the Triangular nos. till n. A Triangular number is formed by addition of consecutive integers starting from 1, e.g.
1+2=3
1+2+3=6
1+2+3+4=10, … Hence the triangular nos. are 3, 6, 10 …
172. WAP to input a number and display the Prime factors of the number e.g if the no. entered by the user is 24 the output is 2,2,2,3
173. WAP to input a no and check whether it is a Smith no. or not, A Smith Number is a composite no. whose sum of digits is equal to the sum of the digits of it’s prime factors obtained as a result of prime factorization.
(Excluding 1) e.g. 666, coz sum of digits is 6+6+6=18, prime factors of 666 = 2, 3, 3, 37 so 2+3+3+(3+7)=18
174. WAP to input a number and print its Primorial. A Primorial number also known as p-prime number (denoted by Pn# which is a product of all the prime numbers up to ‘n’ i.e. P5# = 2 x 3 x 5 = 30
(There is a conflicting definition which says that P5# is the product of first 5 prime numbers i.e. 2 x 3 x 5 x 7 x 11 = 2310, but the most prevailing definition says the former)
175. WAP to input a number and check whether it is a Mersene number or not. A Mersene number has all ones in its Binary counterpart, it can also be defined as n2 – 1 for e.g. Binary of 7 = (111)2 which is also equal to 23 – 1
Binary of 15 = (1111)2 which is also equal to 24 – 1
176. WAP to input a number and check whether it is a Kaprekar’s number or not. Consider an n – digit number K, Square it and add the right ‘n’ number of digits to the left n – 1 digits. If the resultant sum is K, then K is called a Kaprekar’s number. For example, 297 is a Kaprekar’s number as 2972 = 88209 and 297 has 3 digits hence extracting 3 digits from RHS and adding it to the rest of the digits, i.e. 209 + 88 = 297.
ARRAY STRUCTURE PROGRAMS (SINGLE DIMENSION)
177. WAP to store 10 numbers in an array and print the sum of all the ten nos.
178. WAP to input 5 real nos. in an array and display them as integers.
179. WAP to input 10 numbers in an array and display the element whose position is entered by the user.
180. WAP to input 10 numbers in an array and display in reverse order without using a second array.
181. WAP to input 10 numbers in an array and store the elements in reverse order in another array, Display both the arrays.
182. WAP to input 10 numbers in an array and store the elements in reverse order in the same array, Do not use any separate array. After reversing the array display the elements of this array.
183. WAP to input 10 numbers in an array and store the elements in reverse order in the same array, Do not use any separate array. After reversing the array display the elements of this array. Also do NOT use any separate (temporary) variable for swapping.
184. WAP to input ‘n’ numbers in an integer type array, where n is entered by the user and display the first and the last element.
185. WAP to input temperature for 7 days in an array and display the average temperature of the week.
186. WAP to accept 10 nos. integer type numbers in an array and print the largest and smallest number present within the array along with their positions. (Consider the ZEROth subscript as position ONE)
187. WAP to accept 10 numbers in an integer type array. Enter a search element and check how many times it occurs within the array. If the element is not found display “NOT FOUND”. Use Linear Search technique.
188. WAP to input 10 nos. in an array and search a given number from within that array and print all its positions of occurrence using Linear Search Technique. If not found display proper message.
189. WAP to enter 10 numbers in an integer type array and search a given number from within that array using Binary Search technique.
190. WAP to accept 10 integer type numbers in an array and sort the array in ascending order using Bubble Sort Technique. Display the elements of the array before and after sorting.
191. WAP to accept 10 integer type numbers in an array and sort the array in descending order using Bubble Sort Technique. Display the elements of the array before and after sorting. Also display the largest and the smallest element.
192. WAP to accept ‘n’ integer type numbers in an array and sort the array in ascending order using Exchange selection Sort Technique. Display the elements of the array before and after sorting.
193. WAP to accept ‘n’ integer type numbers in an array and sort the array in descending order using Exchange selection Sort Technique. Display the elements of the array before and after sorting. Also display the square and cube of the largest and the smallest element.
194. WAP to input 5 nos. in array A[ ] and 5 nos. in array B[ ] and merge them end to end into a third array C [ ] of size 10 and Display all the three arrays.
195. WAP to input 15 nos. in array A[ ] and 10 nos. in array B[ ] and merge the two dissimilar sized array end to end into a third array C [ ] of size 25 and Display all the three arrays.
196. WAP to input 5 nos. in array A[ ] and 5 nos. in array B[ ] and merge the elements of the array alternately into a third array C [ ] of size 10 and Display all the three arrays.
So if A[ ] = {10, 20, 30, 40, 50} and B[ ] = {11, 22, 33, 44, 55} then
Resulting array C[ ] = {10, 11, 20, 22, 30, 33, 40, 44, 50, 55}
197. WAP to input 10 nos. in array A[ ] and square of its corresponding elements in array B[ ]. Display both the arrays.
198. WAP to accept 10 integers in an array and display how many are positive integers, how many are negative and how many are zeros.
199. WAP to accept ‘n’ integers in an array and display the sum of all the odd and even separately.
200. WAP to accept ‘n’ integers in an array and display the sum of all the negatives and positives separately.
201. WAP to input 10 nos. in array A[ ] extract all the positive numbers and store them in an array P[ ] and negative numbers in array N[ ]. Display all the three arrays.
202. WAP to input 10 nos. in array A[ ] extract all the odd numbers and store them in an array ODD[ ] and even numbers in array EVEN[ ]. Display all the three arrays.
203. WAP to accept 15 integers in an array and display all the perfect numbers within the array. A perfect number is one whose sum of digits (excluding itself) is equal to the number, e.g. 6 as the factors of 6 (excluding 6) are 1, 2, and 3 so their sum, 1+2+3 = 6
204. WAP to create an array of user defined size and initialize by taking input from the user and check whether consecutive elements are twin prime or not, display those that are twin primes.
E.g. if the array A[ ] = {6, 3, 5, 16, 17, 19, 8, 9, 11, 13} then output is Twin Prime Pairs are
3, 5
17, 19
11, 13
205. WAP to accept 10 numbers in an array and display the no. of Palindrome numbers present within the array. Palindrome numbers are those that read the same from both the directions. E.g. 121, 777 etc…
206. WAP to accept 10 numbers in an integer type array and store the Armstrong numbers in another array and sort the second array in descending order using any of the sorting techniques.
207. Create an array of user defined size and initialize by taking input from the user and replace the prime nos. with the nearest prime number greater than the prime no. present within the array in another array.
N = 3
E.g. A [ ] = {45, 11, 17} then the resulting array is B [ ] = {45, 13, 19}
208. WAP to create an integer type array of size 10 and sort the first 5 elements of the array in descending order and remaining 5 elements in ascending order and display the array before and after sorting.
209. WAP to create an integer type array of size 10 and find out the element of highest density, i.e. the element within the array that occurs the maximum number of times.
E.g. A [ ] = {15, 89, 45, 85, 63, 15, 89, 15, 25, 12} then output is 15
210. WAP to create an integer type array of size 5 and fill it up with only double digit numbers, reverse the numbers and store it in another array of same size and display the second array. Make sure that the program accepts only double digit numbers and discards anything else entered by the user.
E.g. A [ ] = {25, 96, 78, 66, 70} then the resulting array B [ ] = = {52, 69, 87, 66, 7}
211. WAP to create a String type array of size 10 and display the longest word out of the 10 words stored in the array.
212. WAP to create a String type array of size 10 and form a word by concatenating the first letter of each word of the array.
213. WAP to create a String type array of size 5 and store the names of the members of Linkin Park and sort the array in lexicographic order using Bubble Sort Technique and display the elements of the sorted array.
E.g. STR [ ] = { “Delson”, “Hahn”, “Shinoda”, “Bourdon”, “Farrell”}
Then output is – Bourdon, Delson, Farrell, Hahn, Shinoda
214. WAP to create a String type array of size 10 and reverse each word within the array element and display the elements of the modified array.
215. WAP to store names of 10 employees in a String type array and their corresponding mobile numbers in another array. The program then displays a menu
a. Search Mobile Number
b. Search Employee Name
As per user choice the program asks for either the name of the employee and displays the mobile number or mobile phone number and displays the corresponding name of the employee. If not found then displays RECORD NOT FOUND.
216. WAP to accept 10 Strings in a String type array and display only those array elements that are Palindrome words.
217. WAP to create two arrays of size 20 each and store 20 country names along with their capital cities. Followed by which when user enters the name of the country the program displays the name of the capital if found otherwise displays “DATA NOT FOUND”
218. WAP to store names of 5 cities in a String type array and display only the ones that starts with a consonant and ends with a vowel. E.g. STR [ ] = {“Agra”, “Bhopal”, “Patna”, “Jamshedpur”, “Kolkata”} then the output is
Patna
Kolkata
219. WAP to create a string type array of size 10 and sort the elements of the array in increasing order of its length from shortest name to longest one.
220. Lucky Numbers are those that are generated by a sieving process similar to that of Sieve or Eratosthenes, consider a list of natural numbers that starts with 1
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 now removing every second element we get
1, 3, 5, 7, 9 now removing every third we get
1, 3, 7, 9 now removing every fourth we get
1, 3, 7 we can no longer remove the fifth so the survivors are 1, 3 and 7 which are Lucky numbers from 1 to 10.
WAP to implement the same using arrays and display the Lucky numbers from 1 to 1
0

Tuesday, March 8, 2016

Java – Glossary of Terms

Java – Glossary of Terms

Related Article  of JAVA
1. Abstract methods : All methods in an interface are abstract i.e., their body needs to be defined in a class that implement the interface.
2. Accessor method : It is a method that returns the value of a data : member of the class. It is actually for Private members.
3. Arguments : The values which are passed to the method during its call from the caller.
4. Base class : It is a class which is inherited by another class.
5. Binary file : store information in a form of bytes.
6. Block : It is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
7. Break : It is a keyword, which is used to terminate the loop or switch case, when the condition is satisfied.
8. Buffer : is a high speed temporary storage, which is also known as cache memory
9. Buffered reader : It buffers the input and improves the performance.
10. Byte stream classes : They are built up to provide the functionality of reading and writing bytes. They help in transmitting data (bytes) in one direction. They are thus useful in Input : Output operation.
11. Casting : Explicit type conversion in which the data of higher precision is forced to get converted into lower precision, which might lead to possible loss of precission or the data going out of range.
12. Character : It is a token which is a distinctive mark or symbol, alphabets (small or capital) or digits.
13. Character stream classes : It support Input : Output operation on characters.
14. Coercion : Implicit data type conversion is termed as coercion in which the data gets converted to its higher type without any intervention of user.
15. Compile time error : Error that the compiler can find during compilation.
16. Compiler : The software which converts high level language instructions to machine level language at once.
17. Compound Statement : Is a set of multiple statement written within braces { } is called a compound statement.
18. Concatenation : The process of adding or appending a string at the end of another string.
19. Continue : is a keyword which is used to skip the current iteration and go for the next iteration, when the condition is satisfied within the program.
20. Dangling else Problem : The nested if : else statement introduces a source of potential ambiguity referred to as dangling else problem. This problem arises when in a nested if statement, number of ifs is more than the number of else clauses. The question arises, with which if does the additional else clause property match : up.
21. Data members : that contain information necessary to represent the class.
22. Debugging : is the process of identifying and correcting errors which occurs during the compilation of the program.
23. Dot (.) Operator : is a special type of operator which is used to accesses instance members of an object or class members of a class.
24. Dynamic Binding is the process of linking the function call with the function signature during the execution of the program.
25. Exception Handling : Way of handling anomalous situations during a program : run.
26. Expression : is any valid and legal combination of operators, constants and variables
27. Fall : through : Missing break statement that leads to fall of control, in which all the cases are executed, irrespective of the switch statement.
28. Field : An individual data item of a record.
29. Final : It is a keyword which when suffixed to a variable makes it a constant.
30. Global Variable : variables which is available to entire class (all the methods).
31. Impure expression : The operands in such an expression are of different datatype
32. Infinite loop : A loop that never ends.
33. Instance Variables : A global variable defined as non static within the class.
34. Integral Promotion : Conversion of shorter integral types into bigger integral types.
35. Interface : is a set of variables and methods like a class.
36. Iteration statement : Statements that allows a set of instructions to be performed repeatedly.
37. Java shorthand : A way of combining an arithmetic operator and an assignment operator.
38. Jump Statement : Statement that unconditionally transfers program control within a function.
39. L : Value The memory location of a variable is called L Value
40. Library class : Pre defined classes of java which get included in an application program itself. They are also known as Standard java classes or built : in : java classes.
41. Local variable : Variable declared inside a method or block.
42. Looping Statement : Iteration Statement. Also called a loop.
43. Mark & Sweep : An algorithm that is used to manage garbage collection in Java.
44. Message passing : is a way of sending / receiving information to / from objects / methods / class.
45. Multilevel Inheritance : A target which inherits a base can be used as a base for another target.
46. Mutator method : It is a method that sets / changes the value of a data : member of the class. It is actually for Private members.
47. Nested Loop : A loop that contains another loop inside its body.
48. New : It is a keyword which allocates the memory and instantiates an object.
49. Non : Static variables : The instance variables that are accessed by associating them with object separated by a dot.
50. Normal flow of control : When a command to execute a program is issued, control keeps on moving sequentially from one statement to other from beginning to an end in order to execute them is known as normal flow of control.
51. Object factory : A producer of objects that accepts some basic information and creates objects on based on this information.
52. Object source : The conversion of High Level Language to Machine Level Language with the help of a translator.
53. Parameter : The variable used with method signature which received value during function call.
54. Piece : of : code (Variable’s scope) : The program part(s) in which a particular piece : of code or a data value (e.g., variable) can be accessed.
55. Prototype : A function or a method prototype is the first line of a function that contains information about access specifier, modifier, return type function name and arguments.
56. Pure expression : All operand in such an expression are of same datatype
57. R : Value The read value (contents or value) of a variable.
58. Reader class : is a Character stream classes i.e., it is used to read characters from source.
59. Record : It is a set of related information.
60. Reusability : The process of adding some additional features to a class without modifying its contents.
61. Run Time Error : Error occurring during program execution.
62. Scope : Region within which a variable/ piece : of : code is accessible.
63. Scope of variable : It is a measure of as to what extent a variable visible and determines the limit within which they can be applied in a program.
64. Selection Statement : Statement that allows choosing a set : of : Instruction for execution depending upon an expression.
65. Sequential construct : The Statements in a program are executed sequentially one after the other from to the last statement.
66. Setter method : It is a method that sets / changes the value of a data : member of the class. It is actually for Private members.
67. Source code : An application program written High Level Language, which is an input to a computer system.
68. Statement : Instruction given to the computer to perform any kind of action.
69. Static initialization : When a variable is initialized with a specific constant before its use in the operation.
70. Static : It is a keyword which in the variable declaration make it class variable. Such variables or methods has only one instance in the physical memory and can be accessed either directly or using the class name. E.g. Math.sqrt( ) where sqrt( ) is a static method of Math class.
71. String Buffer : It is a type of string class, which allows reasonable space to contain a string in such a way that any change brought affects the same string, thus is a mutable class.
72. Subscripts are cell numbers of the array &subscript variable is the variable used along with cell no. E.g., m[4] 4 =Subscript m= subscript variable.
73. Super : It is a keyword used in inheritance for calling base class constructor. It is also used for calling the base class method in case that method has been overridden.
74. Syntax Error : Programming language’s grammar rules violation error.
75. Testing : It is the process of checking program logic manually to ensure whether it contains any error or not.
76. This : It is a keyword (which is actually a reference) sorting the address of the objects currently invoking a member functions.
77. Throws : It is a keyword used to inform the error handling classes that an error has occurred. It is specified with method prototype.
78. Token : The smallest individual unit of a Java program
79. Truncation : Rounding off by removing fractional part or removing parts of a string.
80. Try and catch : Try contains a block of statements to perform any error occurring within the try block is trapped. Further a report is passed to the exception handler about the error, which is done by the catch block.
81. Type Promotion : Conversion of all operands up to all the type of the largest operands.
82. Variable : It is a named memory location which holds a value of a specific datatype
.

Monday, March 7, 2016

Day 5 oop concepts final

What we will learn in OOPs Concepts ?
  • Advantage of OOPs
  • Naming Convention
  • Object and class
  • Method overloading
  • Constructor
  • static keyword
  • this keyword with 6 usage
  • Inheritance
  • Aggregation
  • Method Overriding
  • Covariant Return Type
  • super keyword
  • Instance Initializer block
  • final keyword
  • Abstract class
  • Interface
  • Runtime Polymorphism
  • Static and Dynamic Binding
  • Downcasting with instanceof operator
  • Package
  • Access Modifiers
  • Encapsulation
  • Object Cloning
Further more: 
  1. JAVA TRAINING IN KATHMANDU
  2. JAVA TRAINING IN LALITPUR

Sunday, March 6, 2016

Day 4 Object Oriented

Interfaces

As in the above example Teacher class contains the attributes and methods of Employee as well as Musician.
  1. Java does not support Multiple Inheritance
  2. Interface is similar to an abstract class that contains only abstract methods
  3. Declared with the keyword interface instead of the keyword class
  4. Keyword implements is used to represent the interfaces implemented by the class
Interface: Syntax
class ClassName extends Superclass implements Interface1, Interface2, ....
Example : class MusicTeacher extends Teacher implements Musician
Java OOps, Interface example diagram
All methods in an interface are implicitly public and abstract.
The keyword abstract before each method is optional.
An interface may contain definitions of final variables.
A class may extend only one other class, but it mayimplement any number of interfaces.
When a class implements an interface, it may implement (define) some or all of the inherited abstract methods.
A class must itself be declared abstract if it inheritsabstract methods that it does not define.
An interface reference can point to objects of its implementing classes.
Abstract method
1) A method that is declared but not defined
2) Declared with the keyword abstract
3) Example :
abstract public void playInstrument();
4) Has a header like any other method, but ends with a semicolon instead of a method body
5) Used to put some kind of compulsion on the class who inherits from this class. i.e., the class who inherits MUST provide the implementation of the method else the subclass will also become abstract
6) The following cannot be marked with “abstract” modifier
  • Constructors
  • Static methods
  • Private methods
  • Methods marked with “final” modifier

Abstract Classes

Abstract Classes
Outlines the behavior but not necessarily implements all of its behavior. Also known as Abstract Base Class.
Provides outline for behavior by means of method (abstract methods) signatures without an implementation.
Note 1: There can be some scenarios where it is difficult to implement all the methods in the base class. In such scenarios one can define the base class as an abstract class which signifies that this base class is a special kind of class which is not complete on its own.
A class derived from the abstract base class must implement those member functions which are not implemented in the abstract class.
Note 2: Abstract Class cannot be instantiated.
To use an abstract class one has to first derive another class from this abstract class using inheritance and then provide the implementation for the abstract methods.
Note 3: If a derived class does not implement all the abstract methods (unimplemented methods), then the derived class is also abstract in nature and cannot be instantiated.
Example of Abstract class and Abstract Method:
abstract class Costume {
  abstract public void Stitch();
   public void  setColour (){
     //code to set colour
   }
}
Here the class which inherits abstract class Costume can implement the abstract method Stitch depending upon what kind of Costume it is.

What is a Constructor

1) A method with the same name as class name used for the purpose of creating an object in a valid state
2) It does not return a value not even void
3) It may or may not have parameters (arguments)
4) A class contains one or more constructors for making new objects of that class
5) If (and only if) the programmer does not write a constructor, Java provides a default constructor with no arguments.
The default constructor sets instance variables as:
  • numeric types are set to zero
  • boolean variables are set to false
  • char variables are set to ‘’
  • object variables are set to null.
When a constructor executes, before executing its own code:
It implicitly call the default constructor of it’s super class Or can make this constructor call explicitly, with super(…);
A constructor for a class can call another constructor for the same class by putting this(…); as the first thing in the constructor. This allows you to avoid repeating code.
A Class do not inherit the constructors of a super class.
Generalization and Specialization:
In order to implement the concept of inheritance in an OO solution, one has to first identify the similarities among different classes so as to come up with the base class.
This process of identifying the similarities among different classes is calledGeneralization.
The class which is identified after generalization is the base class. The classes over which the generalization is made are the derived classes.
The classes over which the generalization is built are viewed asSpecialization.
Generalization identifies the common attributes and behavior among different entities whereas specialization identifies the distinct attributes and their behavior which differentiate a derived class from the other classes.
Generalization–Specialization hierarchy is used to depict the relationship between the generalized class and the corresponding specialized classes.

Access Specifiers

Access specifiers in a class
An access specifier is a keyword in OO Programming languages, which specify what access is permitted on a member.
There are three types of access specifiers:
public: Accessible to all. Other objects can also access this member variable or function.
private: Not accessible by other objects. Private members can be accessed only by the methods in the same class. Object accessible only in Class in which they are declared.
protected: The scope of a protected variable is within the class which declares it and in the class which inherits from the class (Scope is Class and subclass)
Default: Scope is Package Level
Approach to Object Oriented Design:
Step 1. Identify all the classes (Nouns; Type of objects) in the requirement specification,
Step 2. Identify the commonalities between all or small groups of classes identified (generalization) if it is obvious. Do not force fit generalization where it doesn’t make sense.
Step 3. In any given situation, start with the simplest object which can be abstracted into individual classes
Step 4. Identify all the member variables and methods the class should have
Step 5. Ensure that the class is fully independent of other classes and contains all the attributes and methods necessary.
Step 6. Keep all the data members private or protected
Step 7. The methods in the class should completely abstract the functionality
Step 8. The methods in the class should not be a force fit of procedural code into a class
Step 9. Inherit and extend classes from the base classes ONLY IF the situation has scope for it
Step 10. Define the relationships among the classes (“Has-A”, “Uses-A”)
Step 11. Keep the number of classes in your application under check (do not create any unnecessary classes)
Further more
  1. JAVA TRAINING IN KATHMANDU
  2. JAVA TRAINING IN LALITPUR