Java Lab Programs
Java Lab Programs
OOPS lab
Structure of Java Program
JAVA COMMENTS
Comments are non-executable parts of a program. A compiler does not execute a comment. It is
used to improve the readability of the code.
/* a multi-line comment is
declared like this
and can have multiple
lines as a comment */
• Save : [Link]
• Compile : javac [Link]
• Run : java Add
EXPERIMENT 1 : JAVA SAMPLE PROGRAMS
class Sum
{ Save : [Link]
public static void main(String args[]) Compile: javac [Link]
{ Run : java Sum
int i,sum=0;
for(i=0;i<=10;i++)
{
sum=sum+i;
}
[Link](“Sum=”+sum);
}
}
EXPERIMENT 1 : JAVA SAMPLE PROGRAMS
1.5 Even or odd program
public class EvenOdd
{
public static void main(String[] args)
{
int number = 10 // Define a number to check
if (number % 2 == 0)
{
[Link](number + " is an even number."); // If the remainder is 0, it's an even number
}
else
{
[Link](number + " is an odd number."); // If the remainder is not 0, it's an odd number
}
}
}
EXPERIMENT 1 : JAVA SAMPLE
PROGRAMS
OUTPUT:
// Main class to test student class
public class TestStudent Student 1 details using Default Constructor:
{ Name: Unknown
public static void main(String[] args) Age: 0
{ Roll Number: 0
// creating a student object using the default constructor
Student s1 = new Student(); Student 2 details using Parameterized
[Link](); Constructor:
Name: John
// Creating a student object using the parameterized constructor Age: 20
Student s2 = new Student(“John", 20, 101); Roll Number: 101
[Link]();
}
}
EXPERIMENT 5 – METHOD OVERLOADING
• Write a Java program that uses method overloading to calculate the area of a
square and a rectangle.
EXPERIMENT 5 – METHOD OVERLOADING
// Class 1: Shape (contains overloaded methods)
class Shape
{
// Method to calculate the area of a square (side length)
public int area(int side)
{
return side * side; // Area of square = side * side
}
// Method to calculate the area of a rectangle (length and breadth)
public int area(int length, int breadth)
{
return length * breadth; // Area of rectangle = length * breadth
}
}
EXPERIMENT 5 – METHOD OVERLOADING
// Calling the overloaded area method for square with side length 5
[Link]("Area of Square : " + [Link](5));
// Calling the overloaded area method for rectangle with length 10 and breadth 4
[Link]("Area of Rectangle : " + [Link](10, 4));
}
}
JAVA PROGRAM USING ARRAY OF OBJECTS
// Loop through the array and call the display method for each Person object
for (int i = 0; i < [Link]; i++)
{
p[i].display(); // Calling display method to show details
}
}
}
JAVA PROGRAM USING STATIC VARIABLES AND METHODS
// Class containing static variables and methods
class StaticExample
{
// Static variable
static int count = 0;
// Static block - executed once when the class is loaded
static
{
[Link]("Static block executed!");
count = 10; // Initializing static variable
}
// Static method to display the count
static void displayCount()
{
[Link]("Count: " + count);
}
// Non-static method to increment the count
void incrementCount()
{ count++; // Increment the static variable
}
}
JAVA PROGRAM USING STATIC VARIABLES AND
METHODS
public class Test
{ OUTPUT:
public static void main(String[] args) Static block executed!
{ Count: 10
// Calling static method using class name from the other class Count: 11
[Link]();
OUTPUT
Animal is eating
Animal is traveling
MULTIPLE INHERITANCE USING INTERFACE
Output:
This is a person.
This person is a student.
Enter marks of 3 subjects: 75 80 85
Total Marks = 240.0
EXPERIMENT 11 – User Defined Package Java
Program
// Initialize components
JTextField t1 = new JTextField(); // Email field
JTextField t2 = new JTextField(); // Password field (JPasswordField
can also be used)
JButton b1 = new JButton("Login");
JLabel l1 = new JLabel("Email:");
JLabel l2 = new JLabel("Password:");
// Set positions for components // Action Listener logic
[Link](20, 30, 80, 30); [Link](new ActionListener() {
[Link](100, 30, 150, 30); public void actionPerformed(ActionEvent evt) {
[Link](20, 70, 80, 30); String email = [Link]();
[Link](100, 70, 150, 30); String pass = [Link]()); // getPassword() is used for JPasswordField
[Link](100, 120, 80, 30);
if (email .equals("admin") && [Link]("pass"))
// Add components to the JFrame object {
[Link](l1); [Link](f, "Login Successful");
[Link](t1); }
[Link](l2); else {
[Link](t2); [Link](f, "Invalid Credentials", "Error",
[Link](b1); JOptionPane.ERROR_MESSAGE);
}
// Frame settings }
[Link](300, 220); });
[Link](null); }
[Link](true);
}
[Link](JFrame.EXIT_ON_CLOSE);
Email
Password Login
Submit
import [Link].*; // 2. Create UI Components
import [Link].*;
JLabel userLabel = new JLabel(" Username:");
JTextField userField = new JTextField();
import [Link].*;
JLabel passLabel = new JLabel(" Password:");
import [Link].*; JPasswordField passField = new JPasswordField();
JButton submitButton = new JButton("Submit");
public class JDBCLoginApp {
public static void main(String[] args) { // 3. Add Components to Frame
// 1. Create the JFrame Object
[Link](userLabel);
[Link](userField);
JFrame frame = new JFrame("Login Database Connection");
[Link](passLabel);
[Link](350, 200); [Link](passField);
[Link](new GridLayout(3, 2, 10, 10)); [Link](new JLabel("")); // Spacer
[Link](JFrame.EXIT_ON_CLOSE); [Link](submitButton);
// 4. Submit Button Logic // SELECT ALL RECORDS
String selectQuery = "SELECT * FROM employee";
[Link](new ActionListener() {
Statement st = [Link]();
public void actionPerformed(ActionEvent e) { ResultSet rs = [Link](selectQuery);
[Link]();
Connection conn = [Link]("jdbc:mysql://localhost:3306/har", "root","" );
} catch (Exception ex) {
// INSERT RECORD
[Link](frame, "Error: " + [Link]());
String insertQuery = "INSERT INTO employee (username, password) VALUES (?, ?)"; [Link]();
}
PreparedStatement insertStmt = [Link](insertQuery); }
[Link](1, username); });
Check table:
SHOW TABLES;
DESC employee;
• How to Execute
• To compile and run from the terminal, you must include the JDBC JAR
in the classpath
• # Compile
• javac -cp .:[Link] [Link]
• # Run
• java -cp .:[Link] JDBCLoginApp