Anilkumar Java OopsAssignment
Anilkumar Java OopsAssignment
Write a program that would print the information (name, year of joining, salary, address,
WorkingHrs) of employees by creating a class named 'Employee'.
Methods:
1 - 'getInfo()' which takes the number of hours of work per day of employee as parameter and
display the details of employee using displayDetails()
method 2 - 'addAppraisal()' contain array of employee as a parameter, which adds $10 to salary of
the employee if it is less than $500 and $15 for employee it is grater than or equal to 500 and display
the details.
3 - 'AddWork()' which adds $5 to salary of employee if the number of hours of work per day is more
than 6 hours.
PROGRAM:
package employee;
import java.util.*;
String name,address,year;
int salary,work;
this.name=name;
this.year=year;
this.address=address;
this.salary=salary;
this.work=work;
if(salary<500) {
salary+=10;
}
else {
salary+=15;
if(work>6) {
salary+=5;
while(true) {
switch(option)
case 1:
obj.addAppraisal(name,year,address,salary,work);
break;
case 2:
obj.AddWork();
break;
case 3:
obj.displayDetails();
break;
case 4:
break;
if(status!=1) {
break;
}
OUTPUT:
2. Lets create a bank account. Create a class named 'BankAccount' with the following data
members.
1 - Name of depositor
2 - Address of depositor
3 - Type of account
4 - Balance in account
5 - Number of transactions
Class 'BankAccount' has a method for each of the following 1 - Generate a unique account
number for each depositor.
For first depositor, account number will be BA1000, for second depositor it will be BA1001 and
so on
1 - Enter the information (name, address, type of account, balance) of the depositors. Number
of depositors are to be entered by user.
3 - Add some amount to the account of any depositor and then display final information of that
depositor
4 - Remove some amount from the account of any depositor and then display final informaion
of that depositor
5 - Change the address of any depositor and then display the final information of that depositor
6 - Randomly repeat these processes for some other bank accounts and after that print the total
number of transactions.
PROGRAM:
import java.util.Scanner;
public class BankApp {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Bank myBank = new Bank();
int user_choice = 2;
do {
//display menu to user
//ask user for his choice and validate it (make sure it is between 1 and 6)
System.out.println();
System.out.println("1) Open a new bank account");
System.out.println("2) Deposit to a bank account");
System.out.println("3) Withdraw to bank account");
System.out.println("4) Print short account information");
System.out.println("5) Print the detailed account information including last transactions");
System.out.println("6) Quit");
System.out.println();
System.out.print("Enter choice [1-6]: ");
user_choice = s.nextInt();
switch (user_choice) {
case 1: System.out.println("Enter a customer name");
String cn = s.next();
System.out.println("Enter a opening balance");
double d = s.nextDouble();
System.out.println("Account was created and it has the following number: " +
myBank.openNewAccount(cn, d));
break;
case 2: System.out.println("Enter a account number");
int an = s.nextInt();
System.out.println("Enter a deposit amount");
double da = s.nextDouble();
myBank.depositTo(an, da);
break;
case 3: System.out.println("Enter a account number");
int acn = s.nextInt();
System.out.println("Enter a withdraw amount");
double wa = s.nextDouble();
myBank.withdrawFrom(acn, wa);
break;
case 4: System.out.println("Enter a account number");
int anum = s.nextInt();
myBank.printAccountInfo(anum);
break;
//case 5: ... break;
}
}
while (user_choice != '6');
}
// Creates a new bank account using the customer name and the opening balance given as
parameters
// and returns the account number of this new account. It also adds this account into the account list
// of the Bank calling object.
public int openNewAccount(String customerName, double openingBalance) {
// Withdraws the given amount from the account whose account number is given. If the account is
// not available at the bank, it should print a message.
public void withdrawFrom(int accountNum, double amount) {
for (int i =0; i<numOfAccounts; i++) {
if (accountNum == accounts[i].getAccountNum() ) {
accounts[i].withdraw(amount);
System.out.println("Amount withdrawn successfully");
return;
}
}
System.out.println("Account number not found.");
}
// Deposits the given amount to the account whose account number is given. If the account is not
// available at the bank, it should print a message.
public void depositTo(int accountNum, double amount) {
for (int i =0; i<numOfAccounts; i++) {
if (accountNum == accounts[i].getAccountNum() ) {
accounts[i].deposit(amount);
System.out.println("Amount deposited successfully");
return;
}
}
System.out.println("Account number not found.");
}
// Prints the account number, the customer name and the balance of the bank account whose
// account number is given. If the account is not available at the bank, it should print a message.
public void printAccountInfo(int accountNum) {
for (int i =0; i<numOfAccounts; i++) {
if (accountNum == accounts[i].getAccountNum() ) {
System.out.println(accounts[i].getAccountInfo());
return;
}
}
System.out.println("Account number not found.");
}
// Prints the account number, the customer number and the balance of the bank account whose
// account number is given, together with last n transactions on that account. If the account is not
// available at the bank, it should print a message.
public void printAccountInfo(int accountNum, int n) {
for (int i =0; i<numOfAccounts; i++) {
if (accountNum == accounts[i].getAccountNum() ) {
System.out.println(accounts[i].getAccountInfo());
System.out.println(accounts[i].getTransactionInfo(n));
return;
}
}
System.out.println("Account number not found.");
}
}
static class BankAccount{
if (amount<=0) {
System.out.println("Amount to be deposited should be positive");
} else {
balance = balance + amount;
transactions[numOfTransactions] = amount;
numOfTransactions++;
}
}
public void withdraw(double amount)
{
if (amount<=0){
System.out.println("Amount to be withdrawn should be positive");
}
else
{
if (balance < amount) {
System.out.println("Insufficient balance");
} else {
balance = balance - amount;
transactions[numOfTransactions] = amount;
numOfTransactions++;
}
}
}
}
}
OUTPUT :
Inheritance
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
5 – Salary
It also has a method named 'printSalary' which prints the salary of the members
Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee' and
'Manager' classes have data members 'specialization' and 'department' respectively. Now, assign
name, age, phone number, address and salary to an employee and a manager by making an
object of both of these classes and print the same.
PROGRAM:
class Ans{
public static void main(String[] args){
Employee e = new Employee();
e.name = "xyz";
e.age = 23;
e.number = "986****";
e.address = "xyzxyz";
e.salary = 1231;
e.specialization = "xyzxyz";
Output:
Polymorphism – Overriding
Q1) Create a base class Fruit with name ,taste and size as its attributes. Create a method called
eat() which describes the name of the fruit and its taste. Inherit the same in 2 other classes
Apple and Orange and override the eat() method to represent each fruit taste.
import java.util.*;
class Fruit{
String name;
String taste;
int size;
void eat(){
void eat(){
void eat(){
class HelloWorld {
Fruit obj;
obj.eat();
obj.eat();
OUTPUT: