0% found this document useful (0 votes)
0 views6 pages

Java Programs 5 To 12

The document contains Java programs that demonstrate various functionalities including matrix transformations, zig-zag patterns, border matrix creation, sentence validity checks, a simple calculator, recursion for factorial calculation, abbreviation generation, and text analysis for vowels and consonants. Each program is structured with a main method to execute specific tasks based on user input. The programs utilize standard input and output methods to interact with users.

Uploaded by

kummardeepak1961
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)
0 views6 pages

Java Programs 5 To 12

The document contains Java programs that demonstrate various functionalities including matrix transformations, zig-zag patterns, border matrix creation, sentence validity checks, a simple calculator, recursion for factorial calculation, abbreviation generation, and text analysis for vowels and consonants. Each program is structured with a main method to execute specific tasks based on user input. The programs utilize standard input and output methods to interact with users.

Uploaded by

kummardeepak1961
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/ 6

Java Programs (5 to 12)

5. Lateral and Vertical Image (Matrix Transform)

import java.util.Scanner;

public class MatrixTransform {


private int[][] matrix;
private int rows, cols;

public void inputMatrix() {


Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
rows = sc.nextInt();
System.out.print("Enter columns: ");
cols = sc.nextInt();
matrix = new int[rows][cols];

System.out.println("Enter elements:");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = sc.nextInt();
}
}

public void verticalMirror() {


System.out.println("Vertical Image:");
for (int i = 0; i < rows; i++) {
for (int j = cols - 1; j >= 0; j--) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}

public void transposeMatrix() {


System.out.println("Lateral (Transpose) Image:");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(matrix[j][i] + "\t");
}
System.out.println();
}
}

public static void main(String[] args) {


MatrixTransform obj = new MatrixTransform();
obj.inputMatrix();
obj.verticalMirror();
obj.transposeMatrix();
}
}

6. Zig-Zag Array

public class ZigZag {


public static void main(String[] args) {
System.out.println("Zig-Zag Pattern (3 to 20):");
boolean flag = true;
for (int i = 3; i <= 20; i++) {
if (flag)
System.out.print(i + " ");
else
System.out.print(-i + " ");
flag = !flag;
}
}
}

7. Border Matrix

import java.util.Scanner;

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) {
System.out.print(val + "\t");
}
System.out.println();
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
BorderMatrix obj = new BorderMatrix();
System.out.print("Enter size of matrix: ");
int n = sc.nextInt();
obj.createBorderMatrix(n);
obj.display();
}
}

8. Sentence Validity and Consonant Word Frequency

import java.util.Scanner;

public class ConsonantWordCounter {

public boolean isValid(String sentence) {


if (sentence.isEmpty()) return false;
char first = sentence.charAt(0);
char last = sentence.charAt(sentence.length() - 1);

return Character.isUpperCase(first) && (last == '.' || last ==


';' || last == '?');
}

public void countConsonantWords(String sentence) {


if (!isValid(sentence)) {
System.out.println("Invalid sentence. It must start with a
capital letter and end with '.', ';', or '?'");
return;
}

sentence = sentence.substring(0, sentence.length() - 1);


String[] words = sentence.split("\s+");

int count = 0;
for (String word : words) {
char ch = Character.toLowerCase(word.charAt(0));
if ("aeiou".indexOf(ch) == -1 && Character.isLetter(ch)) {
count++;
System.out.println(word);
}
}

System.out.println("Total words starting with consonants: " +


count);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
ConsonantWordCounter obj = new ConsonantWordCounter();
System.out.print("Enter a sentence: ");
String input = sc.nextLine();
obj.countConsonantWords(input);
}
}

9. Switch Case Calculator

import java.util.Scanner;

public class SimpleCalculator {


public void calculate() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();

System.out.println("Choose operation: + - * / %");


char op = sc.next().charAt(0);

switch (op) {
case '+': System.out.println("Result: " + (a + b)); break;
case '-': System.out.println("Result: " + (a - b)); break;
case '*': System.out.println("Result: " + (a * b)); break;
case '/':
if (b != 0)
System.out.println("Result: " + (a / b));
else
System.out.println("Cannot divide by zero!");
break;
case '%': System.out.println("Result: " + (a % b)); break;
default: System.out.println("Invalid operator.");
}
}

public static void main(String[] args) {


SimpleCalculator calc = new SimpleCalculator();
calc.calculate();
}
}

10. Recursion - Factorial

import java.util.Scanner;

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(System.in);
FactorialRecursion obj = new FactorialRecursion();
System.out.print("Enter a number: ");
int num = sc.nextInt();
int result = obj.factorial(num);
System.out.println("Factorial of " + num + " is " + result);
}
}

11. Abbreviation Generator

import java.util.Scanner;

public class AbbreviationGenerator {


public void generate(String fullName) {
String[] parts = fullName.trim().split("\s+");
StringBuilder abbr = new StringBuilder();

for (int i = 0; i < parts.length - 1; i++) {

abbr.append(Character.toUpperCase(parts[i].charAt(0))).append(". ");
}
abbr.append(capitalize(parts[parts.length - 1]));

System.out.println("Abbreviation: " + abbr);


}

private String capitalize(String word) {


return Character.toUpperCase(word.charAt(0)) +
word.substring(1).toLowerCase();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
AbbreviationGenerator obj = new AbbreviationGenerator();
System.out.print("Enter full name: ");
String fullName = sc.nextLine();
obj.generate(fullName);
}
}

12. Count Vowels, Consonants, Spaces, and Special Characters

import java.util.Scanner;

public class TextAnalyzer {


public void analyze(String text) {
int vowels = 0, consonants = 0, spaces = 0, special = 0;

for (char ch : text.toCharArray()) {


if (Character.isLetter(ch)) {
char lower = Character.toLowerCase(ch);
if ("aeiou".indexOf(lower) != -1)
vowels++;
else
consonants++;
} else if (ch == ' ') {
spaces++;
} else {
special++;
}
}

System.out.println("Vowels: " + vowels);


System.out.println("Consonants: " + consonants);
System.out.println("Spaces: " + spaces);
System.out.println("Special Characters: " + special);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
TextAnalyzer obj = new TextAnalyzer();
System.out.print("Enter a text: ");
String input = sc.nextLine();
obj.analyze(input);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy