Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Java File Final Yuvraj

The document contains 25 code snippets demonstrating various Java programming concepts like exception handling, inheritance, multithreading, and input/output streams. Each code snippet is accompanied by sample output. The concepts covered include try/catch blocks, exception types, inheritance using extends and super keywords, multitasking using Runnable interface and Thread class, and file I/O streams.

Uploaded by

uday vaidya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java File Final Yuvraj

The document contains 25 code snippets demonstrating various Java programming concepts like exception handling, inheritance, multithreading, and input/output streams. Each code snippet is accompanied by sample output. The concepts covered include try/catch blocks, exception types, inheritance using extends and super keywords, multitasking using Runnable interface and Thread class, and file I/O streams.

Uploaded by

uday vaidya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Page | 1

Index
s.no Pg no. Sign
Title
1 Acknowledgment 4
2 WAP to add two numbers 5

3 WAP to calculate area of circle 6


4 WAP to show that number is +ve or -ve (concept 7
of if-else )

5 WAP to find largest number from three(concept of 9-10


nested if)
6 WAP to make calculator using switch statement 11-12

7 WAP to print a table using for loop. 13


8 WAP to calculate area of rectangle with objects 14
9 WAP to show concept of Method Overloading at least 15
3 functions.
10 WAP to show concept of Method Overriding. 16
11 WAP to show concept of Method Overriding. 17
12 WAP to show concept of single Inheritance . 18-19
13 WAP to show concept of Multilevel Inheritance . 20-21
14 WAP to show concept of Hierarchical Inheritance 22-23
15 WAP to show the concept of constructor overloading 24-25
16 WAP to Implement abstract classes . 26-27
17 WAP to Implement Interfaces . 28
18 WAP to Implement Multiple Inheritance with 29
Interfaces
Page | 2

19 WAP to Concatenate Strings in JAVA 30


20 WAP to Show String Comrison in JAVA 31
21 WAP to Find Substring from a String in JAVA 32
22 WAP to Create and Import User Defined Package 33-35
23 Write a Java Program to to perform various operations 33-35
in MySQL database

24 WAP to show file reader and writer stream 36


25 WAP to show buffered input and output stream 37-38
Page |3

WAP to handle Arithmetic Exception


import java.util.Scanner;

public class ArithmeticExceptionExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the dividend: ");


int dividend = scanner.nextInt();

System.out.print("Enter the divisor: ");


int divisor = scanner.nextInt();

try {
int result = dividend / divisor;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}

scanner.close();
}
}

OUTPUT:

WAP to Handle Multiple Exception using Multiple Catch


Block
Page |4

public class MultipleCatchBlocksExample {


public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
int index = 4;
int result = numbers[index];
System.out.println("Result: " + result);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index out of bounds");
} catch (ArithmeticException e) {
System.out.println("Error: Arithmetic exception occurred");
} catch (Exception e) {
System.out.println("Error: An exception occurred");
}
}
}

OUTPUT:

WAP to Handle Multiple Exception using Throw Keyword


public class ThrowKeywordExample {
public static void main(String[] args) {
Page |5

try {
int age = -5;
validateAge(age);
System.out.println("Age is valid");
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}

public static void validateAge(int age) {


if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
}

OUTPUT:

WAP to Implement Super Keyword


class Animal {
String name;

Animal(String name) {
Page |6

this.name = name;
}

void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


String breed;

Dog(String name, String breed) {


super(name);
this.breed = breed;
}

void sound() {
super.sound(); // calling the sound() method of the parent class
System.out.println("Dog barks");
}

void display() {
System.out.println("Name: " + super.name); // accessing the name variable of the
parent class
System.out.println("Breed: " + breed);
}
}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog("Buddy", "Labrador");
myDog.sound();
myDog.display();
}
}
OUTPUT:
Page |7

WAP to show Multitasking using single thread


class Task1 implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 1 - Count: " + i);
}
}
}
Page |8

class Task2 implements Runnable {


public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 2 - Count: " + i);
}
}
}

class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new Task1());
Thread t2 = new Thread(new Task2());

t1.start();
t2.start();
}
}

OUTPUT:

WAP to show Multitasking using Multiple Threads


class Task1 extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 1 - Count: " + i);
}
}
}
Page |9

class Task2 extends Thread {


public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 2 - Count: " + i);
}
}
}

class Main {
public static void main(String[] args) {
Task1 t1 = new Task1();
Task2 t2 = new Task2();

t1.start();
t2.start();
}
}

OUTPUT:

You might also like