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

Control Structures

The document provides an overview of key Java programming concepts like control structures, arrays, methods, inheritance, file handling, and string manipulation. It includes definitions and examples for if-else statements, switch-case statements, loops, declaring and accessing arrays, defining and invoking methods, constructors, inheritance types, reading and writing files, string concatenation and methods. The document concludes with a sample practice problem to calculate the sum of elements in an array.

Uploaded by

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

Control Structures

The document provides an overview of key Java programming concepts like control structures, arrays, methods, inheritance, file handling, and string manipulation. It includes definitions and examples for if-else statements, switch-case statements, loops, declaring and accessing arrays, defining and invoking methods, constructors, inheritance types, reading and writing files, string concatenation and methods. The document concludes with a sample practice problem to calculate the sum of elements in an array.

Uploaded by

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

• NOTE SOME TOPICS ARE NOT THERE IN SYLLABUS AND BUFFER READER IS NOT THERE

PLEASE KEEP A COPY OF SYLLABUS AND READD


• Control Structures:
o Definition: Control structures dictate the flow of execution in a program, allowing you to
make decisions and repeat actions.
▪ if-else Statements:
• Definition: if-else statements execute a block of code if a specified condition is true, and
another block of code if the condition is false.
• Example:
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is not greater than 5");
}

▪ switch-case Statements:
• Definition: switch-case statements evaluate a variable or expression and execute different
blocks of code based on its value.
• Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
// More cases...
default:
System.out.println("Invalid day");
}

▪ Loops (while, do-while, for):


• Definition: Loops are used to repeat a block of code multiple times until a specified
condition is met.
• Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
int j = 0;
do {
System.out.println(j);
j++;
} while (j < 5);

for (int k = 0; k < 5; k++) {


System.out.println(k);
}

• Arrays:
o Definition: Arrays are data structures that store multiple values of the same type in a
single variable.
o Declaration and Initialization:
▪ Definition: Arrays are declared using a specific data type followed by square brackets, and
can be initialized with values at the time of declaration or later.
▪ Example:
int[] numbers = new int[5]; // Declaration with size
int[] primes = {2, 3, 5, 7, 11}; // Declaration with initialization

o Accessing Elements:
▪ Definition: Elements in an array can be accessed using their index, which starts from 0.
▪ Example:
int[] numbers = {10, 20, 30, 40, 50};
int firstNumber = numbers[0]; // Accessing the first element
int thirdNumber = numbers[2]; // Accessing the third element

o Array Operations:
▪ Definition: Arrays support various operations such as traversal, searching, sorting, and
manipulation.
▪ Example:
j
int[] numbers = {5, 3, 8, 1, 9};
// Traversal
for (int num : numbers) {
System.out.println(num);
}
// Searching
int index = Arrays.binarySearch(numbers, 8); // Returns the index of 8
// Sorting
Arrays.sort(numbers); // Sorts the array in ascending order
// Manipulation
numbers[0] = 10; // Modifying the first element

• Methods:
o Definition: Methods are reusable blocks of code that perform specific tasks and enhance
code modularity and reusability.
o Method Definition and Invocation:
▪ Definition: Methods are defined using the method signature, which includes the method
name, return type, and parameters (if any). They can be invoked by calling their name
along with any required arguments.
▪ Example:
// Method definition
public static int add(int a, int b) {
return a + b;
}
// Method invocation
int result = add(5, 3); // Output: 8

o Return Types:
▪ Definition: Methods may or may not return a value. The return type specifies the type of
value that the method returns.
▪ Example:
j
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}

• Constructors and Objects:


o Definition: Constructors are special methods used for initializing objects. Objects are
instances of classes that encapsulate data and behavior.
o Class and Object Basics:
▪ Definition: Classes define the blueprint for creating objects, while objects are instances of
classes that represent real-world entities.
▪ Example:
class Car {
String brand;
int year;
Car(String b, int y) {
brand = b;
year = y;
}
}
Car car1 = new Car("Toyota", 2022);

o Constructors:
▪ Definition: Constructors have the same name as the class and are used to initialize objects.
▪ Example:
javaCopy code
class Person {
String name;
int age;
Person(String n, int a) {
name = n;
age = a;
}
}
Person person1 = new Person("John", 30);

• Inheritance:
o Definition: Inheritance is a mechanism in which a new class inherits properties and
behaviors from an existing class, promoting code reuse and establishing a parent-child
relationship between classes.
o Concept of Inheritance:
▪ *
Sure, let's continue with the detailed explanations and examples for the remaining topics:
• Inheritance:
o Definition: Inheritance is a mechanism in object-oriented programming where a new class
inherits properties and behaviors from an existing class. This promotes code reuse and
establishes a parent-child relationship between classes.
o Concept of Inheritance:
▪ Definition: Inheritance allows a class (subclass or child class) to inherit attributes and
methods from another class (superclass or parent class).
▪ Example:
// Parent class
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}

o Types of Inheritance:
▪ Definition: There are different types of inheritance in Java, including single inheritance,
multi-level inheritance, hierarchical inheritance, and multiple inheritance (not supported in
Java).
▪ Example:
// Single Inheritance
class A {
// Class members
}
class B extends A {
// Class members
}

// Multi-level Inheritance
class A {
// Class members
}
class B extends A {
// Class members
}
class C extends B {
// Class members
}

// Hierarchical Inheritance
class A {
// Class members
}
class B extends A {
// Class members
}
class C extends A {
// Class members
}

• File Handling:
o Definition: File handling involves reading from and writing to files on a computer's file
system. It includes various operations such as creating, opening, reading, writing, and
closing files.
o Reading and Writing Files:
▪ Definition: File input/output streams are used to perform file operations in Java.
BufferedReader and BufferedWriter classes are commonly used for efficient file reading
and writing.
▪ Example (Reading from a file):
try (BufferedReader br = new BufferedReader(new
FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}

o Exception Handling:
▪ Definition: Exception handling is important when dealing with file operations to handle
errors gracefully. It prevents the program from crashing and allows for proper error
recovery.
▪ Example:
try {
// File operations
} catch (IOException e) {
System.out.println("An error occurred while reading/writing the
file.");
e.printStackTrace();
}

• String Manipulation:
o Definition: String manipulation involves various operations such as concatenation,
substring extraction, searching, and comparison on strings in Java.
o Concatenation and Manipulation:
▪ Definition: Strings in Java are immutable, meaning their values cannot be changed after they
are created. However, you can create new strings by concatenating or manipulating existing
strings.
▪ Example:
String str1 = "Hello";
String str2 = "World";
String result = str1 + " " + str2; // Concatenation
System.out.println(result); // Output: Hello World

o String Methods:
▪ Definition: Java provides numerous built-in methods for string manipulation, including
length(), charAt(), substring(), indexOf(), toUpperCase(), toLowerCase(), etc.
▪ Example:
String str = "Hello, World!";
int length = str.length(); // Returns the length of the string
char firstChar = str.charAt(0); // Returns the character at index 0
String substring = str.substring(7); // Returns the substring from
index 7 to the end
int index = str.indexOf("World"); // Returns the index of the substring
"World"

• Practice Problems and Exercises:


o Definition: This section includes a set of practice problems and exercises aimed at
reinforcing the concepts covered in the syllabus. These problems provide students with an
opportunity to apply their knowledge and skills to solve real-world programming
challenges.
o Example:
// Problem: Write a Java program to find the sum of elements in an
array.
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Sum of elements: " + sum);
}
}

You might also like