0% found this document useful (0 votes)
4 views8 pages

Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LAB TASK-1

1. Write a program for addition of two numbers addition ,subtraction, multiplication and
division .
import java.util.Scanner;

public class calcu {


public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
// Taking user input
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
// 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 = Double.NaN; // Not a Number
}
// Displaying results
System.out.println("Addition: " + addition);
System.out.println("Subtraction: " + subtraction);
System.out.println("Multiplication: " + multiplication);
System.out.println("Division: " + division); } }

OUTPUT:-
2. Write a program to reverse a given number by user
import java.util.Scanner;

public class reverse{


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input from user


System.out.print("Enter a number to reverse: ");
int number = scanner.nextInt();
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


System.out.println("Reversed Number: " + reversed);

scanner.close();
}
}

OUTPUT:-

3.Write 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() {
System.out.println("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
s1.name = "Ravi";
s1.age = 18;
System.out.println("Initialized using reference variable:");
s1.display();
// II) Using method
Student s2 = new Student("Default", 0);
s2.setDetails("Anita", 20);
System.out.println("\nInitialized using method:");
s2.display();
// III) Using constructor
Student s3 = new Student("Vikram", 22);
System.out.println("\nInitialized using constructor:");
s3.display(); }}

OUTPUT:
4.Write a program to demonstrate constructor enhancing with real time example in java .

// BankAccount.java

class BankAccount {

String accountHolder;

String accountNumber;

double balance;

// Default constructor

BankAccount() {

this.accountHolder = "Unknown";

this.accountNumber = "000000";

this.balance = 0.0;

System.out.println("Default constructor called"); }

// Constructor with one parameter

BankAccount(String accountHolder) {

this.accountHolder = accountHolder;

this.accountNumber = "000000";

this.balance = 0.0;

System.out.println("Constructor with accountHolder called"); }

// Constructor with two parameters

BankAccount(String accountHolder, String accountNumber) {

this.accountHolder = accountHolder;

this.accountNumber = accountNumber;

this.balance = 0.0;

System.out.println("Constructor with accountHolder and accountNumber called"); }

// Constructor with all parameters

BankAccount(String accountHolder, String accountNumber, double balance) {

this.accountHolder = accountHolder;

this.accountNumber = accountNumber;

this.balance = balance;

System.out.println("Constructor with all parameters called"); }

void displayDetails(){ System.out.println("Account Holder: " + accountHolder);


System.out.println("Account Number: " + accountNumber);

System.out.println("Balance: ₹" + balance); }}

public class BankApp {

public static void main(String[] args) { // Using default constructor

BankAccount acc1 = new BankAccount();

acc1.displayDetails(); // Using constructor with 1 parameter

BankAccount acc2 = new BankAccount("Amit");

acc2.displayDetails();

// Using constructor with 2 parameters

BankAccount acc3 = new BankAccount("Priya", "12345678");

acc3.displayDetails();

// Using constructor with 3 parameters

BankAccount acc4 = new BankAccount("Ravi", "87654321", 5000.0);

acc4.displayDetails(); }}

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) {

this.name = name; // this refers to the instance variable


this.rollNo = 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() {

Helper.printDetails(this); // Passing current object

// Helper class to accept Student object

class Helper {

static void printDetails(Student s) {

System.out.println("Student Name: " + s.name);

System.out.println("Roll Number : " + s.rollNo);

System.out.println("---------------------------");

// 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");

s1.display();

s2.display();
}

You might also like