Final Examination Semester 2 / Year 2012 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student s ID : Batch No. : Notes to candidates: 1) The question paper consists of 7 pages Section A: 10 questions (20 marks) Section B: 2 questions (20 marks) Section C: 3 questions (60 marks) 2) Answer all questions. 3) Write your answer for Section A and Section B in answer booklet. 4) Save your answer for question in Section C and submit your answer in softcopy to Southern College E-Learning System.
SECTION A: MULTIPLE CHOICE QUESTION (10 X 2 = 20 MARKS) Please choose most appropriate answer. State the answer in the answer booklet. 1. When is a finally{ block executed? a. Only when an exception is thrown by a try block. b. Only when there are no exceptions thrown. c. At the end of a program. d. Always after the execution of a try block, regardless of whether or not an exception is thrown. 2. If a negative value is used for an array index,. a. the last index of the array is automatically accessed instead b. an IndexOutOfBoundsException is thrown c. the program termates immediately d. a NumberFormatException is thrown 3. Which of the following is NOT a reserved word in Java? a. finally b. class c. constant d. private 4. Which of the following is used to initialize a string with 987? a. String[] str = { 9, 8, 1 ; b. String[] str = {9, 8, 7; c. String str = new String( 987 ); d. String str = 987; 5. Which of the following is not a method of class JTextField? a. addactionlistener b. settext c. setvisible d. seteditable 6. Which of the following method is used to catch an error that occurs when opening a file? a. OpenFileException b. FileException c. FileOpenException d. IOException 1/6
7. Which of the following is NOT correct about the Graphics components? a. Import the java.awt.graphics to draw the graphics components such as polygon, rectangles and so on. b. Override the paintcomponent method to tell the container how to draw things. c. The coordinate system located at the upper-right corner of the window. d. Each component has its own coordinate system with the origin (0,0). 8. Suppose s1 and s2 are two strings. Which of the following is NOT correct expression? a. String s3 = s1 + s2 b. String s3 = s1 s2 c. if(s1 == s2) d. int i = s2.length(); 9. A subclass inherits from its superclasses. a. private data b. protected data c. public data d. b and c 10. Which of the following is INCORRECT statements? a. The contents of a string can be partially changed. b. You can add, insert, or delete characters from a string buffer c. You can create a string buffer from a string d. You can convert a string buffer into a string. 2/6
SECTION B: SHORT QUESTIONS (20 MARKS) Question 1 (10 Marks) a) What is displayed on the console when the following program is executed? (5 Marks) public static void main (String[] args) { String s = "Good luck*for your*final*examination"; StringTokenizer st = new StringTokenizer(s, "*", true); while (st.hasmoretokens()){ System.out.println(st.nextToken()); b) What is the output for the following codes? (You are not required to show the actual position of the graphic components. You just need to sketch out what is display and how it looks like when the following codes are executed.) (5 Marks) import javax.swing.*; import java.awt.*; public class DrawGraphic extends JPanel{ public static void main(string[] args) { JFrame frame = new JFrame("Draw Graphic"); frame.add(new DrawGraphic()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(300, 200); frame.setvisible(true); protected void paintcomponent(graphics g) { super.paintcomponent(g); g.drawrect(10,10, 110,110); g.drawline(20, 90, 90, 90); g.drawline(60, 20, 90, 90); g.drawline(20, 90, 60, 20); 3/6
Question 2 (10 Marks) JAVA PROGRAMMING a. In the following code, classes A and B are in the same package. What are the possible values can be replaced in the question marks? (5 Marks) package p1; public class A{?(i) int i;?(ii) void m(){ (a) package p1; public class B extends A{ public static void main (String[] args) { System.out.println(i); m(); (b) b. There are two errors in the following classes. Identify the problems and rewrite the statement with correct codes. (5 Marks). 1 public class Test{ 2 String test; 3 public void Test(String s){ 4 this.test = s; 5 6 public static void main(string[] args) { 7 Test test = new Test("A"); 8 System.out.println(test); 9 10 4/6
SECTION C: CODING QUESTIONS (60 MARKS) Submit your answer in softcopy and upload it to Southern E-Learning System. Question 1 (25 Marks) Design a class named BMI contains: (1 mark) a) A double data field name weight that specifies the weight of the person in kilogram. (1 mark) b) A double data field name height that specifies the height of the person in centimeter. (1 mark) c) An int data field named name that specifies the name of the person. (1 mark) d) A no-arg constructor that creates a default instance. (2 marks) e) A constructor that creates an instance with the specified name, weight and height. (3 marks) f) The accessor and mutator methods for all the data fields. (3 marks) g) A method named calculatebmi() that returns the the BMI value. (5 marks) h) A method named tostring() that return a string description for the name, weight, height and BMI of the person. (3 marks) Write a test program named as TestBMI that creates a BMI object with a name (Alex Tan), weight (60Kg) and height (160cm). Your program is then display the BMI object by invoking their tostring method. (5 marks) Question 2(24 Marks) You need to create an application that allows user to enter an integer number. The application will display a multiplication table that shows a result of multiplying the user entered number by numbers 1 through 9. If the number entered by user is not an integer number then you need to show an error message in the label (as shown in figure 3). You need to work on the application as follows: a) Create a user interface as show in figure 1. (8 marks) Figure 1: User interface 5/6
b) Write the function for showing the multiplication table when user types an integer number in the text box. In figure 2, it will show the multiplication result in label while user enters number 8. (8 marks) c) Write the exception handler function to handle the number format. If user enters an input which is not an integer number, then the application should display the error message as show in figure 3. (5 marks) d) Write the function for exit button. (3 marks) Figure 2: While user enters number 8 in the textbox, it will show the multiplication result in the label. Figure 3: While user enters a letter a, it will show an error message in the label. Question 3 (11 Marks) For this question, you need to download a text file which named as number.txt from e-learning system. The downloaded text file contains an unspecified number of scores. Write a program that reads the scores from the file and display their total and average. 000 6/6