Java Programming Lab
Java Programming Lab
LIST OF EXPERIMENTS :
1) JAVA BASICS
2) ARRAYS
3) STRINGS
4) OVERLOADING & OVERRIDING
5) INHERITANCES
6) INTERFACES
7) EXCEPTION HANDLING
8) I/O STREAMS
9) MULTI THREADING
10) GENERICS
11) COLLECTIONS
12) CONNECTING TO DATABASE
WEEK – 1 : JAVA BASICS
a. Write a java program that prints all real solutions to the quadratic equation
ax2+bx+c=0. Read in a, b, c and use
the quadratic formula.
b. The Fibonacci sequence is defined by the following rule. The first two values
in the sequence are 1 and 1. Every
subsequent value is the sum of the two values preceding it. Write a java
program that uses both recursive and
non-recursive functions.
WEEK – 2 : ARRAYS
a. Write a java program to sort given list of integers in ascending order.
b. Write a java program to multiply two given matrices.
WEEK – 3: STRINGS
a. Write a java program to check whether a given string is palindrome.
b. Write a java program for sorting a given list of names in ascending order.
WEEK – 5: INHERITANCES
Write a java program to create an abstract class named Shape that contains
two integers and an empty method named
print Area (). Provide three classes named Rectangle, Triangle and Circle such
that each one of the classes extends
the class Shape. Each one of the classes contains only the method print Area ()
that prints the area of the given shape.
WEEK – 6 INTERFACES
a. Write a program to create interface A in this interface we have two method
meth1 and meth2. Implements this
interface in another class named My Class.
b. Write a program to give example for multiple inheritances in Java.
WEEK – 7 EXCEPTION HANDLING
Write a program that reads two numbers Num1 and Num2. If Num1 and Num2
were not integers, theprogram
would throw a Number Format Exception. If Num2 were zero, the program
would throw an Arithmetic Exception
Display the exception.
WEEK – 10 GENERICS
a. Write a Java program to swap two different types of data using Generics.
b. Write a Java program to find maximum and minimum of two different types
of data usingGenerics.
WEEK – 11 COLLECTIONS
Create a linked list of elements.
a. Delete a given element from the above list.
b. Display the contents of the list after deletion.
a. Write a java program that prints all real solutions to the quadratic equation
ax2+bx+c=0. Read in a, b, c and use the quadratic formula.
b. The Fibonacci sequence is defined by the following rule. The first two values
in the sequence are 1 and 1. Every subsequent value is the sum of the two
values preceding it. Write a java program that uses both recursive and
non-recursive functions.
a. QuadraticEquation
1. import java.util.Scanner;
3. {
5. {
8. double a = input.nextDouble();
10.double b = input.nextDouble();
12.double c = input.nextDouble();
13.double d= b * b - 4.0 * a * c;
15.{
16.double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
19.}
20.else if (d == 0.0)
21.{
24.}
25.else
26.{
29.}
30.}
Output 1:
Output 2:
b. non recursive
import java.util.Scanner;
class Fib {
int i,a=1,b=1,c=0,n;
n=input.nextInt();
System.out.print(a);
System.out.print(" "+b);
for(i=0;i<n-2;i++) {
c=a+b;
a=b;
b=c;
System.out.print(" "+c);
System.out.println();
Output
Recursive Solution
import java.io.*;
import java.lang.*;
class Fib1 {
int fib(int n) {
if(n==1)
return (1);
else if(n==2)
return (1);
else
return (fib(n-1)+fib(n-2));
}
class FibR {
public static void main(String args[])throws IOException {
int n=Integer.parseInt(br.readLine());
int res=0;
for(int i=1;i<=n;i++) {
res=ob.fib(i);
System.out.print(" "+res);
System.out.println();
Output
WEEK – 2 ARRAYS
sorting of arrays
1. public class SortAsc {
3. //Initialize array
5. int temp = 0;
9. }
17. }
18. }
19. }
20.
21. System.out.println();
26. }
27. }
28.}
Output:
52871
12578
c. Multiply matrix
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
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];
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
}}
Output:
666
12 12 12
18818
WEEK – 3 STRINGS
b. Write a java program for sorting a given list of names in ascending order.
a. Palindrome Program in Java
1. import java.util.*;
2. class PalindromeExample2
3. {
5. {
9. original = in.nextLine();
13. if (original.equals(reverse))
15. else
17. }
18.}
Output :
class GFG {
// storing input in
variable int n = 4;
// swapping
temp = names[i];
names[i] = names[j];
names[j] = temp;
// print output
array
System.out.println(
System.out.println(names[i]);
}
Gourav
Rahul
Riya
WEEK – 4 OVERLOADING & OVERRIDING
a. Write a java program to implement method overloading and constructors
overloading.
a.
method overloading
class MethodOverloading {
private static void display(int a){
display(1);
display(1, 4);
Output:
Arguments: 1
Arguments: 1 and 4
Constructor overloading
3. int id;
4. String name;
5.
6. Student(){
8. }
9.
11. id = i;
12. name = n;
13.}
14.
20.
24.}
25.}
Output:
Student Id : 0
Student Id : 10
b.
3. class Vehicle{
4. //defining a method
11.
16.}
Output:
WEEK – 5 INHERITANCES
Write a java program to create an abstract class named Shape that contains
two integers and an empty method named print Area (). Provide three classes
named Rectangle, Triangle and Circle such that each one of the classes extends
the class Shape. Each one of the classes contains only the method print Area ()
that prints the area of the given shape.
import java.util.*;
abstract class shape
{
int x,y;
}
class Circle extends shape
{
void area(double x,double y)
{
System.out.println("Area of circle is :"+(3.14*x*x));
}
}
class Triangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of triangle is :"+(0.5*x*y));
}
}
public class AbstactDDemo
{
public static void main(String[] args)
{
Rectangle r=new Rectangle();
r.area(2,5);
Circle c=new Circle();
c.area(5,5);
Triangle t=new Triangle();
t.area(2,5);
}
Output:
Area of rectangle is :10.0
WEEK – 6 INTERFACES
a.
interface A
void meth1();
void meth2();
}
{
void meth3();
}
// This class must implement all of A and B
class MyClass implements B
System.out.println(“Implement meth1().”);
class IFExtend
ob.meth1();
ob.meth2();
ob.meth3();
}
output:
method 1
method 2
method 3
b.
interface AnimalEat {
void eat();
interface AnimalTravel {
void travel();
}
class Animal implements AnimalEat, AnimalTravel {
System.out.println("Animal is eating");
System.out.println("Animal is travelling");
a.eat();
a.travel();
Output
Animal is eating
Animal is travelling
import java.util.Scanner;
public class Main {
try {
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
Output :
Eneter num1: 10
Enter num2:5
Result:2
Eneter num1:1
Enter num1 :1
a. Write a java program that reads a file name from the user, and then displays
information about whether the file exists, whether the file is readable,
whether the file is writable, the type of file and the length of the file in bytes.
b. Write a java program that displays the number of characters, lines and
words in a text file.
a.
import java.io.*;
import java.util.*;
class AboutFile{
if(f.exists())
if(f.canRead())
Output:
WEEK – 9 MULTI THREADING
Write a java program that implements a multi-thread application that has three
threads. First thread generates
random integer every 1 second and if the value is even, second thread
computes the square of the number and
prints. If the value is odd, the third thread will print the value of cube of the
number.
import java.util.*;
#output:
a. Write a Java program to swap two different types of data using Generics.
b. Write a Java program to find maximum and minimum of two different types
of data usingGenerics.
a.#program:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class Generics {
static final <T> void swap (T[] a, int i, int j) {
T t = a[i];
a[i] = a[j];
a[j] = t;
}
}
#output:
a:[Cse, Hits ]
l:[Hits , Cse]
b.#program:
import java.util.ArrayList;
import java.util.Collections;
class NewList {
public static void main(String args[]) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
list.add(35);
System.out.println("List: " + list);
System.out.println("Minimum value of list is: "+Collections.min(list));
System.out.println("Maximum value of list is" +Collections.max(list));
}
}
#output:
WEEK – 11 COLLECTIONS
a&b.#program:
import java.io.*;
import java.util.LinkedList;
class List{
public static void main(String args[]){
LinkedList<Integer> list=new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println(list);
list.remove(2);
System.out.println(list);
}
}
#Output:
[1, 2, 3, 4]
[1, 2, 4]
Write a java program that connects to a database using JDBC and does add,
delete, modify and retrieve operations.
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Statement;
#output:
Connection Created
Statement created
data inserted
+--------+----------+
| rollno | name |
+--------+----------+
| 701 | bhanu |
| 702 | Priyanka |
+--------+----------+
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Statement;
#output:
Connection Created
Statement created
data deleted
+--------+-------+
| rollno | name |
+--------+-------+
| 701 | bhanu |
+--------+-------+
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Statement;
#output:
Connection Created
Statement created
data Updated
+--------+-------------------+
| rollno | name |
+--------+-------------------+
| 701 | BhanuPrasad |
+--------+-------------------+
import java.sql.*;
class Dbconnect
{
public static void main(String args[])
{
try
{
// class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
Connection
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/labman","ro
ot","mysql");
System.out.println("connected");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from employee");
while(rs.next())
{
System.out.println("empno="+rs.getString(1)+"
ename="+rs.getString(2)+"job="+rs.getString(3)+"mgr="+rs.getString(4)+"sal="
+rs.getString(5));
}
}
catch(SQLException s)
{
System.out.println(s);
}
}
}
#output:
connected
empno=101 ename=abhi job=manager mgr=1234 sal=10060
empno=102 ename=rohith job=analyst mgr=2345 sal=10060
empno=103 ename=david job=trainee mgr=3456 sal=9000
empno=104 ename=sruthi job=HRmgr=5678 sal=35000