Java File Final
Java File Final
JAVA
(PRACTICAL FILE)
BCA SEMESTER-V
Index
s.n Title Pg no. Sign
o
1 Acknowledgment 4
2 WAP to add two numbers 5
Acknowledgment
I wish to extend my heartfelt gratitude and sincere
appreciation. Acknowledging one's feelings and
putting them into words are often two separate
challenges. In all honesty, I sometimes struggle to
find the right words when I truly want to express our
warmest thanks and profound indebtedness.
class Addition1
{
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
int a = s1.nextInt();
int b = s1.nextInt();
int add = a + b;
System.out.println("Addition of two numbers is " + add);
s1.close();
}
}
OUTPUT:
Page |6
class circle
{
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
Area A = new Area();
System.out.println("Enter Radius of Circle");
int b = S.nextInt();
double AREA = A.circle(b);
System.out.print("Area Of The Circle Is : "+AREA+" sq units.");
S.close();
}
}
OUTPUT:
import java.util.Scanner;
class demo
{
void display(float a,float b)
{
System.out.println("a+b=" + (a+b));
System.out.println("\na-b=" + (a-b));
System.out.println("\na/b=" + (a/b));
System.out.println("\na*b=" + (a*b));
}
}
class operator
{
public static void main(String[] args)
{
Scanner S1 = new Scanner(System.in);
demo d = new demo();
float x = S1.nextInt();
float y = S1.nextInt();
System.out.print("Concept of operators----->");
d.display(x,y);
S1.close();
}
}
OUTPUT:
class ifelse
{
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
int num = S.nextInt();
if(num > 0)
{
System.out.println("Number is positive");
}
else
{
System.out.print("Number is negative");
}
S.close();
}
}
OUTPUT:
{
int bigger(int num1,int num2,int num3)
{
int largest;
if(num1>=num2)
{
if(num1>=num3)
{
largest = num1;
}
else
{
largest = num3;
}
}
else
{
if (num2>=num3)
{
largest = num2;
}
else
{
largest = num3;
}
}
return largest ;
}
}
class biggest_3
{
public static void main(String[] args)
{
largest obj = new largest();
Scanner obj1 = new Scanner(System.in);
System.out.println("Enter number for comparison->");
int a = obj1.nextInt();
int b = obj1.nextInt();
int c = obj1.nextInt();
int BIGGER = obj.bigger(a,b,c);
System.out.print("\nThe Largest Number is "+BIGGER);
obj1.close();
}
P a g e | 10
OUTPUT:
double result = 0;
switch (operator) {
case '+':
result = firstNumber + secondNumber;
break;
case '-':
result = firstNumber - secondNumber;
break;
case '*':
result = firstNumber * secondNumber;
break;
case '/':
result = firstNumber / secondNumber;
break;
default:
System.out.println("Invalid operator.");
break;
}
OUTPUT:
P a g e | 12
}
class table
{
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
demo2 obj = new demo2();
try {
System.out.println("Enter number to print table for :-> ");
int a = S.nextInt();
obj.printable(a);
} finally {
S.close();
}
}
}
OUTPUT:
class rect2
{
P a g e | 14
OUTPUT:
OUTPUT:
P a g e | 15
OUTPUT:
// Subclass
class Dog01 extends Animal01 {
void bark() {
System.out.println("The dog barks.");
}
}
Output:
// Child class 1
class Dog11 extends Animal11 {
void bark() {
System.out.println("The dog barks.");
}
}
// Child class 2
class Cat11 extends Animal11 {
void meow() {
System.out.println("The cat meows.");
}
}
myDog.eat(); // Call the eat() method from the Parent class (Animal) via Dog
myDog.bark(); // Call the bark() method from the Dog class
System.out.println();
myCat.eat(); // Call the eat() method from the Parent class (Animal) via Cat
myCat.meow(); // Call the meow() method from the Cat class
}
}
OUTPUT:
P a g e | 21
// Getter methods
public String getName() {
return name;
}
OUTPUT:
P a g e | 23
// Concrete method
public void display() {
System.out.println("This is a shape.");
}
}
// Main class
class absclass
{
public static void main(String[] args) {
// Creating objects of concrete classes
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
// Calling methods
circle.display();
circle.calculateArea();
rectangle.display();
rectangle.calculateArea();
}
}
OUTPUT:
P a g e | 25
}
}
OUTPUT:
P a g e | 27
interface Mammal {
void eat();
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.eat();
}
}
OUTPUT:
P a g e | 28
// Using StringBuilder
StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(str2);
String result3 = sb.toString();
System.out.println("Using StringBuilder: " + result3);
// Using StringBuffer
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(str1);
stringBuffer.append(str2);
String result4 = stringBuffer.toString();
System.out.println("Using StringBuffer: " + result4);
}
}
OUTPUT:
P a g e | 29
OUTPUT:
P a g e | 30
OUTPUT:
P a g e | 31
package myPackage;
import myPackage.MyClass;
OUTPUT:
P a g e | 32
package myPackage;
import myPackage.MyClass;
OUTPUT:
P a g e | 33
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to the database...");
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
// Create a table
String createTableSQL = "CREATE TABLE IF NOT EXISTS employees (" +
"id INT AUTO_INCREMENT PRIMARY KEY, " +
"first_name VARCHAR(50), " +
P a g e | 34
"last_name VARCHAR(50))";
stmt.executeUpdate(createTableSQL);
System.out.println("Table 'employees' created successfully.");
// Clean up
resultSet.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
P a g e | 35
Output:
P a g e | 36
try {
// Create an instance of FileWriter
FileWriter fileWrite = new FileWriter("charfile.txt");
fileWrite.write(str); // write the string to the file
fileWrite.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Create an instance of FileReader
FileReader fileRead = new FileReader("charfile.txt");
import java.io.*;
try {
// Create a file input stream
FileInputStream fin = new FileInputStream("mydata.dat");
Output: