Revision 2 Important Programs
Revision 2 Important Programs
Member variables
eno : employee number
ename : name of the employee
age : age of the employee
basic : basic salary (Declare the variables using appropriate data types)
Member methods
void accept( ) : accept the details using scanner class
void calculate( ) : to calculate the net salary as per the given specifications
if the age of the employee is above 50 he/she gets an additional allowance of ₹5000
void main( ) : to create an object of the class and invoke the methods
import java.util.Scanner;
Output
2.Define a class to accept the elements of an array from the user and check for the
occurrence of positive number, negative number and zero.
Example
Output:
3 Positive Numbers
2 Negative Numbers
1 Zero
import java.util.Scanner;
Output
3.Define a class Anagram to accept two words from the user and check whether they are
anagram of each other or not.
An anagram of a word is another word that contains the same characters, only the order of
characters is different.
For example, NOTE and TONE are anagram of each other.
import java.util.Scanner;
String s1 = str1.toLowerCase();
String s2 = str2.toLowerCase();
int l1 = s1.length();
int l2 = s2.length();
if (l1 != l2) {
isAnagram = false;
}
else {
int count[] = new int[26];
for (int i = 0; i < l1; i++) {
count[s1.charAt(i) - 97]++;
count[s2.charAt(i) - 97]--;
}
if (isAnagram)
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
}
Output
4.Define a class to accept a number and check whether the number is valid number or not.
A valid number is a number in which the eventual sum of digits of the number is equal to 1.
e.g., 352 = 3 + 5 + 2 = 10 = 1 + 0 = 1
import java.util.Scanner;
while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
}
n = sum;
}
if (n == 1)
System.out.println(num + " is Valid Number");
else
System.out.println(num + " is not Valid Number");
}
}
Output
5.Write the code to print following patterns
(i)
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
(ii)
A A A A A
A A A B B
A A C C C
A D D D D
E E E E E
public class KboatPattern
{
public static void main(String args[]) {
System.out.println("Pattern 1");
for (int i = 1; i <= 5; i++) {
int x = 4;
System.out.print(i + " ");
int t = i;
for (int j = 1; j < i; j++) {
t += x;
System.out.print(t + " ");
x--;
}
System.out.println();
}
System.out.println();
System.out.println("Pattern 2");
char ch = 'A';
for (int i = 1; i <= 5; i++) {
System.out.println();
ch++;
}
}
}
6.Write a program to accept name and total marks of N number of students in two single
subscript array name[ ] and totalmarks[ ].
import java.util.Scanner;
Output
7.Write a program to input a number. Check and display whether it is a Niven number or
not. A number is Niven if it is divisible by the sum of its digits.
import java.util.Scanner;
int digitSum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
}
8.Define a class to accept 10 different decimal numbers (double data type) in a Single Dimensional
Array (say, A). Truncate the fractional part of each number of the array A and store their integer part
in another array (say, B).
import java.util.Scanner;
System.out.println("Truncated numbers");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + ", ");
}
}
}
9.Write a program to print the following patterns.
(i)
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
(ii)
J
I H
G F E
D C B A
public class KboatPattern
{
public static void main(String args[]) {
System.out.println("Pattern 1 ");
for (int k = 5; k >= 1; k--) {
for (int l = k; l <= 5; l++) {
System.out.print(l + " ");
}
System.out.println();
}
System.out.println();
System.out.println("Pattern 2 ");
char ch = 'J';
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(ch + " ");
ch--;
}
System.out.println();
}
}
}
9.Define a class StringMinMax to find the smallest and the largest word present in the
string.
E.g.
Input:
Hello this is wow world
Output:
Smallest word: is
Largest word: Hello
import java.util.Scanner;
word = "";
}
else {
word += ch;
}
}
Member functions/Methods
Employee( - - - - ) : An argumented constructor to assign name, employee code and basic salary to data members/instance
variables
double salCalc( ) : To compute and return the total salary of an employee
void display( ) : To display ename, ecode, basicpay and calculated salary
if the ecode <= 100, then a special allowance (20% of salary) will be added and the maximum amount for special allowance
will be 2500.
if the ecode > 100 then the special allowance will be 1000.
Specify the class Employee giving the details of the constructor, double salCalc() and void display(). Define the main() func tion
to create an object and call the functions accordingly to enable the task.
import java.util.Scanner;
11.Write a program to accept the year of graduation from school as an integer value from the user. Using the
binary search technique on the sorted array of integers given below, output the message "Record exists" if the
value input is located in the array. If not, output the message "Record does not exist".
{ 1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010 }
import java.util.Scanner;
if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}
12.Define a class to accept a number and check whether the number is Neon or not. A number is said to be
Neon if sum of the digits of the square of the number is equal to the number itself.
E.g.
Input: 9
Output:
9 * 9 = 81, 8 + 1 = 9
9 is Neon number.
import java.util.Scanner;
while (sq != 0) {
int d = sq % 10;
sqSum += d;
sq /= 10;
}
if (sqSum == n)
System.out.println("Neon Number");
else
System.out.println("Not a Neon Number");
}
}
Member methods
(i) Student (...) : A parameterised constructor to initialise the data members.
(ii) compute() : To compute the average and the maximum out of three marks.
(iii) display() : To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member methods.
import java.util.Scanner;
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
void display(int n): To check and display if the given number is a Perfect number or not. A number is said to be
perfect, if sum of the factors of the number excluding itself is equal to the original number.
E.g.
6 = 1 + 2 + 3, where 1, 2 and 3 are factors of 6 excluding itself.
import java.util.Scanner;
public class KboatMethodOverload
{
public void display()
{
for (int i = 0; i < 5; i++) {
for (int j = 5 - i; j <= 5; j++) {
System.out.print(j + " ");
}
for (int k = 4 - i; k > 0; k--) {
System.out.print("5 ");
}
System.out.println();
}
}
if (n == sum)
System.out.println(n + " is a perfect number");
else
System.out.println(n + " is not a perfect number");
}
public static void main(String args[]) {
KboatMethodOverload obj = new KboatMethodOverload();
Scanner in = new Scanner(System.in);
System.out.println("Pattern: ");
obj.display();
System.out.println();
}
}
Output
15.Define a class to enter a sentence from the keyboard and count the number of times a particular word
occurs in it. Display the frequency of the search word.
E.g.
Input : Enter the sentence:
Hello, this is wow world
Enter the word:
wow
Output:
Searched word occurs 1 times.
import java.util.Scanner;
if (word.equalsIgnoreCase(ipWord))
count++ ;
word = "";
}
else {
word += str.charAt(i);
}
}
if (count > 0) {
System.out.println("Searched word occurs " + count + " times.");
}
else {
System.out.println("Search word is not present in sentence.");
}
}
}
Output
16.Define a class to accept 5 names in one array and their respective telephone numbers into a second array.
Search for a name input by the user in the list. If found, display "search successful" and print the name along
with the telephone number, otherwise display "Search unsuccessful: Name not enlisted".
import java.util.Scanner;
int idx;
for (idx = 0; idx < n; idx++) {
if (name.compareToIgnoreCase(names[idx]) == 0) {
break;
}
}
if (idx < n) {
System.out.println("Search Successful");
System.out.println("Name : " + names[idx]);
System.out.println("Telephone Number : " + numbers[idx]);
}
else {
System.out.println("Search Unsuccessful : Name not enlisted");
}
}
}