Java Notes java5 java4 java3 java1
Download PDF java 7
src/ └── studentinfo/ └── Student.java └── Main.java package studentinfo; public class Student { private String name; private int rollNo; private double marks; // Constructor public Student(String name, int rollNo, double marks) { this.name = name; this.rollNo = rollNo; this.marks = marks; } // Method to print student details public void printDetails() { System.out.println(“Name: ”…
public class OuterClass { private static String staticMessage = “Static message from OuterClass”; private String nonStaticMessage = “Non-static message from OuterClass”; // Static nested class static class StaticNestedClass { void display() { // Accessing static member of OuterClass System.out.println(“Accessing from StaticNestedClass: ” + staticMessage); // Cannot access non-static members of OuterClass // System.out.println(“Non-static message: ”…
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…