0% found this document useful (0 votes)
64 views

Revision 2 Important Programs

Uploaded by

ranjan.k8a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Revision 2 Important Programs

Uploaded by

ranjan.k8a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CLASS X ICSE COMPUTER APPLICATIONS IMPORTANT PROGRAMS

1. Define a class with the following specifications

Class name : employee

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

net = basic + hra + da - pf


hra = 18.5% of basic
da = 17.45% of basic
pf = 8.10% of basic

if the age of the employee is above 50 he/she gets an additional allowance of ₹5000

void print( ) : to print the details as per the following format

eno ename age basic net

void main( ) : to create an object of the class and invoke the methods

import java.util.Scanner;

public class employee


{
private int eno;
private String ename;
private int age;
private double basic;
private double net;

public void accept()


{
Scanner in = new Scanner(System.in);
System.out.print("Enter name : ");
ename = in.nextLine();
System.out.print("Enter employee no : ");
eno = in.nextInt();
System.out.print("Enter age : ");
age = in.nextInt();
System.out.print("Enter basic salary : ");
basic = in.nextDouble();
}
public void calculate()
{
double hra, da, pf;
hra = 18.5/100.0 * basic;
da = 17.45/100.0 * basic;
pf = 8.10/100.0 * basic;
net = basic + hra + da - pf;
if(age > 50)
net += 5000;
}

public void print()


{
System.out.println(eno + "\t" + ename + "\t" + age + "\t Rs."
+ basic + "\t Rs." + net );
}

public static void main(String args[])


{
employee ob = new employee();
ob.accept();
ob.calculate();
ob.print();
}
}

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

Input Array: 12, -89, -56, 0, 45, 56

Output:
3 Positive Numbers
2 Negative Numbers
1 Zero

import java.util.Scanner;

public class KboatSDANumbers


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int pc = 0;
int nc = 0;
int zc = 0;

System.out.print("Enter size of array : ");


int l = in.nextInt();
int arr[ ] = new int[l];

System.out.println("Enter array elements : ");


for (int i = 0; i < l; i++) {
arr[i] = in.nextInt();
}

for (int i = 0; i < l; i++) {


if (arr[i] < 0)
nc++;
else if(arr[i] > 0)
pc++;
else
zc++;
}

System.out.println(pc + " Positive Numbers");


System.out.println(nc + " Negative Numbers");
System.out.println(zc + " Zero");
}
}

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;

public class Anagram


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the 2 words to check");
System.out.print("Enter first word: ");
String str1 = in.next();
System.out.print("Enter second word: ");
String str2 = in.next();
boolean isAnagram = true;

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]--;
}

for (int i = 0; i < count.length; i++) {


if (count[i] != 0) {
isAnagram = false;
break;
}
}
}

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

Then 352 is a valid number.

import java.util.Scanner;

public class KboatValidNum


{

public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter number : ");
int num = in.nextInt();
int n = num;

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++) {

for (int j = 4; j >= i; j--) {


System.out.print("A ");
}

for (int k = 1; k <= i; k++) {


System.out.print(ch + " ");
}

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[ ].

Calculate and print :

(i) The average of the total marks obtained by N number of students.


[average = (sum of total marks of all the students)/N]

(ii) Deviation of each student's total marks with the average.


[deviation = total marks of a student - average]

import java.util.Scanner;

public class KboatSDAMarks


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();

String name[] = new String[n];


int totalmarks[] = new int[n];
int grandTotal = 0;
for (int i = 0; i < n; i++) {
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}

double avg = grandTotal / (double)n;


System.out.println("Average = " + avg);

for (int i = 0; i < n; i++) {


System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}

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.

e.g. Input: 126

Output: 1 + 2 + 6 = 9 and 126 is divisible by 9.

import java.util.Scanner;

public class KboatNivenNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
int orgNum = num;

int digitSum = 0;

while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
}

if (digitSum != 0 && orgNum % digitSum == 0)


System.out.println(orgNum + " is a Niven number");
else
System.out.println(orgNum + " is not a Niven number");
}
}

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;

public class KboatSDATruncate


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double a[] = new double[10];
int b[] = new int[10];

System.out.println("Enter 10 decimal numbers");


for (int i = 0; i < a.length; i++) {
a[i] = in.nextDouble();
b[i] = (int)a[i];
}

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;

public class StringMinMax


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();

str += " ";


String word = "", lWord = "", sWord = str;
int len = str.length();

for (int i = 0; i < len; i++) {


char ch = str.charAt(i);
if (ch == ' ') {
if (word.length() > lWord.length())
lWord = word;
else if (word.length() < sWord.length())
sWord = word;

word = "";
}
else {
word += ch;
}
}

System.out.println("Smallest word: " + sWord);


System.out.println("Longest word: " + lWord);
}
}
10. A class Employee contains the following member:

Class name : Employee

Data members/Instance variables


String ename : To store the name of employee
int ecode : To store the employee code
double basicpay : To store the basic pay of employee

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

The salary is calculated according to the following rules :

Salary = Basic pay + HRA + DA + TA


where, HRA = 20% of basic pay
DA = 25% of basic pay
TA = 10% of basic pay

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.

Hence, the total salary for the employee will calculated as :

Total Salary = Salary + Special Allowance

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;

public class Employee


{
private String ename;
private int ecode;
private double basicpay;

public Employee(String name,


int code,
double sal) {
ename = name;
ecode = code;
basicpay = sal;
}

public double salCalc() {


double hra = 20.0/100.0 * basicpay;
double da = 25.0/100.0 * basicpay;
double ta = 10.0/100.0 * basicpay;
double allowance;
double sal = basicpay + hra + da + ta;
if (ecode <= 100) {
allowance = 20.0/100.0 * sal;
if(allowance > 2500)
allowance = 2500;
}
else {
allowance = 1000;
}

double tsal = sal + allowance;


return tsal;
}

public void display() {


double totalsal = salCalc();
System.out.println("Name : " + ename);
System.out.println("Employee Code : " + ecode);
System.out.println("Basic Pay : Rs. " + basicpay);
System.out.println("Salary : " + totalsal);
}

public static void main() {


Scanner in = new Scanner(System.in);
double totalsal;
System.out.print("Enter Name : ");
String name = in.nextLine();
System.out.print("Enter Employee Code : ");
int empcode = in.nextInt();
System.out.print("Enter Basic Pay : ");
double basicsal = in.nextDouble();
Employee obj = new Employee(name, empcode, basicsal);
obj.display();
}
}

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;

public class KboatGraduationYear


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003,
2006, 2007, 2009, 2010};

System.out.print("Enter graduation year to search: ");


int year = in.nextInt();

int l = 0, h = n.length - 1, idx = -1;


while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
idx = m;
break;
}
else if (n[m] < year) {
l = m + 1;
}
else {
h = m - 1;
}
}

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;

public class NeonNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int n = in.nextInt();
int sq = n * n;
int sqSum = 0;

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");
}
}

13.Define a class Student described as below

Data members/instance variables


name, age, m1, m2, m3 (marks in 3 subjects), maximum, average

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;

public class Student


{
private String name;
private int age;
private int m1;
private int m2;
private int m3;
private int maximum;
private double average;

public Student(String n, int a, int s1,


int s2, int s3) {
name = n;
age = a;
m1 = s1;
m2 = s2;
m3 = s3;
}

public void compute() {


average = (m1 + m2 + m3) / 3.0;

maximum = Math.max(m1, m2);


maximum = Math.max(maximum, m3);
}

public void display() {


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Subject 1 Marks: " + m1);
System.out.println("Subject 2 Marks: " + m2);
System.out.println("Subject 3 Marks: " + m3);
System.out.println("Maximum Marks: " + maximum);
System.out.println("Average Marks: " + average);
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
String n = in.nextLine();
System.out.print("Enter age: ");
int a = in.nextInt();
System.out.print("Enter Subject 1 Marks: ");
int sm1 = in.nextInt();
System.out.print("Enter Subject 2 Marks: ");
int sm2 = in.nextInt();
System.out.print("Enter Subject 3 Marks: ");
int sm3 = in.nextInt();
Student obj = new Student(n, a, sm1,
sm2, sm3);
obj.compute();
obj.display();
}
}
14.Define a class to overload the method display as follows:

void display(): To print the following format using nested loop

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();
}
}

public void display(int n) {


int sum = 0;

for (int i = 1; i <= n / 2; i++) {


if (n % i == 0) {
sum += i;
}
}

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();

System.out.print("Enter a number: ");


int num = in.nextInt();
obj.display(num);

}
}

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;

public class KboatWordFrequency


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence : ");
String str = in.nextLine();
System.out.print("Enter the word : ");
String ipWord = in.nextLine();
str += " ";
String word = "";
int count = 0;
int len = str.length();

for (int i = 0; i < len; i++) {


if (str.charAt(i) == ' ') {

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;

public class KboatTelephoneNos


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 5;
String names[] = new String[n];
String numbers[] = new String[n];

for (int i = 0; i < n; i++) {


System.out.print("Enter Name: ");
names[i] = in.nextLine();
System.out.print("Enter Telephone number : ");
numbers[i] = in.nextLine();
}

System.out.print("Enter name to be searched : ");


String name = in.nextLine();

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");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy