JAVA PROGRAMMING LAB
ICT-312P
University School of Information Communication and
Technology
Guru Gobind Singh Indraprastha University
Dwarka, Sector 16C, New Delhi
PRACTICAL FILE
Submitted By : Varun Verma SUBMITTED TO :
Enrolment No. : 60516401524 Dr. Jaspreeti Singh
Course : B. Tech (IT) 3RD YR
Semester : 6th SEM
PRACTICAL – 1
AIM : Program to print the reverse of the numbers; the numbers is
taken as input from the user.
SOURCE CODE:
import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num, reverse = 0, rem;
[Link]("Enter a number: ");
num = [Link]();
while (num != 0) {
rem = num % 10;
reverse = reverse * 10 + rem;
num = num / 10;
}
[Link]("Reversed number = " + reverse);
[Link]();
}
}
OUTPUT SCREEN :-
PRACTICAL - 2
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:
class Rectangle {
int length;
int width;
Rectangle(int l, int w) {
length = l;
width = w;
}
Rectangle(Rectangle r) {
length = [Link];
width = [Link];
}
void display() {
[Link]("Length: " + length);
[Link]("Width: " + width);
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle(10, 5);
Rectangle r2 = new Rectangle(r1);
[Link]();
[Link]();
}
}
OUTPUT SCREEN :-
PRACTICAL - 3
AIM : WAP to illustrate the concept of abstract class
SOURCE CODE:
abstract class Shape {
String name;
Shape(String name) {
[Link] = name;
}
abstract double area();
abstract double perimeter();
void display() {
[Link]("Shape: " + name);
}
}
class Rectangle extends Shape {
double length;
double width;
Rectangle(double length, double width) {
super("Rectangle");
[Link] = length;
[Link] = width;
}
double area() {
return length * width;
}
double perimeter() {
return 2 * (length + width);
}
}
class Circle extends Shape {
double radius;
Circle(double radius) {
super("Circle");
[Link] = radius;
}
double area() {
return [Link] * radius * radius;
}
double perimeter() {
return 2 * [Link] * radius;
}
}
class Triangle extends Shape {
double a, b, c;
Triangle(double a, double b, double c) {
super("Triangle");
this.a = a;
this.b = b;
this.c = c;
}
double area() {
double s = (a + b + c) / 2;
return [Link](s * (s - a) * (s - b) * (s - c));
}
double perimeter() {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Shape s1 = new Rectangle(10, 5);
Shape s2 = new Circle(7);
Shape s3 = new Triangle(3, 4, 5);
Shape[] shapes = {s1, s2, s3};
for (Shape s : shapes) {
[Link]();
[Link]("Area: " + [Link]());
[Link]("Perimeter: " + [Link]());
[Link]();
}
}
}
OUTPUT SCREEN :-
PRACTICAL - 8
AIM : Program to maintain bank account. Extend Bank account details
to current and saving account.
SOURCE CODE:
class BankAccount {
String accountHolder;
int accountNumber;
double balance;
BankAccount(String name, int accNo, double bal) {
accountHolder = name;
accountNumber = accNo;
balance = bal;
}
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]("Name: " + accountHolder);
[Link]("Account Number: " + accountNumber);
[Link]("Balance: " + balance);
}
}
class SavingsAccount extends BankAccount {
double interestRate;
SavingsAccount(String name, int accNo, double bal, double rate) {
super(name, accNo, bal);
interestRate = rate;
}
void addInterest() {
double interest = balance * interestRate / 100;
balance += interest;
[Link]("Interest Added: " + interest);
}
}
class CurrentAccount extends BankAccount {
double overdraftLimit;
CurrentAccount(String name, int accNo, double bal, double limit) {
super(name, accNo, bal);
overdraftLimit = limit;
}
void withdraw(double amount) {
if (amount <= balance + overdraftLimit) {
balance -= amount;
[Link]("Withdrawn: " + amount);
} else {
[Link]("Overdraft limit exceeded");
}
}
}
public class Main {
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount("Rahul", 101, 5000, 5);
[Link]();
[Link](1000);
[Link]();
[Link](2000);
[Link]();
[Link]();
CurrentAccount ca = new CurrentAccount("Abhishek", 102, 3000, 2000);
[Link]();
[Link](500);
[Link](4000);
[Link](2000);
[Link]();
}
}
PRACTICAL – 9
AIM : Program to maintain Bank Account using packages.
public class SavingsAccount extends BankAccount {
public double rate;
public SavingsAccount(String name, int accNo, double balance, double rate) {
super(name, accNo, balance);
[Link] = rate;
}
public void addInterest() {
balance += balance * rate / 100;
}
}
package bank;
public class CurrentAccount extends BankAccount {
public double limit;
public CurrentAccount(String name, int accNo, double balance, double limit) {
super(name, accNo, balance);
[Link] = limit;
}
public void withdraw(double amount) {
if (amount <= balance + limit) {
balance -= amount;
} else {
[Link]("Limit Exceeded");
}
}
}
import [Link];
import [Link];
public class BankMain {
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount("Rahul", 101, 5000, 5);
[Link](1000);
[Link]();
[Link]();
[Link]();
CurrentAccount ca = new CurrentAccount("Amit", 102, 3000, 2000);
[Link](500);
[Link](4000);
[Link]();
}
}
OUTPUT SCREEN :-
PRACTICAL - 10
AIM : Program to take a String arrays as “100” , “10.2”, “[Link]”,
“100hello” and check whether it contains valid integer or double using
exception handling.
SOURCE CODE:
public class Main {
public static void main(String[] args) {
String[] arr = {"100", "10.2", "[Link]", "100hello"};
for (String s : arr) {
try {
int i = [Link](s);
[Link](s + " is a valid Integer");
} catch (Exception e1) {
try {
double d = [Link](s);
[Link](s + " is a valid Double");
} catch (Exception e2) {
[Link](s + " is not a valid number");
}
}
}
}
}
OUTPUT SCREEN :-