0% found this document useful (0 votes)
153 views49 pages

Computer Project Isc Class 11 1st Term

Uploaded by

Aaryan Paul
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)
153 views49 pages

Computer Project Isc Class 11 1st Term

Uploaded by

Aaryan Paul
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/ 49

1|Page

CONTENTS

Program Class Name Brief Info. Page No.


No.
1. Electricity Bill Calculates net bill amount 4

2. Parking Calculates total parking charges 8

3. PrimeNumbers Displays all prime numbers between 13


range input by user
4. KrishnaMurthyNums Checks if number input by user is 17
Krishnamurthy or not
5. Pattern Displays a pattern as per number of 22
rows entered by user
6. SmithNum Checks if number input by user is 26
Smith Number or not
7. Words Displays number of words which 32
starts and ends with a vowel
8. PrimeWords Displays list of words whose sum of 36
ASCII value of letters is a prime
number
9. ChangeVowels Displays word entered with vowels 42
changed to uppercase and
consonants to lowercase
10. LetterFreq Displays frequency of each letter in 46
different lines

2|Page
INTRODUCTION

Java Language and Platform:


Java is a high-level, versatile, and object-oriented programming language developed by Sun
Microsystems (now owned by Oracle). It was designed to be platform-independent and is
known for its "write once, run anywhere" capability. Java offers a robust, secure, and portable
environment for developing a wide range of applications, from desktop to web and mobile
applications. The Java platform consists of two main components: the Java Development Kit
(JDK) and the Java Runtime Environment (JRE). The JDK includes tools and libraries
necessary for Java development, such as compilers, debuggers, and application programming
interfaces (APIs). The JRE enables the execution of Java applications by providing the
necessary runtime environment.

Key Features:
- Object-Oriented - Security.
- Exception Handling - Platform Independence
- Rich Standard Library - Memory Management.

BlueJ Software:
BlueJ is an integrated development environment (IDE) specifically designed for teaching and
learning Java programming. It was developed at the University of Kent and Deakin
University to provide a user-friendly environment for beginners to grasp the concepts of
object-oriented programming.

Key Features:
- Simplicity - Object Inspection
- Visual Representation - Integrated Editor
- Interactive Debugger - Educational Focus

Overall, Java is a widely-used programming language with a strong emphasis on platform


independence and object-oriented programming. BlueJ software serves as an educational tool
to teach programming concepts using Java, particularly aimed at beginners and students
learning the language.

3|Page
PROGRAM 1

Question:
Design a class named “ElectricityBill” having the following data members and member
functions:
Data Members:
int units : stores the number of units consumed in a month
billAmt : stores the bill amount payable
Member Functions:
void accept( ) : accepts the number of units consumed in a month
float calcBill( ) : returns the net bill amount to be paid as per the criteria given below:
Units Consumed Rate / unit (in Rs.)
<=50 2.25
<=150 and >50 3.75
<=300 and >150 5.25
<=550 and >300 7.75
>550 9.50
void display( ) : displays the net bill amount
Define a main( ) to invoke the functions of the above class using an object of the above class.

Algorithm:

1. Start of algorithm

2. Import the `java.util.Scanner` package.

3. Define a class named `ElectricityBill`.

4. Declare private instance variables `units` (int) and `billAmt` (float) within the class.

5. Create a non-parameterized constructor `ElectricityBill`:


- Initialize the `units` variable to 0.
- Initialize the `billAmt` variable to 0.0.

4|Page
6. Create a parameterized constructor `ElectricityBill(int units, float billAmt)`:
- Set the `units` and `billAmt` variables using the values passed as arguments.

7. Create a method named `accept()`:


- Initialize a `Scanner` object `sc` to read input from the user.
- Display a message asking the user to enter the number of units consumed.
- Read and store the input in the `units` variable.

8. Create a method named `calcBill()`:


- Use conditional statements to calculate the electricity bill based on the consumed units:
- If `units` is less than or equal to 50, calculate `billAmt = units * 2.25f`.
- If `units` is greater than 50 but less than or equal to 150, calculate `billAmt = (units - 50)
* 3.75f + 50 * 2.25f`.
- If `units` is greater than 150 but less than or equal to 300, calculate `billAmt = (units -
150) * 5.25f + 50 * 2.25f + 150 * 3.75f`.
- If `units` is greater than 300 but less than or equal to 550, calculate `billAmt = (units -
300) * 7.75f + 50 * 2.25f + 150 * 3.75f + 150 * 3.75f`.
- If `units` is greater than 550, calculate `billAmt = (units - 550) * 9.50f + 50 * 2.25f + 150
* 3.75f + 150 * 3.75f + 250 * 7.75f`.
- Return the calculated `billAmt`.

9. Create a method named `display()`:


- Call the `calcBill()` method to get the calculated bill amount.
- Display the final bill amount to the user.

10. In the `main` method:


- Create an instance of the `ElectricityBill` class named `ob`.
- Call the `accept()` method to get the input from the user.
- Call the `display()` method to display the calculated bill amount.

11. End of the program.

5|Page
Source Code:

import java.util.Scanner;// importing java.util package


class ElectricityBill
{ // start of the class
private
int units;
float billAmt;
ElectricityBill()
{ // start of non-parametrized constructor
int units=0;
float billAmt=0.0f;
} // end of non-parametrized constructor
ElectricityBill(int units, float billAmt)
{ // start of parametrized constructor
this.units=units;
this.billAmt=billAmt;
} // end of parametrized constructor
void accept()
{ // start of accept member function
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of units consumed in a month");// accepting the
electrical units consumed in a month
units=sc.nextInt();// stores the number of units consumed in a month
} // end of accept member function
float calcBill()
{ // start of clacBill member function
if(units<=50){
billAmt=(units*2.25f);
}
else if(units<=150){

6|Page
billAmt=(units-50)*3.75f+50*2.25f;
}
else if(units<=300){
billAmt=(units-150)*5.25f+50*2.25f+150*3.75f;
}
else if(units<=550){
billAmt=(units-300)*7.75f+50*2.25f+150*3.75f+150*3.75f;
}
else{
billAmt=(units-550)*9.50f+50*2.25f+150*3.75f+150*3.75f+250*7.75f;
}
return billAmt; // returning the bill amount as per the criteria
} // end of clacBill member function
void display()
{ // start of dislay member function
System.out.println("The Final Bill Amount Of The Customer: "+calcBill());// displaying
the final bill amount
} // end of dislay member function
public static void main(String args[])
{ // start of main method
ElectricityBill ob=new ElectricityBill();
ob.accept();
ob.display();
} // end of main method
} // end of class

Variable Description Table:


VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF
NAME OF VARIABLE VARIABLE
units int stores the number of units Instance
consumed in a month
billAmt float stores the bill amount Instance
payable

7|Page
PROGRAM 2

Question:
Design a class named “Parking” having the following data members and member functions:
Data Members:
String vehNo : stores the vehicle number
int hrs : stores the number of hours parked
Member Functions:
void accept( ) : accepts the details of the vehicle from the user
int getCharges( ) : returns the total parking charges as per the criteria given below:
Parking Hours Rate / hour (in Rs.)
First 3 hours 35
Next 3 hours 30
Next 4 hours 28
Next 5 hours 25
Above 15 hours 22

void display( ) : displays the details of the vehicle and parking charges payable.
Define a main( ) to invoke the functions of the above class using an object of the above class

Algorithm:

1. Start of algorithm.

2. Import the `java.util.Scanner` package.

3. Define a class named `Parking`.

- Declare private instance variables: `vehNo` of type `String` and `hrs` of type `int`.

- Define a non-parametrized constructor:


- Initialize the `vehNo` as an empty string.
- Initialize `hrs` as 0.

8|Page
- Define a parametrized constructor:
- Accept parameters `vehNo` and `hrs`.
- Set `this.vehNo` to `vehNo`.
- Set `this.hrs` to `hrs`.

- Define a method named `accept()`:


- Create a `Scanner` object.
- Prompt the user to input the vehicle number.
- Read and store the vehicle number in `vehNo`.
- Prompt the user to input the number of hours the vehicle is parked.
- Read and store the hours in `hrs`.

- Define a method named `getCharges()`:


- Initialize a variable `ch` to 0 (initial charges).
- Calculate charges based on parking hours using conditional statements:
- If `hrs` is less than or equal to 3, calculate charges as `hrs * 35`.
- If `hrs` is less than or equal to 6, calculate charges as `(3 * 35) + ((hrs - 3) * 30)`.
- If `hrs` is less than or equal to 10, calculate charges as `(3 * 35) + (3 * 30) + ((hrs - 4) *
28)`.
- If `hrs` is less than or equal to 15, calculate charges as `(3 * 35) + (3 * 30) + (28 * 4) +
((hrs - 5) * 25)`.
- Otherwise, calculate charges as `(3 * 35) + (3 * 30) + (28 * 4) + (25 * 5) + (hrs * 22)`.
- Return the calculated charges.

- Define a method named `display()`:


- Print the vehicle number.
- Print the number of hours the vehicle is parked.
- Print the parking charges obtained from the `getCharges()` method.

- Define the `main` method:

9|Page
- Create an instance of the `Parking` class named `ob`.
- Call the `accept()` method on `ob` to input vehicle details.
- Call the `display()` method on `ob` to display the vehicle details and parking charges.

4. In the `main` method, create an object of the `Parking` class named `ob`.

5. Call the `accept()` method on the `ob` object to input vehicle details.

6. Call the `display()` method on the `ob` object to display the vehicle details and calculated
parking charges.

7. End of the `main` method and the `Parking` class.

Source Code:

import java.util.Scanner; // importing java.util package


class Parking{ // start of class
private
String vehNo;
int hrs;
Parking(){ // start of non-parametrized constructor
String vehNo="";
int hrs=0;
} // end of non-parametrized constructor
Parking(String vehNo, int hrs){ // start of parametrized constructor
this.vehNo=vehNo;
this.hrs=hrs;
} // end of parametrized constructor
void accept(){// start of accept member function
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Vehicle Number");

10 | P a g e
vehNo=sc.nextLine();// stores the vehicle number
System.out.println("Enter the number of Hours the vehicle is parked");
hrs=sc.nextInt();// stores the number of hours for which the car is parked
}// end of accept member function
int getCharges(){ // start of getCharges member function
int ch=0; // stores the amount charged according to the criteria
if(hrs<=3){
ch=hrs*35;
}
else if(hrs<=6){
ch=(3*35)+((hrs-3)*30);
}
else if(hrs<=10){
ch=(3*35)+(3*30)+((hrs-4)*28);
}
else if(hrs<=15){
ch=(3*35)+(3*30)+(28*4)+((hrs-5)*25);
}
else{
ch=(3*35)+(3*30)+(28*4)+(25*5)+(hrs*22);
}
return ch; // returning the parking charges as per the criteria
} // end of getCharges member function
void display(){// start of dislay member function
System.out.println("Vehicle Number "+vehNo);
System.out.println("The number of Hours the vehicle is parked "+hrs);
System.out.println("The Parking Charge "+getCharges());
} // end of dislay member function
public static void main(String args[]){ // start of main method
Parking ob=new Parking();

11 | P a g e
ob.accept();
ob.display();
} // end of main method
} // end of class

Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
vehNo String stores the vehicle number Instance

hrs int stores the number of Instance


hours parked
ch int stores the amount charged Class
according to the criteria

12 | P a g e
PROGRAM 3

Question:
Design a class named “PrimeNumbers” having the following data members and member
functions:
Data Members:
int x, y : stores the lower and upper limit of the range of numbers.
Member Functions:
void accept( ) : accepts the lower and upper limits from the user.
boolean isPrime(int num) : returns true if the number ‘num’ is prime otherwise it returns
false.
void display( ) : displays the prime numbers between the range x and y.
Define a main( ) to invoke the functions of the above class using an object of the above class.
Algorithm:

Certainly, here's the algorithm for the provided Java program:

1. Start of algorithm.

2. Import the `java.util` package.

3. Define a class named `PrimeNumbers`.

- Declare instance variables `x` and `y` of type `int`.

- Define a method `accept()`:


- Create a `Scanner` object.
- Prompt the user to input lower and upper limits for prime number range.
- Read and store the lower limit in `x`.
- Read and store the upper limit in `y`.

13 | P a g e
- Define a method `isPrime(int num)` that takes an integer `num` as input and returns a
boolean:
- Declare an integer `i` and initialize it to 1.
- Declare an integer `f` and initialize it to 0 (to count factors).
- Use a loop that iterates from `1` to `num` (inclusive):
- Check if `num` is divisible by `i` (num % i == 0).
- If true, increment `f`.
- Return `f` is equal to `2`. (A prime number has exactly two factors: 1 and itself.)

- Define a method `display()`:


- Use a loop that iterates from `x` to `y` (inclusive):
- Check if the current number (`i`) is prime using the `isPrime()` method.
- If true, print the prime number `i`.

- Define the `main` method:


- Create an instance of the `PrimeNumbers` class named `ob`.
- Call the `accept()` method on `ob` to input lower and upper limits.
- Call the `display()` method on `ob` to display prime numbers in the specified range.

4. In the `main` method:

- Create an object of the `PrimeNumbers` class named `ob`.

- Call the `accept()` method on the `ob` object to input lower and upper limits.

- Call the `display()` method on the `ob` object to display prime numbers within the
specified range.

5. End of the `main` method and the `PrimeNumbers` class.

14 | P a g e
Source Code:

import java.util.*;
class PrimeNumbers
{
int x,y;
void accept()
{
Scanner sc= new Scanner(System.in);
System.out.println(" Enter lower and upper limit ");
x=sc.nextInt();
y=sc.nextInt();
}

boolean isPrime( int num)


{
int i,f=0;
for(i=1;i<=num;i++)
if(num%i==0)
f++;
return f==2;

void display()
{
for(int i=x;i<=y;i++)
{
if(isPrime(i))
System.out.println(i);

15 | P a g e
}
}

public static void main(String args[])


{
PrimeNumbers ob= new PrimeNumbers();
ob.accept();
ob.display();
}
}

Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
x int stores the lower limit Instance
y int stores the upper limit Instance
i int for iteration of loop Instance
f int Counter and check Instance
variable

16 | P a g e
PROGRAM 4

Question:

Design a class named “KrishnaMurthyNums” having the following data members and
member functions:
Data Members:
int n : stores a positive number accepted from the user
Member Functions:
void takeNum( ) : accepts the number from the user

int factorial(int num) : returns the factorial of the number ‘num’


int getSum( ) : returns the sum of the factorial of the digits of the number
boolean isKrishna(int num) : returns true if ‘num’ is a KrishnaMurthy
number otherwise returns false .

Define a main( ) to invoke the functions of the above class using an object of
the above class.

Algorithm:

1. Start of algorithm.

2. Import the `java.util` package.

3. Define a class named `KrishnaMurthyNums`.

- Declare an instance variable `n` of type `int`.

- Define a method `takeNum()`:


- Create a `Scanner` object.
- Prompt the user to input a number.
- Read and store the input number in `n`.
17 | P a g e
- Define a method `factorial(int num)` that takes an integer `num` as input and returns an
integer:
- Declare an integer `i` and initialize it to 1.
- Declare an integer `f` and initialize it to 1.
- Use a loop that iterates from `1` to `num` (inclusive):
- Multiply `f` by `i`.
- Return the value of `f`.

- Define a method `getSum()` that returns an integer:


- Declare integers `d` and `s`, initialize `s` to 0.
- Initialize an integer `m` with the value of `n`.
- Use a loop to extract digits from `m`:
- Calculate `d` as `m % 10`.
- Add the factorial of `d` to `s` using the `factorial()` method.
- Update `m` by dividing it by 10.
- Return the value of `s`.

- Define a method `isKrishna(int num)` that takes an integer `num` as input and returns a
boolean:
- Set `n` to the value of `num`.
- Compare the result of `getSum()` with `n`.
- If they are equal, return `true`, else return `false`.

- Define a method `display()`:


- Check if the input number `n` is a Krishnamurthy number using the `isKrishna()`
method.
- Print "Krishnamurthy number" if true, otherwise print "Not Krishnamurthy number".

- Define the `main` method:


- Create an instance of the `KrishnaMurthyNums` class named `ob`.

18 | P a g e
- Call the `takeNum()` method on `ob` to input a number.
- Call the `display()` method on `ob` to determine and display if the number is
Krishnamurthy.

4. In the `main` method:

- Create an object of the `KrishnaMurthyNums` class named `ob`.

- Call the `takeNum()` method on the `ob` object to input a number.

- Call the `display()` method on the `ob` object to determine and display whether the
number is Krishnamurthy.

5. End of the `main` method and the `KrishnaMurthyNums` class.

Source Code:

import java.util.*;
class KrishnaMurthyNums
{
int n;
void takeNum()
{
Scanner sc= new Scanner(System.in);
System.out.println(" Enter a number ");
n=sc.nextInt();
}

int factorial( int num)


{
int i,f=1;

19 | P a g e
for(i=1;i<=num;i++)
f*=i;
return f;
}

int getSum()
{
int d,s=0,m=n;
while(m!=0)
{
d=m%10;
s=s+factorial(d);
m=m/10;
}
return s;
}

boolean isKrishna(int num)


{
n=num;
if(getSum() == n)
return true;
else
return false;
}

void display()
{
if(isKrishna(n))
System.out.println(" Krishnamurthy number ");

20 | P a g e
else
System.out.println(" Not Krishnamurthy number ");

public static void main(String args[])


{
KrishnaMurthyNums ob= new KrishnaMurthyNums();
ob.takeNum();
ob.display();
}
}
Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
n int stores a positive number Instance
accepted from the user
i int for iteration of loop Class
f int counter variable Class
num int for storing factorial of Class
number
d int Stores digit while digit Class
extraction
s int Stores sum of factorial Class
m int For digit extraction Class

21 | P a g e
PROGRAM 5

Question:

Design a class named “Pattern” having the following data members and member functions:
Data Members:
int rows : stores the number of rows of the pattern
Member Functions:
void getRows( ) : accepts the number of rows from the user
void display( ) : displays the pattern as shown below:
The Patterns shown below is for input rows=5

&
& @ &
& @ @ @ &
& @ @ @ @ @ &
& & & & & & & & &

Define a main( ) to invoke the functions of the above class using an object of the above
class.

Algorithm:

1. Start of algorithm

2. Import the `java.util` package.

3. Define a class named `Pattern`.

- Declare an instance variable `rows` of type `int`.

- Define a method `getRows()`:

22 | P a g e
- Create a `Scanner` object.
- Print a message to prompt the user to input the number of rows.
- Read and store the input number in `rows`.

- Define a method `display()`:


- Initialize three integer variables: `i`, `j`, and `k`.
- Iterate from `1` to `rows` for each row `i`:
- Print spaces to center-align the pattern based on `(rows - i)`.
- Iterate from `1` to `i * 2` for each column `j`:
- Check if `j` is `1`, `i * 2 - 1`, or if `i` is equal to `rows`:
- Print an ampersand character `&`.
- Otherwise, print an at symbol `@`.
- Move to the next line for the next row.

- Define the `main` method:


- Create an instance of the `Pattern` class named `ob`.
- Call the `getRows()` method on `ob` to input the number of rows.
- Call the `display()` method on `ob` to display the pattern.

4. In the `main` method:

- Create an object of the `Pattern` class named `ob`.

- Call the `getRows()` method on the `ob` object to input the number of rows.

- Call the `display()` method on the `ob` object to display the pattern.

5. End of the `main` method and the `Pattern` class.

23 | P a g e
Source Code:

import java.util.*;
class Pattern
{
int rows;
void getRows()
{
Scanner sc= new Scanner(System.in);
System.out.println(" Enter no rows "); rows=sc.nextInt();
}

void display()
{
int i,j,k;
for(i=1;i<=rows;i++)
{
for(k=1;k<=(rows-i);k++)
{
System.out.print(" ");
}
for(j=1;j<i*2;j++)
{
if( j==1 || j==(i*2-1) || i==rows)
System.out.print("&");
else
System.out.print("@");
}
System.out.println();
}

24 | P a g e
}

public static void main(String args[])


{
Pattern ob= new Pattern(); ob.getRows();
ob.display();
}
}

Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
rows int stores the number of rows Instance
of the pattern
i int For iteration of loops Class
j int For iteration of loops Class
k int For iteration of loops Class

25 | P a g e
PROGRAM 6

Question:

Design a class named “SmithNum” having the following data members and member
functions:
Data Members:
int num : stores the number to be checked.
Member Functions:
void getNum( ) : accepts the number from the user

int sumDigits(int n) : returns the sum of the digits of ‘n’.


int getSumofPF(int n) : returns the sum of the digits of the
prime factors of ‘n’

boolean isSmith( ) : returns true if num is Smith number


otherwise returns false.

void display( ) : displays whether ‘num’ is a Smith number


or not.

Define a main( ) to invoke the functions of the above class using an object of the above
class.

Algorithm:

1. Start of algorithm

2. Import the `java.util` package.

3. Define a class named `SmithNum`.

26 | P a g e
- Declare an instance variable `num` of type `int`.

- Define a method `getNum()`:


- Create a `Scanner` object.
- Prompt the user to input a number.
- Read and store the input number in `num`.

- Define a method `sumDigits(int n)` that takes an integer `n` as input and returns an
integer:
- Declare an integer `d` and initialize it to 0.
- Declare an integer `s` and initialize it to 0.
- Use a loop to extract digits from `n`:
- Calculate `d` as `n % 10`.
- Add `d` to `s`.
- Update `n` by dividing it by 10.
- Return the value of `s`.

- Define a method `getSumofPF(int n)` that takes an integer `n` as input and returns an
integer:
- Declare an integer `i` and initialize it to 2.
- Declare an integer `s` and initialize it to 0.
- Use a loop while `i` is less than or equal to `n`:
- Check if `n` is divisible by `i` (n % i == 0).
- If true, add the sum of digits of `i` to `s` using the `sumDigits()` method.
- Update `n` by dividing it by `i`.
- If not divisible, increment `i`.
- Return the value of `s`.

- Define a method `isSmith()` that returns a boolean:


- Compare the result of `sumDigits(num)` with `getSumofPF(num)`.
- If they are equal, return `true`, else return `false`.

27 | P a g e
- Define a method `display()`:
- Check if the input number `num` is a Smith number using the `isSmith()` method.
- Print "Smith number" if true, otherwise print "Not Smith number".

- Define the `main` method:


- Create an instance of the `SmithNum` class named `ob`.
- Call the `getNum()` method on `ob` to input a number.
- Call the `display()` method on `ob` to determine and display if the number is a Smith
number.

4. In the `main` method:

- Create an object of the `SmithNum` class named `ob`.

- Call the `getNum()` method on the `ob` object to input a number.

- Call the `display()` method on the `ob` object to determine and display whether the
number is a Smith number.

5. End of the `main` method and the `SmithNum` class.

The provided Java program checks if a given number is a Smith number, where the sum of
the digits of the number is equal to the sum of the digits of its prime factorization. The
algorithm breaks down the code's functionality into steps to provide a clear understanding of
its structure and logic.

Source Code:
import java.util.*;
class SmithNum
{
int num;

28 | P a g e
void getNum()
{
Scanner sc= new Scanner(System.in);
System.out.println(" Enter a number ");
num=sc.nextInt();
}

int sumDigits( int n)


{
int d,s=0;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
return s;
}

int getSumofPF(int n)
{
int i=2,s1=0 ;
while(i<=n)
{
if(n%i==0)
{
s1=s1+sumDigits(i);
n=n/i;
}
else

29 | P a g e
{
i++;
}
}
return s1;
}

boolean isSmith()
{
if(sumDigits(num) == getSumofPF(num))
return true;
else
return false;
}

void display()
{
if(isSmith())
System.out.println(" Smith number ");
else
System.out.println(" Not Smith number ");
}

public static void main(String args[])


{
SmithNum ob= new SmithNum();
ob.getNum();
ob.display();
}
}

30 | P a g e
Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
num int stores the number to be Instance
checked.
d int For digit extraction Class
s int For digit extraction Class
s1 int For storing sum of digits Class
n int For iteration of loops Class
i int For iteration of loop Class

31 | P a g e
PROGRAM 7

Question:

Design a class named “Words” having the following data members and member functions:
Data Members:
String w : stores a word accepted from the user
int n : stores the number of words to be accepted from the user
Member Functions:
int readWords( ) : accepts the words from the user and returns a count of
the number of words which ends and starts with a vowel.
boolean checkVowel(String s) : returns true if the word stored in ‘s’ starts and
ends with a vowel, otherwise returns false
void display( ) : displays the number of words which
starts and ends with a vowel.

Define a main( ) to invoke the functions of the above class using


an object

Algorithm:

1. Start of algorithm.

2. Import the `java.util` package.

3. Define a class named `Words`.

- Declare instance variables `w` of type `String` and `n` of type `int`.

- Define a method `readWords()`:


- Create a `Scanner` object.

32 | P a g e
- Prompt the user to input the number of words.
- Read and store the input number in `n`.
- Initialize an integer `t` to 0 (to count words that start and end with vowels).
- Use a loop to input `n` words:
- Prompt the user to input a word.
- Read and store the word in `w`.
- Check if the word starts and ends with vowels using the `checkVowel()` method:
- If true, increment `t`.
- Return the value of `t`.

- Define a method `checkVowel(String s)` that takes a string `s` as input and returns a
boolean:
- Get the length of string `s` and store it in `l`.
- Create a string `v` containing all the vowel characters in upper and lower case:
`"AEIOUaeiou"`.
- Get the first character `c` of string `s`.
- Get the last character `d` of string `s`.
- Check if both `c` and `d` are present in the `v` string using the `indexOf()` method:
- If true, return `true` (word starts and ends with a vowel).
- If not, return `false`.

- Define a method `display()`:


- Call the `readWords()` method and store the result in the variable `count`.
- Display the count of words that start and end with a vowel.

- Define the `main` method:


- Create an instance of the `Words` class named `ob`.
- Call the `display()` method on `ob` to perform the word processing and display the
result.

4. In the `main` method:

33 | P a g e
- Create an object of the `Words` class named `ob`.

- Call the `display()` method on the `ob` object to perform the word processing and display
the count.

5. End of the `main` method and the `Words` class.

Source Code:

import java.util.*;

class Words {
String w;
int n;

int readWords() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of words");
n = sc.nextInt();
int t = 0;
for (int i = 1; i <= n; i++) {
System.out.println("Enter a word");
w = sc.next();
if (checkVowel(w))
t = t + 1;
}
return t;
}

boolean checkVowel(String s) {

34 | P a g e
int l = s.length();
String v = "AEIOUaeiou";
char c = s.charAt(0);
char d = s.charAt(l - 1);
if (v.indexOf(c) >= 0 && v.indexOf(d) >= 0)
return true;
else
return false;
}

void display() {
int count = readWords();
System.out.println("Number of words starting and ending with a vowel: " + count);
}

public static void main(String args[]) {


Words ob = new Words();
ob.display();
}
}
Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
w String stores the number of rows Instance
of the pattern
n int For storing number of Instance
words
t int Temporary variable Class
1 int Length of word Class
v String Stores vowels Class
c char Stores first letter Class
d char Stores last letter Class

35 | P a g e
PROGRAM 8

Question:

Design a class named “PrimeWords” having the following data members and member
functions:
Data Members:
String sent : stores the sentence to be operated upon
String fstr : stores a list of words whose sum of the ASCII value of the letters is
a prime number.
Member Functions:
void accept( ) : accepts a sentence from the user
int getSum(String w) : returns the sum of the ASCII value of the letters of the word ‘w’.

void findWords( ) : stores all the words from the sentence ‘s’ whose
sum of the ASCII value of the letters is a prime number into the String
object ‘fstr’.
void show( ) : displays the original sentence and the list of words
whose sum of the ASCII value of the letters is a prime number.
Define a main( ) to invoke the functions of the above class using an object of the above
class.

Algorithm:

1. Start of Algorithm

2. Class PrimeWords
- Declare instance variables `sent` (String) and `fstr` (String).

Method: accept()
- Display "Enter a sentence:"
- Create a `Scanner` object `sc`.
- Read a line of text from user input using `sc`.

36 | P a g e
- Store the input text in `sent`.

Method: getSum(String w)
- Initialize an integer variable `sum` to 0.
- For each character `c` in string `w`:
- Add the ASCII value of `c` to `sum`.
- Return `sum`.

Method: findWords()
- Initialize an empty string array `words`.
- Split the `sent` into words using space as a delimiter and store in `words`.
- For each word `word` in `words`:
- Calculate the sum of ASCII values of characters in `word` using `getSum()`.
- If the sum is prime (using `isPrime()`):
- Append `word` to `fstr`.

Method: isPrime(int num)


- If `num` is less than or equal to 1, return `false`.
- For each `i` from 2 to the square root of `num`:
- If `num` is divisible by `i`, return `false`.
- Return `true`.

Method: show()
- Print "Original Sentence: " followed by `sent`.
- Print "Words with Prime ASCII Sum: " followed by `fstr`.

Method: main(String args[])


- Create an instance of `PrimeWords` named `ob`.
- Call the `accept()` method on `ob` to input the sentence.
- Call the `findWords()` method on `ob` to identify words with prime ASCII sum.

37 | P a g e
- Call the `show()` method on `ob` to display the results.

End of Class PrimeWords


3. Create an object:
- Create an object of the `PrimeWords` class named `ob`.

Method: main(String args[])


4. Call the `main()` method:
- Call the `main()` method on the `ob` object to start the program execution.

5. End of Algorithm

Source Code:

import java.util.*;
class PrimeWords {
String sent;
String fstr;

void accept() {
System.out.println("Enter a sentence:");
Scanner sc = new Scanner(System.in);
sent = sc.nextLine();
}

int getSum(String w) {

38 | P a g e
int sum = 0;
for (int i = 0; i < w.length(); i++) {
sum += (int) w.charAt(i);
}
return sum;
}

void findWords() {
fstr = "";
String[] words = sent.split(" ");
for (String word : words) {
int sum = getSum(word);
if (isPrime(sum)) {
fstr += word + " ";
}
}
}

boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

39 | P a g e
void show() {
System.out.println("Original Sentence: " + sent);
System.out.println("Words with Prime ASCII Sum: " + fstr);
}

public static void main(String args[]) {


PrimeWords ob = new PrimeWords();
ob.accept();
ob.findWords();
ob.show();
}
}

40 | P a g e
Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
sent String stores the number of rows Instance
of the pattern
fstr String stores a list of words Instance
whose sum of the ASCII
value of the letters is a
prime number.
i int For iteration of loops Class
w String For storing word Class
sum int Stores sum of ascii Class
word String[ ] Stores the words in an Class
array
num int Stores number Class

41 | P a g e
PROGRAM 9

Question:

Design a class named “ChangeVowels” having the following data members and member
functions:
Data Members:
String w : stores a word to be operated upon
String nw : stores the changed word
Member Functions:
void readWord( ) : accepts a word from the user.
String change( ) : returns a word with all the vowels changed to
uppercase and consonants changed into lowercase.
void display( ) : displays the original word and the new word.
Define a main( ) to invoke the functions of the above class using an object of the above
class.

Algorithm:
Certainly, here's the algorithm for the provided Java program:

1. Start of Algorithm.

2. Class ChangeVowels
- Declare instance variables `w` (String) to store the input word and `nw` (String) to store
the changed word.

Method: readWord()
- Display "Enter a word:"
- Create a `Scanner` object `sc`.
- Read a word from user input using `sc` and store it in `w`.

42 | P a g e
Method: change()
- Initialize an empty string `nw`.
- For each character `c` in the string `w`:
- If `c` is a vowel (using `isVowel()`):
- Convert `c` to uppercase and append it to `nw`.
- Else:
- Convert `c` to lowercase and append it to `nw`.
- Return `nw`.

Method: isVowel(char c)
- Convert `c` to lowercase.
- If `c` is 'a', 'e', 'i', 'o', or 'u', return `true`; otherwise, return `false`.

Method: display()
- Print "Original Word: " followed by the value of `w`.
- Print "Changed Word: " followed by the result of the `change()` method.

Method: main(String args[])


- Create an instance of `ChangeVowels` named `ob`.
- Call the `readWord()` method on `ob` to input the word.
- Call the `display()` method on `ob` to display the original and changed words.

End of Class ChangeVowels

3. Create an object:
- Create an object of the `ChangeVowels` class named `ob`.

Method: main(String args[])


4. Call the `main()` method:
- Call the `main()` method on the `ob` object to start the program execution.

43 | P a g e
5. End of Algorithm.
Source Code:
import java.util.Scanner;

class ChangeVowels {
// Data Members
String w; // stores a word to be operated upon
String nw; // stores the changed word

// Member Function: readWord()


// Accepts a word from the user.
void readWord() {
System.out.println("Enter a word:");
Scanner sc = new Scanner(System.in);
w = sc.next();
}

// Member Function: change()


// Returns a word with all the vowels changed to uppercase and consonants changed into
lowercase.
String change() {
nw = "";
for (int i = 0; i < w.length(); i++) {
char c = w.charAt(i);
if (isVowel(c)) {
nw += Character.toUpperCase(c);
} else {
nw += Character.toLowerCase(c);
}
}

44 | P a g e
return nw;
}
// Helper Function: isVowel(char c)
// Checks if the given character is a vowel.
boolean isVowel(char c) {
c = Character.toLowerCase(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
// Member Function: display()
// Displays the original word and the new word.
void display() {
System.out.println("Original Word: " + w);
System.out.println("Changed Word: " + change());
}

// Main Method
public static void main(String args[]) {
ChangeVowels ob = new ChangeVowels();
ob.readWord();
ob.display();
}
}
Variable Description Table:

VARIABLE DATA TYPE DESCRIPTION/USAGE SCOPE OF


NAME OF VARIABLE VARIABLE
w string stores a word to be instance
operated upon
nw string stores the changed word instance
i int for iteration of loop class
c char stores each letter of word class

45 | P a g e
PROGRAM 10

Question:
Design a class named “LetterFreq” having the following data members and member functions:
Data Members:
String sent : stores the sentence to be operated upon.

Member Functions:
void readSentence( ) : reads a sentence from the user.

void display( ) : displays the frequency of each alphabet in different lines

Algorithm:

1. Start of algorithm

2. Import the `java.util` package.

3. Define a class named `LetterFreq`.

- Declare an instance variable `sent` of type `String`.

- Define a method `readWord()`:


- Create a `Scanner` object.
- Print a message to prompt the user to input a string.
- Read and store the input string in `sent`.

- Define a method `display()`:


- Initialize an integer variable `i` to represent ASCII values of uppercase letters.
- Initialize an integer variable `l` to store the length of the input `sent`.
- Convert the input `sent` to uppercase and store it in a new string `s`.
- Iterate from `'A'` to `'Z'` (ASCII values of uppercase letters) using `i`:

46 | P a g e
- Initialize an integer variable `t` to count the frequency of the current letter.
- Iterate over each character `c` in the string `s`:
- Compare the current character `c` with the current letter `i`:
- If they are equal, increment the count `t`.
- Check if `t` is greater than `0` (the letter appears in the string):
- If true, print the current letter `i` and its frequency `t`.

- Define the `main` method:


- Create an instance of the `LetterFreq` class named `ob`.
- Call the `readWord()` method on `ob` to input a sentence.
- Call the `display()` method on `ob` to display the frequency of each letter.

4. In the `main` method:

- Create an object of the `LetterFreq` class named `ob`.

- Call the `readWord()` method on the `ob` object to input a sentence.

- Call the `display()` method on the `ob` object to display the frequency of each letter.

5. End of the `main` method and the `LetterFreq` class.

Source Code:
import java.util.*;
class LetterFreq
{
String sent;

void readWord( )
{

47 | P a g e
Scanner sc= new Scanner (System.in); System.out.println(" Enter a string ");
sent=sc.nextLine();
}

void display( )
{
int i,l=sent.length(),j;
String s=sent.toUpperCase();
for(i='A';i<='Z';i++)
{
int t=0;
for( j=0;j<l;j++)
{
char c=s.charAt(j);
if(c==i)
t++;
}
if(t>0)
System.out.println((char)i+" is " + t+" times");
}
}

public static void main(String args[])


{
LetterFreq ob= new LetterFreq();
ob.readWord();
ob.display();

}
}

48 | P a g e
Variable Description Table:

variable name data type description/usage of scope of variable


variable
sent string stores the sentence to be instance
operated upon.
i int for iteration of loop class
l int for storing length of word class
j int for iteration of loop class
t int temporary variable class
c char stores each letter of word class

49 | P a g e

You might also like