import java.util.Scanner; public class VowelFrequencyCounter { public static void main(String[] args) { // Create a Scanner object to read input from the user Scanner scanner = new Scanner(System.in); // Prompt the user to enter a string System.out.print(“Enter a string: “); String input = scanner.nextLine(); // Call the method to count vowels int[] vowelCounts = countVowels(input);…
class ItemPair { private int item1; private int item2; // Constructor to initialize the items public ItemPair(int item1, int item2) { this.item1 = item1; this.item2 = item2; } // Method to display the items public void display() { System.out.println(“Item 1: ” + item1 + “, Item 2: ” + item2); } // Method to swap…
public class StaticDemo { // Static variable static int staticCounter = 0; // Instance variable int instanceCounter = 0; // Static block static { System.out.println(“Static block executed”); // Initialize static variable staticCounter = 10; } // Instance block { System.out.println(“Instance block executed”); // Initialize instance variable instanceCounter = 5; } // Static method static void…
class Stack { private int[] stackArray; private int top; private static final int INITIAL_CAPACITY = 10; // Constructor public Stack() { stackArray = new int[INITIAL_CAPACITY]; top = -1; } // Push an item onto the stack public void push(int value) { if (top == stackArray.length – 1) { grow(); } stackArray[++top] = value; } //…
abstract class Figure { double length; double width; // Constructor public Figure(double length, double width) { this.length = length; this.width = width; } // Abstract method to compute the area abstract double computeArea(); } class Rectangle extends Figure { // Constructor public Rectangle(double length, double width) { super(length, width); } // Override method to compute…
class Triangle { double base, height, side1, side2, side3; // Constructor for Equilateral Triangle public Triangle(double side) { this.side1 = this.side2 = this.side3 = side; } // Constructor for Isosceles Triangle public Triangle(double base, double height) { this.base = base; this.height = height; } // Constructor for Scalene Triangle public Triangle(double side1, double side2, double…
import java.util.Scanner; public class StudentNamesSorter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Get the number of students System.out.print(“Enter the number of students: “); int numberOfStudents = scanner.nextInt(); scanner.nextLine(); // Consume the newline // Array to store student names String[][] studentNames = new String[numberOfStudents][]; // Get student names from the…
import java.util.Scanner; public class MatrixOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(“Enter the number of rows and columns of the first matrix:”); int rows1 = scanner.nextInt(); int cols1 = scanner.nextInt(); int[][] matrix1 = new int[rows1][cols1]; System.out.println(“Enter the elements of the first matrix:”); for (int i = 0; i <…
public class BooleanOperations { // Method for AND operation public static boolean andOperation(boolean a, boolean b) { return a && b; } // Method for OR operation public static boolean orOperation(boolean a, boolean b) { return a || b; } // Method for XOR operation public static boolean xorOperation(boolean a, boolean b) { return a…
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Date { private int day; private int month; private int year; public Date(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } public void addDays(int days) { LocalDate currentDate = LocalDate.of(year, month, day); LocalDate newDate = currentDate.plusDays(days); this.day = newDate.getDayOfMonth(); this.month =…