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

Assignment 1

..n.

Uploaded by

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

Assignment 1

..n.

Uploaded by

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

FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY

Muhammad Usman

Reg # 64472

Assignment # 01

Object Oriented Programming

Page 1 of 8
FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY
Object Oriented
Course: Instructor: Sabah
Programming
Marks Allocated: 5 Marks Time Allocated: 01 Week
GA-2 Knowledge for
CLO – 2
CLO Allocated: GA Allocated: Solving Computing
(C3)
Problems

ASSIGNMENT # 01

Construct the programs for the following problems.

Question # 01

You are required to Build a class hierarchy to represent different types of employees in a
company. The base class should be Employee, and it should be extended by two derived
classes: Manager and Developer.

 The Employee class should have the following attributes:


o name (String): The name of the employee.
o employeeId (int): The employee ID.

The Employee class should also have a constructor to initialize the attributes and a
method displayDetails() that prints the employee's details.

 The Manager class should inherit from Employee and have an additional attribute:
o department (String): The department the manager is in charge of.

The Manager class should override the displayDetails() method to include the
department information.

 The Developer class should inherit from Employee and have an additional attribute:
o programmingLanguage (String): The programming language the developer
specializes in.

The Developer class should override the displayDetails() method to include the
programming language information.

Tasks:

1. Define the Employee class with the required attributes and methods.
2. Define the Manager and Developer classes that inherit from Employee, adding the
required attributes and methods.

Page 2 of 8
FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY
3. In the main class, create an array of Employee references that holds objects of both
Manager and Developer types. Iterate through the array and call displayDetails() on
each object.

package assignment;

class Employee {
private String name;
private int employeeId;

public Employee(String name, int employeeId) {


this.name = name;
this.employeeId = employeeId;
}

public void displayDetails() {


System.out.println("Name: " + name + ", Employee ID: " + employeeId);
}
}

class Manager extends Employee {


private String department;

public Manager(String name, int employeeId, String department) {


super(name, employeeId);
this.department = department;
}

public void displayDetails() {


super.displayDetails();
System.out.println("Department: " + department);
}
}

class Developer extends Employee {


private String programmingLanguage;

public Developer(String name, int employeeId, String programmingLanguage) {


super(name, employeeId);
this.programmingLanguage = programmingLanguage;
}

public void displayDetails() {


super.displayDetails();
System.out.println("Programming Language: " + programmingLanguage);
}

Page 3 of 8
FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY
}

class Main {
public static void main(String[] args) {

Employee emp1 = new Manager("Muhammad Usman", 001, "Database Management");


Employee emp2 = new Developer("Awais", 002, "Object Oriented Programming");
Employee emp3 = new Manager("Rayyan Yousuf", 003, "Account Department ");
Employee emp4 = new Developer("Muhammad Irfan", 004, "JavaScript");

Employee[] employees = { emp1, emp2, emp3, emp4 };

for (Employee employee : employees) {


employee.displayDetails();
System.out.println();
}
}
}

Question # 02

Develop a class BankAccount that represents a bank account. The class should include the
following:

 Properties:
o accountNumber (String)
o balance (double)

 Methods:
o A constructor that initializes the account with an account number and a
balance.

Page 4 of 8
FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY
o A method deposit(double amount) that deposits the specified amount into the
account.
o An overloaded method deposit(double amount, String message) that deposits
the specified amount into the account and prints a confirmation message.
o A method withdraw(double amount) that withdraws the specified amount from
the account if sufficient balance is available.

Tasks:

1. Implement the BankAccount class in Java.


2. Create a main method in another class to demonstrate the usage of the BankAccount
class by creating objects and calling the methods.
3. Demonstrate the method overloading feature by making deposits using both deposit()
methods.

package assignment;

public class BankAccount {


private String accountNumber;
private double balance;

public BankAccount(String accountNumber, double balance) {


this.accountNumber = accountNumber;
this.balance = balance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposit of Rs:" + amount + " was successful.");
} else {
System.out.println("Invalid deposit amount.");
}
}

public void deposit(double amount, String message) {


if (amount > 0) {
balance += amount;
System.out.println(message + " Deposit of Rs:" + amount + " was successful.");
} else {
System.out.println("Invalid deposit amount.");
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawal of Rs:" + amount + " was successful.");

Page 5 of 8
FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY
} else if (amount > balance) {
System.out.println("Insufficient balance for withdrawal.");
} else {
System.out.println("Invalid withdrawal amount.");
}
}

public void displayAccountDetails() {


System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: Rs:" + balance);
}

public static void main(String[] args) {

BankAccount account1 = new BankAccount("1********0", 500.00);

account1.displayAccountDetails();
System.out.println();

account1.deposit(1500.00);
account1.displayAccountDetails();
System.out.println();

account1.deposit(2000.00, "Special bonus:");


account1.displayAccountDetails();
System.out.println();

account1.withdraw(2500.00);
account1.displayAccountDetails();
System.out.println();

account1.wi thdraw(300
0.00);

account1.di splayAccou
ntDetails();
}
}

Page 6 of 8
FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY

Question # 03

Construct a JavaFX application in Eclipse that displays a registration form for a workshop.
The form should include the following components:

1. Labels for each field: "First Name", "Last Name", "Email", "Phone Number", and
"Workshop Selection".
2. Text fields for "First Name", "Last Name", "Email", and "Phone Number".
3. A ComboBox for "Workshop Selection" with the following options:

"Java Basics",
"Advanced Java",
"Web Development with JavaFX",
"Java for Mobile Applications".

4. A Submit Button that, when clicked, displays the entered information in a new
window.

Requirements:

 Arrange the components using a suitable layout (e.g., GridPane).


 Implement basic input validation (e.g., ensure that no fields are left empty and that the
email format is correct).
 Display the information entered by the user in a new window or an alert dialog when
the "Submit" button is clicked.

Page 7 of 8
FACULTY OF ENGINEERING, SCIENCES AND TECHNOLOGY

Page 8 of 8

You might also like