Java Lab Material
Java Lab Material
a. Write a JAVA program to display default value of all primitive data type of JAVA
Code :
class MainClass {
static byte a;
static short b;
static int c;
static long d;
static float e;
static double f;
static char g;
static boolean h;
Output :
b. Write a java program that display the roots of a quadratic equation ax2+bx=0. Calculate the discriminate
D and basing on value of D, describe the nature of root.
Code :
import java.util.Scanner;
class QuadraticEquation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter co-efficient 'a' :");
double a = sc.nextDouble();
System.out.println("Enter co-efficient 'b' :");
double b = sc.nextDouble();
System.out.println("Enter co-efficient 'c' :");
double c = sc.nextDouble();
double D = b * b - 4 * a * c;
if (D > 0) {
double root1 = (-b + Math.sqrt(D)) / (2 * a);
double root2 = (-b - Math.sqrt(D)) / (2 * a);
System.out.println("Root 1 :" + root1);
System.out.println("Root 2 :" + root2);
} else if (D == 0) {
double root = -b / (2 * a);
System.out.println("Root :" + root);
} else {
double realroot = -b / (2 * a);
double ip = Math.sqrt(-D) / (2 * a);
Output :
Exercise - 2
a. Write a JAVA program to search for an element in a given list of elements using binary search mechanism.
Code :
import java.util.Arrays;
import java.util.Scanner;
class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size :");
int size = sc.nextInt();
int[] arr = new int[size];
// storing array dynamically
for (int i = 0; i < arr.length; i++) {
System.out.println("Enter Any value at the index :" + i);
arr[i] = sc.nextInt();
}
if (result == -1) {
System.out.println("Element not found");
} else {
System.out.println("Element found at index :" + result);
}
}
if (arr[mid] == target) {
return mid;
}
Output :
b. Write a JAVA program to sort for an element in a given list of elements using bubble sort
Code :
import java.util.Arrays;
import java.util.Scanner;
class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size :");
int size = sc.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
System.out.println("Enter Value at the index :" + i);
arr[i] = sc.nextInt();
}
bubbleSort(arr);
}
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
Output :
Code :
class StringBufferClass {
public static void main(String[] args) {
StringBuffer s1=new StringBuffer("Hello world");
System.out.println("Actual String : "+s1);
//delete String at the index 0 to 5
s1.delete(0, 5);
System.out.println("deleted String at the index 0 to 5 :"+s1);
//delete character based on the index value
s1.deleteCharAt(2);
System.out.println("Deleting character at the index 2 :"+s1);
}
}
Output :
Exercise - 3
a. Write a JAVA program to implement class mechanism. Create a class, methods and invoke them inside
main method.
Code :
public class MainClass {
public static void main(String[] args) {
Demo.add();
Demo.sub(10,20);
System.out.println(Demo.mul());
}
}
class Demo {
//static method
public static void add() {
System.out.println("This is add method in Demo class...");
}
Output :
Output :
Output :
d. Write a JAVA program to implement constructor overloading.
Code :
public class DemoClass {
int a, b, c;
String str;
boolean k;
DemoClass(int a) {
this.a = a;
}
DemoClass(int b, int c) {
this.b = b;
this.c = c;
}
DemoClass(String str) {
this.str = str;
}
protected DemoClass(String str, boolean k) {
this.str = str;
this.k = k;
}
public static void main(String[] args) {
DemoClass d1=new DemoClass(100);
DemoClass d2=new DemoClass(200,300);
DemoClass d3=new DemoClass("hello java");
DemoClass d4=new DemoClass("hey raju",true);
System.out.println(d1.a);
System.out.println(d2.b+" , "+ d2.c);
System.out.println(d3.str);
System.out.println(d4.str+" , "+d4.k);
}
}
Output :
Exercise - 4
a. Write a JAVA program to implement Single Inheritance
Code :
class Animal{
public void eat() {
System.out.println("Animal eating food....");
}
}
Output :
Code :
class GrandParentClass{
public void land1() {
System.out.println("grand parent earn 4 acers land");
}
}
class ParentClass extends GrandParentClass{
public void land2() {
System.out.println("parent earn 2 acers land");
}
}
class ChildClass extends ParentClass{
public void land3() {
System.out.println("Child earn 1 acers land....");
}
}
Output :
c. Write a JAVA program for abstract class to find areas of different shapes
Code :
abstract class Shape {
abstract double calculateArea();
void displayArea() {
System.out.println("Area :" + calculateArea());
}
}
//Area of circle
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
double calculateArea() {
double area = 3.14 * radius * radius;
return area;
}
}
//Area of rectangle
class Rectangle extends Shape {
double length;
double width;
double calculateArea() {
double area = length * width;
return area;
}
}
//Area of Triangle
class Triangle extends Shape {
double base;
double height;
double calculateArea() {
double area = 0.5 * base * height;
return area;
}
}
class AbstractClass {
public static void main(String[] args) {
Circle c1 = new Circle(10);
System.out.println("*****Circle******");
c1.displayArea();
Rectangle r1 = new Rectangle(10, 20);
System.out.println("******Rectangle******");
r1.displayArea();
Triangle t1 = new Triangle(5, 10);
System.out.println("******Triangle******");
t1.displayArea();
}
}
Output :
Exercise - 5
a. Write a JAVA program give example for “super” keyword.
Code :
//main class
public class MainClass {
public static void main(String[] args) {
WhatsAppVersionNew v1 = new WhatsAppVersionNew();
v1.features();
System.out.println("Version : "+v1.version);
}
}
Output :
b. Write a JAVA program to implement Interface. What kind of Inheritance can be achieved?
Code :
//define the first interface
interface Animal{
void sound() ;
void eat();
}
Code :
package method_overriding;
class YouTube{
public void watch() {
System.out.println("User watching something in youtube");
}
}
class Studies extends YouTube{
public void watch() {
System.out.println("User accessing Studies....");
}
}
class Movies extends YouTube{
public void watch() {
System.out.println("User accessing Movies....");
}
}
class Technology extends YouTube{
public void watch() {
System.out.println("User can access technology...");
}
}
class User{
public static void access(YouTube a) {
a.watch();
}
}
}
Output :
Exercise - 6
a. Write a JAVA program that describes exception handling mechanism
Code :
public class MainClass {
public static void main(String[] args) {
try {
int a=10/0;
System.out.println(a);
}catch(ArithmeticException e) {
System.out.println("Arithmetic Exception Handled....");
}finally {
System.out.println("Exception completed....");
}
}
}
Output :
Code :
package exception;
import java.util.Scanner;
public class MainClass1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter numerator :");
int a=sc.nextInt();
System.out.println("Enter denominator :");
int b=sc.nextInt();
try {
int result =a/b;
System.out.println("The division of a and b is :"+result);
System.out.println(str.charAt(ch));
}
catch(ArithmeticException e) {
System.out.println("caught Arithmetic Exception");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("caught Array Index Out Of Bounds Exception ");
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("caught String Index Out Of Bounds Exception");
}finally {
System.out.println("Exception completed ...");
}
}
Output :
Case 1 :
Case 2:
Case 3:
c. Write a JAVA program for creation of Java Built-in Exceptions
Code :
import java.util.Scanner;
class Display {
public static void main(String[] args) {
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception handled");
Output :
Case 1:
Case 2:
Case 3 :
Code :
import java.util.Scanner;
class AgeNotFoundException extends Exception {
String msg;
AgeNotFoundException(String msg) {
this.msg = msg;
}
}
class AgeVerification {
public static void verification() throws AgeNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your Age :");
int age = sc.nextInt();
if (age > 18) {
System.out.println("Your eligible to apply voter ID ....please apply ");
} else {
throw new AgeNotFoundException("Your not eligible to apply voter ID is
please wait still "+(18-age)+" years") ;
}
}
}
Output :
Case 1:
Case 2:
Exercise - 7
a. Write a JAVA program that creates threads by extending Thread class.First thread display “Good
Morning “every 1 sec, the second thread displays “Hello “every 2 seconds and the third display
“Welcome” every 3 seconds,(Repeat the same by implementing Runnable)
Code :
Output :
Code :
}
}
Code :
System.out.println("Starting treads....");
t1.start();
t2.start();
try {
// wait for thread 1 to finish
t1.join();
System.out.println("Thread 1 has finished execution");
}
}
Output :
Code :
m1.start();
m2.start();
}
}
Output :