Comp
Comp
Library Classes
Wrapper Class (V.V. Imp)
Boxing (Auto Boxing): Conversion of primitive data type into an object of its
wrapper class.
Example: Integer a = new Integer(10);
char Character
boolean Boolean
int Integer
byte Byte
short Short
long Long
float Float
double Double
Page 2
Conversions
String to Integer:
Integer.parseInt()
Integer.valueOf()
String to Long:
Long.parseLong()
Long.valueOf()
String to Float:
Float.parseFloat()
Float.valueOf()
String to Double:
Double.parseDouble()
Double.valueOf()
Integer to String:
Integer.toString()
Long to String:
Long.toString()
Float to String:
Float.toString()
Double to String:
Double.toString()
Page 3
Examples
int x;
String s = "2005";
x = Integer.parseInt(s);
int a = Double.valueOf(s);
System.out.println(x); // It will print 2005
System.out.println(a); // It will print 2005.0
Character Functions
Character.isLetter('p'); → Correct
P.isLetter(); → Incorrect
Boolean Results
Results are either true or false.
More Output-Based Questions Expected
Page 4
String Manipulation in Java
Page 5
compareTo(): Compares two strings lexicographically.
Example:
String x = "ABC";
String y = "abc";
System.out.println(x.compareTo(y)); // returns -32
If the result is:
0 → Both strings are equal.
< 0 → First string is less than the second.
> 0 → First string is greater than the second.
Page 6
Sample Questions
Page 7
String Programs
Count the total number of characters, digits, and whitespace in a string.
Count the number of vowels and consonants in a string.
Reverse a string.
Check if a string is a palindrome.
Print the initials of a name.
Replace a letter with another in a string.
Change the case of each alphabet in a string.
Count the number of uppercase, lowercase, and digits in a string.
Print the string in alphabetical order.
Sorting and searching in strings.
Page 8
Arrays
1D Arrays:
Direct assignment:
int[] arr = {2, 3, 4, 6, 8};
char[] chArr = {'e', 'x', 'a', 'm'};
String[] strArr = {"red", "blue", "green"};
2D Arrays:
Direct assignment:
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Array Programs
Binary Search.
Linear Search.
Bubble Sort.
Selection Sort.
Array Example
int[] a = {6, 0, 0, 2};
int b = a[1] + a[2];
a[1] = b + a[3];
int d = a[1] + a[0];
System.out.println(d); // Prints 17
User-Defined Methods
Prototype Questions: Write the prototype or correct the prototype.
Example:
boolean number(int a, int b, int c);
Types of Methods
User-Defined Methods.
Pre-Defined Methods: length(), nextInt(), nextFloat(), Math.sqrt().
Call by Value:
Changes made inside the function do not reflect in the original value.
Call by Reference:
Changes made inside the function affect the original value.
Method Overloading:
Same function name but different arguments or return types.
Polymorphism:
Implemented through method overloading.
Access Modifiers
Private: Accessible only within the class.
Public: Accessible from anywhere.
Protected: Accessible within the class and its subclasses.
Program Practice
Practice programs with functions, method overloading, and constructors.
Constructors
A constructor is a member method with the same name as the class, used to
initialize instance variables.
Types:
Default Constructor.
Parameterized Constructor.
Copy Constructor.
Constructor Programs
Default Constructor:
Example:
java
Copy
class Example {
Example() {
// Code
}
}
Parameterized Constructor:
Example:
java
Copy
class Example {
Example(int a, int b) {
// Code
}
}
Scope of Variables
Program Structure
Code:
Write the program logic.
Comments:
Add at least 3-4 comments to explain the code.
Class Naming:
Use proper class names and follow naming conventions.
GR 9
1. Class is a blue print of an object. It is known as object factory
2. Object is a unique entity with characteristics and behaviour. It is known as
instance of the class.
3. Features of Object oriented Programming. Emphasis is on data than functions.
Follows bottom up approach
8. Java compiler converts source code to an intermediate code (binary form) called
byte code.
9. Java interpreter (JVM – Java Virtual Machine) converts bytecode into machine
code.
10. JVM is referred virtual machine because it converts byte code from different
source codes(non-java environment) into machine code.
11. Java statement to create an object mp4 of class digital.
digital mp4= new digital();
12. ASCII
• A-Z: (65-90)
• a-z:(97-122)
• blank space:32
• Numbers(0-9): 48-57
20. Explicit data type is also known as type casting. The data type gets converted
based on users choice.
Byte -1 byte
Short – 2 bytes
Int -4 bytes
Long -8 bytes
Float – 4 bytes
Double – 8 bytes
35. Syntax Error: Error when rules of programming are not followed. Example missing
semicolon, typing keywords incorrectly.
36. Logical error: Error in programmers logic so it does not give correct result.
37. Runtime error : Error due to incorrect mathematical operations. Example divide
by zero finding square root of negative numbers.