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

java16

The document contains various Java programming examples, including basic programs like HelloWorld, temperature conversion, and palindrome checks. It also includes more complex topics such as heap sort, matrix multiplication, object-oriented programming concepts like inheritance, interfaces, and multithreading. Each section provides code snippets demonstrating the implementation of these concepts.

Uploaded by

harinineha97
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 views

java16

The document contains various Java programming examples, including basic programs like HelloWorld, temperature conversion, and palindrome checks. It also includes more complex topics such as heap sort, matrix multiplication, object-oriented programming concepts like inheritance, interfaces, and multithreading. Each section provides code snippets demonstrating the implementation of these concepts.

Uploaded by

harinineha97
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
You are on page 1/ 11

Java

1.HelloWorld

2. Fahrenheit to Celsius
3. Find Odd or Even

4. Palindrome
5, Display Date and Time

6. Fibonacci Series

7. Heap Sort
public class Heapsort {
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
System.out.println("Original Array:");
printArray(arr);

heapSort(arr);

System.out.println("Sorted Array:");
printArray(arr);
}

public static void heapSort(int[] arr) {


int n = arr.length;

// Build a max heap


for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}

// Extract elements from the heap one by one


for (int i = n - 1; i >= 0; i--) {
// Move the current root to the end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

// Call heapify on the reduced heap


heapify(arr, i, 0);
}
}

public static void heapify(int[] arr, int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

// Find the largest element among the root, left child, and right
child
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}

// If the largest is not the root


if (largest != i) {
// Swap the root and the largest element
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;

// Recursively heapify the affected sub-tree


heapify(arr, n, largest);
}
}

public static void printArray(int[] arr) {


for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
Output:

8. Matrix Multiplication
import java.util.Scanner;
public class martixmul{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the dimensions of the matrices (n x m and


m x p):");
int n = scanner.nextInt(); // Rows of the first matrix
int m = scanner.nextInt(); // Common dimension
int p = scanner.nextInt(); // Columns of the second matrix

int[][] matrix1 = new int[n][m];


int[][] matrix2 = new int[m][p];

System.out.println("Enter elements for the first matrix:");


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix1[i][j] = scanner.nextInt();
}
}

System.out.println("Enter elements for the second matrix:");


for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
matrix2[i][j] = scanner.nextInt();
}
}

int[][] result = new int[n][p];

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


for (int j = 0; j < p; j++) {
for (int k = 0; k < m; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

System.out.println("Resultant Matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}

scanner.close();
}
}

Output:

9. Remove Elements from an ArrayList


10. Java Program to print the duplicate elements of an array
11. Java Program to print the elements of an array

12. Java Program to count the total number of characters in a string

13. Package
package pack;
public class A {
public void msg() {
System.out.println("Welcom to java");
}
}
14. Interface
public interface Shape {

double calculateArea();
}
class Circle1 implements Shape {
private double radius;

public Circle1(double radius) {


this.radius = radius;
}
public double calculateArea() {
return Math.PI * radius * radius;
}
}

15. Inheritance
class A {
int a=10;
public void display1() {
System.out.println(a);
}
}
class B extends A {
int b=10;
public void display2() {
System.out.println(b+a);
}
}
class C extends A {
int c=10;
public void display3() {
System.out.println(b+a+c);
}
}

16.Multithreading
class two extends Thread{
public void run() {
for(int i=1;i<=5;i++) {
int a=i*2;
System.out.println("2"+"*"+i+"="+a);
}
}
}
class three extends Thread{
public void run() {
for(int i=1;i<=5;i++) {
int a=i*3;
System.out.println("3"+"*"+i+"="+a);
}
}
}
class four extends Thread{
public void run() {
for(int i=1;i<=5;i++) {
int a=i*4;
System.out.println("4"+"*"+i+"="+a);
}
}
}
public class multithreading {
public static void main(String args[]) {
two t1=new two();
three t2=new three();
four t3=new four();
t1.start();
t2.start();
t3.start();
}
}

You might also like