Вы находитесь на странице: 1из 20

Problem-1:

(i) PROBLEM DEFINITION: Create an abstract class Account as shown below:


a. Data Members:
i. accountNumber
ii. accountHolderName
iii. address
iv. balance
b. Methods:
i. withdraw () - abstract
ii. deposite () - abstract
iii. display () to show the balance of the account.
Create a subclass of Account named SavingsAccount that has the following details:
a. Data Members:
i. rateOfInterest
b. Methods:
i. calculateAmount () to calculate the total balance after calculating interest.
ii. display () to display rate of interest with new balance and full account holder details.
Create another subclass of Account named CurrentAccount that has the following details:
a. Data members:
i. overdraftLimit
b. Methods:
i. display () to show overdraft limit along with the full account holder details.
Create objects of these two classes and call their methods. Use appropriate constructors.

(ii) CODE:
import java.util.*;
import java.text.*;

abstract class Account {


long accountNumber;
String accountHolderName;
String address;
double balance;

public Account(long acNo, String acHN, String ad, double bal) {


this.accountNumber = acNo;
this.accountHolderName = acHN;
this.address = ad;
this.balance = bal;
}

public abstract void withdraw(double a);


public abstract void deposite(double b);
public abstract double calculateAmount(long c);
public void display(int count) {
System.out.println("The Account Balance is = "+balance);
}
}

class SavingsAccount extends Account {


float rateOfInterest;

public SavingsAccount(long acNo, String acHN, String ad, double bal) {


super(acNo,acHN,ad,bal);
this.rateOfInterest = 4;
}
public void withdraw(double am) {
balance -= am;
}
public void deposite(double am) {
balance += am;

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);
}
}

class CurrentAccount extends Account {


double overdraftLimit;
public CurrentAccount(long acNo, String acHN, String ad, double bal) {
super(acNo,acHN,ad,bal);
this.overdraftLimit = 20000;
}
public void withdraw(double am) {
if(am<=overdraftLimit)
balance -= am;
else
System.out.println("You can't withdraw over Rs."+overdraftLimit);
}
public void deposite(double am) {
balance += am;
}
public double calculateAmount(long noOfDays) {
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("OVER DRAFT LIMIT: "+overdraftLimit);
System.out.println("NEW BALANCE: "+balance);
}
}

public class AccountMain {


public static long dayDifference(Date dateBefore, Date dateAfter) {
long daysBetween = 0;
try {
long difference = dateAfter.getTime() - dateBefore.getTime();
daysBetween = (difference / (1000*60*60*24));
} catch(Exception e) {
e.printStackTrace();
}
return daysBetween;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number of Account Holder: ");
int size = sc.nextInt();
Account[] ac = new Account[size];
sc.nextLine();

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.*;

abstract class Person {


String name;

public Person(String n) {
this.name = n;
}
public abstract void display(int ct1, int ct2);
}
class Employee extends Person {
int empNo;
String address;

public Employee(int no, String n, String ad) {


super(n);
this.empNo = no;
this.address = ad;
}
public void display(int ct1, int ct2) {
System.out.println("\n******** EMPLOYEE-"+ct1+" ********"
+ "\nEmployee No.: "+empNo
+ "\nEmployee Name: "+name
+ "\nAddress: "+address);
}
}
class Worker extends Person {
int workingHours;

public Worker(String n, int wh) {


super(n);
this.workingHours = wh;
}
public void display(int ct1, int ct2) {
System.out.println("\n******** WORKER-"+ct2+" ********"
+ "\nName of the Worker: "+name
+ "\nWoring Hours: "+workingHours);
}
}

public class PersonMain {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number of Persons: ");
int size = sc.nextInt();
Person[] p = new Person[size];
int[] ch = new int[size];

for(int i=0; i<size; i++) {


System.out.print("Enter the type of Person ( Employee=1 / Worker=2 )
: ");
ch[i] = sc.nextInt();
if(ch[i] == 1) {
System.out.print("Enter the Employee no.: ");
int no = sc.nextInt();
sc.nextLine();
System.out.print("Enter the Name of the Employee: ");
String n = sc.nextLine();

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);
}

class Sum implements Numbers{


public int process(int x, int y) {
return x+y;
}
}

class Average implements Numbers {


public int process(int x, int y) {
return (x+y)/2;
}
}

public class NumbersMain {


public static void main(String[] args) {
Numbers n;
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print(" 1.Calculate Summation"
+ "\n 2.Calculate Average"
+ "\n 3.Exit"
+ "\nChoose any One: ");
int ch = sc.nextInt();
if(ch==3) {
System.out.println("Exiting...");
System.exit(0);
}
System.out.print("Enter the First Number: ");
int firstNumber = sc.nextInt();
System.out.print("Enter the Second Number: ");
int secondNumber = sc.nextInt();
int result;
if(ch==1) {
n = new Sum();
result = n.process(firstNumber, secondNumber);
System.out.println("The Sum of "+firstNumber+" and
"+secondNumber+" is = "+result);
} else if(ch==2) {
n = new Average();
result = n.process(firstNumber, secondNumber);
System.out.println("The Average of "+firstNumber+" and
"+secondNumber+" is = "+result);
} else {
System.out.println("Invalid Input Given!!!");
}
}
}
}

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);
}

class Result implements Examination, Classify {


public boolean pass(int[] marks) {
int size = marks.length;
for(int i=0; i<size; i++) {
if(marks[i]<50)
return false;
}
return true;
}
public String division(int average) {
if(average>=60)
return "First";
else if(average>=50)
return "Second";
else
return "No Division";
}
}

public class ExamMain {


public static void main(String[] args) {
Examination ex = new Result();
Classify cl = new Result();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number of Subjects: ");
int noOfSub = sc.nextInt();
int[] marks = new int[noOfSub];
int sum = 0, avg;
for(int i=0; i<noOfSub; i++) {
System.out.print("Enter the Marks Obtained in Subject-"+(i+1)+": ");
marks[i] = sc.nextInt();
sum += marks[i];
}
sc.close();
boolean isPass = ex.pass(marks);
avg = sum/noOfSub;
String div = cl.division(avg);
String res;
if(isPass == true)
res = "Pass";
else
res = "Fail";
System.out.println("\nRESULT: "+res);
System.out.println("DIVISION: "+div);
}
}

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);
}

class Sphere implements Shape {


public double calculateArea(double r, double h) {
return 4*3.14*r*r;
}
public double calculateVolume(double r, double h) {
return 4*3.14*r*r*r/3;
}
}
class Cone implements Shape {
public double calculateArea(double r, double h) {
double l = h*h+r*r;
return 3.14*r*(r+Math.sqrt(l));
}
public double calculateVolume(double r, double h) {
return 3.14*r*r*h/3;
}
}
class Cylinder implements Shape {
public double calculateArea(double r, double h) {
return (2*3.14*r*h)+(2*3.14*r*r);
}
public double calculateVolume(double r, double h) {
return 3.14*r*r*h;
}
}
public class TestShape {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Shape s;
while(true) {
System.out.print(" 1.Sphere"
+ "\n 2.Cone"
+ "\n 3.Cylinder"
+ "\nChoose any Shape: ");
int ch = sc.nextInt();
if(ch!=1 && ch!=2 && ch!=3) {
System.out.println("Invalid Input Given!!!");
}
double radius, height, area, vol;
if(ch==1) {
s = new Sphere();
System.out.print("Enter the radius: ");
radius = sc.nextDouble();
area = s.calculateArea(radius, 0);
vol = s.calculateVolume(radius, 0);
System.out.println("The Area of the Sphere is = "+area);
System.out.println("The Volume of the Sphere is = "+vol);
} else if(ch==2 || ch==3) {
System.out.print("Enter the radius: ");
radius = sc.nextDouble();
System.out.print("Enter the height: ");
height = sc.nextDouble();

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();
}

public class StackTest implements Stack{


int[] stack = new int[1000];
int maxSize;
int sp=-1;

public int push(int ele) {


if(sp==maxSize-1) {
System.out.println("Stack Overflow!\nCreating a new Stack of bigger
size");
return 0;
}
else {
stack[++sp] = ele;
System.out.println(ele+" Pushed Successfully");
return 1;
}
}
public void pop() {
if(sp==-1)
System.out.println("Stack Underflow!");
else {
System.out.println("Popped Element = "+stack[sp--]);
}
}
public boolean isEmpty() {
if(sp==-1)
return true;
else
return false;
}
public void display() {
if(isEmpty()) {
System.out.println("\nThere are no elements in the Stack.");
return;
}
System.out.println("\nStack Elements:");
for(int i=sp;i>=0;i--)
System.out.println(stack[i]);
}
public static void main(String[] args) {
StackTest s1 = new StackTest();
Scanner sc = new Scanner(System.in);
s1.maxSize = 2;
while(true) {
System.out.print("\n1.PUSH\n2.POP\n3.IS THE STACK EMPTY??\n4.DISPLAY
STACK\n5.EXIT\nEnter your Choice: ");

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);
}

class StringReversal implements Reversal {


public void reversal(String str1, char[] str2) {
int len = str1.length();
for(int i=len-1; i>=0; i--) {
str2[len-1-i] = str1.charAt(i);
}
}
}

public class StringReversalTest {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringReversal sr = new StringReversal();
System.out.println("Enter Any String: ");
String original = sc.nextLine();
sc.close();
char[] reverse = new char[original.length()];
sr.reversal(original, reverse);
String rev = new String(reverse);
System.out.println("The Reverse of the Given String is: "+rev);
}
}

(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;

public class Addition {


int firstNumber;
int secondNumber;
public Addition(int fn, int sn) {
this.firstNumber = fn;
this.secondNumber = sn;
}
public int add() {
return this.firstNumber+this.secondNumber;
}
}

TestPackage.java
import java.util.Scanner;
import pack.Addition;

public class TestPackage {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter First Number: ");
int fn = sc.nextInt();
System.out.print("Enter Second Number: ");
int sn = sc.nextInt();
sc.close();
Addition a = new Addition(fn, sn);
int sum = a.add();
System.out.println("The Summation is = "+sum);
}
}

(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;

public class FactorialIterative {


public long factorialI(int number)
{
if(number == 0 || number == 1)
return 1;
int fact = 1;
for(int i = number; i>=1; i--)
fact = fact*i;
return fact;
}
}

FactorialRecursive.java
package recursive;

public class FactorialRecursive {


public long factorialR(int number) {
if(number == 0 || number == 1)
return 1;
else
return number*factorialR(number-1);
}
}

TestFactorial.java
import java.util.*;
import iterative.*;
import recursive.*;

public class TestFactorial {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any Integer Value: ");
int number = sc.nextInt();
sc.close();
FactorialRecursive fr = new FactorialRecursive();
long fact1 = fr.factorialR(number);
System.out.println("THE FACTORIAL OF "+number+" USING RECURSIVE METHIOD IS
= "+fact1);
FactorialIterative fi = new FactorialIterative();
long fact2 = fi.factorialI(number);
System.out.println("THE FACTORIAL OF "+number+" USING ITERATIVE METHIOD IS
= "+fact2);
}
}

61
(iii) OUTPUT:

62

Вам также может понравиться