Unit - I: Core Java
Unit - I: Core Java
UNIT - I
ii. Enterprise Applications: Java is a popular choice for developing enterprise-level applications,
including Customer Relationship Management (CRM) systems, Enterprise Resource Planning
(ERP) systems, and Human Resource Management (HRM) systems.
iii. Mobile Applications (Android): Java is one of the primary programming languages for developing
Android applications. Developers use Java to write Android apps using the Android Studio IDE
and the Android SDK.
iv. Desktop Applications: Java can be used to create cross-platform desktop applications with
graphical user interfaces (GUIs) using libraries like JavaFX and Swing. These applications can run
on different operating systems without modification.
v. IoT (Internet of Things) Applications: Java can be used for developing IoT applications, especially
for devices with more processing power and memory. It provides the advantage of platform
independence and security features.
UNIT - II
1. Nested if statement with example.
Ans. Nested if statements are a way to create more complex conditional logic in programming by
placing one or more if statements inside another if statement. This allows you to make decisions
based on multiple conditions.
-> For example:
public class NestedIfExample {
public static void main(String[] args) {
int age = 25;
int income = 50000;
switch (dayOfWeek) {
case 1:
System.out.println("It's Monday.");
break;
case 2:
System.out.println("It's Tuesday.");
CORE JAVA
break;
case 3:
System.out.println("It's Wednesday.");
break;
case 4:
System.out.println("It's Thursday.");
break;
case 5:
System.out.println("It's Friday.");
break;
case 6:
System.out.println("It's Saturday.");
break;
case 7:
System.out.println("It's Sunday.");
break;
default:
System.out.println("Invalid day of the week.");
}
}
}
-> Output:
It's Wednesday.
UNIT - III
1. Multiple inheritance.
Ans. In Java, multiple inheritance of behavior is achieved through interfaces. A class can implement
multiple interfaces, inheriting their method signatures. Java does not support multiple inheritance in
the way some other programming languages like C++ do, where a class can inherit from multiple base
classes directly. However, Java does provide a form of multiple inheritance through interfacess.
-> Example:
interface Walkable {
void walk();
}
interface Swimmable {
void swim();
}
@Override
public void swim() {
System.out.println("Dog is swimming.");
}
}
UNIT - IV
2. Advantages of Multi-threading.
Ans. Multi-threading is a programming technique that allows a computer's CPU to execute multiple
threads or tasks concurrently, sharing the same resources and running in parallel. This approach
offers several advantages in software development and system performance:
i. Improved Performance: One of the most significant advantages of multi-threading is improved
performance. By splitting a program into multiple threads that can run concurrently, you can
fully utilize the CPU's processing power. This leads to faster execution times and more efficient
resource utilization.
ii. Parallelism: Multi-threading enables parallelism, where multiple tasks are executed
simultaneously. This is particularly beneficial for computationally intensive or time-consuming
operations, such as data processing, rendering, and simulations.
iii. Responsiveness: In graphical user interfaces (GUIs) and interactive applications, multi-threading
can help maintain responsiveness. By running time-consuming tasks in separate threads, the
main UI thread remains responsive to user input, providing a smoother user experience.
iv. Concurrency: Multi-threading allows for concurrency, enabling different parts of a program to
make progress independently. This is valuable in scenarios where multiple tasks need to run
concurrently, such as in server applications handling multiple client requests.
v. Resource Sharing: Threads within the same process share memory and resources, reducing the
overhead of inter-process communication (IPC). This simplifies data sharing and communication
between different parts of an application.
UNIT - V
choice.add("Option 1");
choice.add("Option 2");
choice.add("Option 3");
frame.add(choice);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
ii. List Class:
- The List class is also part of the AWT library, but it is used for creating list boxes, which allow users
to select one or more items from a list of options.
- Unlike Choice, `List` components allow multiple selections. You can specify the number of visible
items and whether the user can select one or multiple items.
CORE JAVA
- Example:
import java.awt.*;
public class ListExample {
public static void main(String[] args) {
Frame frame = new Frame("List Example");
List list = new List(3, true); // Allow multiple selections with 3 visible rows
list.add("Option 1");
list.add("Option 2");
list.add("Option 3");
list.add("Option 4");
list.add("Option 5");
frame.add(list);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
By using the MouseAdapter class, we streamline our code and make it more concise and readable
while still achieving the desired functionality. This is a practical example of how adapter classes
simplify interface implementation in Java.