LAB TASK-1
1. Write a program for addition of two numbers addition ,subtraction, multiplication and
division .
import [Link];
public class calcu {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner([Link]);
// Taking user input
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
// Performing operations
int addition = num1 + num2;
int subtraction = num1 - num2;
int multiplication = num1 * num2;
double division;
// Handling division by zero
if (num2 != 0) {
division = num1 / num2;
} else {
division = [Link]; // Not a Number
}
// Displaying results
[Link]("Addition: " + addition);
[Link]("Subtraction: " + subtraction);
[Link]("Multiplication: " + multiplication);
[Link]("Division: " + division); } }
OUTPUT:-
2. Write a program to reverse a given number by user
import [Link];
public class reverse{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input from user
[Link]("Enter a number to reverse: ");
int number = [Link]();
int reversed = 0;
// Logic to reverse the number
while (number != 0) {
int digit = number % 10; // Get last digit
reversed = reversed * 10 + digit; // Append digit
number /= 10; // Remove last digit
}
// Output the reversed number
[Link]("Reversed Number: " + reversed);
[Link]();
}
}
OUTPUT:-
[Link] a program to create a class student and initialize the object of student class
I) By using reference variable
II) By using method
III) By using constructor
// Student class definition
class Student {
String name;
int age;
// I) Initialization using reference variable
void display() {
[Link]("Name: " + name + ", Age: " + age); }
// II) Initialization using method
void setDetails(String n, int a) {
name = n;
age = a;
} // III) Constructor initialization
Student(String n, int a) {
name = n;
age = a; }}
public class StudentDemo {
public static void main(String[] args) {
// I) Using reference variable
Student s1 = new Student("Default", 0); // constructor requires parameters
[Link] = "Ravi";
[Link] = 18;
[Link]("Initialized using reference variable:");
[Link]();
// II) Using method
Student s2 = new Student("Default", 0);
[Link]("Anita", 20);
[Link]("\nInitialized using method:");
[Link]();
// III) Using constructor
Student s3 = new Student("Vikram", 22);
[Link]("\nInitialized using constructor:");
[Link](); }}
OUTPUT:
[Link] a program to demonstrate constructor enhancing with real time example in java .
// [Link]
class BankAccount {
String accountHolder;
String accountNumber;
double balance;
// Default constructor
BankAccount() {
[Link] = "Unknown";
[Link] = "000000";
[Link] = 0.0;
[Link]("Default constructor called"); }
// Constructor with one parameter
BankAccount(String accountHolder) {
[Link] = accountHolder;
[Link] = "000000";
[Link] = 0.0;
[Link]("Constructor with accountHolder called"); }
// Constructor with two parameters
BankAccount(String accountHolder, String accountNumber) {
[Link] = accountHolder;
[Link] = accountNumber;
[Link] = 0.0;
[Link]("Constructor with accountHolder and accountNumber called"); }
// Constructor with all parameters
BankAccount(String accountHolder, String accountNumber, double balance) {
[Link] = accountHolder;
[Link] = accountNumber;
[Link] = balance;
[Link]("Constructor with all parameters called"); }
void displayDetails(){ [Link]("Account Holder: " + accountHolder);
[Link]("Account Number: " + accountNumber);
[Link]("Balance: ₹" + balance); }}
public class BankApp {
public static void main(String[] args) { // Using default constructor
BankAccount acc1 = new BankAccount();
[Link](); // Using constructor with 1 parameter
BankAccount acc2 = new BankAccount("Amit");
[Link]();
// Using constructor with 2 parameters
BankAccount acc3 = new BankAccount("Priya", "12345678");
[Link]();
// Using constructor with 3 parameters
BankAccount acc4 = new BankAccount("Ravi", "87654321", 5000.0);
[Link](); }}
OUTPUT:-
5. Write a program for three uses of this keyword
class Student {
String name;
int rollNo;
// 1. Using 'this' to refer to current class variables
Student(String name, int rollNo) {
[Link] = name; // this refers to the instance variable
[Link] = rollNo;
// 2. Using 'this()' to call another constructor
Student(String name) {
this(name, 0); // Calls the above constructor
// 3. Passing 'this' as an argument to another method
void display() {
[Link](this); // Passing current object
// Helper class to accept Student object
class Helper {
static void printDetails(Student s) {
[Link]("Student Name: " + [Link]);
[Link]("Roll Number : " + [Link]);
[Link]("---------------------------");
// Main class to run the program
public class ThisKeywordDemo {
public static void main(String[] args) {
Student s1 = new Student("Amit", 101);
Student s2 = new Student("Priya");
[Link]();
[Link]();
}