Program no 1:
class Car{
String brand;
String model;
int year;
void start()
{
[Link](brand + " ");
}
}
// Creating an object
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object of the Car class
[Link] = "Toyoto";
[Link] = "Camry";
[Link]=2025;
[Link](); // Calling a method on the object
//Creating another object of Car
Car anotherCar = new Car(); // Creating an object of the Car class
[Link] = "Honda";
[Link] = "Civic";
[Link]=2024;
[Link]();
}
}
Program no 2
class Calculator {
// Constructor Overloading
// Default constructor
public Calculator() {
[Link]("Calculator object created with default values.");
}
// Constructor with one parameter
public Calculator(int initialValue) {
[Link]("Calculator object created with initial value: " +
initialValue);
}
// Constructor with two parameters
public Calculator(double val1, double val2) {
[Link]("Calculator object created with two initial values: " + val1+"
and " + val2);
}
// Method Overloading
// Method to add two integers
public int add(int a, int b) {
[Link]("Adding two integers.");
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
[Link]("Adding three integers.");
return a + b + c;
}
// Method to add two doubles
public double add(double a, double b) {
[Link]("Adding two doubles.");
return a + b;
}
// Method to concatenate two strings
public String add(String s1, String s2) {
[Link]("Concatenating two strings.");
return s1 + s2;
}
}
// Main class to test the Calculator class
public class OverloadingDemo {
public static void main(String[] args) {
// Demonstrating Constructor Overloading
Calculator calc1 = new Calculator(); // Calls default constructor
Calculator calc2 = new Calculator(10); // Calls constructor with one int
parameter
Calculator calc3 = new Calculator(5.5, 2.3); // Calls constructor with two
double parameters
[Link]("\n--- Demonstrating Method Overloading ---");
// Demonstrating Method Overloading
int sum1 = [Link](5, 7); // Calls add(int, int)
[Link]("Sum of 5 and 7: " + sum1);
int sum2 = [Link](1, 2, 3); // Calls add(int, int, int)
[Link]("Sum of 1, 2, and 3: " + sum2);
double sum3 = [Link](10.5, 20.3); // Calls add(double, double)
[Link]("Sum of 10.5 and 20.3: " + sum3);
String concatenatedString = [Link]("Hello", " World!"); // Calls add(String,
String)
[Link]("Concatenated string: " + concatenatedString);
}
}
Program no 3
1) class Animal {// Parent class
void sound() {
[Link]("Animal makes a sound");
}
}
// Child class
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
// Child class
class Cat extends Animal {
void sound() {
[Link]("Cat meows");
}
}
// Child class
class Cow extends Animal {
void sound() {
[Link]("Cow moos");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
[Link]();
a = new Cat();
[Link]();
a = new Cow();
[Link]();
}
}
2) public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int result = divide(10, 0); // This will throw an
ArithmeticException
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero. " +
[Link]());
} finally {
[Link]("Finally block executed.");
}
try {
String text = null;
[Link]([Link]()); // This will throw a
NullPointerException
} catch (NullPointerException e) {
[Link]("Error: Null pointer encountered. " +
[Link]());
}
}
public static int divide(int numerator, int denominator) throws
ArithmeticException {
if (denominator == 0) {
throw new ArithmeticException("Division by zero is not
allowed.");
}
return numerator / denominator;
}
}
Program no 4
import [Link].*;
import [Link].*;
public class MyFirstServlet extends GenericServlet
{
public void service(ServletRequest req,
ServletResponse resp)
throws ServletException, IOException
{
[Link]("text/html");
PrintWriter pw = [Link]();
[Link]("<html>");
[Link]("<head><title>My first Servlet</title></head>");
[Link]("<body>");
[Link]("<h2>Welcome To Servlet World!</h2>");
[Link]("</body>");
[Link]("</html>");
[Link]();
}
}
Designing [Link] File
<web-app>
<servlet>
<servlet-class>MyFirstServlet</servlet-class>
<servlet-name>MyFirstServlet</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/MyFirstServlet</url-pattern>
</servlet-mapping>
</web-app>
HTTP SERVLET
Program no 5
// Import required java libraries
import [Link].*;
import [Link].*;
import [Link].*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException {
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
[Link]("text/html");
// Actual logic goes here.
PrintWriter out = [Link]();
[Link]("<h1>" + message + "</h1>");
}
public void destroy() {
// do nothing.
}
}
Designing [Link] File
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>