Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2011 COURSE COURSE CODE TIME DEPARTMENT LECTURER : JAVA PROGRAMMING : PROG1114 : 2 1/2 HOURS : COMPUTER SCIENCE : LIM PEI GEOK Student s ID Batch No. : : Notes to candidates: 1) The question paper consists of 8 pages Section A: 10 questions (20 marks) Section B: 2 questions (20 marks) Section C: 2 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. A class, method or data is accessible to all clients. A method or data is only accessible inside the class. a. public, private b. public, protected c. private, public d. none, none 2. Identify which of the following is the concept of constructors? a. Constructors of superclass are inherited in the subclass. b. The return type of constructor is void. c. Constructor is invoke using the new operator when an object is created. d. A constructor called default constructor is provided automatically for all classes. 3. Which of the following is NOT a reserved word in Java? a. constant b. double c. class d. final 4. Which of the following method use to return a new String object that remove all white-space character in the String s? a. s.remove() b. s.trim() c. s.remove(, ) d. s.trim(, ) 5. Which of the following is a NOT javax.swing component? a. JLabel b. JPanel c. JFrame d. Button 6. Which of the following is not a method of class JTextField? a. addactionlistener b. settext c. setvisible d. seteditable 7. Which of the following is NOT kind of layout manager? a. GridLayout b. BorderLayout c. FlowLayout d. TableLayout 1/7
8. Which of the following is NOT correct about the Graphics components? a. Each component has its own coordinate system with the origin (0,0). b. The coordinate system located at the upper-right corner of the window. c. Import the java.awt.graphics to draw the graphics components such as polygon, rectangles and so on. d. Override the paintcomponent method to tell the container how to draw things. 9. Which of the following events will be trigger if mouse is pressed, released, clicked, entered and exited? a. addmousemotionlistener() b. addactionlistener() c. MouseExited() d. MouseClose() 10. 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 2/7
SECTION B: SHORT QUESTIONS (20 MARKS) Question 1 (12 Marks) a) What is displayed on the console when the following program is executed? (4 Marks) public static void main(string[] args) { String s1 = new String("Thank you"); String s2 = thank you"; if(s1.equals(s2)){ System.out.println("s1 and s2 have the same content"); else{ System.out.println("s1 and s2 do not have the same content"); if(s1 == s2){ System.out.println("s1 and s2 have the same reference"); else{ System.out.println("s1 and s2 do not have the same reference"); b) What is the output for the following codes? (You no need to show the actual position of the graphic components. You just need to sketch out what is display and how it look like when the following codes is executed.) (4 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.drawstring(""good luck!", 40, 60); 3/7
c) Show the output for the following program. (4 Marks) public static void main (String[] args) { String s = "Hello+!Nice+to-meet+you."; StringTokenizer st = new StringTokenizer(s, "+!"); while (st.hasmoretokens()){ System.out.println(st.nextToken() + " "); Question 2 (8 Marks) a. Suppose that s1 and s2 are two Strings. Which of the following statements or expression are incorrect? State the line number of the incorrect statements or expression. (3 Marks) 1 String s = new String( new string ); 2 String s3 = s1 + s2; 3 String s4 = s1 s2; 4 s1 == s2 5 s1 >= s2 6 int i = s1.length(); 7 char c = s1(0); b. There are two errors in the following classes. Identify the problems and rewrite the statement with correct codes. (5 Marks). 1 class Rectangle{ 2 private double length; 3 private double width; 4 public Rectangle(double length, double width){ 5 this.length = length; 6 this.width = width; 7 8 double findarea(){ 9 return length * width; 10 11 class Box extends Rectangle{ 12 private double height; 13 public double findarea(){ 14 return findarea()*height; 15 16 public Box(double length, double width ){ 17 Rectangle(length, width); 18 19 4/7
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 FixedInvestment contains: (1 Mark) a) A double data field name depositamount that specifies the investment amount (default 1000). (1 Mark) b) A double data field name annualinterestrate that specifies the fixed interest rate (default 5%). (1 Mark) c) An int data field named numberofyears that specifies the investment duration (default 1). (1 Mark) d) A no-arg constructor that creates a default instance. (2 Marks) e) A constructor that creates an instance with the specified depositamount, annualinterestrate and numberofyears. (4 Marks) f) The accessor methods for depositamount, numberofyears, and annualinterestrate. (3 Marks) g) The mutator method for numberofyears. (1 Mark) h) A method named gettotalreturn() that returns the investment return after the specified number of years. (6 Marks) The total return can be computed using the following formula: numberofyears * 12 totalreturn = investmentamount x (1 + montlyinterestrate) Write a test program named as TestFixedInvestment that creates a FixedInvestment object with a deposit amount of 10000 and an annual interest rate of 8.5% for 5 years. Your program is then display the investment return for the year 1 to year 5 based on the investment duration. The output as follow: (5 Mark) Years Investment return 1 10883.9091 2 11845.9476 3 12893.0217 4 14032.6475 5 15273.0060 Question 2(35 Marks) Write a program to perform account registration. This account registration system requires the bank staff to enter the customer account number, first name, last name and the balance. The system should able to check the user input to ensure that the correct information was entered by the bank staff user. The account number should only consist of integer value and the balance should be a double value. The bank staff can click the Save to File button to save the account record to a file. You need to complete the following specifications: a) Create a user interface as show in figure 1. (10 Marks) 5/7
b) Write the exception handler to handle the number format of account number and balance. You need to display the error message as show in figure 2 when it is appropriate. (5 Marks) c) Save the customer information which include account number, first name, last name and balance into a file when user click on Save to File button. The name of the file should be same as the account number such as 123456.txt for account 123456. The format of file should same as the output as show in figure 3. (12 Mark) d) You are required to write a status message to tell the user about the records saving status. The message Records was save successfully! when the records was saved successfully into the file. The message Duplicate account number is not allowed! is shown when the same account number exists. If this case happened, user will stop the process and display the error message. Figure 4 and 5 show the example output of error message. (8 Marks). Figure 1: User interface Figure 2: Dialog box is displayed when user entered incorrect number format either on account number or balance textfields. 6/7
Figure 3: Output of record.txt file. Figure 4: Example of status message when file was saved sucessfully. Figure 5: Example of status message when the account number was duplicated. 000 7/7