Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

Anilkumar Java OopsAssignment

The document discusses creating classes for an Employee and BankAccount with methods to add and display employee details and perform bank transactions. It includes code to create Employee and BankAccount classes with properties like name, salary, and balance, and methods like getInfo(), addAppraisal(), displayDetails(), deposit(), withdraw(), and print account information. The code provides examples of using the classes to add employee data, perform bank transactions, and display results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Anilkumar Java OopsAssignment

The document discusses creating classes for an Employee and BankAccount with methods to add and display employee details and perform bank transactions. It includes code to create Employee and BankAccount classes with properties like name, salary, and balance, and methods like getInfo(), addAppraisal(), displayDetails(), deposit(), withdraw(), and print account information. The code provides examples of using the classes to add employee data, perform bank transactions, and display results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Write a program that would print the information (name, year of joining, salary, address,
WorkingHrs) of employees by creating a class named 'Employee'.

Properties: Array of Employee objects

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.

4 - 'displayDetails()' display the details of employee details.

PROGRAM:

package employee;

import java.util.*;

public class Employee {

String name,address,year;

int salary,work;

Employee(String name,Stringyear,Stringaddress,intsalary,int work){

this.name=name;

this.year=year;

this.address=address;

this.salary=salary;

this.work=work;

public void addAppraisal(String name,Stringyear,Stringaddress,intsalary,int work) {

if(salary<500) {

salary+=10;

}
else {

salary+=15;

System.out.println("Name "+"Year of joining"+" Address"+" Salary "+" WorkingHrs");

System.out.println(name+" "+year+" "+address+" "+("$"+salary)+" "+work);

public void AddWork() {

if(work>6) {

salary+=5;

public void displayDetails() {

System.out.println("Name "+"Year of joining"+" Address"+" Salary "+" WorkingHrs");

System.out.println(name+" "+year+" "+address+" "+("$"+salary)+" "+work);

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

while(true) {

System.out.println("enter the name :");

String name = input.nextLine();

System.out.println("enter the year of joining :");

String year = input.nextLine();

System.out.println("enter the adress :");

String address = input.nextLine();

System.out.println("enter the salary :");

int salary = input.nextInt();

System.out.println("enter the working hrs");

int work = input.nextInt();


System.out.println("enter your option :");

int option = input.nextInt();

Employee obj = new Employee(name,year,address,salary,work);

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;

System.out.println("if you want to enter new employee data press 1");

int status = input.nextInt();

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

2 - Display information and balance of depositor

3 - Deposit more amount in balance of any depositor

4 - Withdraw some amount from balance deposited


5 - Change address of depositor.

After creating the class, do the following operations

1 - Enter the information (name, address, type of account, balance) of the depositors. Number
of depositors are to be entered by user.

2 - Print the information of any depositor.

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

static class Bank {


private BankAccount[] accounts; // all the bank accounts at this bank
private int numOfAccounts; // the number of bank accounts at this bank

// Constructor: A new Bank object initially doesn’t contain any accounts.


public Bank() {
accounts = new BankAccount[100];
numOfAccounts = 0;
}

// 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) {

BankAccount b = new BankAccount(customerName, openingBalance);


accounts[numOfAccounts] = b;
numOfAccounts++;
return b.getAccountNum();
}

// 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{

private int accountNum;


private String customerName;
private double balance;
private double[] transactions;
private int numOfTransactions;
private static int noOfAccounts=0;

public String getAccountInfo(){


return "Account number: " + accountNum + "\nCustomer Name: " + customerName +
"\nBalance:" + balance +"\n";
}

public String getTransactionInfo(int n)


{
numOfTransactions = n;
return n;

public BankAccount(String abc, double xyz){


customerName = abc;
balance = xyz;
noOfAccounts ++;
accountNum = noOfAccounts;
transactions = new double[100];
transactions[0] = balance;
numOfTransactions = 1;
}

public int getAccountNum(){


return accountNum;
}
public void deposit(double amount){

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

Q1) Create a class named 'Member' having the following members:

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:

import java.util.*; class Member{


String name;
int age;
String number;
String address;
int salary;

public void printSalary(){


System.out.println(salary);
}

class Employee extends Member{


String specialization;
}

class Manager extends Member{


String department;
}

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

Manager m = new Manager();


//Same goes for Manager
}
}

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{

Scanner input = new Scanner(System.in);

String name;

String taste;

int size;

void eat(){

System.out.println("The name of the fruit is :"+name);

System.out.println("The taste of "+name+"is creamy and fruity taste");

class Apple extends Fruit{

void eat(){

System.out.println("The taste of apple is sweet ");

class Orange extends Fruit{

void eat(){

System.out.println("The taste of orange is fruity taste");


}

class HelloWorld {

public static void main(String[] args) {

Fruit obj;

obj = new Apple();

obj.eat();

obj = new Orange();

obj.eat();

OUTPUT:

You might also like