|
// Sample Java file for syntax highlighting |
|
|
|
import java.util.Date; |
|
import java.util.Arrays; |
|
|
|
public class Sample { |
|
// Constants |
|
public static final double PI = 3.14159; |
|
public static final double E = 2.71828; |
|
|
|
// Main method |
|
public static void main(String[] args) { |
|
printWelcomeMessage(); |
|
|
|
Circle circle = new Circle(5); |
|
System.out.println(circle); |
|
System.out.println("Area: " + circle.area()); |
|
System.out.println("Perimeter: " + circle.perimeter()); |
|
|
|
int number = 5; |
|
System.out.println("Factorial of " + number + ": " + calculateFactorial(number)); |
|
|
|
printGoodbyeMessage(); |
|
|
|
// Array operations |
|
int[] squares = new int[10]; |
|
for (int i = 0; i < squares.length; i++) { |
|
squares[i] = i * i; |
|
} |
|
System.out.println("Squares: " + Arrays.toString(squares)); |
|
|
|
// Lambda function (using an anonymous class for demonstration) |
|
MathOperation add = new MathOperation() { |
|
@Override |
|
public int operate(int a, int b) { |
|
return a + b; |
|
} |
|
}; |
|
System.out.println("Sum of 3 and 5: " + add.operate(3, 5)); |
|
|
|
// Using Math class |
|
System.out.println("Cosine of 0: " + Math.cos(0)); |
|
System.out.println("Natural log of E: " + Math.log(E)); |
|
|
|
// Escaped characters |
|
System.out.println("This is a string with a newline character.\nThis is on a new line."); |
|
System.out.println("This is a string with a tab character.\tThis is after the tab."); |
|
} |
|
|
|
// Function to print a welcome message |
|
public static void printWelcomeMessage() { |
|
System.out.println("Welcome to the Student Management System!"); |
|
} |
|
|
|
// Function to print a goodbye message |
|
public static void printGoodbyeMessage() { |
|
System.out.println("Thank you for using the Student Management System. Goodbye!"); |
|
} |
|
|
|
// Function to calculate factorial |
|
public static int calculateFactorial(int n) { |
|
if (n == 0) { |
|
return 1; |
|
} |
|
return n * calculateFactorial(n - 1); |
|
} |
|
|
|
// Interface for demonstrating lambda functions |
|
interface MathOperation { |
|
int operate(int a, int b); |
|
} |
|
} |
|
|
|
// Abstract class definition |
|
abstract class Shape { |
|
protected String color; |
|
|
|
public Shape(String color) { |
|
this.color = color; |
|
} |
|
|
|
public abstract double area(); |
|
public abstract double perimeter(); |
|
|
|
@Override |
|
public String toString() { |
|
return "Shape with color: " + color; |
|
} |
|
} |
|
|
|
// Subclass definition |
|
class Circle extends Shape { |
|
private double radius; |
|
|
|
public Circle(double radius) { |
|
super("Blue"); |
|
this.radius = radius; |
|
} |
|
|
|
@Override |
|
public double area() { |
|
return Sample.PI * radius * radius; |
|
} |
|
|
|
@Override |
|
public double perimeter() { |
|
return 2 * Sample.PI * radius; |
|
} |
|
|
|
@Override |
|
public String toString() { |
|
return "Circle with radius: " + radius + " and color: " + color; |
|
} |
|
} |