Java Twork
Java Twork
if unit<100 bill=1.20*unit
unit<200 bill=(100*1.20)+(unit-100*2)
unit<300 bill=(100*1.20)+(unit-100*2)+(unit-200=3)
import java.util.*;
class Amp{
int unit;
double bill;
void get_bill()
System.out.println("enter Unit");
unit =sc.nextInt();
void reading()
if(unit<100)
bill=(1.20)*unit;
System.out.println("Bill="+bill);
else if(unit<200)
bill=(100*1.20)+(unit-100*2);
System.out.println("Bill="+bill);
}
else if(unit <300)
System.out.println("Bill="+bill);
a.get_bill();
a.reading();
Output-
enter Unit
298
Bill=318.0
2- WAJP to create a class account with the instance variables accno, name, balance , ammount
type . Define four method getData(), widraw(), deposit().
import java.util.*;
class Account
int ammount;
int accno;
String name;
int balance=1000;
void getData()
accno=sc.nextInt();
if(accno==1212)
name=sc.next();
else{
int widraw()
ammount=sc.nextInt();
if(ammount<balance)
balance=balance-ammount;
return balance;
else{
System.out.println("Insufficient Balance");
return 0;
int diposit()
ammount=sc.nextInt();
balance=balance+ammount;
return balance;
void display()
System.out.println("your Ac no is "+accno);
d.getData();
System.out.println("1-widraw");
System.out.println("2-diposit");
System.out.println("3-display");
int a;
a=sc.nextInt();
switch(a){
case 1:
int rem=d.widraw();
break;
case 2:
int rem=d.diposit();
break;
case 3:
d.display();
break;
default:
{
System.out.println("Invalid choice");
Output-
1212
Gaurav singh
1-widraw
2-diposit
3-display
200
import java.util.*;
class Student {
Student()
{
int roll_no;
int age ;
String name;
String course;
roll_no=sc.nextInt();
age=sc.nextInt();
name=sc.next();
course=sc.next();
new Student();
Output-
58
Enter your age
18
Gaurav
BCA
Your roll_no. is 58
Your age is 18
int a=0,b=1,c=0;
int n=Integer.parseInt(args[0]);
System.out.print(a+" "+b);
for(int i=1;i<n-1;i++)
c=a+b;
a=b;
b=c;
System.out.print(" "+c);
}
}
Output-
Fibonacci series is 1 1 2 3 5
5- WAJP to create class RECTANGLE define parameterized constructer and calculate the area
of rectangle and display them.
import java.util.*;
class Rectangle {
int length;
int breadth;
this.length=length;
this.breadth=breadth;
int area;
area=length*breadth;
Output-
Area of rectangle is 8
6- WAJP to calculate simple intrest and total amount using cinstructor overloading.
class s1 {
double si=(p*r*t)/100;
class s2 extends s1
super(p,r,t);
class simpleI
s2 s=new s2(5000,15,1);
Output-
choclateName=name;
this.price=price;
Student(Student c)
choclateName=c.choclateName;
price=c.price;
int showP()
return price;
String showN()
return choclateName;
Output-
Price 180
Price 180
class Count {
Count()
count++;
Output-
NO of obj 11
9- WAJP to create a class student with field roll_no, name, college name .college name as a static
data member.
import java.util.*;
class student2 {
int roll_no;
String name ;
void getDetails()
roll_no=sc.nextInt();
System.out.println("Enter name");
name =sc.next();
void putDetails()
{
System.out.println("Your roll no "+roll_no);
System.out.println("Name is "+name);
s.getDetails();
s.putDetails();
Output-
Enter roll no
12
Enter name
Gaurav
roll no 58
Name Gaurav
10- WAJP to find the area of rectangle, triangle, square by using function overloading.
import java.util.*;
class Area {
double Area=0.5*base*hight;
int Area=l*b*h;
void area(int r)
a.area(4);
a.area(6, 4);
a.area(2, 4, 6);
Output-
Area of rectange= 48
12- WAJP to compute the area of rectangle, triangle, square using the concept of abstract class
method and overriding. create a class shape and extends class triangle, square, rectangle will
extends the shape class
{
abstract void area();
int base=12;
int hight=6;
double Area=0.5*base*hight;
int l=2;
int b=2;
int h=2;
int Area=l*b*h;
int r=2;
double Area=r*r;
System.out.println("Area of square is= "+Area);
n.area();
n1.area();
n2.area();
Output-
Area of rectangle= 8
13- WAJP to create classs circle with the final instance variable AS PI and radius define two
methods circumfrence() and area().
class circle {
int radius;
}
void area(int radius)
c.circumfrence(4);
c.area(5);
Output-
import java.util.*;
class minmax {
for(int i=0;i<10;i++)
arr[i]=sc.nextInt();
}
int min=arr[0];
int max=arr[0];
for(int j=0;j<10;j++)
if(arr[j]>max)
max=arr[j];
if(arr[j]<min)
min=arr[j];
System.out.println("min= "+min);
System.out.println("max= "+max);
Output-
12
212
34
56
23
11
7
67
45
min= 2
max= 212
import java.util.*;
class search {
int search;
for(int i=0;i<10;i++)
arr[i]=sc.nextInt();
search=sc.nextInt();
for(int i=0;i<10;i++)
if(arr[i]==search)
}
}
Output-
21
23
345
543
65
68
243
35
63
353
35
35 is found at 8 position
16- WAJP to create interface shape with abstract method area(),and find the area of circle ,
rectangle, triangle, and override the area() method.
interface Shape{
{
int base=12;
int hight=6;
double Area=0.5*base*hight;
int l=2;
int b=2;
int h=2;
int Area=l*b*h;
int r=2;
double Area=3.14*r*r;
n.area();
n1.area();
n2.area();
Output-
Area of rectangle= 8
for(int i=0;i<10;i++)
System.out.println("thread A ended");
for(int i=0;i<10;i++)
System.out.println("thread B ended");
class Threads{
t.start();
t2.start();
Output-
elements are 0
elements are 1
elements are 2
elements are 0
elements are 1
elements are 2
elements are 3
elements are 4
elements are 5
elements are 6
elements are 7
elements are 3
elements are 8
elements are 9
thread B ended
elements are 4
elements are 5
elements are 6
elements are 7
elements are 8
elements are 9
thread A ended
import java.lang.*;
System.out.println("inside run");
class METHODS
{
M t1 = new M();
M t2 = new M();
t1.setPriority(7);
t2.setPriority(8);
Thread.currentThread().setPriority(10);
Output-
19. Write a program to establish connection with the database using JDBC.
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection( "jdbc:mysql:/
/localhost:3306/xyz","root","123");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
58 XYZ
112 Varsha Pandey
20. Write a program to retrieve records from student table using JDBC create a
database in mysql with the table student and fields as rollno, name, age and college.
import java.sql.*;
class RetrieveStudentRecords {
String JDBC_URL = "jdbc:mysql://localhost:3306/xyz";
String USER = "root";
String PASSWORD = "123";
String SELECT_QUERY = "SELECT rollno, name, age, college
FROM student";
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
PreparedStatement preparedStatement =
connection.prepareStatement(SELECT_QUERY);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int rollNo = resultSet.getInt("rollno");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String college = resultSet.getString("college");
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("College: " + college);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output :
Roll no- 58
name- xyz
age - 18
college -GEHU HLD
21. Write a program to create a database emp with the table employee attributes
emp_id, salary, designation. Establish a connection with the database and retrieve the
records.
import java.sql.*;
class EmployeeDatabaseExample {
String JDBC_URL = "jdbc:mysql://localhost:3306/emp";
String USER = "root";
String PASSWORD = "123";
String CREATE_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS
employee (emp_id INT PRIMARY KEY, salary DOUBLE, designation
VARCHAR(255))";
String INSERT_RECORD_QUERY = "INSERT INTO employee
(emp_id, salary, designation) VALUES (?, ?, ?)";
String SELECT_RECORDS_QUERY = "SELECT * FROM employee";
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
Statement statement = connection.createStatement();
statement.executeUpdate(CREATE_TABLE_QUERY);
insertRecords(connection);
retrieveRecords(connection);
}
catch (Exception e) {
e.printStackTrace();
}
}
static void insertRecords(Connection connection) throws
SQLException {
PreparedStatement preparedStatement =
connection.prepareStatement(INSERT_RECORD_QUERY);
preparedStatement.setInt(1, 101);
preparedStatement.setDouble(2, 50000.0);
preparedStatement.setString(3, "Software Engineer");
preparedStatement.executeUpdate();
preparedStatement.setInt(1, 102);
preparedStatement.setDouble(2, 60000.0);
preparedStatement.setString(3, "Senior Software
Engineer");
preparedStatement.executeUpdate();
preparedStatement.setInt(1, 103);
preparedStatement.setDouble(2, 70000.0);
preparedStatement.setString(3, "Team Lead");
preparedStatement.executeUpdate();
System.out.println("Records inserted successfully.");
}
static void retrieveRecords(Connection connection) throws
SQLException {
Statement statement = connection.createStatement();
ResultSet resultSet =
statement.executeQuery(SELECT_RECORDS_QUERY);
while (resultSet.next()) {
int empId = resultSet.getInt("emp_id");
double salary = resultSet.getDouble("salary");
String designation = resultSet.getString("designation");
System.out.println("Emp ID: " + empId);
System.out.println("Salary: " + salary);
System.out.println("Designation: " + designation);
}
}
}
Output :
original Array
55 34 56 78 90
sorted array
34 55 56 78 90
1 XYZ
112 Varsha Pandey