Grade 10 C.sc. Study Material 2023-24
Grade 10 C.sc. Study Material 2023-24
Grade 10 C.sc. Study Material 2023-24
Koramangala
Grade 10
(2023 - 24)
Computer Science
Java Programming
Contents
4 Introduction to Arrays 18
• Control Variable : A variable, which starts with an initial value and determines the duration of
the repetition is known as Control Variable or initial value.
• Body of the Loop: A set of statements, which are executed within the loop simultaneously.
• Test Condition : Each loop contains a test condition. Whether the loop has to be repeated or
terminated, depends upon the test condition. The control enters the body of the loop for
execution till the test condition is true, otherwise it terminated.
• Step Value: The step value in a looping structure determines the increment or decrement of
the control variable unless the test condition is false.
Based upon the nature of iterations the loops are of two types as shown below:
LOOP
For Loop : A for-loop is a control flow statement for specifying iteration, which allows code to be
executed repeatedly.
Syntax:
statement(s)
• Initialization condition: Here, we initialize the variable in use. It marks the start of a for loop.
An already declared variable can be used or a variable can be declared, local to loop only.
• Testing Condition: It is used for testing the exit condition for a loop. It must return a boolean
value. It is also an Entry Control Loop as the condition is checked prior to the execution of
the loop statements.
Page | 1
• Statement execution: Once the condition is evaluated to true, the statements in the loop
body are executed.
• Increment/Decrement: It is used for updating the variable for next iteration.
• Loop termination: When the condition becomes false, the loop terminates marking the end
of its life cycle.
{
int x=2;
Output:
Value of x:2
Value of x:3
Value of x:4
Page | 2
Chapter 1
While – Do While -- Nested For Loop
When we apply a For loop within another For loop, the structure is termed as
nested for loop.
Syntax:
Output:
1
12
123
1234
12345
Page | 3
While Loop: This loop can be applied to a program where number of iterations are not fixed. The
syntax of while statement is given below:
loop statements...
• While loop starts with the checking of condition. If it evaluated to true, then the loop body
statements are executed otherwise first statement following the loop is executed. For this
reason it is also called Entry control loop
• Once the condition is evaluated to true, the statements in the loop body are executed.
Normally the statements contain an update value for the variable being processed for the
next iteration.
• When the condition becomes false, the loop terminates which marks the end of its life cycle.
int x = 1;
while (x <= 4)
Output:
Value of x:1
Value of x:2
Value of x:3
Value of x:4
Page | 4
Do While Loop: This loop is used in a program where number of iterations is not fixed. In this
system, the control enters the loop without checking any condition, executes the given steps and
then checks the condition for further continuation of the loop. Thus, this type of the loop executes
the tasks at atleast once. If the condition is not satisfied, then the control exits the loop.
do
{
statements..
}
while (condition);
• do while loop starts with the execution of the statement(s). There is no checking of any
condition for the first time.
• After the execution of the statements, and update of the variable value, the condition is
checked for true or false value. If it is evaluated to true, next iteration of loop starts.
• When the condition becomes false, the loop terminates which marks the end of its life cycle.
• It is important to note that the do-while loop will execute its statements atleast once before
any condition is checked, and therefore is an example of exit control loop.
}
}
Output:
Value of x: 21
{
if (another condition is true)
break;
Statement(s);
}
Page | 5
// Java program to illustrate break.
import java.util.Scanner;
public class forLoopDemo
while(n!=0)
if (n==0)
break;
if (n%2==0)
}
}
}
Exercises:
a) 1 b) 1 c) 1 2 3 4 5
1 0 2 3 1 2 3 4
1 0 1 4 5 6 1 2 3
1 0 1 0 7 8 9 10 12
1 0 1 0 1 11 12 13 14 15 1
Page | 6
Chapter 2
String Manipulation in Java
Introduction
In Java language, a single character and a set of characters (i.e., String) are considered
differently. Each character is assigned an ASCII code, which is taken into consideration during
Java programming. In Java, a variable is declared as given below.
Strings, which are widely used in Java programming, are a sequence of characters. In Java,
strings are objects rather than a primitive data type. The Java platform provides the String
class to create and manipulate strings. String class is encapsulated under java.lang package.
Although string literals are used in a manner similar to other literals in a program, they are
handled differently by the Java application. With a string literal, java stores the value as a
string object.
Java language provides relevant functions (i.e., character function and string function) to
perform the desired tasks. Some of the important functions are explained below.
Page | 7
Character Functions
These functions deal with only the character manipulations. Some of them are:
a) isLetter( )
This function is used to check whether a given argument is an alphabet or not. It returns
a boolean type value either true or false.
b) isDigit( )
This function returns a Boolean type value ‘true’ if a given argument is a digit, otherwise
‘false’.
c) isWhitespace( )
This function can be used to check for existing blankspace or gap in a String. It will return
a Boolean type value (i.e., true) if the given argument is a white space (blank) and false,
otherwise.
Page | 8
d) isUpperCase( )
This function will return ‘true’ if the given argument is an upper case letter, otherwise
‘false’.
e) isLowerCase( )
This function will return ‘true’ if the given argument is a lower case letter, otherwise ‘false’.
f) toUpperCase( )
This function returns the given argument in upper case character. The given character
remains same if it is already in upper case. It returns the same character if the character
used with the function is non-alphabet.
g) toLowerCase( )
This function returns the given argument in lower case character. The given character
remains same if it is already in lower case. It returns the same character if the character
used with the function is non-alphabet.
Page | 9
Syntax : char variable = Character.toLowerCase(character)
String Functions
a) length( )
This function is used to return the length of a string i.e., number of characters present
in the string. The length is an integer number and hence it returns an integer value.
Syntax: int variable = string variable.length();
Eg: String str = “PROGRAMMING”
int k = str.length();
Here, k = 15, as number of characters including the spaces in the string is 15.
b) charAt ( )
This function returns a character from the given index i.e., the position number of the
string. The index number starts from zero.
Here c = ‘P’ as the character at 3rd index is ‘P’. The index should not exceed the
length of the string.
Page | 10
c) indexOf ( )
This function returns the index i.e., the position number of the string.
The function will return the index of ‘A’ available in the string from the 4th index.
Hence,
n=5.
Eg: String s = “COMPUTER”;
int n = s.indexOf(‘C’, 3);
Checks for 'C' from the 3rd position onwards. It returns -1 as character ‘C’ is not available
after 3rd index.
d) toLowerCase( )
This function returns the characters of the string in lower case letters. If any character
is already in lower case or the character is a special character then it remains same.
Page | 11
f) concat( )
Here, both the strings x and y will be joined together. Hence, z results in
“COMPUTERAPPLICATIONS”.
Java language also uses an operator ‘+’ to concatenate two or more strings together.
g) substring( )
This function is used to extract a part of string characters from one index to another.
It returns a part of string from 3rd positon to the 6th position by excluding the
character, which is available at 6th position. Hence, p results in PUT.
Page | 12
h) replace( )
This function is used to replace a character by another character or to replace a String
by another String at all its occurrence in the given String.
Syntax :
Here, each character ‘X’ of the string is replaced by ‘A’. Hence, the new string p
results in “MALAYALAM”.
Here, each sub string “green” of the given string is replaced by “red”.
Hence, the new string p results in “The red bottle is in the red bag”.
i) equals( )
This function is used to compare two strings together to check whether they are
identical or
not. It returns a Boolean type value ‘true’ if both are same, ‘false’ otherwise.
Page | 13
j) equalsIgnoreCase( )
This function is also used to compare two strings to ensure whether both are identical
or not after ignoring the case i.e., corresponding upper and lower case characters are
treated the same. It also returns boolearn data as true or false.
If(x.equalsIgnoreCase(y))
System.out.println(“same”);
else
System.out.println(“different”);
Exercises:
a) c)
B B L U E J
L L U E J B
U E J B L
U
E J B L U
E J B L U E
J
b)
B
B L
B L U
B L U E
B L U E J
Page | 14
2. accept a String in lower case and replace ‘e’ with ‘*’ in the given String. Display the new
string.
Sample Input: computer science
Sample Output : comput*r sci*nc*
4. accept a string in lower case. Convert all the first characters of the string in upper case
and display the new string.
Sample Input : programming is fun
Sample Output : Programming Is Fun
Page | 15
Chapter 3
Java Packages for Mathematical Functions
1. Math.pow( )
This function is used to find the power raised to a specified base. It always returns a
double type value.
2. Math.sqrt( )
This function is used to find the square root of a positive number. It returns a double
type value.
3. Math.abs( )
This function is used to return absolute value i.e., only magnitude of the number. It
returns int/long/double value depending upon the arguments supplied.
Page | 16
4. Math.round( )
This function returns the value in rounded form. It always returns in double data type. If
the fractional part of the number is below 0.5, it returns the same integer value
otherwise, it returns the next integer value in double data type.
Exercise:
Page | 17
Chapter 4
Introduction to Arrays
An array is a data structure to store a collection of elements of the same type. It is a container
object and holds a fixed number of values of a single type. The length of an array is established
when the array is created. After creation, its length is fixed. Arrays are sequential in nature.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you
can declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables.
Element at index 7
First
Index 0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
Array length is 10
Fig 1 - An array of 10 elements.
The size of the array is 10. Each item in an array is called as an element, and each element is
accessed by its numerical index. As shown in the above illustration, numbering begins at 0.
The 9th element, for example, would therefore be accessed at index 8. All the elements are
stored in a sequential manner.
Array declarations
Arrays of various data types may be declared as shown below.
1. int A[] = new int[10]
Creates an array A of type int of size10. Array A can store 10 elements of type integer.
2. String C[] = new String[5]
Creates an array of strings of size 5. Now array C may store 5 names like “Suman”,
“Harish”,”Rahul”, “Pooja”, “Neha”
In the same manner, arrays of other data types may be created.
Incidentally, arrays may also be initialized during declaration. This is as shown below.
1. int A[] ={3,5,7,9,11} Creates an array of size 5.
2. float wts[] ={3.5, 5.6, 7.8,12.9} Creates an array of size 4.
3. String names[]= {“Neha”,”Kiran”,”Pooja”} Creates an array of size 3.
Page | 18
Input/output of array elements
Typically a for loop is used while entering values into or reading values from an array. Since
the size of the array is fixed and known before execution of the program, for loop is quite
apt for this purpose. This is shown in the following program.
import java.util.Scanner;
public class testingArrays
{
public static void main(()
{
Scanner sc= new Scanner(System.in);
int A[]=new int[5];
for(int i=0; i<5;i++) //Storing values
{
System.out.println(“enter a value”);
A[i] = sc.nextInt();
}
//Displaying all the values
for(int j=0; j<5;j++)
System.out.println(A[j]);
}
}
Solved programs:
1. Program to input age of 10 students and display the same in the reverse order.
import java.util.Scanner;
public class arr1
{
public static void main()
{
Scanner sc= new Scanner(System.in);
int Age[]=new int[10];
for(int i=0; i<10;i++) //Storing values
{
System.out.println(“enter age”);
Age[i] = sc.nextInt();
}
//Displaying in reverse order
for(int i=9; i>=0; i--)
System.out.println(Age[i]);
}
}
Page | 19
2. Program to find the average height of a class of 6 students.
import java.util.Scanner;
public class arr2
{
public static void main()
{
Scanner sc= new Scanner(System.in);
float ht[]=new float[6];
float sum =0.0,avg;
for(int i=0; i<6; i++) //Storing values
{
System.out.println(“enter height”);
ht[i] = sc.nextFloat();
sum= sum+ht[i];
}
avg = sum/6;
System.out.println(“The average height of the class is “+ avg);
}
}
Page | 20
4. A company has announced 10% of the salary as the bonus amount. Input basic
salaries of 5 employees and display their net salary after adding the bonus amount
to their basic salaries.
5. Using an array display the first 10 terms of the Fibonacci series
(0,1,1,2,3,5,8,13,21,34).
6. Input an array of 6 integers and display only the prime numbers.
***********************
Page | 21
Chapter 5
Linear Search & Finding Maximum and Minimum
Searching is one of the basic operations that can be performed on arrays. It is a process to
determine whether a given item i.e., a number, a character or a word is present in the array
or not. There are different search algorithms, such as Linear Search, Binary Search etc.
Linear Search
It is one of the simplest techniques in which the searching of an item begins at the start of the
array, i.e., at the 0th index position of the array. The process continues, where each element
is checked and compared with the given data item until either the item is found or end of the
array is reached. This process is also called as Sequential Search.
Linear Search works well for small and unsorted arrays.
Algorithm:
Step 3: If key element is found, return the index position of the array element
Page | 22
Exercise:
Write programs for the following.
1. Declare an integer array of 10 elements. Input a number and search if it is present or
not using linear search. Display appropriate messages upon successful or
unsuccessful search.
2. Declare an array of 5 names. Now, input a name and check if it is present in the
array or not using linear search.
3. Declare an array to store 10 alphabets input by the user. Display the count of vowels.
Exercise:
Page | 23
Chapter 6
Selection sort:
The basic idea of a selection sort is to repeatedly select the smallest element (or largest
element if descending) in the remaining unsorted array and bringing it to its appropriate
position.
It is illustrated in the following example. The array is sorted in the ascending order.
Step 1: Consider the following array. At first the smallest element is selected through
iteration from the unsorted array and interchanged with the number at index 0.
0 1 2 3 4 5 6 7
44 97 49 56 89 27 15 77
.
Step 2: The next smallest element from 1st index onward is found and exchanged with the
element at 1st index position.
0 1 2 3 4 5 6 7
15 97 49 56 89 27 44 77
.
Step 3: The next smallest element from 2nd index onward is found and exchanged with the
element at 2nd index position.
0 1 2 3 4 5 6 7
15 27 49 56 89 97 44 77
Page | 24
Step 4: The next smallest element from 3rd index onward is found and exchanged with the
element at 3rd index position.
0 1 2 3 4 5 6 7
15 27 44 56 89 97 49 77
.
Step 5: The next smallest element from 4th index onward is found and exchanged with the
element at 4th index position.
0 1 2 3 4 5 6 7
15 27 44 49 89 97 56 77
.
Step 6: The next smallest element from 5th index onward is found and exchanged with the
element at 5th index position.
0 1 2 3 4 5 6 7
15 27 44 49 56 97 89 77
.
Step 7: The next smallest element from 6th index onward is found and exchanged with the
element at 6th index position. No exchange happens in this case.
0 1 2 3 4 5 6 7
15 27 44 49 56 77 89 97
.
Page | 25
Exercise:
Page | 26