CS 406 Lab Manual
CS 406 Lab Manual
CS 406 Lab Manual
// Fields (attributes)
private String color;
private String model;
//Constructor
public Car(String color, String model) {
this.color=color;
this.model= model;
}
//Method
public void displayInfo() {
myCar.displayInfo();
}
4. Write a program to show Type Casting in
JAVA
// Program - 04
public class TypeCasting {
}
}
// Program -05
public class Exception_andling_Example {
try {
//program - 06
//Vehicle attribute
protected String brand="Ford";
//Vehicle method
public void honk() {
System.out.println("Tuut, tuut!");
}
}
}
}
}
}
8. Write a program to show Access Specifiers (Public, Private, Protected) in
JAVA
//Program - 08
class AccessSpecifierDemo {
System.out.println(demo.publicVar);
//Systemout.println(demma,privateVar); //Error: Cannot access
// Systemout.println(demaprotectedVar); //Error: Cannot access outside
the class without inheritance
}
}
class Car {
private String model;
private int year;
//Constructor
public Car (String model, int year) {
this.model =model;
this.year=year;
}
// Program - 10
interface Animal {
void sound(); // Interface method (does not have a body) }
}
myDog.sound();
myCat.sound();
}
}
11. Write a program to Add a Class to a
Package
// program - 11
package com.example.project;
public class program11 {
import java.awt.*;
import java.awt.event.*;
// program - 14
package com.example.project;
// program - 15
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
// Establish Connection
Connection conn = DriverManager.getConnection(url, user,
password);
// Create a statement
Statement stmt = conn.createStatement();
// Execute a query
ResultSet rs = stmt.executeQuery("SELECT * FROM yourTable");
// Clean up environment
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: Applets are deprecated as of Java 9 and removed in later versions, and modern browsers no
longer support Java applets due to security concerns. However, here's how it
would have been written for historical understanding.
// program 17
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
// program -18
Again, applets are deprecated, but for educational purposes, the code might
look like:
import
java.applet.Applet;
import
java.awt.Graphics;
System.out.println("Applet
initialized");
}
}
System.out.println("Applet starting");
System.out.println("Applet
stopping");
}
System.out.println("Applet
destroyed");
}
g.drawString("See console for life cycle messages.",
10, 20);
20. Write a program to demonstrate the concept of a servlet.
Servlets are Java programs that run on a server, typically to process web requests. Below is a
simple servlet example that displays "Hello Servlet" in response to HTTP requests.
import javax.servlet.*;
import
javax.servlet.http.*;
import java.io.*;
throws ServletException,
IOException {
response.setContentType("text/
html");
PrintWriter out =
response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello
Servlet</h1>");
out.println("</body></
html>");
}
}
This servlet would need to be deployed on a servlet container such as Apache Tomcat. You would also
need to configure it in the web.xml file or use annotations in newer Java versions to specify URL
patterns.