Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Comp

The document provides a comprehensive overview of Java programming concepts, including wrapper classes, string manipulation, arrays, user-defined methods, and object-oriented programming principles. It covers data type conversions, method overloading, encapsulation, and various control structures such as loops and conditionals. Additionally, it includes practical examples, sample questions, and programming practices to reinforce understanding of Java syntax and functionality.

Uploaded by

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

Comp

The document provides a comprehensive overview of Java programming concepts, including wrapper classes, string manipulation, arrays, user-defined methods, and object-oriented programming principles. It covers data type conversions, method overloading, encapsulation, and various control structures such as loops and conditionals. Additionally, it includes practical examples, sample questions, and programming practices to reinforce understanding of Java syntax and functionality.

Uploaded by

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

Page 1

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

Unboxing: Conversion of an object of the wrapper class into a primitive object.


Example:

Integer a = new Integer(10);


int b = a;

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

Primitive to String Conversions

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

length(): Calculates the length of the string.


Example:
String s = "Java";
s.length(); // returns 4

substring(): Extracts a portion of the string.


Example:
String s = "I love programming";
s.substring(4); // returns "ve programming"

substring() with start and end index:


Example:
String s = "Computers";
s.substring(2, 6); // returns "mput"

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

Extract the last letter of a string:


char ch = s.charAt(s.length() - 1);

Extract the second last letter of a string:


char ch = s.charAt(s.length() - 2);

Concatenate two strings a and b:


String c = a + b;

Join the first three characters of strings a and b:


String p = a.substring(0, 3);
String q = b.substring(0, 3);
String c = p.concat(q);

Check if the first character of a string is uppercase:


if (Character.isUpperCase(a.charAt(0)))
{
System.out.println("true");
}

Count the number of characters in a string:


int d = p.length();

Convert the 6th character of a string to uppercase:


char ch = Character.toUpperCase(p.charAt(5));

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.

Sample Array Questions


Initialize an array with values 5 to 10:
int[] a = {5, 6, 7, 8, 9, 10};
Print the sum of the first and last elements of an array:
int sum = a[0] + a[a.length - 1];

Combine elements of two arrays a and b into a third array c:


int[] c = new int[a.length + b.length];

Access the second element of a 1D array:


System.out.println(m[1]);

Access the third element of a 2D array:


System.out.println(m[0][2]);

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

2D Array Programs (Must Do)


Sum of rows.
Sum of columns.
Printing diagonals.
Calculating the sum of diagonals.
Transpose of a matrix.

User-Defined Methods
Prototype Questions: Write the prototype or correct the prototype.
Example:
boolean number(int a, int b, int c);

Correct the Prototype:


accept(int, int) → void accept(int a, int b)
boolean(int a) → boolean calculate(int a)
int palindrome(a, b) → int palindrome(int a, int b)

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.

Call by Value vs. Call by Reference


Call by Value:
A copy of the actual parameter is passed.
Changes made inside the function do not reflect in the original value.
Call by Reference:
The memory address of the actual parameter is passed.
Changes made inside the function affect the original value.

Formal vs. Actual Parameters


Formal Parameters:
Variables defined in the function declaration.
Example:
void sum(int x, int y)
{
int s = x + y;
System.out.println(s);
}
Actual Parameters:
Values or variables passed to the function during the call.
Example:
sum(10, 20);

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

Encapsulation and Access Modifiers


Private: Accessible only within the class.
Public: Accessible from anywhere.
Protected: Accessible within the class and its subclasses.

Scope of Variables

Instance Variables: Declared within a class but outside any method.


Class Variables: Declared with the static keyword.
Local Variables: Declared within a method or block.

Program Structure

Code:
Write the program logic.

Comments:
Add at least 3-4 comments to explain the code.

Variable Description Table:


Describe each variable used in the program.

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

4. Data Abstraction: Act of representing the essential features without knowing


background details. E.g.: Car – you need to know driving background details like
working of engine doesn’t matter.
5. Inheritance: Link and share common properties of one class with another. It
promotes reusability. E.g.: child inheriting from parent.
6. Polymorphism: Process of using a function or method for more than one purpose.
It is implemented by function overloading. E.g. duck is a water bird and also a
term used in cricket match. Father taking up many roles in life.
7. Encapsulation: wrapping of data and functions into a single unit .

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

13. Literals are fixed values that cannot be changed.


Integer Literal 13,15,17
Real Literal- 45.6,67.8,3.4
Character Literal –‘a’ , ‘f’ , ‘0’
String Literal –“abs” , “system”
Boolean Literal – true, false

14. Separators : comma(,) , Brackets () , Curly Brackets {}, Square brackets []


15. Punctuators: Question mark(?) , dot(.), semicolon( ; )
16. Primitive data types are the basic data types used to declare a variable
Example : int,float,double.
17. Non primitive data types is derived from primitive data types. Example array,
class. It is also known as composite data type.
18. Keywords are reserved words understood by compiler that have special meaning.
19. Implicit conversion is also known as coercion. The data type gets converted to
highest data type automatically.
Byte -> Char -> Short -> Int -> Long -> Float -> Double

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

21. Arithmetic operators :+,-,*,/,%


22. Relational Operators: <,>,<=,>=,==,!=
23. Logical :AND OR NOT
24. The new operator is used to create the object of the class.
25. The dot operator is used to invoke the members of the class to carry out task.
E.g. import java.util.*;
26. Pure Expression- All components(variables) of same data type.
27. Impure expression- Mixture of different data types.
28. Shorthand expression a=a+b // a+=b

29. Static Initialization- Direct assignment of constant to variable. Eg a=5;


String b=”comp”
30. Dynamic Initialization: Initialized at runtime. Example C=a+b
31. Unary: works on one operand.+,-,++,-- E.g. if a=8 the +a is 8 and -a is -8
32. Binary: works on two operand E.g.: +,* a+b
33. Ternary: works with three operand. Also known as conditional assignment
operator. E.g. ?:
34. Input:
Scanner sc= new Scanner(System.in);
Integer – sc.nextInt()
Float – sc.nextFloat()
Double – sc.nextDouble()
String – sc.next() and nextLine()
Character – sc.next().charAt(0)

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.

38. All mathematical functions are part of java.lang package.


39. The default java package is java.lang.

40. Return type of mathematical functions


• Math.log- double
• Math.sqrt()- double
• Math.cbrt() – double
• Math.pow()- double
• Math.round() – integer
• Math.rint()-double
• Math.ceil()- double
• Math.floor()-double
• Math.random() double

41. Switch is a multiple branching statement.


42. A break is used to terminate each case of switch. Also known as case
terminator.
43. If no break is found after case control enters into another case. This is
called fall through.
44. Default case is executed when no case is available for given switch variable.
45. Difference
Switch If
This is a multiple branching statement It is bi directional flow of control.
Makes use of break statement to terminate No break statement
46. There are three types of loop – for, while and do while
47. For is a fixed loop
48. While and do while are unfixed loop.
49. For and while are entry controlled loop
50. Do while is exit controlled loop
51. There are three jump statements- break, continue and return.
52. The break statement is used in loop to terminate the loop.
53. The continue statement is used to skip rest of statements and go for next
iteration.
54. Difference: While Do while Will not execute if condition is false Will execute
at least once.
Known as entry controlled loop Known as exit controlled loop
55. Finite loop: Loop runs fixed number of times.
56. Infinite loop: the loop never comes to end.
57. Delay or Null loop: The loop does not have any statements.
58. Continuous loop: Control variable updated by 1. ( for i=0;i<5;i++) // every
time i is
increased by 1
59. Step loop: Control variable updated by given value. . ( for i=0;i<5;i=i+3) //
every time i is increased by 3

You might also like