[Go to site: main page, start]

0% found this document useful (0 votes)
4 views42 pages

Java Assignment

The document is an index of programming exercises and their corresponding Java code solutions, covering various topics such as prime numbers, factorials, array manipulations, and string operations. Each exercise includes a brief description, code implementation, and example output. The exercises are designed to help learners practice and understand fundamental programming concepts in Java.

Uploaded by

newone0123no
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views42 pages

Java Assignment

The document is an index of programming exercises and their corresponding Java code solutions, covering various topics such as prime numbers, factorials, array manipulations, and string operations. Each exercise includes a brief description, code implementation, and example output. The exercises are designed to help learners practice and understand fundamental programming concepts in Java.

Uploaded by

newone0123no
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

Page
S. No Question / Program
No
1 Enter a number from keyboard and check whether it is prime or not 3
2 Write a program to find prime numbers in a given range 4
3 Write a program to find factorial of a given number 5
4 Write a program to find sum of all the digits of a given number 6
5 Write a program to print a 3x5 star pattern 7
Write a program to convert temperature from Fahrenheit to Celsius using
6 8
C = (F - 32) / 1.8
Write a program to find numbers and sum of all integers >100 and <200
7 9
divisible by 7
8 Write a program to find the reverse of a given number 10
Write a program to find whether a given number is Armstrong number or
9 11
not
10 Write a program to print all the Armstrong numbers in a given range 12
11 Write a program to print Fibonacci series for a given number 13
12 Java Program to copy all elements of one array into another array 14
13 Java Program to print the elements of an array 15
14 Java Program to print the elements of an array in reverse order 16
15 Java Program to print the elements of an array present on even position 17
16 Java Program to print the elements of an array present on odd position 18
17 Java Program to print the largest element in an array 19
18 Java Program to print the smallest element in an array 20
19 Java Program to sort the elements of an array in ascending order 21
20 Java Program to sort the elements of an array in descending order 22
21 Java Program to Add Two Matrices 23
22 Java Program to Multiply Two Matrices (Element-wise) 24
23 Java Program to subtract the two matrices 25
24 Java Program to find the product of two matrices (Matrix Multiplication) 26
25 Java Program to find the sum of each row and each column of a matrix 27
Page
S. No Question / Program
No
26 Java Program to find the transpose of a given matrix 28
27 Java Program to count the total number of characters in a string 29
Java Program to count the total number of vowels and consonants in a
28 30
string
Java Program to replace lower-case characters with uppercase and
29 31
vice-versa
30 Java Program to replace the spaces of a string with a specific character 32
31 Java Program to determine whether a given string is palindrome 33
32 Write a Java program to implement Function Overloading 34
33 Write a Java program to implement Function Overriding 35
Write a Java Program to implement Single Level, Multi Level and
34 36
Hierarchical Inheritance
35 Write a program in Java to implement Multiple Inheritance using Interface 37
36 Write a program in Java to create user defined package and implement 38
37 Write a program in Java to implement Multi-Threading in java 39
38 Write a program in Java to implement Exception Handling 40
39 Write a program in Java for making smiling face using Applets 41
40 Write a Program in Java to read a file and write text in it using I/O in Java 42
Ques 1. Enter a number from keyboard and check whether it is prime
or not
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
boolean isPrime = true;
if (n <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](n + " is a Prime number");
} else {
[Link](n + " is not a Prime number");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter a number: 17
17 is a Prime number
Ques 2. Write a program to find prime numbers in a given range
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter start: ");
int start = [Link]();
[Link]("Enter end: ");
int end = [Link]();
[Link]("Prime numbers between " + start + " and " +
end + ":");
for (int i = start; i <= end; i++) {
boolean isPrime = true;
if (i <= 1) isPrime = false;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) { isPrime = false; break; }
}
if (isPrime) [Link](i + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter start: 1
Enter end: 30
Prime numbers between 1 and 30:
2 3 5 7 11 13 17 19 23 29
Ques 3. Write a program to find factorial of a given number
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
}
[Link]("Factorial of " + n + " = " + factorial);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter a number: 6
Factorial of 6 = 720
Ques 4. Write a program to find sum of all the digits of a given number
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
int temp = n;
while (temp != 0) {
sum = sum + (temp % 10);
temp = temp / 10;
}
[Link]("Sum of digits of " + n + " = " + sum);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter a number: 1234
Sum of digits of 1234 = 10
Ques 5. Write a program to print a 3x5 star pattern
Code:
class Solution {
public static void main(String[] args) {
int rows = 3;
int cols = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
[Link]("* ");
}
[Link]();
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
* * * * *
* * * * *
* * * * *
Ques 6. Write a program to convert temperature from Fahrenheit to
Celsius using C = (F - 32) / 1.8
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter temperature in Fahrenheit: ");
double f = [Link]();
double c = (f - 32) / 1.8;
[Link]("Temperature in Celsius = " + c);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius = 37.0
Ques 7. Write a program to find numbers and sum of all integers >100
and <200 divisible by 7
Code:
class Solution {
public static void main(String[] args) {
int sum = 0, count = 0;
[Link]("Numbers between 100 and 200 divisible by 7:");
for (int i = 101; i < 200; i++) {
if (i % 7 == 0) {
[Link](i + " ");
sum = sum + i;
count = count + 1;
}
}
[Link]();
[Link]("Count = " + count);
[Link]("Sum = " + sum);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Numbers between 100 and 200 divisible by 7:
105 112 119 126 133 140 147 154 161 168 175 182 189 196
Count = 14
Sum = 2107
Ques 8. Write a program to find the reverse of a given number
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int reverse = 0, temp = n;
while (temp != 0) {
int digit = temp % 10;
reverse = reverse * 10 + digit;
temp = temp / 10;
}
[Link]("Reverse of " + n + " = " + reverse);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter a number: 5678
Reverse of 5678 = 8765
Ques 9. Write a program to find whether a given number is Armstrong
number or not
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0, temp = n;
while (temp != 0) {
int digit = temp % 10;
sum = sum + (digit * digit * digit);
temp = temp / 10;
}
if (sum == n)
[Link](n + " is an Armstrong number");
else
[Link](n + " is not an Armstrong number");
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter a number: 153
153 is an Armstrong number
Ques 10. Write a program to print all the Armstrong numbers in a
given range
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter start: ");
int start = [Link]();
[Link]("Enter end: ");
int end = [Link]();
[Link]("Armstrong numbers between " + start + " and "
+ end + ":");
for (int i = start; i <= end; i++) {
int sum = 0, temp = i;
while (temp != 0) {
int d = temp % 10;
sum = sum + (d * d * d);
temp = temp / 10;
}
if (sum == i) [Link](i + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter start: 1
Enter end: 500
Armstrong numbers between 1 and 500:
1 153 370 371 407
Ques 11. Write a program to print Fibonacci series for a given number
Code:
import [Link];

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();
int a = 0, b = 1;
[Link]("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
[Link](a + " ");
int next = a + b;
a = b;
b = next;
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Enter number of terms: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
Ques 12. Java Program to copy all elements of one array into another
array
Code:
class Solution {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copy = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
copy[i] = original[i];
}
[Link]("Copied Array: ");
for (int i = 0; i < [Link]; i++) {
[Link](copy[i] + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Copied Array: 10 20 30 40 50
Ques 13. Java Program to print the elements of an array
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20, 25, 30};
[Link]("Array elements: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Array elements: 5 10 15 20 25 30
Ques 14. Java Program to print the elements of an array in reverse
order
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20, 25, 30};
[Link]("Array in reverse: ");
for (int i = [Link] - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Array in reverse: 30 25 20 15 10 5
Ques 15. Java Program to print the elements of an array present on
even position
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20, 25, 30};
[Link]("Elements at even positions (2,4,6...): ");
for (int i = 1; i < [Link]; i = i + 2) {
[Link](arr[i] + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Elements at even positions (2,4,6...): 10 20 30
Ques 16. Java Program to print the elements of an array present on
odd position
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20, 25, 30};
[Link]("Elements at odd positions (1,3,5...): ");
for (int i = 0; i < [Link]; i = i + 2) {
[Link](arr[i] + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Elements at odd positions (1,3,5...): 5 15 25
Ques 17. Java Program to print the largest element in an array
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {12, 45, 7, 89, 34, 56};
int largest = arr[0];
for (int i = 1; i < [Link]; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
[Link]("Largest element = " + largest);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Largest element = 89
Ques 18. Java Program to print the smallest element in an array
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {12, 45, 7, 89, 34, 56};
int smallest = arr[0];
for (int i = 1; i < [Link]; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
[Link]("Smallest element = " + smallest);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Smallest element = 7
Ques 19. Java Program to sort the elements of an array in ascending
order
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {45, 12, 89, 7, 34, 56};
int temp;
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
[Link]("Ascending Order: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Ascending Order: 7 12 34 45 56 89
Ques 20. Java Program to sort the elements of an array in descending
order
Code:
class Solution {
public static void main(String[] args) {
int[] arr = {45, 12, 89, 7, 34, 56};
int temp;
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
[Link]("Descending Order: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Descending Order: 89 56 45 34 12 7
Ques 21. Java Program to Add Two Matrices
Code:
class Solution {
public static void main(String[] args) {
int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
int[][] b = {{9,8,7},{6,5,4},{3,2,1}};
int[][] sum = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
[Link]("Sum of two matrices:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](sum[i][j] + " ");
}
[Link]();
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Sum of two matrices:
10 10 10
10 10 10
10 10 10
Ques 22. Java Program to Multiply Two Matrices (Element-wise)
Code:
class Solution {
public static void main(String[] args) {
int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
int[][] b = {{9,8,7},{6,5,4},{3,2,1}};
int[][] result = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = a[i][j] * b[i][j];
}
}
[Link]("Element-wise Multiplication:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Element-wise Multiplication:
9 16 21
24 25 24
21 16 9
Ques 23. Java Program to subtract the two matrices
Code:
class Solution {
public static void main(String[] args) {
int[][] a = {{9,8,7},{6,5,4},{3,2,1}};
int[][] b = {{1,2,3},{4,5,6},{7,8,9}};
int[][] result = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = a[i][j] - b[i][j];
}
}
[Link]("Subtraction of two matrices:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Subtraction of two matrices:
8 6 4
2 0 -2
-4 -6 -8
Ques 24. Java Program to find the product of two matrices (Matrix
Multiplication)
Code:
class Solution {
public static void main(String[] args) {
int[][] a = {{1,2},{3,4}};
int[][] b = {{5,6},{7,8}};
int[][] product = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
product[i][j] = product[i][j] + a[i][k] * b[k][j];
}
}
}
[Link]("Matrix Product:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
[Link](product[i][j] + " ");
}
[Link]();
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Matrix Product:
19 22
43 50
Ques 25. Java Program to find the sum of each row and each column
of a matrix
Code:
class Solution {
public static void main(String[] args) {
int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < 3; i++) {
int rowSum = 0;
for (int j = 0; j < 3; j++) rowSum = rowSum + mat[i][j];
[Link]("Sum of Row " + (i+1) + " = " + rowSum);
}
for (int j = 0; j < 3; j++) {
int colSum = 0;
for (int i = 0; i < 3; i++) colSum = colSum + mat[i][j];
[Link]("Sum of Column " + (j+1) + " = " + colSum);
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Sum of Row 1 = 6
Sum of Row 2 = 15
Sum of Row 3 = 24
Sum of Column 1 = 12
Sum of Column 2 = 15
Sum of Column 3 = 18
Ques 26. Java Program to find the transpose of a given matrix
Code:
class Solution {
public static void main(String[] args) {
int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
int[][] transpose = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transpose[j][i] = mat[i][j];
}
}
[Link]("Transpose of Matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](transpose[i][j] + " ");
}
[Link]();
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Transpose of Matrix:
1 4 7
2 5 8
3 6 9
Ques 27. Java Program to count the total number of characters in a
string
Code:
class Solution {
public static void main(String[] args) {
String str = "Hello World";
int count = 0;
for (int i = 0; i < [Link](); i++) {
count = count + 1;
}
[Link]("String: " + str);
[Link]("Total characters (with spaces) = " + count);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
String: Hello World
Total characters (with spaces) = 11
Ques 28. Java Program to count the total number of vowels and
consonants in a string
Code:
class Solution {
public static void main(String[] args) {
String str = "Hello World";
int vowels = 0, consonants = 0;
for (int i = 0; i < [Link](); i++) {
char ch = [Link]([Link](i));
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') {
vowels = vowels + 1;
} else if (ch >= 'a' && ch <= 'z') {
consonants = consonants + 1;
}
}
[Link]("String: " + str);
[Link]("Vowels = " + vowels);
[Link]("Consonants = " + consonants);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
String: Hello World
Vowels = 3
Consonants = 7
Ques 29. Java Program to replace lower-case characters with
uppercase and vice-versa
Code:
class Solution {
public static void main(String[] args) {
String str = "Hello World";
String result = "";
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
result = result + [Link](ch);
} else if ([Link](ch)) {
result = result + [Link](ch);
} else {
result = result + ch;
}
}
[Link]("Original : " + str);
[Link]("Converted: " + result);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Original : Hello World
Converted: hELLO wORLD
Ques 30. Java Program to replace the spaces of a string with a
specific character
Code:
class Solution {
public static void main(String[] args) {
String str = "Hello World Java";
char replaceWith = '_';
String result = "";
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ' ') {
result = result + replaceWith;
} else {
result = result + [Link](i);
}
}
[Link]("Original : " + str);
[Link]("Modified : " + result);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Original : Hello World Java
Modified : Hello_World_Java
Ques 31. Java Program to determine whether a given string is
palindrome
Code:
class Solution {
public static void main(String[] args) {
String str = "madam";
String reverse = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reverse = reverse + [Link](i);
}
[Link]("String: " + str);
if ([Link](reverse)) {
[Link](str + " is a Palindrome");
} else {
[Link](str + " is not a Palindrome");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
String: madam
madam is a Palindrome
Ques 32. Write a Java program to implement Function Overloading
Code:
class Solution {
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
[Link]("add(10, 20) = " + add(10, 20));
[Link]("add(10, 20, 30) = " + add(10, 20, 30));
[Link]("add(1.5, 2.5) = " + add(1.5, 2.5));
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
add(10, 20) = 30
add(10, 20, 30) = 60
add(1.5, 2.5) = 4.0
Ques 33. Write a Java program to implement Function Overriding
Code:
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


void sound() {
[Link]("Dog says: Woof");
}
}

class Solution {
public static void main(String[] args) {
Animal a = new Animal();
[Link]();
Dog d = new Dog();
[Link]();
Animal ref = new Dog();
[Link]();
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Animal makes a sound
Dog says: Woof
Dog says: Woof
Ques 34. Write a Java Program to implement Single Level, Multi Level
and Hierarchical Inheritance
Code:
class A {
void showA() { [Link]("Class A - Single Inheritance"); }
}
class B extends A {
void showB() { [Link]("Class B extends A"); }
}
class C extends B {
void showC() { [Link]("Class C - Multi Level"); }
}
class D extends A {
void showD() { [Link]("Class D - Hierarchical"); }
}

class Solution {
public static void main(String[] args) {
[Link]("--- Single Inheritance ---");
B obj1 = new B();
[Link](); [Link]();

[Link]("--- Multi Level Inheritance ---");


C obj2 = new C();
[Link](); [Link](); [Link]();

[Link]("--- Hierarchical Inheritance ---");


D obj3 = new D();
[Link](); [Link]();
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
--- Single Inheritance ---
Class A - Single Inheritance
Class B extends A
--- Multi Level Inheritance ---
Class A - Single Inheritance
Class B extends A
Class C - Multi Level
--- Hierarchical Inheritance ---
Class A - Single Inheritance
Class D - Hierarchical
Ques 35. Write a program in Java to implement Multiple Inheritance
using Interface
Code:
interface Flyable {
void fly();
}

interface Swimmable {
void swim();
}

class Duck implements Flyable, Swimmable {


public void fly() {
[Link]("Duck can fly");
}
public void swim() {
[Link]("Duck can swim");
}
}

class Solution {
public static void main(String[] args) {
Duck d = new Duck();
[Link]();
[Link]();
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Duck can fly
Duck can swim
Ques 36. Write a program in Java to create user defined package and
implement
Code:
// File 1: mypackage/[Link]
package mypackage;

public class Greet {


public void sayHello() {
[Link]("Hello from mypackage!");
}
}

// File 2: [Link]
import [Link];

class Solution {
public static void main(String[] args) {
Greet g = new Greet();
[Link]();
}
}

Output:
user@User MINGW64 ~
$ javac mypackage/[Link]
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Hello from mypackage!
Ques 37. Write a program in Java to implement Multi-Threading in java
Code:
class MyThread extends Thread {
String name;
MyThread(String name) { [Link] = name; }
public void run() {
for (int i = 1; i <= 3; i++) {
[Link](name + " - count " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]("Thread interrupted");
}
}
}
}

class Solution {
public static void main(String[] args) {
MyThread t1 = new MyThread("Thread-1");
MyThread t2 = new MyThread("Thread-2");
[Link]();
[Link]();
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Thread-1 - count 1
Thread-2 - count 1
Thread-1 - count 2
Thread-2 - count 2
Thread-1 - count 3
Thread-2 - count 3
Ques 38. Write a program in Java to implement Exception Handling
Code:
class Solution {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int result = a / b;
[Link]("Result = " + result);
} catch (ArithmeticException e) {
[Link]("ArithmeticException: " + [Link]());
}
try {
int[] arr = {1, 2, 3};
[Link](arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("ArrayIndexOutOfBounds: " +
[Link]());
}
try {
int num = [Link]("abc");
} catch (NumberFormatException e) {
[Link]("NumberFormatException: " +
[Link]());
}
try {
[Link]("Inside try block");
} finally {
[Link]("Finally block always executes");
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
ArithmeticException: / by zero
ArrayIndexOutOfBounds: Index 5 out of bounds for length 3
NumberFormatException: For input string: "abc"
Inside try block
Finally block always executes
Ques 39. Write a program in Java for making smiling face using
Applets
Code:
import [Link];
import [Link];
import [Link];

/* <applet code="[Link]" width="300" height="300"> </applet> */


public class Solution extends Applet {
public void paint(Graphics g) {
// Face
[Link]([Link]);
[Link](50, 50, 200, 200);
// Eyes
[Link]([Link]);
[Link](100, 100, 30, 30);
[Link](170, 100, 30, 30);
// Smile
[Link]([Link]);
[Link](90, 140, 120, 70, 0, -180);
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ appletviewer [Link]
[Applet window displays a yellow smiley face]
Ques 40. Write a Program in Java to read a file and write text in it
using I/O in Java
Code:
import [Link];
import [Link];
import [Link];
import [Link];

class Solution {
public static void main(String[] args) {
String filename = "[Link]";
String text = "Hello! This is written using Java File I/O.";
try {
FileWriter fw = new FileWriter(filename);
[Link](text);
[Link]();
[Link]("Text written to file successfully.");
} catch (IOException e) {
[Link]("Write error: " + [Link]());
}
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String line;
[Link]("Reading from file:");
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (IOException e) {
[Link]("Read error: " + [Link]());
}
}
}

Output:
user@User MINGW64 ~
$ javac [Link]
user@User MINGW64 ~
$ java Solution
Text written to file successfully.
Reading from file:
Hello! This is written using Java File I/O.

You might also like