0% found this document useful (0 votes)
20 views32 pages

Java Twork

The document contains 10 programming problems and their solutions in Java. Some key examples include: 1. Creating an Electricity class that calculates electricity bills based on usage units. 2. Creating an Account class with methods to deposit, withdraw, and display account details. 3. Creating a Student class that initializes attributes like name and roll number using a constructor. 4. Printing the Fibonacci series using a command line argument for number of terms. 5. Creating classes to calculate simple interest and total amount using constructor overloading.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
0% found this document useful (0 votes)
20 views32 pages

Java Twork

The document contains 10 programming problems and their solutions in Java. Some key examples include: 1. Creating an Electricity class that calculates electricity bills based on usage units. 2. Creating an Account class with methods to deposit, withdraw, and display account details. 3. Creating a Student class that initializes attributes like name and roll number using a constructor. 4. Printing the Fibonacci series using a command line argument for number of terms. 5. Creating classes to calculate simple interest and total amount using constructor overloading.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1/ 32

1- WAJP to create class Electricity and calculate the bill based on the following conditions

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");

Scanner sc=new Scanner(System.in);

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)

bill= (100*1.20)+(unit -100*2)+(unit-200+2);

System.out.println("Bill="+bill);

public class Electricity{

public static void main(String[] args) {

Amp a=new Amp();

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()

System.out.println("Enter your Account no....");

Scanner sc=new Scanner(System.in);

accno=sc.nextInt();

if(accno==1212)

System.out.println("Enter your name");

name=sc.next();

System.out.println("Your accont balance is "+balance);

else{

System.out.println("Invalid pin! retry again");

int widraw()

Scanner sc=new Scanner(System.in);

System.out.println("enter your widraw ammount");

ammount=sc.nextInt();

if(ammount<balance)

balance=balance-ammount;
return balance;

else{

System.out.println("Insufficient Balance");

return 0;

int diposit()

Scanner sc=new Scanner(System.in);

System.out.println("enter your diposit ammount");

ammount=sc.nextInt();

balance=balance+ammount;

return balance;

void display()

System.out.println("your Ac no is "+accno);

System.out.println("your name is "+name);

System.out.println("your Account balance is "+balance);

public static void main(String[] args) {

Account d=new Account();

d.getData();

System.out.println("Enter your choice");

System.out.println("1-widraw");
System.out.println("2-diposit");

System.out.println("3-display");

int a;

Scanner sc =new Scanner(System.in);

a=sc.nextInt();

switch(a){

case 1:

int rem=d.widraw();

System.out.println("Now your current balwnce is "+rem);

break;

case 2:

int rem=d.diposit();

System.out.println("Now your current balwnce is "+rem);

break;

case 3:

d.display();

break;

default:
{

System.out.println("Invalid choice");

Output-

Enter your Account no....

1212

Enter your name

Gaurav singh

Your accont balance is 1000

Enter your choice

1-widraw

2-diposit

3-display

enter your widraw ammount

200

Now your current balwnce is 800

3- WAJP to create a DEFAULT CONSTRUCTOR student[rollno., age, name, caurse] initialize


these variable using Constructor and print the details of it.

import java.util.*;

class Student {

Student()
{

int roll_no;

int age ;

String name;

String course;

Scanner sc =new Scanner(System.in);

System.out.println("Enter your Roll_no");

roll_no=sc.nextInt();

System.out.println("Enter your age");

age=sc.nextInt();

System.out.println("Enter your name");

name=sc.next();

System.out.println("Enter your caurse name");

course=sc.next();

System.out.println("Your roll_no. is "+roll_no);

System.out.println("Your age is "+age);

System.out.println("Your name is "+name);

System.out.println("Your caurse name is "+course);

public static void main(String[] args) {

new Student();

Output-

Enter your Roll_no

58
Enter your age

18

Enter your name

Gaurav

Enter your caurse name

BCA

Your roll_no. is 58

Your age is 18

Your name is Gaurav

Your caurse name is BCA

4- WAJP to print the fibonacci series using command line argument.

public class four{

public static void main(String args[])

int a=0,b=1,c=0;

int n=Integer.parseInt(args[0]);

System.out.println("Fibonacci series is "+n);

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;

Rectangle(int length,int breadth)

this.length=length;

this.breadth=breadth;

int area;

area=length*breadth;

System.out.println("Area of rectangle is "+area);

public static void main(String[] args) {

Rectange r=new Rectangle(2, 4);

Output-

Area of rectangle is 8
6- WAJP to calculate simple intrest and total amount using cinstructor overloading.

class s1 {

s1(int p,int r, int t)

double si=(p*r*t)/100;

System.out.println("SIMPLE INTREST IS "+si);

System.out.println("Total amount is "+(si+p));

class s2 extends s1

s2(int p,int r,int t)

super(p,r,t);

class simpleI

public static void main(String[] args) {

s2 s=new s2(5000,15,1);

Output-

SIMPLE INTREST IS 750.0

Total amount is 5750.0


7- WAJP to show the use of copy constructor Student class.

public class Student{

private String choclateName;

private int price;

Student(String name,int price)

choclateName=name;

this.price=price;

Student(Student c)

choclateName=c.choclateName;

price=c.price;

int showP()

return price;

String showN()

return choclateName;

public static void main(String[] args) {

Student f1 = new Student("DairyMilk", 180);

System.out.println("Student favourite Chocolate name : "+


f1.showN());

System.out.println("Price "+ f1.showP());


Student f2 = new Student(f1);

System.out.println("Student favourite Chocolate name "+


f2.showN());

System.out.println("Price "+ f2.showP());

Output-

Student favourite Chocolate name : DairyMilk

Price 180

Student favourite Chocolate name DairyMilk

Price 180

8- WAJP to count the no. of object using static method.

class Count {

static int count=0;

Count()

count++;

public static void main(String[] args) {

Count c=new Count();

Count c1=new Count();

Count c2=new Count();

Count c3=new Count();

Count c4=new Count();

Count c5=new Count();

Count c6=new Count();


Count c7=new Count();

Count c8=new Count();

Count c9=new Count();

Count c10=new Count();

System.out.println("NO of obj "+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 ;

static String college_name;

void getDetails()

Scanner sc=new Scanner(System.in);

System.out.println("Enter roll no");

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);

public static void main(String[] args) {

student2 s=new student2();

s.getDetails();

s.putDetails();

System.out.println(student2.college_name="Graphic Era Hill


University");

Output-

Enter roll no

12

Enter name

Gaurav

roll no 58

Name Gaurav

Graphic era hill university

10- WAJP to find the area of rectangle, triangle, square by using function overloading.

import java.util.*;

class Area {

void area(int base,int hight)

double Area=0.5*base*hight;

System.out.println("Area of Triangle= "+Area);


}

void area(int l,int b,int h)

int Area=l*b*h;

System.out.println("Area of rectange= "+Area);

void area(int r)

System.out.println("Area of square is= "+3.14*r*r);

public static void main(String[] args) {

Area a=new Area();

a.area(4);

a.area(6, 4);

a.area(2, 4, 6);

Output-

Area of square is= 50.24

Area of Triangle= 12.0

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 class Shape

{
abstract void area();

class Triangle extends Shape{

public void area()

int base=12;

int hight=6;

double Area=0.5*base*hight;

System.out.println("Area of Triangle= "+Area);

class rectangle extends Shape{

public void area()

int l=2;

int b=2;

int h=2;

int Area=l*b*h;

System.out.println("Area of rectangle= "+Area);

class circle extends Shape{

public void area()

int r=2;

double Area=r*r;
System.out.println("Area of square is= "+Area);

public class abs {

public static void main(String[] args) {

Shape n=new circle();

n.area();

Shape n1=new rectangle();

n1.area();

Shape n2=new Triangle();

n2.area();

Output-

Area of square is= 4.0

Area of rectangle= 8

Area of Triangle= 36.0

13- WAJP to create classs circle with the final instance variable AS PI and radius define two
methods circumfrence() and area().

class circle {

final double PI=3.14;

int radius;

void circumfrence(int radius)

System.out.println("Circumfrence of Circle is "+2*PI*radius);

}
void area(int radius)

System.out.println("Area of circle is "+PI*(radius*radius));

public static void main(String[] args) {

circle c=new circle();

c.circumfrence(4);

c.area(5);

Output-

Circumfrence of Circle is 25.12

Area of circle is 78.5

14- WAJP to find the minimum and maximum element in array.

import java.util.*;

class minmax {

public static void main(String[] args)

int arr[]=new int[10];

Scanner sc=new Scanner(System.in);

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

15- WAJP to search a element in array.

import java.util.*;

class search {

public static void main(String[] args) {

int arr[]=new int[10];

int search;

System.out.println("Enter array element");

Scanner sc=new Scanner(System.in);

for(int i=0;i<10;i++)

arr[i]=sc.nextInt();

System.out.println("Enter serach element");

search=sc.nextInt();

for(int i=0;i<10;i++)

if(arr[i]==search)

System.out.println(search +" is found at "+(i+1) +"


position");

}
}

Output-

Enter array element

21

23

345

543

65

68

243

35

63

353

Enter serach element

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{

abstract void area();

class Triangle implements Shape{

public void area()

{
int base=12;

int hight=6;

double Area=0.5*base*hight;

System.out.println("Area of Triangle= "+Area);

class rectangle implements Shape{

public void area()

int l=2;

int b=2;

int h=2;

int Area=l*b*h;

System.out.println("Area of rectangle= "+Area);

class circle implements Shape{

public void area()

int r=2;

double Area=3.14*r*r;

System.out.println("Area of circle is= "+Area);

public class interfacee {

public static void main(String[] args) {


Shape n=new circle();

n.area();

Shape n1=new rectangle();

n1.area();

Shape n2=new Triangle();

n2.area();

Output-

Area of circle is= 12.56

Area of rectangle= 8

Area of Triangle= 36.0

17- WAJP to create class thread and demonstrate its working.

class Threadss extends Thread {

public void run()

for(int i=0;i<10;i++)

System.out.println("elements are "+i);

System.out.println("thread A ended");

class Threads2 extends Thread{

public void run()


{

for(int i=0;i<10;i++)

System.out.println("elements are "+i);

System.out.println("thread B ended");

class Threads{

public static void main(String[] args)

Threadss t=new Threadss();

Threads2 t2=new Threads2();

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

18- WAJP to implements the methods in the the THREAD class.

import java.lang.*;

class M extends Thread

public void run()

System.out.println("inside run");

class METHODS
{

public static void main(String argvs[])

M t1 = new M();

M t2 = new M();

System.out.println("Priority of the thread t1 is " + t1.getPriority());

System.out.println("Priority of the thread t2 is " + t2.getPriority());

t1.setPriority(7);

t2.setPriority(8);

System.out.println("Priority of the thread t1 is " + t1.getPriority());

System.out.println("Priority of the thread t2 is " + t2.getPriority());

System.out.println("Currently Thread name is " +


Thread.currentThread().getName());

System.out.println("main thread priority " +


Thread.currentThread().getPriority());

Thread.currentThread().setPriority(10);

System.out.println("main thread Priority " +


Thread.currentThread().getPriority());

Output-

Priority of the thread t1 is 5

Priority of the thread t2 is 5

Priority of the thread t3 is 5

Priority of the thread t1 is 7

Priority of the thread t2 is 8

Priority of the thread t3 is 9


Currently Thread name is main

main thread Priority 5

main thread Priority 10

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())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getSt


ring(3));
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
Output :

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 :

Record inserted successfully


Rey
Emlary: 50000.0
22. Write a program to implement bubble sort.
class BubbleSort {
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Original Array:");
printArray(array);
bubbleSort(array);
System.out.println("\nSorted Array:");
printArray(array);
}
static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
static void printArray(int[] arr) {
for (int value : arr) {
System.out.print(value + " ");
}
System.out.println();
}
}
Output :

original Array
55 34 56 78 90

sorted array
34 55 56 78 90

23. Write a program to demonstrate the use of executequery() methods.


import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.cj.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())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getSt


ring(3));
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
Output :

1 XYZ
112 Varsha Pandey

You might also like