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

Assignment 01 Fall 2020

The document describes an assignment to create classes that model different levels of employees in a company by using inheritance. It involves creating an abstract Employee class with subclasses for Managers, Executives, and Clerks that each add additional data members and methods specific to that type of employee. The main method demonstrates creating objects of each subclass and using their methods to insert and display employee data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Assignment 01 Fall 2020

The document describes an assignment to create classes that model different levels of employees in a company by using inheritance. It involves creating an abstract Employee class with subclasses for Managers, Executives, and Clerks that each add additional data members and methods specific to that type of employee. The main method demonstrates creating objects of each subclass and using their methods to insert and display employee data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Ali Jawed Nikzad

37975
ITC 225 Programming II
Dr. Ahmad Aljanad
Fall 2020
Assignment 01 (ITC 225)

The objective of this assignment is to revise inheritance and to understand the


concepts of Abstract class & Interface in Java

1) Imagine a company that has multiple levels of employees, from the managers,
executives down to clerk. Draw the object relationship diagram that shows a class of
Employees, where data like Employee No (integer), name, address and tel. no are
stored. Directly derived from the Employee class, are three other classes, which are
Managers, Executives and Clerks.
(1 mark)
a) From the object relationship diagram in the question above, program the class Employee. It
should have:
- (public) constructor: set the employeeNo to 0, name to “John Doe”, address to
“nowhere” and tel no to “n/a”.
- (public) insertData() function to insert all the data members from the user
- (public) and a displayData() function to display the value of all the data members.

(1 mark)

package assignment.pkg1;

import java.util.Scanner;

public class Employee {

public Scanner input = new Scanner(System.in);


private int employeeNo;
private String name;
private String address;
private String telNo;

public Employee() {

employeeNo = 0;
name = "John Doe";
address = "nowhere";
telNo = "n/a";
}

public void insertData(){

System.out.println("Please Enter employee's number: ");


employeeNo = input.nextInt();
System.out.println("Please Enter employee's name: ");
name = input.next();
System.out.println("please Enter employee's address: ");
address = input.next();
System.out.println("please Enter employee's telNo: ");
telNo = input.next();
}

public void displayData(){

System.out.println("Employee's Number: "+employeeNo);


System.out.println("Employee's Name: "+name);
System.out.println("Employee's Address: "+address);
System.out.println("Employee's Tel Number: "+telNo);
}
}
b) Now program the class Managers that is a derived class from the Employee class.

In the Managers class, include:

Data members

a. (private) jobTitle : the title of the manager’s job i.e. “General Manager”
b. (private) companyCar: the company’s car the manager entitled to.

Function members

c. (public) constructor, set the jobTitle and companyCar to “unknown”.


d. (public) insertData (), sets the jobTitle and companyCar.
e. (public) displayData(), display all the data.
(1 mark)

package assignment.pkg1;
public class Manager extends Employee{
private String jobTitle;
private String companyCar;

public Manager(){
jobTitle = "unknown";
companyCar = "unknown";
}

@Override
public void insertData(){
System.out.println("Enter Employee's jobTitle: ");
jobTitle = input.next();
System.out.println("Please Enter company‘s Car: ");
companyCar = input.next();
}

@Override
public void displayData(){
System.out.println("Employee's JobTitle: "+ jobTitle);
System.out.println("Company’s Car: "+ companyCar);
}
}
c) In the main function, create an object E of the class Employee. Use the displayData() function
and you should have this displayed on the screen:

(1 mark)
package assignment.pkg1;
public class Assignment1 {
public static void main(String[] args) {
Employee E = new Employee();
E.displayData();

Output:

d) Now create an object M of the class Managers. Use the insertData() function and later the
displayData function, refer to the screenshot below:

(1 mark)
package assignment.pkg1;
public class Assignment1 {
public static void main(String[] args) {
Manager M = new Manager();
M.insertData();
M.displayData();

Output:

e) Create a class Executives, which is also derived from the class Employee. For this class, include:

Data member

- (private) dept: the dept where the executive is assigned to i.e. “IT”

Function members

- (public) constructor, set the dept to “unknown”.


- (public) insertData (), sets the dept.
- (public) displayData(), display all the data.

(1 mark)
package assignment.pkg1;
public class Executives extends Employee{
private String dept;

public Executives(){
dept = "unknown";
}
@Override
public void insertData(){
System.out.println("Executives: ");
super.insertData();
System.out.println("Enter Employee's Department: ");
dept = input.next();
}
@Override
public void displayData(){
System.out.println("Executives...........");
super.displayData();
System.out.println("Employee's department is: "+dept);
}
}

f) Create a class Clerks, which is also derived from the class Employee. For this class, include:

Data members

- (private) overtimeRate: rate per hour for overtime i.e $5


- (private) overtimeHour: number of hours of overtime.

Function members

- (public) contructor, set the overtimeRate and overtimeHour to 0.


- (public) insertData (), sets the overtimeRate and overtimeHour.
- (public) displayData(), display all the data, including total Overtime which is equal to
overtimeRate * overtimeHour.
(1 mark)
package assignment.pkg1;
public class Clerks extends Employee{
private double overTimeRate;
private double overTimeHour;

public Clerks(){
overTimeHour = 0;
overTimeHour = 0;
}
@Override
public void insertData(){
System.out.println("Clerks");
super.insertData();
System.out.println("Please enter overtime rate: ");
overTimeRate = input.nextDouble();
System.out.println("Please enter overtime hour: ");
overTimeHour = input.nextDouble();
}
@Override
public void displayData(){
System.out.println("Clerks...........");
super.displayData();
System.out.println("Employee's overtime Rate: "+overTimeRate);
System.out.println("Employee's overtime Hour: "+overTimeHour);
System.out.println("Employee's Total overtime:
"+overTimeHour*overTimeRate);
}
}

g) In the main function, create object Ex of the class Executives and object C of the Clerks object.
Display the data members of these objects right after you have created them. Example of result:

package assignment.pkg1;

public class Assignment1 {


public static void main(String[] args) {

Executives Ex = new Executives();


Clerks C = new Clerks();

Ex.displayData();
C.displayData();
Output:

h) Now use the insertData() function to insert data into the object Ex and C.
Executives -----

Enter employee's number:

Enter employee's name:

Ahmed

Enter employee's address:

Kabul

Enter employee's phone number:

0709

Enter employee's department:

IT

Clerks -----

Enter employee's number:

99

Enter employee's name:

Ali

Enter employee's address:

88

Enter employee's phone number:


33

Enter overtime rate:

90

Enter overtime hour:

Executives -----

Employee's Number : 1

Employee's Name : Ahmed

Employee's Name : Kabul

Employee's Tel Number : 0709

Employee's department: IT

Clerks -----

Employee's Number : 99

Employee's Name : Ali

Employee's Name : 88

Employee's Tel Number : 33

Employee's Overtime Rate: 90.0

Employee's Overtime Rate: 9

Total overtime: 810.0

(1 mark)
package assignment.pkg1;
public class Assignment1 {
public static void main(String[] args) {
Executives Ex = new Executives();
Clerks C = new Clerks();

Ex.insertData();
C.insertData();

Ex.displayData();
C.displayData();
}
}

Output:
2) Create an abstract class MotorVehicle with the following details:
Data Members:
(a) modelName (b) modelNumber (c) modelPrice
Methods:
(a) display() to show all the details :
(2 Marks)

package assignement.pkg11;

abstract public class MotorVehicle {


String modelName = "Benz";
int modelNumber = 2020;
double modelPrice = 20000.5;

abstract public void display();


}

Create a subclass of this class Car that inherits the class MotorVehicle and add the following
details:
Data Members:
(a) discountRate
Methods:
(a) display() method to display the Car name, model number, price and the discount rate.
(b) discount() method to compute the discount.

package assignement.pkg11;

class Car extends MotorVehicle{

double discountRate = 10;


double discount;
@Override
public void display() {
System.out.println("The Car name is: "+modelName);
System.out.println("The car model number is: "+modelNumber);
System.out.println("The Car model price is: "+modelPrice);
System.out.println("The Car discount Rate is: "+discountRate);
}
public void discount(){
discount = modelPrice * discountRate/100;
System.out.println("Discount is: "+discount);
}
}
package assignement.pkg11;

public class Assignement11 {

public static void main(String[] args) {


Car c = new Car();
c.display();
c.discount();
}
}

Output:

You might also like