Final 246
Final 246
C. Apply the concepts of GUI programming and event handling. 5 Part III
1/21
Part I: (15 Marks)
Question 1. [10 marks] Select the correct answer (Each has one mark)
int a = 25;
double b = 76.78956;
String c = "Sharjah";
System.out.println("12345678901234567890");
System.out.printf("%4d %5.2f%n%15s%n”, a, b, c);
a) 12345678901234567890
25 76.78956 Sharjah
b) 12345678901234567890
25 76.78956
Sharjah
c) 12345678901234567890
25 76.79
Sharjah
d) 12345678901234567890
25 76.79 Sharjah
String s1 = "Sharjah";
String s2 = new String("Sharjah");
String s3 = "Sharjah";
2/21
3. Consider the following method definition:
public boolean myMethod( const int[] arr1, const int[] arr2 )
{
if ( arr1.length() != arr2.length() )
return false;
return true;
}
a) This method checks if the lengths of two arrays are the same.
b) This method checks if the contents of two arrays are the same.
c) This method can also be used for double arrays because of the const keyword.
d) None of the above.
6. Which of the following concept in Object Oriented refers to the bundling of data with the mechanisms
or methods that hide, access, and operate on the data?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
3/21
7. Consider the following class hierarchy:
Animal
Mammal Bird
4/21
9. Which of the following defines a legal abstract class?
a) class abstractClass {
abstract void method() {
// method implementation
________;
}
}
b) class abstractClass {
abstract void method ();
}
a) interface myInterface {
public abstract void printObject();
}
b) interface myInterface {
public abstract void printObject() {
//method implementation
_________;
}
}
c) interface myInterface {
default void printObject();
}
d) interface myInterface {
public static void printObject()
}
5/21
Question 2. [5 marks] Trace the Code
myAnimal.printAnimal();
myCat.printAnimal();
}
}
Answer:
6/21
2. What is the content of myArray after executing the following code snippet? [2 Marks]
Answer:
3. What is the output of the following code snippet? The first line of the output is provided.
[1 Mark]
System.out.println("123456789012345");
System.out.printf("%5d %6.2f %n", num, rate);
Answer:
123456789012345
_______________
7/21
Part II (20 marks)
Q1. Implement a Java Application (15 marks)
You are asked to implement a Java application to keep track of which employee is working under which
manager in a company. You need to develop Person, Manager, and Employee classes as described
below. The main() method and its output are given as follows:
System.out.println("Managers:");
for (int i = 0; i < Manager.numberOfManager; i++)
System.out.println(managers[i].toString());
System.out.println();
managers[0].addStaff(employees[0]);
managers[0].addStaff(employees[1]);
managers[0].addStaff(employees[0]);
managers[0].addStaff(employees[2]);
managers[0].addStaff(employees[3]);
managers[1].addStaff(employees[2]);
System.out.println();
System.out.println("Employee:");
for (int i = 0; i < Employee.numberOfEmployee; i++)
System.out.println(employees[i].toString());
System.out.println();
System.out.println("Managers:");
for (int i = 0; i < Manager.numberOfManager; i++)
System.out.println(managers[i].toString());
}
8/21
Output:
Managers:
Manager: Name: Kasim Age: 52 Department: Marketing Number of Employee: 0
Manager: Name: Fatima Age: 45 Department: Personal Number of Employee: 0
Manager: Name: Ahmad Age: 55 Department: Production Number of Employee: 0
Employee:
Employee: Name: Farid Age: 23 Manager: Kasim Department: Marketing
Employee: Name: Aysha Age: 42 Manager: Kasim Department: Marketing
Employee: Name: Davud Age: 33 Manager: Fatima Department: Personal
Employee: Name: Hamdan Age: 52 Manager: Not assigned Department: Not assigned
Employee: Name: Aliya Age: 28 Manager: Not assigned Department: Not assigned
Managers:
Manager: Name: Kasim Age: 52 Department: Marketing Number of Employee: 3
Manager: Name: Fatima Age: 45 Department: Personal Number of Employee: 1
Manager: Name: Ahmad Age: 55 Department: Production Number of Employee: 0
9/21
1.1 [3 marks]
Implement the Person class which stores the name (String) and age (int) of a person. The class has the
following methods:
− A constractor with arguments ( name and age )
− get() and set() methods for the data fields.
− toString() method to return the name and age of the person.
Answer:
10/21
1.2 [6 marks]
Implement the Manager class which extends the Person class and stores the following data fields:
− The total number of managers created so far, numberOfManager.
− The department name of the manager, department.
− A list of Person taken by the manager, employeeList (Assume that a manager can take
up to 3 persons).
− The number of employees allocated to this manager, numberOfEmploee (Initially no
employee is allocated).
The class has the following methods:
− A constractor with arguments ( name, age, and department )
− get() and set() methods for the data fields.
− toString() method to return the name and age of the manager as well as his/her
department and the current number of employees allocated to his/her.
− addStaff() method to assign an employee to the manager.
First, if the manager’s employee list is full it outputs an error message (check the sample
output for the message) and returns false.
Then it checks whether the employee has already been added to the manager’s employee list
(search for the name of the Employee object in the employee list).
If the employee is on the list, output the error message as shown in the sample output and
return false.
11/21
Answer I.1.2:
12/21
Answer I.1.2 (continue):
13/21
1.3 [6 marks]
Implement the Employee class which extends the Person class and stores the following data fields:
− a Manager object, manager, indicating the manager of this employee (The default value is
null).
− The total number of employees created so far, numberOfEmployee.
The class has the following methods:
− A constractor with arguments ( name and age )
− getManager() method which returns the assigned manager’s name using the
getName() method of manager. If manager is null it returns the message “Not
assigned”.
− Department() method which returns the assigned department using the
getDepartment() method of manager. If manager value is null it returns the
message “Not assigned”.
− toString() method to return the name and age of the employee as well as his/her
manager’s name and department (Check the sample output).
Answer I.1.3:
14/21
Answer I.1.3 (continue):
15/21
Q2. Fill in the blanks (5 marks)
Rectangle () {
this(1.0, 1.0);
}
16/21
The following Test program generates the Sample Output:
public class Main {
public static void main(String[] args) {
System.out.println( myBox.toString() );
System.out.println( "Area: " + myBox.area() );
System.out.println( "Volume: " + myBox.volume() );
}
}
Sample Output:
< Box: [ Rectangle: side1 = 5.0 side2 = 4.0 ], Height: 6.0 >
Area: 148.0
Volume: 120.0
17/21
Fill out the following implementation of a class named Box using the Rectangle class as its superclass.
Box() {
__________________;
this.height = 1.0;
}
18/21
Part III : JavaFX (5 Marks)
MCQ: Select the correct one ( Each has a half mark)
a. Stage
b. Pane
c. Scene
d. Scanner
a. Pane is a container for JavaFX elements like text, shapes, and text fields.
b. The JavaFX main class implements javafx.application.Application class.
c. A Stage object called primaryStage is automatically created by the Java Virtual Machine
when the JavaFX application is started.
d. A JavaFX program can have only one stage which can accommodate multiple Panes.
4. Which of the following correctly sets the color of the outline of a rectangle object, rect, to red?
a. rect.setFill(color.RED);
b. rect.setStroke(Color.RED);
c. Rectangle.setStroke(color.RED);
d. Rect.setStrokeLine(Color.RED);
5. Which of the following ImageView method or methods is/are used for changing image size associated
with an ImageViwe object?
19/21
6. Which of the following is correct?
a. FlowPane arranges the nodes horizontally from left to right or vertically from top to bottom
in the order in which they were added.
b. Pane is the base class for layout panes.
c. In a GridPane, the nodes are placed in the specified row and column indices.
d. HBox and VBox lay out their children in a single horizontal row or a single vertical column,
respectively.
a. pane.add(node,2,3);
b. pane.getChildren().add(node);
c. pane.add(node);
d. pane.getChildren().add(node, 2, 3);
9. Which of the following is NOT correct for registering a source for an action event?
a. source.setOnActionHandler( myHandlerObject );
d. source.setOnAction( e -> {
//Handling the event
______________________;
});
20/21
10. Which of the following will change the text of a Button node, bt, between “OK” and “CANCEL” after
clicking the mouse?
b. bt.setOnMouseClicked(
bt.setText(“OK”), bt.setText(“CANCEL”) );
c. bt.setOnAction( e -> {
if ( bt.getText() == “OK”)
bt.setText(“CANCEL”);
else
bt.setText(“OK”);
});
d. bt.setOnMouseClicked ( e-> {
bt.changeText(“OK”, “CANCEL”);
});
21/21