JAVA I
JAVA I
PART I
Part 1
Introduction to JAVA Programming
Practical Assignment
PART 3
Variables, Operators Control and Structures in JAVA
a. Variables in JAVA:
Definition of Variables: Variables are essential for storing data during program
execution. In this section, we will understand what variables are and their role in JAVA
programming.
Declaring and Initializing Variables: We will explore how to declare variables in
JAVA and initialize them with appropriate values.
Data Types for Variables: JAVA supports various data types for variables. We will
discuss primitive data types like int, double, char, Boolean, and non-primitive data
types like String, and their memory allocation in detail.
Examples:
b. Operators in JAVA:
Arithmetic Operators: Arithmetic operators perform basic mathematical operations in
JAVA.
Addition (+): Performs addition of two operands.
Subtraction (-): Performs subtraction of two operands.
Multiplication (*): Performs multiplication of two operands.
Division (/): Performs division of two operands.
Modulus (%): Calculates the remainder of a division operation.
Relational Operators: Relational operators are used for comparing values in JAVA.
Equal to (==): Checks if two operands are equal.
Not equal to (!=): Checks if two operands are not equal.
Greater than (>): Checks if the left operand is greater than the right operand.
Less than (<): Checks if the left operand is less than the right operand.
Greater than or equal to (>=): Checks if the left operand is greater than or equal
to the right operand.
Less than or equal to (<=): Checks if the left operand is less than or equal to the
right operand.
Logical Operators: Logical operators are used for evaluating conditions in JAVA.
Logical AND (&&): Returns true if both operands are true.
Logical OR (||): Returns true if at least one of the operands is true.
Logical NOT (!): Reverses the result of a condition.
Examples:
c. Decision-Making Constructs:
if, else if, and else Statements: Decision-making constructs allow us to control the
flow of a program based on certain conditions. We will cover the if, else if, and else
statements for making single-way decisions.
switch-case Statements: The switch-case statement provides a multi-way decision-
making structure in JAVA.
The Ternary Operator: The ternary operator is a shorthand for simple if-else
statements, providing concise code for certain conditions.
Lecture 4: Control Flow and Loops in JAVA
1. Control Flow Statements:
Introduction to Control Flow: Control flow refers to the order in which statements are
executed in a program. Understanding control flow is crucial for designing efficient and
logical programs.
The if-else if-else Ladder: The if-else if-else ladder allows us to execute multiple
conditional statements in sequence. Each condition is evaluated one by one until a true
condition is encountered, and the corresponding block of code is executed.
Example:
2. Looping Constructs:
The while Loop: The while loop executes a block of code repeatedly as long as the
specified condition remains true.
Example:
The do-while Loop: The do-while loop executes a block of code at least once, and then
it continues to repeat as long as the specified condition remains true.
Example:
The for Loop: The for loop is used when we know the number of times we want to
execute a block of code. It allows us to control the loop variable and specify the
condition for execution.
Example:
The continue Statement: The continue statement is used to skip the current iteration and
move to the next iteration in the loop based on a certain condition.
Example:
In this lecture, we covered control flow statements and looping constructs in JAVA.
These concepts are essential for designing dynamic and efficient programs. Practical
examples and exercises will help you grasp the concepts better and build confidence in
your JAVA programming skills.
Lecture 5: Arrays and Functions in JAVA
1. Arrays in
JAVA:
Introduction to Arrays: Arrays are an essential data structure in programming that
allow us to store multiple values of the same data type in a single variable. They
provide a convenient and efficient way to manage collections of data.
Declaring, Initializing, and Accessing Elements in a One-Dimensional Array: To
use an array, we first declare it by specifying the data type followed by square brackets
[]. We can then initialize the array with values using curly braces {}. Individual
elements in the array can be accessed using their indices, which start from 0 for the first
element.
Example:
2. Functions in JAVA:
Understanding Functions: Functions, also known as methods, are blocks of code that
perform specific tasks and can be called multiple times from different parts of the
program. Functions enhance code organization, modularity, and reusability.
10 | P a g e
Defining and Calling Functions: To define a function, we specify the return type (if
any), followed by the function name, and then list any parameters the function accepts.
The body of the function contains the code that executes when the function is called.
We can call a function using its name and providing the necessary arguments (if any).
Example:
3. Method Overloading:
Explanation of Method Overloading: Method overloading is the ability to define
multiple methods with the same name but different parameter lists. It provides
flexibility and convenience when working with similar tasks but with different input
requirements.
11 | P a g e
Creating Overloaded Methods: To create overloaded methods, we define methods
with the same name but different parameter lists. This can include different data types, a
different number of parameters, or a combination of both.
Example:
In this lecture, we explored arrays and functions in JAVA. Arrays allow us to store and
manipulate collections of data efficiently, while functions enable code reuse and
modular programming. Understanding these concepts is crucial for developing robust
and efficient JAVA programs. Practical examples and exercises will further reinforce
your understanding of arrays and functions in JAVA programming.
12 | P a g e
Lecture 6: Object-Oriented Programming (OOP) Concepts in JAVA
1. Introduction to Object-Oriented Programming (OOP):
Understanding the Principles of OOP: Object-Oriented Programming (OOP) is a
paradigm that focuses on modeling real-world entities and their interactions as objects.
We'll explore essential OOP concepts, including:
Abstraction: The process of simplifying complex entities by highlighting
relevant characteristics and hiding unnecessary details.
Encapsulation: Bundling data (attributes) and methods (functions) together to
form a single unit (object) and controlling access to them.
Inheritance: The ability of a class (subclass) to inherit properties and behaviors
from another class (superclass), promoting code reuse and hierarchy.
Polymorphism: The ability of objects to take on different forms, allowing
multiple classes to be treated as instances of a common superclass.
Advantages of Using OOP: We'll discuss the benefits of OOP in software
development, such as code reusability, modularity, maintainability, and enhanced
problem-solving through real-world modeling.
2. Classes and Objects in JAVA:
Definition of Classes and Objects: In OOP, a class is a blueprint for creating objects
with shared attributes and behaviors. We'll delve into class declarations, specifying
attributes (instance variables) and writing methods (member functions).
Example:
13 | P a g e
Instantiating Objects and Accessing Attributes and Methods: We'll cover object
instantiation (creating instances of classes) and accessing their attributes and methods
using the dot notation.
Example:
14 | P a g e
Example:
Example:
15 | P a g e
4. Inheritance and Polymorphism:
Understanding Inheritance: Inheritance allows a subclass to inherit properties and
behaviors from a superclass. We'll explore how inheritance promotes code reuse and
supports the "is-a" relationship.
Example:
16 | P a g e
Lecture 7: File Handling and Exception Handling in JAVA
1. File Handling in JAVA:
Understanding File Handling: File handling is a crucial aspect of programming when
dealing with external files for reading and writing data. We'll discuss the significance
of file handling in various applications and scenarios.
Exploring File Handling Classes: In JAVA, several classes are dedicated to file
handling, including FileReader, FileWriter, and BufferedReader. We'll delve into these
classes' functionalities and their specific uses.
Reading from Text Files: We'll demonstrate how to read data from text files using the
FileReader and BufferedReader classes. Reading data from files allows applications to
retrieve information from external sources.
Example:
Writing to Text Files: We'll cover writing data to text files using the FileWriter class.
This functionality enables applications to store data in external files for future use or
sharing.
17 | P a g e
Example:
18 | P a g e
Multiple Catch Blocks: We'll cover scenarios where multiple exceptions may occur and
how to handle them using multiple catch blocks.
Example:
The finally Block: The finally block ensures that certain code gets executed, regardless
of whether an exception occurs or not. We'll discuss its purpose and practical
applications.
Example:
19 | P a g e
3. Throwing Custom Exceptions:
Creating Custom Exception Classes: Sometimes, predefined exceptions may not be
sufficient to handle specific scenarios. We'll learn how to create custom exception
classes to handle unique situations effectively.
Example:
Throwing and Catching Custom Exceptions: We'll explore how to throw and catch
custom exceptions in JAVA programs, providing developers with more control over
exception handling.
In this lecture, we'll cover essential concepts of file handling and exception handling in
JAVA. You'll learn how to read and write data to external files, handle different types
of exceptions, and even create custom exceptions for specific scenarios. Practical
examples will enhance your understanding and enable you to apply these concepts in
real-world programming scenarios.
20 | P a g e
PRACTICALS
Practical One: Hello World
Instructions:
Open your preferred Java development environment (IDE) or a text editor.
Create a new Java file and name it something like "HelloWorld.java".
Inside the file, write the Java code to print "Hello, World!" to the console:
Save the file and compile it. If you are using an IDE, you can simply run
the program
Save the file and compile the program by running the following command in the
terminal or command prompt:
If there are no errors, run the program using the following command:
21 | P a g e
Practical Two: Basic Input/Output
Instructions:
Create a new Java file and name it something like "Greeting.java".
Write the code to read user input (their name) and output a personalized
greeting:
22 | P a g e
Practical Four: Odd or Even
Instructions:
Create a new Java file and name it something like "OddEven.java".
Write code to check whether a given integer is odd or even:
23 | P a g e
Practical Six: Fibonacci Series
Instructions:
Create a new Java file and name it something like "FibonacciSeries.java".
Implement code to generate the Fibonacci series up to a given number of terms:
24 | P a g e
Practical Eight: Array Operations
Instructions:
Create a new Java file and name it something like "ArrayOperations.java".
Implement code to perform various operations on arrays, like finding the largest
element, the sum of elements, and searching for an element:
25 | P a g e
Practical Nine: Pattern Printing
Create a new Java file (e.g., PatternPrinting.java).
Write a program to print a right-angled triangle pattern using nested loops. For
example, to print a triangle with 5 rows, write the following code in the file:
26 | P a g e
Practical Ten: File Handling
Create a new Java file named FileHandling.java.
Create an input file named input.txt in the same directory, containing some text
to be processed.
Write the following code in the file:
27 | P a g e
Practical Eleven: Exception Handling
Create a new Java file named ExceptionHandling.java.
Write the following code in the file:
28 | P a g e
Practical Twelve: Class and Object
Create a new Java file named Student.java.
Write the following code in the file:
29 | P a g e
Practical Thirteen: Inheritance
Create a new Java file named Animal.java.
Write the following code in the file: