Lab Copy 3
Lab Copy 3
(ii) CODE:
import java.util.*;
import java.text.*;
43
}
public double calculateAmount(long noOfDays) {
balance = balance + balance*noOfDays*rateOfInterest/(100*365);
return balance;
}
public void display(int count) {
System.out.println("\n********** ACCOUNT HOLDER-"+count+" **********");
System.out.println("NAME: "+accountHolderName);
System.out.println("ACCOUNT NO.: "+accountNumber);
System.out.println("ADDRESS: "+address);
System.out.println("RATE OF INTEREST: "+rateOfInterest);
System.out.println("NEW BALANCE: "+balance);
}
}
for(int i=0;i<size;i++) {
System.out.println("Enter the Name of the Account Holder "+(i+1)+":
");
String nm = sc.nextLine();
System.out.println("Enter the Account no. of the Account Holder
"+(i+1)+": ");
44
long acNo = sc.nextLong();
System.out.println("Enter the Address of the Account Holder
"+(i+1)+": ");
sc.nextLine();
String ad = sc.nextLine();
System.out.print("Enter the Account opening Date of the Account
Holder "+(i+1)+": ");
String dayBefore = sc.nextLine();
System.out.print("Enter the Amount deposited while opening Account:
");
double bal = sc.nextDouble();
System.out.print("Enter the type of the Account of Account Holder
"+(i+1)+"(Savings/Current): ");
sc.nextLine();
String type = sc.nextLine();
if(type.equalsIgnoreCase("Savings")) {
ac[i] = new SavingsAccount(acNo, nm, ad, bal);
System.out.print(" Enter the Number of Transactions of Account
Holder "+(i+1)+": ");
int noOfTrans = sc.nextInt();
Date dateBefore = null;
Date dateAfter = null;
for(int j=0;j<noOfTrans;j++) {
System.out.print(" Enter the type of Transaction
(Deposite/Withdrawal): ");
sc.nextLine();
String typeOfTrans = sc.nextLine();
System.out.print(" Enter the Date of "+typeOfTrans+":
");
String dateOfTrans = sc.nextLine();
SimpleDateFormat myFormat = new
SimpleDateFormat("dd/MM/yyyy");
try {
dateBefore = myFormat.parse(dayBefore);
dateAfter = myFormat.parse(dateOfTrans);
} catch(Exception e) {
e.printStackTrace();
}
if(typeOfTrans.equalsIgnoreCase("Deposite")) {
System.out.print(" Enter the Amount to be
Deposited: ");
double am = sc.nextDouble();
ac[i].calculateAmount(dayDifference(dateBefore,
dateAfter));
ac[i].deposite(am);
dayBefore = dateOfTrans;
} else {
System.out.print(" Enter the Amount to be
Withdrawn: ");
double am = sc.nextDouble();
ac[i].calculateAmount(dayDifference(dateBefore,
dateAfter));
ac[i].withdraw(am);
dayBefore = dateOfTrans;
}
}
Date currentDate = new Date();
ac[i].calculateAmount(dayDifference(dateAfter, currentDate));
} else {
ac[i] = new CurrentAccount(acNo, nm, ad, bal);
System.out.print(" Enter the Number of Transactions of Account
Holder "+(i+1)+": ");
int noOfTrans = sc.nextInt();
for(int j=0;j<noOfTrans;j++) {
System.out.print(" Enter the type of Transaction
(Deposite/Withdrawal): ");
sc.nextLine();
45
String typeOfTrans = sc.nextLine();
System.out.print(" Enter the Date of "+typeOfTrans+":
");
String dateOfTrans = sc.nextLine();
if(typeOfTrans.equalsIgnoreCase("Deposite")) {
System.out.print(" Enter the Amount to be
Deposited: ");
double am = sc.nextDouble();
ac[i].deposite(am);
} else {
System.out.print(" Enter the Amount to be
Withdrawn: ");
double am = sc.nextDouble();
ac[i].withdraw(am);
}
}
}
sc.nextLine();
}
sc.close();
for(int i=0;i<size;i++)
ac[i].display(i+1);
}
}
(iii) OUTPUT:
46
Problem-2:
(i) PROBLEM DEFINITION: Create an abstract class Person. Define two subclasses Employee and
Worker from it. Use proper method to accept and display the details for the same. The fields of Employee are
empNo, empName, address. Similar fields for Worker are name and workingHours.
(ii) CODE:
import java.util.*;
public Person(String n) {
this.name = n;
}
public abstract void display(int ct1, int ct2);
}
class Employee extends Person {
int empNo;
String address;
47
System.out.println("Enter the Address of the Employee: ");
String ad = sc.nextLine();
p[i] = new Employee(no, n, ad);
} else if(ch[i] == 2) {
System.out.print("Enter the Name of the Worker: ");
sc.nextLine();
String n = sc.nextLine();
System.out.print("Enter the Working-hour of the Worker: ");
int wh = sc.nextInt();
p[i] = new Worker(n, wh);
} else {
System.out.print("Invalid Input!");
i--;
}
}
sc.close();
int j = 1, k = 1;
for(int i=0; i<size; i++) {
p[i].display(k, j);
if(ch[i] == 2)
j++;
else
k++;
}
}
}
(iii) OUTPUT:
48
Problem-3:
(i) PROBLEM DEFINITION: Write an interface called Numbers with a method int process (int x, int y).
Write a class called Sum, int which the method process () finds the sum of two numbers and returns an integer
value. Write another class called Average, in which the process () method finds the average of the two numbers
and returns an integer value.
(ii) CODE:
import java.util.*;
interface Numbers {
public int process(int x, int y);
}
49
(iii) OUTPUT:
50
Problem-4:
(i) PROBLEM DEFINITION: Write an interface Examination with a method pass (int marks) that returns
a Boolean value. Write another interface called classify with a method division (int average) which returns a
string. Write a class Result which implements both Examination and Classify. The pass () method should
return true if the is greater than or equal to 50 else false. The method division() should return “First” when the
parameter average is 60 or more, “Second” when the average is 50 or more but less than 60, “No Division”
when average is less than 50.
(ii) CODE:
import java.util.*;
interface Examination {
public boolean pass(int[] marks);
}
interface Classify {
public String division(int average);
}
51
(iii) OUTPUT-1:
OUTPUT-2:
52
Problem-5:
(i) PROBLEM DEFINITION: Create an interface Shape. Derive three classes Sphere, Cone and Cylinder
from it. Calculate area and volume of all (using method overriding).
(ii) CODE:
import java.util.*;
import java.lang.Math;
interface Shape {
public double calculateArea(double radius, double height);
public double calculateVolume(double radius, double height);
}
53
if(ch == 2) {
s = new Cone();
area = s.calculateArea(radius, height);
vol = s.calculateVolume(radius, height);
System.out.println("The Area of the Cone is = "+area);
System.out.println("The Volume of the Cone is = "+vol);
} else {
s = new Cylinder();
area = s.calculateArea(radius, height);
vol = s.calculateVolume(radius, height);
System.out.println("The Area of the Cylinder is = "+area);
System.out.println("The Volume of the Cylinder is = "+vol);
}
}
System.out.print("Do you want to Continue? (Y/N): ");
char c = sc.next().charAt(0);
if(c == 'N' || c == 'n') {
System.out.println("Exiting...");
System.exit(0);
}
}
}
}
(iii) OUTPUT:
54
Problem-6:
(i) PROBLEM DEFINITION: Design an interface named stack with the following methods:
a. To push elements into the stack.
b. To pop elements from the stack.
c. To check whether the stack is empty or not.
Implement the stack with the help of array if the size of the array becomes too small to hold the elements,
create a new one. Test this interface by inheriting by it in its subclass StackTest.java.
(ii) CODE:
import java.util.*;
interface Stack {
public int push(int ele);
public void pop();
public boolean isEmpty();
public void display();
}
55
int ch = sc.nextInt();
int element;
switch(ch) {
case 1:
System.out.print("Enter the Element to be Pushed: ");
element = sc.nextInt();
int result = s1.push(element);
if(result == 0) {
s1.maxSize++;
s1.push(element);
}
break;
case 2:
s1.pop();
break;
case 3:
boolean b = s1.isEmpty();
if(b)
System.out.println("Yes, The Stack is Empty.");
else
System.out.println("No, The Stack is not Empty.");
break;
case 4:
s1.display();
break;
case 5:
System.exit(0);
default:
System.out.println("INVALID INPUT!");
}
}
}
}
(iii) OUTPUT:
56
Problem-7:
(i) PROBLEM DEFINITION: Design an interface named Queue with the following methods:
a. To add elements into the queue.
b. To remove elements from the Queue.
c. To check whether the stack is empty or not.
Implement the queue with the help of array and if the size of the array becomes too small tohold the element,
create a new one. Test this interface by inheriting it in its subclass QueueTest.java.
(ii) CODE:
import java.util.*;
interface Queue {
public int insert(int ele);
public void delete();
public boolean isEmpty();
public void display();
}
public class QueueTest implements Queue{
int[] q = new int[100];
int maxSize = 2, front = 0, rear = 0;
public int insert(int ele) {
if(rear-front == maxSize) {
System.out.println("Queue is Full!\nCreating a new Queue of bigger
size...");
return 0;
}
else {
q[rear] = ele;
System.out.println(ele+" inserted Successfully");
rear++;
return 1;
}
}
public void delete() {
if(rear==front)
System.out.println("Queue is Empty!");
else {
System.out.println(q[front]+" deleted Successfully");
for(int i=1;i<rear;i++)
q[i-1] = q[i];
rear--;
}
}
public boolean isEmpty() {
if(rear==front)
return true;
else
return false;
}
public void display() {
if(isEmpty()) {
System.out.println("\nThere are no elements in the Queue.");
return;
}
System.out.println("QUEUE ELEMENTS:");
if(front==rear)
System.out.println("Queue is Empty!");
for(int i=front;i<rear;i++)
System.out.print(q[i]+" ");
}
public static void main(String[] args) {
QueueTest q1 = new QueueTest();
Scanner sc = new Scanner(System.in);
while(true) {
57
System.out.print("\n1.INSERT ELEMENT\n2.DELETE ELEMENT\n3.IS THE
QUEUE EMPTY??\n4.DISPLAY QUEUE\n5.EXIT\nEnter your Choice: ");
int ch = sc.nextInt();
int element;
switch(ch) {
case 1:
System.out.print("Enter the Element to be Inserted: ");
element = sc.nextInt();
int result = q1.insert(element);
if(result == 0) {
q1.maxSize++;
q1.insert(element);
}
break;
case 2:
q1.delete();
break;
case 3:
boolean b = q1.isEmpty();
if(b)
System.out.println("Yes, The Queue is Empty.");
else
System.out.println("No, The Queue is not Empty.");
break;
case 4:
q1.display();
break;
case 5:
System.exit(0);
default:
System.out.println("INVALID INPUT!");
}
}
}
}
(iii) OUTPUT:
58
Problem-8:
(i) PROBLEM DEFINITION: Design an interface with a method reversal. This method takes String as its
input and returns the reserved String. Create a class StringReversal that implements the method [do not use
predefined methods].
(ii) CODE:
import java.util.*;
interface Reversal {
public void reversal(String str1, char[] str2);
}
(iii) OUTPUT:
59
Problem-9:
(i) PROBLEM DEFINITION: Write a program to create a package named pack and store Addition class
in it. Now create another class TestPackage containing main() and use and object of Addition class in the
main() method. Place TestPackage class in the default package. In this cases run the program TestPackage
without using an IDE.
(ii) CODE:
Addition.java
package pack;
TestPackage.java
import java.util.Scanner;
import pack.Addition;
(iii) OUTPUT:
60
Problem-10:
(i) PROBLEM DEFINITION: Create a class FactorialIterative which defines a method for finding out the
factorial of a given number in iteratively. Create another class c which defines a method for finding out the
factorial of a given number in recursively. Place the FactorialIterative class in “iterative” package and the
FactorialRecursive class in “recursive” package. Create a class TestFactorial containing main() method in
“default” package which uses both classes (i.e. FactorialIterative and FactorialRecursive) to find out the
factorial of a number. Compile and run this program without using an IDE.
(ii) CODE:
FactorialIterative.java
package iterative;
FactorialRecursive.java
package recursive;
TestFactorial.java
import java.util.*;
import iterative.*;
import recursive.*;
61
(iii) OUTPUT:
62