Bca Java Lab
Bca Java Lab
import java.util.Scanner;
public classRootsOfQuadraticEquation{
Scanner sc =newScanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
if(determinant>0){
}elseif(determinant ==0){
OUTPUT:
Practical 2: Write a Java program that prompts the user for an integer and then prints out all prime
numbers up to that integer.
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,j,n,c;
System.out.println("Enter the number till which you want prime numbers");
n=sc.nextInt();
System.out.println("Prime numbers are :-");
for(i=2;i<=n;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
System.out.print(i+" ");
}
}
}
}
Output:
Practical-3: Write a Java program to create a Student class with following fields
i. Hall ticket number ii. Student Name iii. Department
Create „n‟ number of Student objects where „n‟ value is passed as input to constructor
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Student[] arr;
int Hallticketnumber;
String StudentName;
String Department;
Scanner sc=new Scanner(System.in);
System.out.println("enter n value");
int n=sc.nextInt();
arr=new Student[n+1];
for(int i=1;i<=n;i++)
{
System.out.println("enter student"+i+"details");
Hallticketnumber=sc.nextInt();
StudentName=sc.next();
Department=sc.next();
arr[i]=new Student(Hallticketnumber,StudentName,Department);
}
for(int i=1;i<=n;i++)
{
arr[i].display();
}
}
}
class Student
{
int Hallticketnumber;
String StudentName;
String Department;
Student(int x,String y,String z)
{
Hallticketnumber=x;
StudentName=y;
Department=z;
}
public void display()
{
System.out.println("Hallticketnumber is "+Hallticketnumber+" StudentName is
"+StudentName+" Department is "+Department);
}
}
Output :
Practical-4 : Write Java program to implement Hierarchical Inheritance
class A
{
public void dispA()
{
System.out.println("Displaying method A");
}
}
class B extends A
{
public void dispB()
{
System.out.println("Displaying method B");
}
}
class C extends A
{
public void dispC()
{
System.out.println("Displaying method C");
}
}
public class Student
{
public static void main(String args[])
{
C x=new C();
x.dispC();
x.dispA();
B y=new B();
y.dispB();
y.dispA();
}
}
OUTPUT:
Practical-5 : Write Java program to implement multiple inheritance through interface .
interface AnimalEat {
void eat();
interface AnimalTravel {
void travel();
System.out.println("Animal is eating");
System.out.println("Animal is travelling");
a.eat();
a.travel();
OUTPUT:
Practicl-6 : Write a Java program to demonstrate String comparison using == and equals method.
Program 6:
Practical-7 Write a Java program that creates three threads. First thread displays “Good Morning”
everyone second, the second thread displays “Hello” every two seconds and the third thread displays
“Welcome” every three seconds
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Good Morning");
try {
sleep(1000);
} catch(Exception e) {
}
}
}
}
class B extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Hello");
try {
sleep(2000);
} catch(Exception e) {
}
}
}
}
class C extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Welcome");
try {
sleep(3000);
} catch(Exception e) {
}
}
}
}
public class Student
{
public static void main(String[] args)
{
A x=new A();
B y=new B();
C z=new C();
x.start();
y.start();
z.start();
}
}
OUTPUT:
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
finally
{
System.out.println("This is a finally block");
}
y=a/(b+c);
System.out.println("y="+y);
}
}
OUTPUT:
Practical 9: Write a Java program that displays the number of characters, lines and words in a
text file
import java.util.*;
import java.io.*;
class Student
{
public static void main(String args[])throws IOException
{
int nl=1,nw=0;
char ch;
Scanner scr=new Scanner(System.in);
System.out.print("\nEnter File name: ");
String str=scr.nextLine();
FileInputStream f=new FileInputStream(str);
int n=f.available();
for(int i=0;i<n;i++)
{
ch=(char)f.read();
if(ch=='\n')
nl++;
else if(ch==' ')
nw++;
}
System.out.println("\nNumber of lines : "+nl);
System.out.println("\nNumber of words : "+(nl+nw));
System.out.println("\nNumber of characters : "+n);
}
}
OUTPUT:
Practical 10: Write a Java Program to create Applet for timer
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.text.*;
public class DigitalClock extends Applet implements Runnable {
Thread t;
String Time="";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
Output:
// Importing database
importjava.sql.*;
// Importing required classes
importjava.util.*;
// Main class
class Main {
System.out.println("enter name");
String name = k.next();
System.out.println("enter class");
String cls = k.next();
// Registering drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver());
// Creating a statement
Statement st = con.createStatement();
// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println(
"inserted successfully : " + sql);
else
System.out.println("insertion failed");
// Closing the connections
con.close();
}
// Catch block to handle exceptions
catch (Exception ex) {
// Display message when exceptions occurs
System.err.println(ex);
}
}
}