Java Programs: Character Frequency, Inheritance, Palindrome, Matrix Multiplication & Polymorphism

Classified in Computers

Written at on English with a size of 5.03 KB.

Character Frequency

FREQUENCY

import java.util.Scanner;
class Test2 {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.print("ENTER THE STRING:");
String abc = s.nextLine();
System.out.println("Enter The Character for checking:");
char ch = s.nextLine().charAt(0);
int count = 0;
for(int i = 0; i < abc.length(); i++) {
if(ch == abc.charAt(i)) {
count++;
}
}
System.out.println("The given character repeats " + count + " times");
} }

OUTPUT

ENTER THE STRING: MALAYALAM
Enter The Character for checking:
M
The given character repeats 2 times

Inheritance Example

import java.util.Scanner;
class Employee {
String name = "Name";
String address = "Address";
int age = 23, phn_no = 123456789, salary = 500000;
void printsalary() {
System.out.println("Salary :" + salary);
}
}
class Officer extends Employee {
String spcln = "coding";
}
class Manager extends Employee {
String dpt = "Training";
}
class inher {
public static void main(String args[]) {
Officer s = new Officer();
Manager k = new Manager();
System.out.println(s.name + " " + s.address + " " + k.age + " " + k.phn_no + " ");
System.out.println(s.spcln);
System.out.println(k.dpt);
k.printsalary();
} }

OUTPUT: Name Address 23 123456789 coding
Training
Salary : 500000

Palindrome Check

import java.util.Scanner;
class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string:");
String str = sc.nextLine();
int len = str.length();
int flag = 0;
for (int i = 0; i < len / 2; i++) {
if (str.charAt(i) != str.charAt(len - i - 1)) {
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println("it is a palindrome");
} else {
System.out.println("it is not a palindrome");
} } }

Output

Enter the string: malayalam
it is a palindrome

Matrix Multiplication

MATRIX MUL

import java.util.Scanner;
class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of first row");
int r1 = sc.nextInt();
System.out.println("Enter size of first column");
int c1 = sc.nextInt();
System.out.println("Enter size of second row");
int r2 = sc.nextInt();
System.out.println("Enter size of second column");
int c2 = sc.nextInt();
int a1[][] = new int[r1][c1];
int a2[][] = new int[r2][c2];
int c[][] = new int[r1][c2];
System.out.println("Enter elements of first matrix");
for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
a1[i][j] = sc.nextInt();
System.out.println("Enter elements of second matrix");
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
a2[i][j] = sc.nextInt();
if (r2 != c1) {
System.out.println("\nMultiplication Not Possible");
return;
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < r2; k++)
c[i][j] += a1[i][k] * a2[k][j];
}
}
System.out.println("\nResultant Matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println("");
} } }

OUTPUT: Enter size of first row 2
Enter size of first column 2
Enter size of second row 2
Enter size of second column 2
Enter elements of first matrix 1 2 3 4
Enter elements of second matrix 1 2 3 4
Resultant Matrix:
7 10
15 22

Polymorphism

POLYMORPHISM

import java.util.Scanner;
abstract class Shape {
abstract void numberofsides();
}
class Rectangle extends Shape {
void numberofsides() {
System.out.println("number of sides of the rectangle : 4");
}
}
class Hexagon extends Shape {
void numberofsides() {
System.out.println("number of sides of hexagon : 6");
}
}
class Triangle extends Shape {
void numberofsides() {
System.out.println("number of sides of Triangle : 3");
}
}
class polymorphism {
public static void main(String args[]) {
Shape s;
s = new Rectangle();
s.numberofsides();
s = new Triangle();
s.numberofsides();
s = new Hexagon();
s.numberofsides();
} }

OUTPUT

number of sides of the rectangle : 4
number of sides of Triangle : 3
number of sides of hexagon : 6

Entradas relacionadas: