JAVA
JAVA
|Assignment 1
Features of Java
1. Object-Oriented
2. Platform-Independent
4. Secure
6. Multithreading Support
7. Distributed Computing
● Java has built-in networking capabilities that allow
the creation of distributed applications.
8. High Performance
Conclusion
1. Introduction
2. Key Differences
1. Variable
A variable is a named memory location used to store
values in a program. The value of a variable can change
during execution.
2. Data Types
Data types define the type of values a variable can hold.
Java has two main categories of data types:
3. Operator
An operator is a symbol that performs operations on
variables and values.
4. Keyword
A keyword is a reserved word in Java that has a
predefined meaning and cannot be used as a variable
name.
Conclusion
Term Definition
Example:
public class SingleLineComment {
Example:
int sum = a + b;
Example:
/**
* @author Aaditya
* @version 1.0
*/
/**
*/
return a + b;
}
public static void main(String[] args) {
}.
1. if Statement
● It checks a condition.
Syntax:
if (condition) {
Example:
2. if-else Statement
● If the condition is true, the if block executes.
Syntax:
if (condition) {
} else {
Example:
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
3. if-else-if Ladder
● Used when you have multiple conditions to check.
Syntax:
if (condition1) {
} else if (condition2) {
} else if (condition3) {
} else {
Example:
int marks = 75;
System.out.println("Grade A");
System.out.println("Grade C");
} else {
System.out.println("Fail");
Conclusion:
● Use if for a single condition.
1. while loop
Syntax:
while(condition) {
// code to execute
Example:
int i = 1;
while(i <= 5) {
System.out.println(i);
i++;
2. do-while loop
● Executes code once, then checks the condition.
Syntax:
do {
// code to execute
} while(condition);
Example:
int i = 1;
do {
System.out.println(i);
i++;
3. for loop
● Used when you know how many times to run the
loop.
Syntax:
for(initialization; condition; update) {
// code to execute
Example:
for(int i = 1; i <= 5; i++) {
System.out.println(i);
4. for-each loop
● Used to iterate over arrays or collections.
Syntax:
for(dataType variable : array) {
// code to execute
Example:
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(num);
|Assignment 2
1. Define the terms Class, Object, and Constructor in
Java.
1. Class
Syntax:
class Car {
int speed;
void drive() {
System.out.println("Car is driving");
2. Object
Syntax:
Car myCar = new Car(); // myCar is an object of class Car
myCar.speed = 60;
myCar.drive();
3. Constructor
Syntax:
class Car {
int speed;
// Constructor
Car() {
speed = 50;
System.out.println("Constructor called!");
// Method
void displayInfo() {
}
// Main method
myCar.displayInfo();
Explanation:
1. Class File –
Student.java
// Student.java
String name;
int age;
// Constructor
this.name = name;
this.age = age;
// MainClass.java
s1.displayInfo();
How to Run:
2.Compile both:
javac Student.java
javac MainClass.java
3.Run the main class:
java Mainclass
1.
String name;
int age;
class MainClass {
s1.name = "Aaditya";
s1.age = 21;
s1.display();
}
2.
Using a Method
You create a method to initialize the object’s attributes.
public class Student {
String name;
int age;
name = n;
age = a;
class MainClass {
s2.setDetails("Teju", 18);
s2.display();
}
Example:
class Student {
String name;
int age;
// Default constructor
Student() {
name = "Unknown";
age = 0;
void display() {
class MainClass {
public static void main(String[] args) {
s.display();
2. Parameterized Constructor
Example:
class Student {
String name;
int age;
// Parameterized constructor
Student(String n, int a) {
name = n;
age = a;
void display() {
class MainClass {
s.display();
Example:
class Student {
String name;
int age;
// Parameterized constructor
Student(String n, int a) {
name = n;
age = a;
}
// Copy constructor
Student(Student s) {
name = s.name;
age = s.age;
void display() {
class MainClass {
s2.display();
}
|Assignment 3
Syntax:
class Parent {
Benefits of Inheritance:
● Polymorphism support.
1. Single Inheritance
class Animal {
void eat() {
void bark() {
System.out.println("Dog barks.");
2. Multilevel Inheritance
● A class inherits from a class which is itself inherited
from another class.
class Animal {
void eat() {
System.out.println("Animal eats.");
void bark() {
System.out.println("Dog barks.");
void weep() {
System.out.println("Puppy weeps.");
3. Hierarchical Inheritance
● Multiple child classes inherit from a single parent
class.
class Animal {
void eat() {
System.out.println("Animal eats.");
void bark() {
System.out.println("Dog barks.");
void meow() {
System.out.println("Cat meows.");
interface A {
void methodA();
interface B {
void methodB();
class C implements A, B {
System.out.println("Method A");
System.out.println("Method B");
Note:
Java does not support multiple inheritance with classes to
avoid ambiguity (like the Diamond Problem).
This means the child class gets access to the fields and
methods of the parent class.
Example:
// Parent Class
class Animal {
void eat() {
// Child Class
void bark() {
System.out.println("Dog barks.");
}
// Main Class
Output:
Dog barks.
Explanation:
Example:
// Grandparent Class
class Animal {
void eat() {
System.out.println("Animal eats.");
// Parent Class
void bark() {
System.out.println("Dog barks.");
// Child Class
System.out.println("Puppy weeps.");
// Main Class
Output:
Animal eats.
Dog barks.
Puppy weeps.
Explanation:
● Puppy inherits from Dog, and Dog inherits from
Animal.
2. Hierarchical Inheritance
Example:
// Parent Class
class Animal {
void eat() {
System.out.println("Animal eats.");
void bark() {
System.out.println("Dog barks.");
}
void meow() {
System.out.println("Cat meows.");
// Main Class
}
Output:
Animal eats.
Dog barks.
Animal eats.
Cat meows.
Explanation:
class A {
void show() {
System.out.println("Class A");
class B {
void show() {
System.out.println("Class B");
Imagine:
A
/\
B C
\/
interface A {
void show();
interface B {
void show();
class C implements A, B {
Summary:
Example:
class Calculator {
return a + b;
return a + b;
return a + b + c;
Example:
class Animal {
void sound() {
void sound() {
System.out.println("Dog barks");
System.out.println("Cat meows");
class MainClass {
Animal a;
a = new Dog();
a = new Cat();
Definition:
Purpose:
class Calculator {
return a + b;
return a + b;
return a + b + c;
Output:
30
10.0
Definition:
Function overriding means a subclass provides its own
version of a method that is already defined in its
superclass.
Purpose:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
@Override
void sound() {
System.out.println("Cat meows");
Animal a;
a = new Dog();
a = new Cat();
Output:
Dog barks
Cat meows
Inheritance No Yes
Required
|Assignment 4
1. what is exception handling in Java and what is
difference between check and uncheck exception.
Exception Handling in Java is a mechanism to handle
runtime errors so that the normal flow of the program can
be maintained. It helps to catch, handle, and resolve
errors gracefully without crashing the program.
Key Terms:
● Try – The block where you write the code that might
throw an exception.
try {
} catch (ArithmeticException e) {
System.out.println(fileInput.readLine());
fileInput.close();
import java.io.*;
System.out.println(fileInput.readLine());
fileInput.close();
}
2.write note on Java exceptions key word.
1. try
try {
int result = 10 / 0;
}
2. catch
catch (ArithmeticException e) {
3.finally
finally {
4. throw
● The throw keyword is used to manually throw an
exception.
finally {
5. throws
Keyword Purpose
Syntex:
try {
} catch (ExceptionType e) {
}
Example:
public class TryCatchExample {
try {
} catch (ArithmeticException e) {
Output:
Error: Cannot divide by zero!
● 10 / 0 causes an ArithmeticException.
// risky code
Note: All exception types in a multi-catch block must be unrelated (not in a parent-child
relationship).
Example:
public class MultiCatchExample {
try {
Output:
Caught an exception: java.lang.NullPointerException
Syntax:
try {
try {
// Inner try block
} catch (ExceptionType1 e) {
} catch (ExceptionType2 e) {
Example:
try {
try {
} catch (ArrayIndexOutOfBoundsException e) {
} catch (ArithmeticException e) {
}
}
Output:
Conclusion:
● Each try block can have its own specific catch block,
improving error handling clarity.
○ Closing files
○ Releasing resources (e.g., database connections)
Syntax:
try {
} catch (Exception e) {
// handle exception
} finally {
Example:
try {
int result = 10 / 2;
} catch (ArithmeticException e) {
System.out.println("Exception caught!");
} finally {
System.out.println("This block always executes.");
Output:
Result: 5
○ An exception is thrown
Syntax:
throw new ExceptionType("Error message");
} else {
Output:
try {
validateAge(16);
} catch (ArithmeticException e) {
Output:
Key Points:
● throw is used to explicitly generate an exception.