BAHRIA UNIVERSITY (Karachi Campus) Department of Computer Science Mid Term Examination – Spring 2020 OBJECT-ORIENTED PROGRAMMING (CSC-210) (TAKE HOME ASSIGNMENT) Class : BSCS 2nd Semester (A/B) (Morning) Course Instructor(s) : Sameena Javaid Submission Deadline: 31st May 2020 Marks : 20 Student’s Name: ________________________________ Reg. # : ______________ QUESTION NO. 1 (Inheritance - Case Study) (10 Marks) Maintain an employee management system for a company. The following UML diagram shows the relationship between the classes. Employee - Name:String - Id :int - Qualifiction:String + Employee( ) + setName( String n): void + getName( ): String + setId( int i): void + getId( ): Int + setQualification(String q): void + getQualification( ): String + DisplayInfo( ) : void + getData( ):void MonthlySalEmployee - Basicpay : double - Allowances : double + getData ( ): void + setBasicpay( double bp): void + getBasicpay( ):double + setAllowances( double al): void + getAllowances( ): double + DisplayInfo( ) : void + SearchSalEmp( String name) : Boolean Company - arr1[ ] : MonthlySalEmployee + addMonEmp(a : MonthlySalEmployee) : void + displayEmpList( ) :void Page 2 of 4 a) Write in java the MonthlySalEmployee class having following attributes and behaviors: i. Basicpay: represents the basic pay of monthly salaried employee ii. Allowances : represents the allowances of monthly salaried employee. iii. getBasicpay() returns the employee basic pay & setBasicpay() change the employee basic pay iv. getAllowance( ) returns the employee allowance & setAllowance( ) changes the employee allowance v. getdata() takes information of employee as input vi. displayinfo() displays all information related to employee b) Write a Java class Company that contains the following attribute and methods: i. arr1[ ]: an array of objects of MonthlySalEmployee type ii. addMonEmp(a:MonthlaySalEmployee) adds objects of class MonthlySalEmployee in an array of objects (Max 5 employees can be added) QUESTION NO. 2 (reasoning questions) (05x1=05 Mark) Answer the following questions (maximum of 2 sentences) 1. Access specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Consider that java also take non-access specifiers to restrict access. If the developer is willing to define class attributes and methods such that these are shared by all the objects of the class? Help the developer to identify the type and name of specifier. 2. Predict output of the following and explain the working also. public class University{ public static void main(String[] args) { int[] list = new int[]{1, 3, 5, 7, 9, 11, 15, 17, 19, 21}; System.out.println(list[8%3]); } } Page 3 of 4 3. Is following code correct? Yes/No? Explain the answer in either cases. 4. Do you think the below code compiles successfully even though it is calling super class’s protected constructor outside the package? 5. Read following paragraph carefully and answer the question given after this paragraph: Write 2 features of JAVA specifically defined / discussed in above paragraph. package one; class A {} package two; class B extends A {} package one; public class A { protected A(int x) { //protected constructor } } package two; import one.A; class B extends A { public B() { super(100); //calling super class's protected constructor } } JVM stands for Java Virtual Machine. Java code is compiled down into an intermediary language called byte code. The Java Virtual Machine is then responsible for executing this byte code. This is unlike languages such as C++ which are compiled directly to native code for a specific platform. This is what gives Java its ‘Write once, run anywhere’ ability. Page 4 of 4 QUESTION NO. 3 (logic development) (2+2+1=05 Mark) Sparse matrices are matrices whose elements are predominantly zero. This question develops a Java representation for them called SparseMatrix. The code below seeks to use an ArrayList of LinkedLists to implement the concept efficiently. It defines a class Element to store the column number and value for an element. Each row is represented by a LinkedList of Elements with non-zero values only. Few, if any, rows are all zeros and so the ArrayList is used to store a LinkedList for every row in ascending row order. Read the given code carefully and answer the given questions: public class Element { public int column; public int value; } public class SparseMatrix { private int mRows; // Number of rows private int mCols; // Number of columns private ArrayList > mMatrix; // Data } 1. Give two reasons why Element should not have public state and provide a better mutable Element definition. 2. Explain why ArrayList and LinkedList are appropriate choices in this context. And if this is not an appropriate choice then which technique you will use to create a sparse matrix. 3. Write a constructor for SparseMatrix that takes arguments specifying the number of rows and columns and initializes state appropriately. ************************End of Paper**********************