0% found this document useful (0 votes)
8 views

java.mid2

Uploaded by

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

java.mid2

Uploaded by

kishwaryarani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Matrix multiplication
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] A = {{1, 2}, {3, 4}};
int[][] B = {{5, 6}, {7, 8}};
int[][] result = new int[2][2];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
result[i][j] = A[i][0] * B[0][j] + A[i][1] * B[1][j];
}
}

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}

2. Super keyword
class Parent {
Parent() {
System.out.println("Parent constructor called:");
}
void display() {
System.out.println("Parent method called");
}
}

class Child extends Parent {


Child() {
super(); // Calls the Parent class constructor
}

void show() {
super.display(); // Calls the Parent class method
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child(); // Creates an instance of Child
c.show(); // Calls the show method of the Child class
}
}
3.Develop a program to use the Math class for performing
operations
like power, square root, and trigonometric calculations.
public class Main
{
public static void main(String[] args)
{
System.out.println("sqrt of 16:"+Math.sqrt(16));
System.out.println("2 cube:"+Math.pow(2,3));
double angle=Math.toRadians(30);
System.out.println("sin of 30:"+Math.sin(angle));
System.out.println("tan of 30:"+Math.tan(angle));
}
}

4. Implement a program that throws and catches a custom checked


exception.
class AgeException extends Exception
{
public AgeException(String message)
{
super(message);
}
}
public class Main
{
public static void main(String[] args)
{
int age=18;
try
{
if(age<18)
{
throw new AgeException("Age less than 18");
}
else
{
System.out.println("Age is valid");
}}
catch(Exception e)
{
System.out.println(e);
}
}
}
5. program to replace all occurrences of a specific word in a string
with another word.
public class Main
{
public static void main(String[] args)
{
String sentence=”hii how are you";
String oldword="hii";
String newword="hello";
String updatedsentence=sentence.replace(oldword,newword);
System.out.println(updatedsentence);
}
}
6. program to inhibit inheritance for a specific class using the final
keyword and demonstrate its effect..
final class FinalClass
{
void display()
{
System.out.println("this class cant be inherited");
}
}
class subclass extends FinalClass #error occure(cant extends from
final class)
{
void show()
{
System.out.println("This is a subclass");
}
}
public class Main
{
public static void main(String[] args)
{
FinalClass f=new FinalClass();
f.display();
}
}
7.Define a nested interface in a class and implement it in a different
class.
class OuterClass {
interface NestedInterface {
void display();
}
}

class AnotherClass implements OuterClass.NestedInterface {


public void display() {
System.out.println("Hello from the nested interface!");
}
}

public class Main {


public static void main(String[] args) {
AnotherClass obj = new AnotherClass();
obj.display();
}
}
8. program that demonstrates autoboxing and unboxing with
wrapper classes
public class Main {
public static void main(String[] args) {
int k = 10;
Integer r = k;
System.out.println("Autoboxing integer: " + r);

Integer i = new Integer(20);


int s = i;
System.out.println("Unboxing integer: " + s);
}
}
9. Write a program to demonstrate multiple catch clauses
public class Main {
public static void main(String[] args) {
try
{
int[] a={1,2,3};
int result=a[5];
System.out.println(result);
int k=10/0;
System.out.println(k);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
System.out.println("program continues");
}}
10. Create a multithreaded program where one thread generates
numbers, and another thread calculates their square
class NumGenerator extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println("Generated: " + i);
Square squareThread = new Square(i);
squareThread.start();
} catch (Exception e) {
System.out.println(e);
}
}
}
}

class Square extends Thread {


private int num;

public Square(int num) {


this.num = num;
}

public void run() {


int result = num * num;
System.out.println("Square of " + num + ": " + result);
}
}

public class Main {


public static void main(String[] args) {
NumGenerator n = new NumGenerator();
n.start();
}
}

You might also like