CSE1115_Final_191
CSE1115_Final_191
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]
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]
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
myframe.add(top, BorderLayout.NORTH);
myframe.add(center, BorderLayout.CENTER);
//Set frame visibility true
}
public static void main(String[] args) {
new Menu();
}
}