Computer Programming (Java)
Computer Programming (Java)
Objective: To write and execute a Java Applet program that displays the message "HELLO
JAVA" in a web browser or applet viewer.
Apparatus/Software Required:
Theory:
An applet is a Java program that runs in a web browser or applet viewer. Unlike standalone
applications, applets do not require a main() method. Instead, they inherit from the Applet class
and override methods like paint() to perform actions such as drawing on the screen.
Source Code:
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="HelloJavaApplet.class" width=300 height=150>
</applet>
*/
Observation:
After execution, the message "HELLO JAVA" appears centered on the applet window.
Result:
The applet successfully displays the message “HELLO JAVA” in the applet window.
Conclusion:
The experiment demonstrates how to create a basic Java Applet and display text using the
Graphics class and drawString() method.
Experiment No: 4
Objective: To write and execute a Java program that connects to a database and performs
basic operations such as retrieving and displaying data.
Apparatus/Software Required:
Theory:
Java Database Connectivity (JDBC) is an API that enables Java programs to interact with
databases. Using JDBC, we can connect to a database, send SQL queries, and retrieve
results. The typical steps in JDBC are:
Source Code
Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.ResultSet;
Import java.sql.Statement;
Try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
// Create a statement
// Execute a query
// Display results
While (rs.next()) {
System.out.println(“ID: “ + rs.getInt(1) +
“, Name: “ + rs.getString(2));
// Close connection
Con.close();
} catch (Exception e) {
System.out.println€;
Procedure:
Observation:
The console displays the contents of the students table, showing ID and Name of each
student.
Result:
The program successfully connects to the MySQL database and retrieves data using JDBC.
Conclusion:
The experiment demonstrates how to use Java JDBC to establish a connection with a
database and execute SQL queries.
Experiment No . 3
Objective: To write and execute a Java program demonstrating interfacing between two
classes using an interface.
Apparatus/Software Required:
Theory:
In Java, an interface is a reference type, similar to a class, that can contain only constants,
method signatures, default methods, static methods, and nested types. Interfaces provide
a way to achieve abstraction and multiple inheritance in Java.
When two classes need to communicate, one class can implement an interface defined by
another class, ensuring loose coupling and modularity.
Source Code:
// Interface
Interface Message {
Void showMessage();
Procedure:
Observation:
Result:
The program successfully demonstrates interfacing between two classes using the
Message interface.
Conclusion:
This experiment shows how interfaces in Java are used to allow communication between
classes, enforce abstraction, and support multiple inheritance.
Experiment No 2.
Theory:
Exception handling in Java is a mechanism to handle runtime errors, so the normal flow of the
application can be maintained. Java provides five keywords to handle exceptions:
Program:
public class ExceptionHandlingDemo {
public static void main(String[] args) {
int[] numbers = {10, 0, 5};
Output:
Dividing 100 by 10
Result: 10
This block always executes.
Dividing 100 by 0
Error: Cannot divide by zero.
This block always executes.
Dividing 100 by 5
Result: 20
This block always executes.
Conclusion:
The program successfully demonstrates how exception handling allows the program to catch and
manage runtime errors such as divide-by-zero, and continue executing without crashing.
Experiment No 1
Objective: To understand the concept of a class in Java and how it is used to define and
create objects.
Theory:
In Java, a class is a blueprint from which individual objects are created. A class contains
fields (variables) and methods to define the behavior of an object.
Program:
// Class definition
Class Student {
// Fields (attributes)
String name;
Int age;
Void displayInfo() {
// Main class
Student1.name = “John”;
Student1.age = 20;
Student1.displayInfo();
Output:
Student Age: 20
Conclusion:
This program demonstrates how to define a class, create an object, assign values to its
attributes, and call its methods in Java. It helps to understand the foundation of object-
oriented programming.