0% found this document useful (0 votes)
21 views

Assignment# 2

Uploaded by

Haider Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Assignment# 2

Uploaded by

Haider Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment #2

Task-1
public class Employee {
private String name;
private int idNumber;
private String department;
private String position;

public Employee(String name, int idNumber, String department, String position) {


this.name = name;
this.idNumber = idNumber;
this.department = department;
this.position = position;
}

public Employee(String name, int idNumber) {


this(name, idNumber, "", "");
}

public Employee() {
this("", 0, "", "");
}

public void setName(String name) {


this.name = name;
}

public void setIdNumber(int idNumber) {


this.idNumber = idNumber;
}

public void setDepartment(String department) {


this.department = department;
}

public void setPosition(String position) {


this.position = position;
}

public String getName() {


return name;
}
public int getIdNumber() {
return idNumber;
}

public String getDepartment() {


return department;
}

public String getPosition() {


return position;
}
}

public class Main {


public static void main(String[] args) {
Employee employee1 = new Employee("John Smith", 12345, "Sales", "Manager");
Employee employee2 = new Employee("Jane Doe", 67890, "Marketing", "Coordinator");
Employee employee3 = new Employee();

System.out.println("Employee 1:");
System.out.println("Name: " + employee1.getName());
System.out.println("ID Number: " + employee1.getIdNumber());
System.out.println("Department: " + employee1.getDepartment());
System.out.println("Position: " + employee1.getPosition());

System.out.println("Employee 2:");
System.out.println("Name: " + employee2.getName());
System.out.println("ID Number: " + employee2.getIdNumber());
System.out.println("Department: " + employee2.getDepartment());
System.out.println("Position: " + employee2.getPosition());

System.out.println("Employee 3:");
System.out.println("Name: " + employee3.getName());
System.out.println("ID Number: " + employee3.getIdNumber());
System.out.println("Department: " + employee3.getDepartment());
System.out.println("Position: " + employee3.getPosition());
}
}

Output is
Employee 1:
Name: John Smith
ID Number: 12345
Department: Sales
Position: Manager
Employee 2:
Name: Jane Doe
ID Number: 67890
Department: Marketing
Position: Coordinator
Employee 3:
Name:
ID Number: 0
Department:
Position:

Task -2
public class BankAccount {
private double balance;

public BankAccount() {
balance = 0.0;
}

public BankAccount(double startBalance) {


balance = startBalance;
}

public BankAccount(String str) {


balance = Double.parseDouble(str);
}

public void deposit(double amount) {


balance += amount;
}

public void deposit(String str) {


double amount = Double.parseDouble(str);
balance += amount;
}

public void withdraw(double amount) {


balance -= amount;
}

public void withdraw(String str) {


double amount = Double.parseDouble(str);
balance -= amount;
}
public void setBalance(double balance) {
this.balance = balance;
}

public void setBalance(String str) {


double balance = Double.parseDouble(str);
this.balance = balance;
}

public double getBalance() {


return balance;
}
}

public class Main {


public static void main(String[] args) {
BankAccount account = new BankAccount(100.0);

System.out.println("Current balance: " + account.getBalance());

account.deposit(50.0);
System.out.println("After depositing $50.0: " + account.getBalance());

account.withdraw(25.0);
System.out.println("After withdrawing $25.0: " + account.getBalance());

account.setBalance(200.0);
System.out.println("After setting balance to $200.0: " + account.getBalance());
}
}

Output is
Current balance: 100.0
After depositing $50.0: 150.0
After withdrawing $25.0: 125.0
After setting balance to $200.0: 200.0

Task-3

You might also like