Code:
public class Employee {
private String firstName;
private String lastName;
private double monthlySalary;
public Employee(String firstName, String lastName, double monthlySalary) {
[Link] = firstName;
[Link] = lastName;
setMonthlySalary(monthlySalary);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
[Link] = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
[Link] = lastName;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
if (monthlySalary > 0) {
[Link] = monthlySalary;
} else {
[Link] = 0.0;
}
}
public double getYearlySalary() {
return monthlySalary * 12;
}
public void giveRaise(double percent) {
if (percent > 0) {
monthlySalary += monthlySalary * (percent / 100);
}
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee emp1 = new Employee("Alice", "Johnson", 30000);
Employee emp2 = new Employee("Bob", "Smith", 40000);
[Link]("Yearly Salaries Before Raise:");
[Link]("%s %s: Rs %.2f%n", [Link](), [Link](),
[Link]());
[Link]("%s %s: Rs %.2f%n", [Link](), [Link](),
[Link]());
[Link](10);
[Link](10);
[Link]("\nYearly Salaries After 10% Raise:");
[Link]("%s %s: Rs %.2f%n", [Link](), [Link](),
[Link]());
[Link]("%s %s: Rs %.2f%n", [Link](), [Link](),
[Link]());
}
}
Output:
Code;
public class StudentInfo {
private String name;
private int rollNumber;
private String course;
public StudentInfo() {
[Link] = "Unknown";
[Link] = 0;
[Link] = "Undeclared";
}
public StudentInfo(String name, int rollNumber, String course) {
[Link] = name;
[Link] = rollNumber;
[Link] = course;
}
public void displayDetails() {
[Link]("Student Details:");
[Link]("Name : " + name);
[Link]("Roll Number: " + rollNumber);
[Link]("Course : " + course);
[Link]("-------------------------");
}
}
public class Student {
public static void main(String[] args) {
StudentInfo student1 = new StudentInfo();
[Link]();
StudentInfo student2 = new StudentInfo("Alice", 101, "Computer Science");
[Link]();
StudentInfo student3 = new StudentInfo("Bob", 102, "Mechanical Engineering");
[Link]();
}
}
Output:
Code:
public class MatrixOperations {
public static void main(String[] args) {
int[][] A = {
{1, 2},
{3, 4}
};
int[][] B = {
{5, 6},
{7, 8}
};
[Link]("Matrix A:");
printMatrix(A);
[Link]("Matrix B:");
printMatrix(B);
[Link]("A + B:");
printMatrix(add(A, B));
[Link]("A - B:");
printMatrix(subtract(A, B));
[Link]("A * B:");
printMatrix(multiply(A, B));
[Link]("Transpose of A:");
printMatrix(transpose(A));
}
// Matrix Addition
public static int[][] add(int[][] A, int[][] B) {
int rows = [Link];
int cols = A[0].length;
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
return result;
}
// Matrix Subtraction
public static int[][] subtract(int[][] A, int[][] B) {
int rows = [Link];
int cols = A[0].length;
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] - B[i][j];
}
}
return result;
}
// Matrix Multiplication
public static int[][] multiply(int[][] A, int[][] B) {
int rows = [Link];
int cols = B[0].length;
int sumLength = [Link];
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int sum = 0;
for (int k = 0; k < sumLength; k++) {
sum += A[i][k] * B[k][j];
}
result[i][j] = sum;
}
}
return result;
}
// Matrix Transpose
public static int[][] transpose(int[][] A) {
int rows = [Link];
int cols = A[0].length;
int[][] result = new int[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
result[i][j] = A[j][i];
}
}
return result;
}
public static void printMatrix(int[][] M) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < M[0].length; j++) {
[Link](M[i][j] + "\t");
}
[Link]();
}
}
}
Output:
Code:
public class AreaCalculator {
public static double rect(double length, double width) {
return length * width;
}
public static double tri(double base, double height, boolean isTriangle) {
return 0.5 * base * height;
}
public static double circ(int radius) {
return [Link] * radius * radius;
}
public static void main(String[] args) {
double rectArea = rect(5.0, 3.0);
double triArea = tri(4.0, 6.0, true);
double circArea = circ(7); // radius as integer
[Link]("Area of Rectangle = " + rectArea);
[Link]("Area of Triangle = " + triArea);
[Link]("Area of Circle = " + circArea);
}
}
Output:
Code:
public class Car {
private String brand;
private String model;
private int year;
private double price;
public Car(String brand, String model, int year, double price) {
[Link] = brand;
[Link] = model;
[Link] = year;
[Link] = price;
}
public String getBrand() { return brand; }
public String getModel() { return model; }
public int getYear() { return year; }
public double getPrice() { return price; }
public void setPrice(double price) {
[Link] = price;
}
public void displayInfo() {
[Link]("Car Information:");
[Link]("Brand : " + brand);
[Link]("Model : " + model);
[Link]("Year : " + year);
[Link]("Price : $" + price);
}
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Camry", 2020, 25000.0);
Car car2 = new Car("Tesla", "Model 3", 2023, 40000.0);
[Link]();
[Link]();
[Link]();
}
}
Output: