Java Programming Final Examination on August 22, 2015 Summer 2015 Department of Computer Science and Information Engineering National Taiwan University Problem 1 (20 points) Multiple choice questions. Each is worth 2 points. 1. Which of the following reserved words in Java is used to create an instance of a class? (a) class (b) public (c) import (d) new. 2. Given 1 double x = 5, y = 2 ; What is the value of m after the following statement is executed? 1 i n t m = ( i n t ) ( x + y + x / y x * y x / (10 * y ) ) ; (a) 1 (b) 0.75 (c) 0.5 (d) 0 (e) 1. 3. Object-oriented programming allows you to derive new classes from existing ones. This is called (a) inheritance (b) abstraction (c) encapsulation (d) generalization. 4. Variables of a supertype can refer to a subtype object. This is called (a) inheritance (b) polymorphism (c) encapsulation (d) dynamic binding. 5. The visibility of Java modifiers increases in which of the following order: (a) private, package, protected, and public. (b) private, protected, package, and public. (c) package, private, protected, and public. (d) package, protected, private, and public. 6. Which of the following is the type of all Java exceptions? (a) RuntimeException (b) Exception (c) Error (d) Throwable. 7. Variables that are shared by every instances of a class are (a) class variables (b) public variables (c) instance variables (d) private variables. 8. Inside a static method of a class, we can use the keyword this. (T/F) 9. Similar to classes which can extend only one other class, an interface can extend only one other interface. (T/F) 1
10. All constant values defined in an interface are implicitly public, static, and final. (T/F) Problem 2 (20 points) Short questions. Each is worth 2 points. Please give an example for each question. 1. What is the definition of object-oriented paradigm. 2. What is the constructor of the class? 3. What is the difference between constructors and other methods? 4. Explain the difference between method overloading and method overriding. 5. Explain the difference between final, finally, and finalize? 6. What is this and super? 7. Explain the difference between static and instance members in one class. 8. Explain the difference between throw and throws? 9. Explain the difference between abstract classes and interfaces? 10. Let x be any input of some algorithm Γ(x), n = x be the input size, and f(n) be the growth rate of Γ(x). Suppose f(n) = 4n 3 100n + 1500. By definition, there exists some g(n) such that f(n) O(g(n))). Note that g(n) are simple functions like n, n 2, and log n. Determine the degree of g(n) which is a tight bound for f(n). Problem 3 (10 points) Write a program which estimates 3 by Monte Carlo simulation. It is clear that x is less than 3 if x 2 is less than 3. You may use Math.random() in the program. ˆ Input - n: the number of random numbers ˆ Output - Approximate number for 3 Problem 4 (10 points) Given a number x, and an integer n 0, write a program which produces x n by a recursive approach. For example, 3 4 = 3 (3 (3 (3))). ˆ Input - x R - n N {0} ˆ Output - x n 2
Problem 5 (10 points) Write a program for lottery tickets ( 樂透 ). The program draws a set of 6 distinct numbers from 1 to 49. Meanwhile, the user enters 6 distinct numbers. In the end, the program shows you how many numbers you guess right. ˆ Input - 6 distinct numbers ˆ Output - The lucky numbers - The chosen numbers - The correct numbers you have For example, Problem 6 (10 points) Use Point class to define a new class Circle which has the following members: Creates two Circle objects: ˆ c 1 : center = (0, 0), radius = 3 3
ˆ c 2 : center = (3, 4), radius = 2 Write a program which determines if the two circles share two intersecting points. You may consider the following snippet: 1 import java. util. Scanner ; 2 3 p u b l i c c l a s s CircleMain { 4 p u b l i c s t a t i c void main ( String [ ] args ) { 5 Scanner input = new Scanner ( System. in ) ; 6 Circle c1 = new Circle ( c1, new Point ( 0, 0), 3) ; 7 Circle c2 = new Circle ( c2, new Point ( 3, 4), 2) ; 8 input. close ( ) ; 9 showcircle ( c1 ) ; 10 showcircle ( c2 ) ; 11 System. out. println ( That c1 o v e r l a p s c2 i s + c1. isoverlap ( c2 ) +. ) ; 12 } 13 14 s t a t i c void showcircle ( Circle c ) { 15 System. out. printf ( %s : (%.2 f, %.2 f ) with r = %.2 f, area = %.2 f, p e r i m e t e r = %.2 f \n, 16 c. getname ( ), 17 c. getcenter ( ). getx ( ), 18 c. getcenter ( ). gety ( ), 19 c. getradius ( ), 20 c. getarea ( ), 21 c. getperimeter ( ) ) ; 22 } 23 } The result looks like: 1 c1 : ( 0. 0 0, 0. 0 0 ) with r = 3. 0 0, area = 2 8. 2 7, perimeter = 1 8. 8 5 2 c2 : ( 3. 0 0, 4. 0 0 ) with r = 2. 0 0, area = 1 2. 5 7, perimeter = 1 2. 5 7 3 That c1 overlaps c2 is f a l s e. Problem 7 (10 points) Extract the member method getdistancefrom in Point class and put it in an interface metric. Also define a new class Point3D for a point in the 3D world. Their relationship can be organized as follows: 4
Note that you should implement the function getdistancefrom in both classes. Use the following snippet to check if the classes are well-defined. In the test program, create two Point3D objects: (0, 0, 0) and (1, 2, 3). Then show the distance between these points. Besides, create an Object object which is not an object of Point3D. Try to calculate the distance. Yet, an exception occurs and is thrown by the function getdistancefrom. 1 p u b l i c c l a s s Point3Dtest { 2 p u b l i c s t a t i c void main ( String [ ] args ) { 3 Point3D p1 = new Point3D ( 0, 0, 0) ; 4 Point3D p2 = new Point3D ( 1, 2, 3) ; 5 Object p3 = new Object ( ) ; 6 t r y { 7 System. out. printf ( The d i s t a n c e between p1 and p2 i s %f. \ n, p1. getdistancefrom ( p2 ) ) ; 8 } catch ( Exception e ) { 9 System. out. println ( e. getmessage ( ) ) ; 10 } 11 12 t r y { 13 System. out. printf ( The d i s t a n c e between p1 and p3 i s %f. \ n, p1. getdistancefrom ( p3 ) ) ; 14 } catch ( Exception e ) { 15 System. out. println ( e. getmessage ( ) ) ; 16 } 17 } 18 } Problem 8 (10 points) Write a program which simulates a TV object. diagram to define the TV class as follows: We use the class ˆ First, you need to define a TV class. ˆ Then, write a unit test to simulate the usage of the TV object. Note that the test program repeats itself until the user enter -1 to terminate the program. You can refer to the following demonstration: 5
SUBMISSION 1. Pack source codes into a zip or rar file with your full name. 2. Send the zip/rar file to d00922011@ntu.edu.tw. 3. If you have any suggestion for improving this class, feel free to leave your feedback in the e-mail. Congratulations!!! To start, you don t have to be good; to be good, you have to start. Slogan of NTU Toastmasters 6