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

CSE1115_Final_191

The document is a final exam paper for the Object-Oriented Programming course at United International University, covering various topics including anonymous inner classes, file reading, exception handling, and GUI creation in Java. It consists of multiple coding tasks requiring students to fix code snippets, write Java programs, and implement exception classes. The exam is structured with specific questions and marks allocated for each task.

Uploaded by

Anik Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CSE1115_Final_191

The document is a final exam paper for the Object-Oriented Programming course at United International University, covering various topics including anonymous inner classes, file reading, exception handling, and GUI creation in Java. It consists of multiple coding tasks requiring students to fix code snippets, write Java programs, and implement exception classes. The exam is structured with specific questions and marks allocated for each task.

Uploaded by

Anik Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

United International University (UIU)

Dept. of Computer Science and Engineering (CSE)


FINAL EXAM :: SPRING 2019
Course Code: CSI 211 Course Title: Object-Oriented Programming
Date: 30/04/19 Total Marks: 40 Time: 2 Hours

1. a) Create an object with reference type A using an Anonymous Inner Class. [4]
abstract class A {
public void printSum(int a, int b) {
System.out.println(a + b);
}
public abstract void printSum(int a, int b, int c);
public abstract void printSum(int a, int b, int c, int d);
}
b) Fix the following code and re-write the correct one. You cannot remove any lines. You can only add or edit
existing lines. [4]

class C
{
static int outer_x = 10;
int outer_y = 20; public class MainClass
private int outer_private = 30; {
class D public static void main(String[] args)
{ {
void display() C.D obj = new C.D();
{ obj.display();
System.out.println("outer_x = " + outer_x); }
System.out.println("outer_y = " + outer_y); }
System.out.println("outer_private = "
outer_private);
}
}
}
2. You are given a text file named “numbers.txt” which contains some numbers in each line that are separated with
commas. Write a Java program to read the file and for each line print the max of the numbers in console. A sample
input and output is provided below: [8]

Input.txt Console
10,11,12 12
2,13 13
33,22,1,1 33
1 1
3. a) Fix the following code and re-write the correct one. You cannot remove any lines. You can only add or edit
existing lines. [4]

public class MyTread implements Runnable{ public class Main{


String name; public static void main(String[] args) {
public MyTread(String name) { MyTread t1 = new MyTread("First Thread");
this.name = name; t1.start();
} t1.join();
public void run(int n){ }
System.out.printf("Running:%s %d }
times.\n", name, n);
}
}

b) Consider the following java program. [4]

I. Sort students ArrayList in ascending order by cgpa


II. Sort students ArrayList in descending order by n
class Student class Main
{ {
float cgpa; public static void main (String[] args)
String name; {
ArrayList<Student> students = new
public Student(float cgpa, String name) ArrayList<Student>();
{ students.add(new Student(3.44, "Afnan"));
this.cgpa = cgpa; students.add(new Student(2.1, "Ullash"));
this.name = name; }
}
}
public String toString()
{
return this.cgpa + " " + this.name;
}
}
4. a) Observe the code below, the sample run/output and create the Exception Class mentioned in the code;
LowBatteryException, so that the program produces the following outputs. [4]

import java.util.Scanner; Sample run/output


public class MyException{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); Enter current charge percent: 25

for(int i = 0; i < 2; i++) { Enough charge in battery.


System.out.print("Enter current charge percent: ");
int chargeAmount = sc.nextInt();
try { Enter current charge percent: 16
if (chargeAmount <= 20)
throw new LowBatteryException(chargeAmount); Battery is low! Should be above 20.
else Current value: 16
System.out.println("Enough charge in battery.");
} catch (LowBatteryException e) {
System.out.println(e.getMessage());
}
}
}
}
b) Consider the following java program. The values of String s, integer b and c are taken as input from the user. Write the
output of the program for the following values of s, b and c: [4]

1. 2. 3. 4.
s = "a" s = "2" s = "20" s = "100"
b=5 b=1 b=0 b = 200
c = 10 c=3 c=6 c = 10
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try{
String s = sc.next();
int a = Integer.valueOf(s);
int b = sc.nextInt();
int c = sc.nextInt();
int [] array = new int[5];
int d = a / b;
array[c] = d;
System.out.println(array[c]);
}
catch (NumberFormatException e){
System.out.println("Input was not an Integer.");
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array index should be less than 5");
}
catch (ArithmeticException e){
System.out.println("Can not divide by 0");
}
}
}
Submit this page with your answer sheet
Name: ID:
5. Complete the code to get a gui like this. [8]

//Import Necessary package here

public class Menu {


Menu()
{
JFrame myframe;
myframe=new JFrame();
myframe.setLayout(new BorderLayout());
//Set Frame title here

myframe.setSize(200, 200);
JPanel center = new JPanel();
center.setLayout(new GridLayout(3,3));
JLabel cell[] = new JLabel[9];
for(int i=0; i<9; i++){
//Complete the code to add label 0 to 9 and add to necessary panel

cell[i].setFont(new Font("Cambria", 2, 24));


}
JPanel top = new JPanel();
top.setLayout(new GridLayout());
//Add code for button Change and Reset and add to necessary panel

myframe.add(top, BorderLayout.NORTH);
myframe.add(center, BorderLayout.CENTER);
//Set frame visibility true

}
public static void main(String[] args) {
new Menu();
}
}

You might also like