[Go to site: main page, start]

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

Java File

The document contains multiple Java programs demonstrating various concepts such as reversing a number, creating and using classes with constructors, maintaining bank accounts with inheritance, using packages, validating string inputs, and working with abstract classes. Each program includes source code and aims to illustrate specific programming principles. The examples cover input handling, class design, and exception management.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Java File

The document contains multiple Java programs demonstrating various concepts such as reversing a number, creating and using classes with constructors, maintaining bank accounts with inheritance, using packages, validating string inputs, and working with abstract classes. Each program includes source code and aims to illustrate specific programming principles. The examples cover input handling, class design, and exception management.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRACTICAL NO.

AIM: Write a Java program to print the reverse of the numbers, the numbers are taken as input from
the user.

SOURCE CODE:-

// Program to reverse a Number

import [Link];

public class ReverseNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Taking input from user


[Link]("Enter a number: ");
int num = [Link]();

int reversed = 0;

// Logic to reverse the number


while (num != 0) {
int digit = num % 10; // Get last digit
reversed = reversed * 10 + digit; // Append digit
num = num / 10; // Remove last digit
}

// Output result
[Link]("Reversed number: " + reversed);

[Link]();
}
}
OUTPUT:-
PRACTICAL NO. -

AIM: Write a Java program to create a class called Rectangle with instance variables length and width.
Implement a parameterized constructor and a copy constructor that initializes a new object using
the values of an existing object. Print the values of the variables.

SOURCE CODE:-

// Program to demonstrate parametrized and copy constructor

import [Link];

class Rectangle {
double length;
double width;

// Parameterized Constructor
Rectangle(double l, double w) {
length = l;
width = w;
}

// Copy Constructor
Rectangle(Rectangle r) {
[Link] = [Link];
[Link] = [Link];
}

// Method to display values


void display() {
[Link]("Length: " + length);

[Link]("Width: " + width);


}
}

public class RectangleDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Taking input from user


[Link]("Enter length: ");
double l = [Link]();
[Link]("Enter width: ");
double w = [Link]();

// Creating object using parameterized constructor


Rectangle rect1 = new Rectangle(l, w);

// Creating object using copy constructor


Rectangle rect2 = new Rectangle(rect1);

// Displaying values
[Link]("\nOriginal Rectangle:");
[Link]();

[Link]("\nCopied Rectangle:");
[Link]();

[Link]();
}
}

OUTPUT:-
PRACTICAL NO. -

AIM: Write a Java program to maintain a bank account. Extend bank account details to current and
saving accounts.

SOURCE CODE:-

// Program to Demonstrate the Concept of Inheritance using A Demo Bank Account

import [Link];

// Base Class
class BankAccount {
int accNo;
String name;
double balance;

// Constructor
BankAccount(int accNo, String name, double balance) {
[Link] = accNo;
[Link] = name;
[Link] = balance;
}

void deposit(double amount) {


balance += amount;
[Link]("Deposited: " + amount);
}

void withdraw(double amount) {


if (amount <= balance) {
balance -= amount;
[Link]("Withdrawn: " + amount);
} else {
[Link]("Insufficient balance!");
}
}

void display() {
[Link]("Account No: " + accNo);
[Link]("Name: " + name);
[Link]("Balance: " + balance);
}
}

// Saving Account
class SavingAccount extends BankAccount {
double interestRate;

SavingAccount(int accNo, String name, double balance, double interestRate) {


super(accNo, name, balance);
[Link] = interestRate;
}

void addInterest() {
double interest = balance * interestRate / 100;
balance += interest;
[Link]("Interest added: " + interest);
}
}

// Current Account
class CurrentAccount extends BankAccount {
double minBalance;

CurrentAccount(int accNo, String name, double balance, double minBalance) {


super(accNo, name, balance);
[Link] = minBalance;
}

@Override
void withdraw(double amount) {
if ((balance - amount) >= minBalance) {
balance -= amount;
[Link]("Withdrawn: " + amount);
} else {
[Link]("Cannot withdraw! Minimum balance must be maintained.");
}
}
}

// Main Class
public class BankDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Common details
[Link]("Enter Account Number: ");
int accNo = [Link]();
[Link]();

[Link]("Enter Name: ");


String name = [Link]();

[Link]("Enter Initial Balance: ");


double balance = [Link]();

// -------- Saving Account --------


[Link]("\nEnter Interest Rate for Saving Account: ");
double rate = [Link]();

SavingAccount sa = new SavingAccount(accNo, name, balance, rate);

[Link]("\n--- Saving Account ---");

[Link]("Enter amount to deposit: ");


double dep1 = [Link]();
[Link](dep1);

[Link]("Enter amount to withdraw: ");


double wit1 = [Link]();
[Link](wit1);

[Link]();
[Link]();

// -------- Current Account --------


[Link]("\nEnter Minimum Balance for Current Account: ");
double minBal = [Link]();

CurrentAccount ca = new CurrentAccount(accNo, name, balance, minBal);

[Link]("\n--- Current Account ---");

[Link]("Enter amount to deposit: ");


double dep2 = [Link]();
[Link](dep2);

[Link]("Enter amount to withdraw: ");


double wit2 = [Link]();
[Link](wit2);
[Link]();

[Link]();
}
}

OUTPUT:-
PRACTICAL NO. -

AIM: Write a Java program to maintain a bank account using Packages.

SOURCE CODE:-

// Creating a Package named bank

package bank;

public class BankAccount {


int accNo;
String name;
double balance;

// Constructor
public BankAccount(int accNo, String name, double balance) {
[Link] = accNo;
[Link] = name;
[Link] = balance;
}

// Deposit
public void deposit(double amount) {
balance += amount;
[Link]("Deposited: " + amount);
}

// Withdraw
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
[Link]("Withdrawn: " + amount);
} else {
[Link]("Insufficient balance!");
}
}

// Display
public void display() {
[Link]("Account No: " + accNo);
[Link]("Name: " + name);
[Link]("Balance: " + balance);
}
}

// Program to maintain bank account using package

import [Link];
import [Link];

public class BankDemoPackage {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter Account Number: ");


int accNo = [Link]();
[Link]();

[Link]("Enter Name: ");


String name = [Link]();

[Link]("Enter Initial Balance: ");


double balance = [Link]();

BankAccount acc = new BankAccount(accNo, name, balance);

[Link]("Enter amount to deposit: ");


double dep = [Link]();
[Link](dep);

[Link]("Enter amount to withdraw: ");


double wit = [Link]();
[Link](wit);

[Link]("\nAccount Details:");
[Link]();

[Link]();
}
}
OUTPUT:-
PRACTICAL NO. -

AIM: Write a java Program to take a string array as "100", "10.2", "[Link]", "100hello" and check
Whether it contains valid integers or doubles using exception handling.

SOURCE CODE:-

// Program to Check valid or Invalid inputs using exception handling

public class CheckNumbers {


public static void main(String[] args) {

String[] arr = {"100", "10.2", "[Link]", "100hello"};

for (String str : arr) {

// Check for Integer


try {
int i = [Link](str);
[Link](str + " -> Valid Integer");
}
catch (NumberFormatException e1) {

// Check for Double


try {
double d = [Link](str);
[Link](str + " -> Valid Double");
}
catch (NumberFormatException e2) {
[Link](str + " -> Invalid Number");
}
}
}
}
}
OUTPUT:-
PRACTICAL NO. -

AIM: Write a java program to show the working and usage of abstract class.

SOURCE CODE:-

// Program to show the usage and working of abstract classes

import [Link];

// Abstract Class
abstract class Shape {
double dim1, dim2;

// Constructor
Shape(double a, double b) {
dim1 = a;
dim2 = b;
}

// Abstract method (no body)


abstract double area();

// Concrete method
void display() {
[Link]("Dimensions: " + dim1 + ", " + dim2);

}
}

// Derived Class - Rectangle


class Rectangle extends Shape {

Rectangle(double a, double b) {
super(a, b);
}

// Implementing abstract method


double area() {
return dim1 * dim2;
}
}
// Derived Class - Triangle
class Triangle extends Shape {

Triangle(double a, double b) {
super(a, b);
}

// Implementing abstract method


double area() {
return 0.5 * dim1 * dim2;
}
}

// Main Class
public class AbstractDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter length and breadth of Rectangle: ");


double l = [Link]();
double b = [Link]();

[Link]("Enter base and height of Triangle: ");


double base = [Link]();
double height = [Link]();

// Using abstract class reference


Shape s;

// Rectangle object
s = new Rectangle(l, b);
[Link]();
[Link]("Rectangle Area: " + [Link]());

// Triangle object
s = new Triangle(base, height);
[Link]();
[Link]("Triangle Area: " + [Link]());

[Link]();
}
}
OUTPUT:-

You might also like