Java Programs (5 to 12)
5. Lateral and Vertical Image (Matrix Transform)
import [Link];
public class MatrixTransform {
private int[][] matrix;
private int rows, cols;
public void inputMatrix() {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows: ");
rows = [Link]();
[Link]("Enter columns: ");
cols = [Link]();
matrix = new int[rows][cols];
[Link]("Enter elements:");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
[Link]("Element [" + i + "][" + j + "]: ");
matrix[i][j] = [Link]();
}
}
public void verticalMirror() {
[Link]("Vertical Image:");
for (int i = 0; i < rows; i++) {
for (int j = cols - 1; j >= 0; j--) {
[Link](matrix[i][j] + "\t");
}
[Link]();
}
}
public void transposeMatrix() {
[Link]("Lateral (Transpose) Image:");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
[Link](matrix[j][i] + "\t");
}
[Link]();
}
}
public static void main(String[] args) {
MatrixTransform obj = new MatrixTransform();
[Link]();
[Link]();
[Link]();
}
}
6. Zig-Zag Array
public class ZigZag {
public static void main(String[] args) {
[Link]("Zig-Zag Pattern (3 to 20):");
boolean flag = true;
for (int i = 3; i <= 20; i++) {
if (flag)
[Link](i + " ");
else
[Link](-i + " ");
flag = !flag;
}
}
}
7. Border Matrix
import [Link];
public class BorderMatrix {
private int[][] matrix;
public void createBorderMatrix(int n) {
matrix = new int[n][n];
int val = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
matrix[i][j] = val++;
else
matrix[i][j] = 0;
}
}
}
public void display() {
for (int[] row : matrix) {
for (int val : row) {
[Link](val + "\t");
}
[Link]();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
BorderMatrix obj = new BorderMatrix();
[Link]("Enter size of matrix: ");
int n = [Link]();
[Link](n);
[Link]();
}
}
8. Sentence Validity and Consonant Word Frequency
import [Link];
public class ConsonantWordCounter {
public boolean isValid(String sentence) {
if ([Link]()) return false;
char first = [Link](0);
char last = [Link]([Link]() - 1);
return [Link](first) && (last == '.' || last ==
';' || last == '?');
}
public void countConsonantWords(String sentence) {
if (!isValid(sentence)) {
[Link]("Invalid sentence. It must start with a
capital letter and end with '.', ';', or '?'");
return;
}
sentence = [Link](0, [Link]() - 1);
String[] words = [Link]("\s+");
int count = 0;
for (String word : words) {
char ch = [Link]([Link](0));
if ("aeiou".indexOf(ch) == -1 && [Link](ch)) {
count++;
[Link](word);
}
}
[Link]("Total words starting with consonants: " +
count);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ConsonantWordCounter obj = new ConsonantWordCounter();
[Link]("Enter a sentence: ");
String input = [Link]();
[Link](input);
}
}
9. Switch Case Calculator
import [Link];
public class SimpleCalculator {
public void calculate() {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Choose operation: + - * / %");
char op = [Link]().charAt(0);
switch (op) {
case '+': [Link]("Result: " + (a + b)); break;
case '-': [Link]("Result: " + (a - b)); break;
case '*': [Link]("Result: " + (a * b)); break;
case '/':
if (b != 0)
[Link]("Result: " + (a / b));
else
[Link]("Cannot divide by zero!");
break;
case '%': [Link]("Result: " + (a % b)); break;
default: [Link]("Invalid operator.");
}
}
public static void main(String[] args) {
SimpleCalculator calc = new SimpleCalculator();
[Link]();
}
}
10. Recursion - Factorial
import [Link];
public class FactorialRecursion {
public int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
FactorialRecursion obj = new FactorialRecursion();
[Link]("Enter a number: ");
int num = [Link]();
int result = [Link](num);
[Link]("Factorial of " + num + " is " + result);
}
}
11. Abbreviation Generator
import [Link];
public class AbbreviationGenerator {
public void generate(String fullName) {
String[] parts = [Link]().split("\s+");
StringBuilder abbr = new StringBuilder();
for (int i = 0; i < [Link] - 1; i++) {
[Link]([Link](parts[i].charAt(0))).append(". ");
}
[Link](capitalize(parts[[Link] - 1]));
[Link]("Abbreviation: " + abbr);
}
private String capitalize(String word) {
return [Link]([Link](0)) +
[Link](1).toLowerCase();
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
AbbreviationGenerator obj = new AbbreviationGenerator();
[Link]("Enter full name: ");
String fullName = [Link]();
[Link](fullName);
}
}
12. Count Vowels, Consonants, Spaces, and Special Characters
import [Link];
public class TextAnalyzer {
public void analyze(String text) {
int vowels = 0, consonants = 0, spaces = 0, special = 0;
for (char ch : [Link]()) {
if ([Link](ch)) {
char lower = [Link](ch);
if ("aeiou".indexOf(lower) != -1)
vowels++;
else
consonants++;
} else if (ch == ' ') {
spaces++;
} else {
special++;
}
}
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
[Link]("Spaces: " + spaces);
[Link]("Special Characters: " + special);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
TextAnalyzer obj = new TextAnalyzer();
[Link]("Enter a text: ");
String input = [Link]();
[Link](input);
}
}