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

Computer Applications Lab Manual 2024-25

The document contains a series of assignments for Class X Computer Applications at Blue Ridge Public School for the academic year 2024-25. It covers fundamental concepts in Java programming, including definitions of key terms, coding assignments, and explanations of various programming principles such as object-oriented programming, data types, method overloading, and error handling. Additionally, it includes practical coding exercises and examples to reinforce the learning of Java concepts.

Uploaded by

altvassy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Computer Applications Lab Manual 2024-25

The document contains a series of assignments for Class X Computer Applications at Blue Ridge Public School for the academic year 2024-25. It covers fundamental concepts in Java programming, including definitions of key terms, coding assignments, and explanations of various programming principles such as object-oriented programming, data types, method overloading, and error handling. Additionally, it includes practical coding exercises and examples to reinforce the learning of Java concepts.

Uploaded by

altvassy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 1

1. Define the following terms:


a. Class
Ans: A class is a blueprint or a template for its objects.
b. Object
Ans: An object is an instance of a class
c. Abstraction
Ans: Showing of the essential features while hiding the complexity is
known as abstraction.
d. Encapsulation
Ans: Keeping together the code and the data that it manipulates is
known as encapsulation.
e. Inheritance
Ans: When a child class inherits the features of a parent class and adds
its own functionality, it is known as inheritance.
f. Polymorphism
Ans: In a class there can be more than one method with the same
name.

2. Why is class called as an object factory?


Ans: The class is called an object factory as objects are created from the class
that contain common attributes and behavior.

3. Define the following terms:


a. JVM
Ans: JVM (Java Virtual Machine) is an abstract machine. It is a
specification that provides runtime environment in which java bytecode
can be executed. JVM is an interpreter.
b. JRE
Ans: The Java Runtime Environment is the implementation of the JVM.
It physically exists.
c. JDK
Ans: The Java Development Kit physically exists. It contains the JRE
and other development tools.

4. What is ‘Unicode’?
Ans: Unicode is a standard encoding system. It is a wide representation of
characters in numeric form.

5. What are tokens?


Ans: Tokens are the basic building blocks of the Java language.
All the characters of a Java program are grouped into symbols called tokens.

6. State the rules for naming an identifier.


Ans: An identifier can be of any length.
It may contain alphabets, digits, underscore (_) or dollar sign ($)
characters.
.It must not be a keyword or true, false or null literal.
It must not begin with a digit.
It can start with only 2 special characters $ and _
Ex: String _s; or int $n1;

7. Define Keywords. List any ten keywords available in Java.


Ans: A keyword is one of the 50 reserved words that have a predefined
meaning in the language.
Keywords have special meaning to the Java compiler.
Eg: public, static, void, new, int, byte, short, long, char, boolean, float

8. What are operators?


Ans: An operator is a symbol or letter, which makes the compiler perform a
specific operation on the operands in a program.
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 2

1. Evaluate the following:

a) int x=20, y=30;

z=++x + y++ - x/2;

Soln:

z= 21 + 30 - 21/2

z= 51-10

z=41

b) int p = 3, q= 8;

r=q++%p * ++q + p++;

Soln:

r= 8 % 3 * 10 + 3

r = 2 * 10 + 3

r= 23

c) int a=5, b=8, c=9, d=4;

d-=a++ + --b *d + b/a - ++c;

Soln:

d= 4 – (5 + 7 * 4 + 7/6 – 10)

d= 4 – (5 + 28 + 1 – 10)

d= 4 – 24

d= -20
d) int a=4, b=8, c=12, d=4;

d/=a++ + --b *d + b%a - ++c;

Soln:

d= 4/(4 + 7*4 + 7%5 -13)

d=4/21

d=0

2. What is associativity?
Ans: When two operators have the same precedence, associativity determines
the evaluation order.
Associativity can be either left to right or right to left.

3. Distinguish between:

A. Integer and Floating types

Ans:

Integer Type Floating Type


A variable declared integer type A variable declared floating type can
contains a whole number. store a fraction.
byte, short, int and long are integer float and double are floating types
types

B. Implicit and Explicit type casting.

Ans:

Implicit Typecasting Explicit Typecating


Known as automatic or widening Known as narrowing conversion.
conversion.
Takes place when the two types are When you are assigning a larger
compatible and the target type is type value to a variable of smaller
larger than the source type. type, then you need to perform
explicit type casting.

4. Give the capacity in bytes for the int and long data types.
Ans: int – 4 bytes, long – 8 bytes

5. What do you mean by primitive data type? Give examples.


Ans: The datatypes which are independent of any other type are known as
primitive datatypes. They are pre-defined in the system.
Eg: boolean, char, byte, short, int, long

6. Assign the value of the following constants to a variable with


requisite data type.
a) 7.243 b) 3112233L c) 245.453f d) true

Ans:

a) double d = 7.243
b) long a = 3112233
c) float f = 245.453f
d) boolean b =true

7. Differentiate between Static Initialization and Dynamic


Initialization.

Ans:

Assigning a specific value to a variable is known as initialization.

The two types of initializations are static and dynamic initialization.

Static Initialization: Assigning a constant to a variable at the time of its


declaration is known as static initialization.
Eg: int a =5;

Dynamic Initialization: When a variable gets initialized at run time, it is known


as dynamic initialization.

Eg: int c =a + b;

8. What are escape sequences? Give examples.

Ans:

Escape sequences are used to signal an alternative interpretation of a series


of characters.

In Java, a character preceded by backslash is an escape sequence. It has


special meaning to the Java compiler.

Eg: \t, \n, \\, \’, \”


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 3

1. Name any two built in Java libraries.


Ans:

java.util.*
java.lang.*
java.io.*

2. Create an object xyz of class Alphabets


Ans:

Alphabets xyz = new Alphabets();

3. Create a prototype of function Test that accepts two integers as


arguments and returns either true or false.

Ans:

boolean Test(int a, int b)

4. State and explain the 3 types of comments.


Ans:
a. Single line
b. Multi line
c. Documentation
(write explanation)

5. State and explain the 3 types of errors.


Ans:
a. Syntax error
b. Logical error
c. Runtime error

(write explanation)
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 4

Write the output of the following:


1. Math.floor(3.9)

2. Math.ceil(5.1)

3. Math.max(Math.ceil(-3.7),-4))

4. Math.ceil(Math.abs(-8.6))

5. Math.pow(7,3)

6. Math.sqrt(Math.min(25,16))

7. Math.cbrt(Math.max(27,64))

8. Math.round(Math.abs(-12.2))

Write the Java expression for the following:

1. x(a+b)

2. (ab)/3

3. 𝟐xy + 𝟑yz + 𝟏xz


𝟓 𝟐 𝟑

Answer the following:

1. Create an object of the scanner class.

Ans:

Scanner sc = new Scanner(System.in)

2. Convert the following for loop to while loop.


for(int i = 1; i<=15; i++)
{
System.out.println(i);
}
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 5: Implementation of Classes

Design a class RailwayTicket with the following description:


➢ Instance Variables/Data Members
• String name: To store the name of the customer
• String coach: To store the type of coach the customer wants to
travel in
• long mobno: To store the customer’s mobile number
• int amt: To store basic amount of ticket
• int totalamt: To store the amount to be paid after updating the
original amount

➢ Member Methods
• void accept(): To take input of name, coach, mobile number
and amount
• void update(): To update amount as per coach selected

• Extra amount to be added in the amount as per follows:

Types of Coaches Amount


First_AC 700

Second_AC 500

Third_AC 250

sleeper none

• void display(): To display all the details of the customer such as


name, coach, mobile number and total amount.

Write the main method to create an object of the class and invoke the
member methods.

import java.util.*;
class RailwayTicket
{
String name, coach;
long mobno;
int amt, totalamt;

void accept()
{

Scanner sc = new Scanner(System.in);


System.out.println(“Enter Customer name: ”);
name = sc.nextLine();
System.out.println(“Enter desired coach: ”);
coach = sc.nextLine();
System.out.println(“Enter mobile number: ”);
mobno = sc.nextLong();
System.out.println(“Enter basic amount: ”);
amt = sc.nextInt();

}//end of accept method

void update()
{
if(coach.equalsIgnoreCase(“First_AC”)
{
totalamt = amt + 700;
}
else if(coach.equalsIgnoreCase(“Second_AC”)
{
totalamt = amt + 500;
}
else if(coach.equalsIgnoreCase(“Third_AC”)
{
totalamt = amt + 250;
}
else if(coach.equalsIgnoreCase(“sleeper”)
{
totalamt = amt;
}
else
{
System.out.println(“Invalid Coach”);
System.exit(0);
}

}//end of update method

void display()
{
System.out.println(“Customer Name: ” +
name);
System.out.println(“Desired Coach: ” +
coach);
System.out.println(“Mobile Number: ” +
mobno);
System.out.println(“Total amount to be paid:
Rs.” + totalamt);

}//end of display method

public static void main(String [] args)


{
RailwayTicket obj = new RailwayTicket();
obj.accept();
obj.update();
obj.display();

}//closed main method

}//program terminates

OUTPUT
Enter Customer Name:
Aniruddh Mathur
Enter desired coach:
First_AC
Enter mobile number:
8888844444
Enter basic amount:
10000
Customer Name: Aniruddh Mathur
Desired Coach: First_AC
Mobile Number: 8888844444
Total amount to be paid: Rs.10700

Variable Name Data type Description

name String stores the customer’s name

coach String stores the type of coach the


customer wants to travel in

mobno long stores customer’s mobile


number

amt int stores the basic price of the


ticket

totalamt int Stores the amount to be


paid after updating the
original amount
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 6: Method Overloading

Design a class Overload to overload a function area() as follows:


1. double area(double a, double b, double c)
• return the area of a scalene triangle using the formula:
area = √s(s-a)(s-b)(s-c)
where s = (a+b+c)/2
2. double area(int a, int b, int height)
• with three integer arguments, returns the area of a trapezium
using the formula:
area = ½ * height * (a +b)
3. double area(double diagonal1, double diagonal2)
• with two double arguments, returns the area of a rhombus
using the formula:
area = ½ * (diagonal1 * diagonal2)

class Overload
{
//method to find the area of a scalene triangle
double area(double a, double b, double c)
{
double s = (a + b + c)/2;
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return area;

}//end of method

double area(int a, int b, int height)


{
double area = 0.5 * height * (a+b);
return area;

}//end of method

double area(double diagonal1, double diagonal2)


{
double area = 0.5 * diagonal1 * diagonal2;
return area;

}//end of method
}//class ends

Variable Name Data type Description

a, b, c double to store the values of the


sides of the input scalene
triangle

a, b int to store the values of the


parallel sides of the input
trapezium

height int to store the value of the


height of the input
trapezium

diagonal1, diagonal2 double to store the values of the


diagonals of the input
rhombus
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 7: Method Overloading

Design a class Overload to overload a function volume () as


follows:
1. double volume (double r)
where radius (r) as an argument
• returns the volume of a sphere using the formula:
V = 4/3 * 22/7 * r3
2. double volume(double h, double r)
• with height(h) and radius (r) as arguments, returns
the volume of a cylinder using the formula:
V = 22/7 * r2 * h
3. double volume(double l, double b, double h)
• with length(l), breadth (b) and height (h) as
arguments, returns the volume of a cuboid using the
formula:
V=l*b*h

No need to write the main method. Please do write the VDT.

public class OverLoad1


{
double V;
double volume(double R)
{
V= 4/3 * 22/7 * R * R * R;
return V;
}
double volume(double H, double R)
{
V= 22/7 * R * R * H;
return V;
}
double volume(double L, double B, double H)
{
V=L*B*H;
return V;
}
}
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 8:

Write a program to determine if a given number is a Cyclo number or


not.

import java.util.*;
class Cyclo
{

void checkCyclo()
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a number:”);
int n = sc.nextInt();

//convert integer to string


String str = Integer.toString(n);

//determine length of the string


int len = str.length();

//extract the first and last digits


char first = str.charAt(0);
char last = str.charAt(len-1);

//compare the digits to check for cyclo

if(first == last)
System.out.println(n + “ is a Cyclo
number”);
else
System.out.println(n + “ is not a
Cyclo number”);

}//end of method

public static void main(String [] args)


{
Cyclo obj = new Cyclo();
obj.checkCyclo();
}

}//class ends
OUTPUT

Enter a number:
704327
704327 is a Cyclo number

Variable Name Data type Description

n int to store the input number

str String to store the String form of


the number

len int to store the length of the


string

first char to store the first element of


the string

last char to store the last element of


the string
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 9

1. Write a program in Java to determine if a given number is a nest


number or not.

import java.util.*;
public class Nest
{
public void determineNest()
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a number: “);
int n = sc.nextInt();
int p = 1, tempnum = n;
while(tempnum != 0)
{
p = p * (tempnum % 10);
tempnum /= 10;
}
if(p == 0)
System.out.println(n + “ is a Nest number”);
else
System.out.println(n + “ is not a Nest number”);
}//closed the method

public static void main(String [] args)


{
Nest obj = new Nest();
obj.determineNest();
}//closed main method
}//program terminates

OUTPUT Enter a number:


103467
103467 is a Nest number

Variable Name Data type Description

n int stores the input number

tempnum int stores an integer value

p int stores the product of


digits of n
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 10

1. Write a program in Java to find the sum of the following series:

1 2 3
S= + + +……upto n terms
𝑎1 𝑎3 𝑎5
import java.util.*;
public class Series
{
public void sumSeries()
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number of terms: “);
int n = sc.nextInt();
System.out.println(“Enter the denominator value: “);
int a = sc.nextInt();
int j = 0; double sum = 0;
for(int i = 1; i <= n; i++)
{
j = 2 * i - 1;
sum += i/Math.pow(a,j);
}
System.out.println(“Sum of the series is: “ + sum);

}//closed the method

public static void main(String [] args)


{
Series obj = new Series();
obj.sumSeries();
}//closed main method
}//program terminates

OUTPUT Enter number of terms:


10
Enter the denominator value:
2
Sum of the series is: 0.8888816833496094

Variable Name Data type Description

n int stores the input number


of terms

a int stores the input of


denominator value

sum double stores the sum of the


series

i int loop counter

j int stores power of


denominator value
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 11

Write a program in Java to accept the names of 10 cities in single


dimension string array and their STD codes in another single dimension
array. Seach for a name of a city input by the user in the list, using
linear search technique. If found, display “Search Succesful” and print
the name of the city along with its STD code, or else display the
message “Search Unsuccesful, no such city in the list”

import java.util.*;
public class ArrLinearSearch
{
String [] cityNames = new String[10];
int [] citySTD = new int[10];
String searchCity;
public void input()
{
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 10; i++)
{
System.out.print("Enter City Name: ");
cityNames[i] = sc.next();
System.out.print("Enter City STD Code: ");
citySTD[i] = sc.nextInt();

}
System.out.print("Enter name of the city to be
searched: ");
searchCity = sc.next();
}//end of input method
public void linearSearch()
{
boolean isFound = false;
int i = 0;
for(i=0; i < 10; i++)
{
if(cityNames[i].equalsIgnoreCase(searchCity))
{
isFound = true;
break;
}
}
if(isFound)
{
System.out.println("Search Successful");
System.out.println("City: " + cityNames[i]);
System.out.println("City STD Code: " +
citySTD[i]);
}
else
{
System.out.println("Search Unsuccessful, no such
city in the list");
}
} //end of search method

public static void main(String [] args)


{
ArrLinearSearch obj = new ArrLinearSearch();
obj.input();
obj.linearSearch();
} //main method ends
} //program ends
OUTPUT
Enter City Name:
Mumbai
Enter City STD Code:
022
Enter City Name:
Pune
Enter City STD Code:
020
Enter City Name:
Delhi
Enter City STD Code:
011
Enter City Name:
Bangalore
Enter City STD Code:
080
Enter City Name:
Chennai
Enter City STD Code:
044
Enter City Name:
Hyderabad
Enter City STD Code:
040
Enter City Name:
Kolkata
Enter City STD Code:
033
Enter City Name:
Chandigarh
Enter City STD Code:
0172
Enter City Name:
Coimbatore
Enter City STD Code:
0422
Enter City Name:
Darjeeling
Enter City STD Code:
0354
Enter name of the city to be searched:
Kolkata
Search Successful
City: Kolkata
City STD Code: 033

Variable Name Data type Description

searchCity String stores input of city name


to be searched

isFound boolean stores a boolean value

i int stores integer value

cityNames String [] stores a String array

citySTD int [] stores an integer array


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 12

1. Write a program in Java to search an element in a descending order


array using binary search technique:

import java.util.*;
public class ArrDescBinarySearch
{
int [] arr;
int ns;

public void input(){


Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array");
int n = sc.nextInt();
arr = new int[n];

System.out.println("Enter the integer elements of


the array in a descending order:");
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}

System.out.println("Enter the number to be searched


in the array:");
ns = sc.nextInt();

}//end of input method


public void binarySearch(){

boolean isFound = false;


int pos = 0, beg = 0, end = arr.length-1, mid = 0;
while(beg <= end){
mid = (beg + end)/2;
if(ns == arr[mid]){
isFound = true;
pos = mid+1;
break;
}
else if(ns < arr[mid])//for descending array
beg = mid + 1;
else
end = mid-1;

}
if(isFound){
System.out.println("Search Number " + ns
+ " found at position "
+ pos + " in the array");
}
else{
System.out.println("Search Number " + ns
+ " not found in the
array");
}
} //end of binary search method
public static void main(String [] args){
ArrDescBinarySearch obj = new ArrDescBinarySearch();
obj.input();
obj.binarySearch();
}//main method ends
}//program ends

OUTPUT

Enter the size of the array:


10
Enter the integer elements of the array in a descending order:
57
43
36
33
28
25
23
19
14
6
Enter the number to be searched in the array:
23
Search number 23 is found at position 7 in the array
Variable Name Data type Description

n, ns, i, pos, beg, end, int stores integer values


mid

isFound boolean stores a boolean value

arr int [] stores an integer array


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 13

1. Write a program to input and store integer elements in a double


dimensional array of size 3 x 3 array and print the array. Also, find
the sum of the elements in the left diagonal as well as the right
diagonal and display the same.

import java.util.*;
public class ArrDiagonalSum
{
int [][] arr = new int[3][3];
public void input2DArray()
{
Scanner sc = new Scanner (System.in);
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.print("Enter element " + i + ","
+ j + ": ");
arr[i][j] = sc.nextInt();
}
}
} //input method ends
public void print2DArray()
{
//print elements of the array in a matrix form
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}//end of for loop
} //print method ends

public void sumDiagonals()


{
int ldiagsum = 0, rdiagsum = 0;
for(int row = 0; row < 3; row++)
{
for(int col = 0; col < 3; col++)
{
//compute sum of left diagonal elements
if(row == col)
ldiagsum += arr[row][col];
//compute sum of right diagonal elements
if(row + col == 3-1)
rdiagsum += arr[row][col];
}
}
System.out.println("Sum of the left diagonal
elements is " + ldiagsum );
System.out.println("Sum of the right diagonal
elements is " + rdiagsum );

} //diagonal sum method ends

public static void main(String [] args)


{
ArrDiagonalSum obj = new ArrDiagonalSum();
obj.input2DArray();
obj.print2DArray();
obj.sumDiagonals();

} //main method ends


} //program ends

OUTPUT

Enter element 0,0:


1
Enter element 0,1:
3
Enter element 0,2:
5
Enter element 1,0:
4
Enter element 1,1:
6
Enter element 1,2:
8
Enter element 2,0:
9
Enter element 2,1:
2
Enter element 2,2:
4
1 3 5
4 6 8
9 2 4
Sum of the left diagonal elements is: 11
Sum of the right diagonal elements is: 20
Variable Name Data type Description

i, j, row, col, ldiagsum, int stores integer values


rdiagsum

arr int [] stores an integer array


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 14

1. Write a program to input and store twenty names in an array.


Arrange these names in descending order of alphabets, using the
bubble sort technique.

import java.util.*;
public class StringArraySort
{
String [] name = new String[20];
public void input()
{
Scanner sc = new Scanner(System.in);
for(int i = 0; i < name.length; i++)
{
System.out.print("Enter a name: ");
name[i] = sc.next();
}
} //end of input method

public void bubbleSort()


{
String temp;
for(int i = 0; i < name.length - 1; i++)
{
for(int j = 0; j < name.length-1-i; j++)
{
if(name[j].compareToIgnoreCase(name[j+1]) < 0)
{
//swap values
temp = name[j];
name[j] = name[j+1];
name[j+1] = temp;
} //swap ends
} //inner for loop ends
} //outer for loop ends
} //sort method ends

public void printArray()


{
for(int i = 0; i < name.length; i++)
{
System.out.print(name[i] + " ");
}
System.out.println();
} //print method ends

public static void main(String [] args)


{
StringArraySort obj = new StringArraySort();
obj.input();
System.out.println("The unsorted array is: ");
obj.printArray();
obj.bubbleSort();
System.out.println("The sorted array in
descending order is: ");
obj.printArray();
}
}
OUTPUT

Enter a name: Aero1


Enter a name: aero2
Enter a name: Brio3
Enter a name: brio4
Enter a name: Char5
Enter a name: char6
Enter a name: Dew7
Enter a name: dew8
Enter a name: Elf9
Enter a name: elf10
Enter a name: Fan11
Enter a name: fan12
Enter a name: Google13
Enter a name: google14
Enter a name: Help15
Enter a name: help16
Enter a name: Xion17
Enter a name: xion18
Enter a name: Zeus19
Enter a name: zeus20
The unsorted array is:
Aero1 aero2 Brio3 brio4 Char5 char 6 Dew7 dew8 Elf9 elf10 Fan11 fan12 Google13
google14 Help15 help16 Xion17 xion18 Zeus19 zeus20
The sorted array in descending order is:
zeus20 Zeus19 xion18 Xion17 help16 Help15 google14 Google13 fan12 Fan11 Elf9
elf10 dew8 Dew7 char6 Char5 brio4 Brio3 aero2 Aero1
Variable Name Data type Description

i, j int stores integer values

temp String stores a String value

name String [] stores a String array


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 15

1. Write a program showing the various methods of the String class.

public class StringUse


{
void StrDisp()
{
//length method
String str1 = "My name is Java";
System.out.println("Length of the String is "
+str1.length());
//trim method
String str2 = " My name is Java ";
System.out.println("Length of the string with leading
and trailing spaces is "+str2.length());
String str3 =str2.trim();
System.out.println("Length of the string after trimming
the spaces is " +str3.length());
//concat method
String str4 = "Hello";
String str5 = "Java";
System.out.println("Concatenating the Strings "
+str4.concat(str5));
//Uppercase Lowercase
String str6 = "java";
System.out.println("After converting to Uppercase "
+str6.toUpperCase());
String str7 = "JAVA";
System.out.println("After converting to lowercase
"+str7.toLowerCase());
//charAt method
String str8 = "My name is Java";
System.out.println("Using charAt method "
+str8.charAt(6));
//indexOf method
String str9 = "My name is Java";
System.out.println("Using indexOf(char)
"+str9.indexOf('a'));
System.out.println("Using indexOf(char,int)
"+str9.indexOf('a',6));
System.out.println("Using indexOf(String)
"+str9.indexOf("name"));
System.out.println("Using indexOf(String,int)
"+str9.indexOf("name", 6));
System.out.println("Using lastIndexOf(char) " +
str9.lastIndexOf('a'));
//equals method
String str10 = "My name is java";
String str11 = "My name is Java";
System.out.println("Using equals()
"+str10.equals(str11));
System.out.println("Using equalsIgnoreCase()
"+str10.equalsIgnoreCase(str11));
//compareTo method
System.out.println("Using compareTo()
"+str10.compareTo(str11));
System.out.println("Using compareToIgnoreCase()
"+str10.compareToIgnoreCase(str11));
//substring method
String str12= "I am learning to use substring";
System.out.println("Using substring(int) "
+str12.substring(6));
System.out.println("Using substring(int,int) "
+str12.substring(6,15));
//replace method
String str13 ="Beginning to use Replace";
System.out.println("Using replace "
+str13.replace('e','a'));
//startswith Method
String str14="Java is a programming language";
System.out.println("Output of startsWith method: "
+str14.startsWith("Java"));
//endswith Method
String str15="Java is a programming language";
System.out.println("Output of endsWith method: "
+str14.endsWith("Java"));
//valueOf method
String str16= String.valueOf(14);
System.out.println("Output of valueOf method: "
+str16);
}
public static void main(String [] args)
{
StringUse obj =new StringUse();
obj.StrDisp();
}
}
OUTPUT

Length of the String is 15


Length of the string with leading and trailing spaces is 17
Length of the string after trimming the spaces is 15
Concatenating the Strings HelloJava
After converting to Uppercase JAVA
After converting to lowercase java
Using charAt method e
Using indexOf(char) 4
Using indexOf(char,int) 12
Using indexOf(String) 3
Using indexOf(String,int) -1
Using lastIndexOf(char) 14
Using equals() false
Using equalsIgnoreCase() true
Using compareTo() 32
Using compareToIgnoreCase() 0
Using substring(int) earning to use substring
Using substring(int,int) earning t
Using replace Baginning to usa Raplaca
Output of startsWith method: true
Output of endsWith method: false
Output of valueOf method: 14

Variable Name Data type Description

str1, String stores a String value


str2,str3,str4,str5,str6,str7,str8,
str9,str10,str11,str12,str13,
str14,str15,str16
Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 16

1. Write a program to accept a word from the user and check if it is a


unique word.
A word is said to be unique if there are no characters repeated in it.

import java.util.*;
public class StringUnique
{
public void checkUnique()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the word: ");
String str = sc.next();
str = str.toUpperCase();
char ch ;
int count = 0;
for(int i = 0; i < str.length(); i++)
{
for(int j = i+1; j < str.length(); j++)
{
if(str.charAt(i) == str.charAt(j))
count++;
}
}
if(count >= 1)
System.out.println("The word " + str + " is not
unique");
else
System.out.println("The word " + str + " is
unique");
}
public static void main(String [] args)
{
StringUnique obj = new StringUnique();
obj.checkUnique();
}
}

OUTPUT

Enter the word:


Ishan
The word Ishan is unique

Variable Name Data type Description

str String stores a String value

ch char stores a character value

count int stores an integer value


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 17

1. Write a program to accept a sentence from the user and convert the
first character of every word to uppercase.

Sample Input: I like to read


Sample Output: I Like To Read

import java.util.*;
public class StringCaseChange
{
public void stringConvert(String s)
{
s = " " + s;
String cs = "";
char ch;
for(int i = 0; i < s.length(); i++)
{
ch = s.charAt(i);
if(Character.isWhitespace(ch))
{
ch = Character.toUpperCase(s.charAt(i+1));
if(i!= 0)
cs = cs + " " + ch;
else
cs = cs + ch;
i++;
}
else
cs += ch;
}
System.out.println("The original string is: " +
s.trim());

System.out.println("The converted string is: " +


cs.trim());

public static void main(String [] args)


{
StringCaseChange obj = new StringCaseChange();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
obj.stringConvert(sc.nextLine());
}
}
OUTPUT

Enter a sentence:
We are in a cyber world
The original string is: We are in a cyber world
The converted string is: We Are In A Cyber World

Variable Name Data type Description

s, cs String stores String values

ch char stores a character value

i int stores an integer value


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 18

1. Write a program to accept a sentence from the user and convert the
first character of every word to uppercase and also punctuate every
word with a dot.

Sample Input: I like to read


Sample Output: I.Like.To.Read

import java.util.*;
public class StringCaseChange2
{
public void stringConvert(String s)
{
s = " " + s;
String cs = "";
char ch;

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


{
ch = s.charAt(i);
if(Character.isWhitespace(ch) && i==0)
{
ch = Character.toUpperCase(s.charAt(i+1));
cs = cs + ch;
i++;

}
else if(Character.isWhitespace(ch))
{
ch = Character.toUpperCase(s.charAt(i+1));
cs = cs + "." + ch;
i++;
}
else
cs += ch;
}
System.out.println("The original string is: " +
s.trim());

System.out.println("The converted string is: " +


cs.trim());

public static void main(String [] args)


{
StringCaseChange2 obj = new StringCaseChange2();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
obj.stringConvert(sc.nextLine());
}
}
OUTPUT

Enter a sentence:
Welcome to java
The original string is: Welcome to java
The converted string is: Welcome.To.Java

Variable Name Data type Description

s, cs String stores String values


ch char stores a character value

i int stores an integer value


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 19

1. Write a program to accept a sentence from the user and display only
the initials (i.e. first letter of each word) and punctuate every initial
with a dot.

Sample Input: I like to read


Sample Output: I.l.t.r.

import java.util.*;
public class StringInitials
{
public void stringConvert(String s)
{
s = " " + s;
String cs = "";
char ch;

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


{
ch = s.charAt(i);
if(Character.isWhitespace(ch))
{
ch = s.charAt(i+1);
cs = cs + ch + ‘.’;
i++;
}
}
System.out.println("The original string is: " +
s.trim());
System.out.println("The converted string is: " +
cs.trim());

}
public static void main(String [] args)
{
StringInitials obj = new StringInitials();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
obj.stringConvert(sc.nextLine());
}
}
OUTPUT

Enter a sentence:
India is my country
The original string is: India is my country
The converted string is: I.i.m.c.

Variable Name Data type Description

s, cs String stores String values

ch char stores a character value

i int stores an integer value


Blue Ridge Public School

Class X – Computer Applications

A.Y. 2024-25

ASSIGNMENT – 20

1. Write a program to accept a sentence from the user, convert it to


uppercase and print the frequency of each character.

Sample Input: computer hardware


Sample Output: Char Frequency
A 2
C 1
D 1
E 2
H 1
M 1
O 1
P 1
R 3
T 1
U 1
W 1

import java.util.*;
public class Frequency
{
public void findFrequency()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = sc.nextLine();
str = str.toUpperCase();
int count = 0;
char ch1;
System.out.println("Char" + "\t" + "Frequency");
for(char ch = 'A'; ch <= 'Z'; ch++)
{
count = 0;
for(int j = 0; j < str.length() ; j++)
{
ch1 = str.charAt(j);
if(ch == ch1)
count++;
}
if(count != 0)
{
System.out.println(ch + "\t" + count);
}
}
}
public static void main(String [] args)
{
Frequency obj = new Frequency();
obj.findFrequency();
}
}
OUTPUT

Enter a string:
computer applications
Char Frequency
A 2
C 2
E 1
I 2
L 1
M 1
N 1
O 2
P 3
R 1
S 1
T 2
U 1
Variable Name Data type Description

str String stores String values

ch, ch1 char stores character values

j, count int stores integer values

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