OOPs With Java Lab Record
OOPs With Java Lab Record
OOPs With Java Lab Record
Reg.No :_____________________________________________________________
Campus Id :_____________________________________________________________
Specialization :_____________________________________________________________
Subject : _____________________________________________________________
Year : _____________________________________________________________
Semester : _____________________________________________________________
THE YENEPOYA INSTITUTE OF ARTS,
SCIENCE, COMMERCE AND MANAGEMENT
A Constituent Unit of YENEPOYA (Deemed to be University)
Reg.No :_____________________________________________________________
Campus Id :_____________________________________________________________
Specialization :_____________________________________________________________
Subject : _____________________________________________________________
Year : _____________________________________________________________
Semester : _____________________________________________________________
THE YENEPOYA INSTITUTE OF ARTS,
________________________________ ______________________________
________________________________ ______________________________
import java.util.Scanner;
public class A1
{
public static int fact(int num)
{
int fact=1;
for(int i=1; i<=num; i++)
fact = fact*i;
return fact;
}
public static void main(String[] args)
{
int n, r, ncr, npr;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Value of n: ");
n = s.nextInt();
System.out.print("Enter the Value of r: ");
r = s.nextInt();
if (n<r){
System.out.println("n should be greater than or equal to r");
}
else {
npr = (fact(n)) / (fact(n-r));
ncr = npr/fact(r);
System.out.println("\nnCr = " +ncr);
System.out.println("nPr = " +npr);
}
}
}
Output
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output
8. Type conversions
8. Type conversions
Perimeter(int l,int b)
{
length=l;
breadth=b;
}
void calculatePerimeter()
{
int per;
per=2*(length+breadth);
System.out.println("Perimeter: "+per);
}
void calculateArea()
{
int area;
area=length*breadth;
System.out.println("Area: "+area);
}
}
public class ParaConstructor
{
public static void main(String[] args)
{
Perimeter p1=new Perimeter(6,8);
Perimeter p2=new Perimeter(20,10);
Perimeter p3=new Perimeter(10,5);
System.out.println("Details of rectangle 01: ");
System.out.println("Length: " + p1.length);
System.out.println("Breadth: " + p1.breadth);
p1.calculatePerimeter();
p1.calculateArea();
System.out.println();
System.out.println("Details of rectangle 02: ");
System.out.println("Length: " + p2.length);
System.out.println("Breadth: " + p2.breadth);
p2.calculatePerimeter();
p2.calculateArea();
System.out.println();
System.out.println("Details of rectangle 03: ");
System.out.println("Length: " + p3.length);
import java.util.*;
public class Calculator {
public static void main(String[] args) {
float[] operands;
float res = 0;
int choice, op;
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.print("Enter Your Choice (1-5): ");
choice = s.nextInt();
switch (choice) {
case 1:
case 2:
case 3:
System.out.print("Enter the number of operands: ");
op = s.nextInt();
operands = new float[op];
System.out.print("\nEnter " + op + " Numbers: ");
for (int i = 0; i < op; i++) {
operands[i] = s.nextFloat();
}
res = calculate(choice, operands);
break;
case 4:
operands = new float[2]; // Division always uses 2 operands
System.out.print("Enter the first operand: ");
operands[0] = s.nextFloat();
System.out.print("Enter the second operand: ");
operands[1] = s.nextFloat();
res = calculate(choice, operands);
break;
case 5:
return;
default:
System.out.println("\nInvalid choice!");
break;
}
System.out.println("\nResult = " + res + "\n");
}
}
switch (choice) {
case 1: // Addition
for (int i = 0; i < operands.length; i++) {
result += operands[i];
}
break;
case 2: // Subtraction
result = operands[0]; // Initialize result with the first operand
for (int i = 1; i < operands.length; i++) {
result -= operands[i];
}
break;
case 3: // Multiplication
result = 1; // Initialize result with 1 for multiplication
for (float operand : operands) {
result *= operand;
}
break;
case 4: // Division
if (operands[1] != 0) {
result = operands[0] / operands[1];
} else {
System.out.println("Error: Division by zero!");
return 0;
}
break;
}
return result;
}
}
Output
// Parent class
class Person {
String name;
void setName(String name) {
this.name = name;
}
void display() {
System.out.println("Name: " + name);
}
}
class Student extends Person { // Single Inheritance
int id;
void setid(int id) {
this.id = id;
}
void displayStudent() {
System.out.println("Student Class");
display(); // Inherited from Person
System.out.println("Student ID: " + id);
System.out.println("Single Inheritance. Student class is a child of Person class");
}
}
class Faculty extends Person { // Hierarchical Inheritance
String sub;
void setsub(String sub) {
this.sub = sub;
}
void displayFaculty() {
System.out.println("Faculty Class");
display(); // Inherited from Person
System.out.println("Subject: " + sub);
System.out.println("Hierarchical Inheritance. Faculty class is also a child of
Person class");
}
}
class Hod extends Faculty { // Multilevel Inheritance
String dept;
void setdept(String dept) {
this.dept = dept;
}
void displayHod() {
System.out.println("Hod Class");
displayFaculty();
System.out.println("Department: " + dept);
System.out.println("Multilevel Inheritance. Hod class is a child of Faculty class,
which is a child of Person class");
}
}
public class Inheritance {
public static void main(String[] args) {
// Single Inheritance
Student stu = new Student();
stu.setName("Thor");
stu.setid(12345);
stu.displayStudent();
System.out.println();
// Hierarchical Inheritance
Faculty fac = new Faculty();
fac.setName("Natasha");
fac.setsub("OOPs with Java");
fac.displayFaculty();
System.out.println();
// Multilevel Inheritance
Hod h = new Hod();
h.setName("Loki");
h.setsub("Computer Science");
h.setdept("CS Department");
h.displayHod();
}
}
Output
//Method Overriding
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");}
DemoServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
throws ServletException,IOException
pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");
} }
web.xml file
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
Output
16. JDBC
16. JDBC
import java.sql.*;
static final String QUERY = "SELECT id, first, last, age FROM Employees";
// Open a connection
ResultSet rs = stmt.executeQuery(QUERY);) {
while (rs.next()) {
} catch (SQLException e) {
e.printStackTrace();
Output
Connecting to database...
Creating statement...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
Login()
label1.setText("Username:");
label2.setText("Password:");
SUBMIT=new JButton("SUBMIT");
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
String value1=text1.getText();
String value2=text2.getText();
page.setVisible(true);
page.getContentPane().add(label);
else{
"Error",JOptionPane.ERROR_MESSAGE);
class LoginDemo
try
frame.setSize(300,100);
frame.setVisible(true);
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
Output