University of Engineering and Technology, Taxila: Final Exam

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

University of Engineering and Technology, Taxila

(Software Engineering Department)

FINAL EXAM

Subject: Object Oriented Programming

Submitted By: Zain Iqbal

Registration No: 19-SE-28

Section: Omega (Ω)

Submitted To: Madam Madiha Liaqat


Question 1:
If you are required to make a Notepad application which framework you will use? What components you
will add to your program? List them. Can you identify one problem in current Notepad application that
needs to be resolved?

Answer
There are different frameworks that can be used to make Notepad application but I will use Java
Swing framework. Because Java Swing is platform independent and its components are
lightweight.

Components:
The components which are needed for our program are given below:
JFrame
TextField
JTextArea
JPanel
JLabel
JOptionPane
JDialog
JMenuItem
JColorChooser
JCheckBox
JMenuBar
JFileChooser
Clipboard

Problems:
The problem in current Notepad is that it does not autosave text. There are high chance that user
can lose his work if system shuts down unexpectedly. So, this problem needs to be resolved.
Question 2:
Consider the following two classes:
public class A {
public void mOne(int i) {
} public void mTwo(int i) {
} public static void mThree(int i) {
} public static void mFour(int i) {
}}
public class B extends A {
public static void mOne(int i) {
} public void mTwo(int i) {
} public void mThree(int i) {
} public static void mFour(int i) {
}}
Answer the following:

a: Which method overrides a method in the superclass?


Ans: “mTwo()” method overrides a method in the superclass because “mTwo()” method of superclass
(A) and subclass (B) have the same access modifier and return type.

b: Which method hides a method in the superclass?


Ans: “mFour()” method hides a method in the superclass because “mFour()” method in both superclass
(A) and subclass (B) is static.

c: What do the other methods do?


Ans: “mThree()” cannot override a method in the superclass because “mThree()” method in superclass
(A) is static while in subclass (B) it is not static. “mOne” cannot hide method in the superclass because
“mOne()” method in superclass (A) is non-static while it is static in the subclass (B).
Question 3:
Write a Java program that uses multi-threaded concepts. The program should consists of main class
namely MathThreads. Main thread starts three more threads namely FSeries, RNumber and SqNumber.
These threads run different tasks, i.e., generate Fabonacci Series, generate random number and compute
square of a number.

Answer:
CODE:

import java.util.Random;
import java.util.Scanner;

class FSeries extends Thread {


int n, a, b, c;

public void run() {


Scanner sc = new Scanner(System.in);
System.out.print("Enter limit of series: ");
n = sc.nextInt();
a = 0;
b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
c = a + b;
a = b;
b = c;
}
System.out.println("\n");
}
}
class RNumber extends Thread {
public void run() {
Random r = new Random();
int n = r.nextInt();
System.out.println("Random number: " + n);
if ((n % 2) == 0)
System.out.println("Random number is even\n");
else
System.out.println("Random number is odd\n");
}
}

class SqNumber extends Thread {


int n;

public void run() {


Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
n = sc.nextInt();
System.out.println("Square of " + n + " is: " + n * n); // generates the square of the
number
}
}

public class MathThreads {


public static void main(String args[]) throws Exception {
FSeries th1 = new FSeries();
RNumber th2 = new RNumber();
SqNumber th3 = new SqNumber();
th1.start();
th1.join();
th2.start();
th2.join();
th3.start();
}
}

OUTPUT:

You might also like