NTU-Exam 板


LINE

課程名稱︰物件導向程式設計 課程性質︰系必修 課程教師︰林軒田 開課學院:電資學院 開課系所︰資工系 考試日期(年月日)︰2009.4.18 考試時限(分鐘):120 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題 : Midterm Examination Problem Sheet Time: 04/18/2009, 19:00-21:00 This is a open-textbook exam. You can use the "Absolute Java" textbook as your reference during the exam. Any other references are not allowed. Any form of sheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or fail the class and/or be kicked out of school and/of receive other punishments for those kinds of misconducts. Both English and Traditional Chinese (if suited) are allowed fo ranswering the questions. We do not accept any other languages. 1. Java Night Countdown (30%) Before the coming of Java night, people are so excited that they want to know the number of minutes until the holy event. Thus, we should design a countdown program for the purpose. Please answer the following questions after reading the code. You can assume that the event date/time is always later than now. 1 //Count down.java 2 package javanight.util; 3 import java.util.*; 4 5 public class Countdown{ 6 int nDay, nHour, nMinute; 7 8 Countdown(int year, int month, int day, int hour, int min){ 9 Date today = new Date(); 10 Date goal = (new GregorianCalendar(Year,month-1,day,hour,min)).getTime(); 11 int diff = (int)((goal.getTime()-today.getTime()))/(1000*60); 12 nDay = diff/(60*24); 13 diff %= (6-*24); 14 nHour = diff/(60); 15 diff %= (60); 16 nMinute = diff; 17 } 18 boolean coundown(){return decrease_minute();} 19 boolean decrease_day(){ 20 if(nDay > 0){nDay--; return true;} 21 else return false; 22 } 23 boolean decrease_hour(){ 24 if(nHour > 0){nHour--; return true;} 25 else if(decrease_day()){nHour = 23; return true;} 26 else return false; 27 } 28 booelan decrease_minute(){ 29 //(2) 30 } 31 int get_total_minutes(){ 32 //(3) 33 } 34 int get_minutes(){return nMinute;} 35 int get_hours(){return nHour;} 36 int get_days(){return nDay;} 37 } 1 //Timer.java 2 package javanight.publicity; 3 //(4) 4 5 public class Timer{ 6 public static void main(String[] arg){ 7 Countdown timer = new Countdown(2010,4,18,19,0); 8 do{ 9 System.out.print(timer.get_total_minutes() + ",u"); 10 System.out.print(timer.get_minutes() + ",u"); 11 System.out.print(timer.get_hours() + ",_"); 12 System.out.print(timer.get_days()); 13 }while(timer.countdown()); 14 } 15 } (1)(2%) What is the fully-qualified name of the class Coutdown? (2)(4%) Complete the method decrease_minute(marked with //(2) above). Yes, you need to "guess" the intended fumctionality. (3)(4%) Complete the method get_total_minutes(marked with //(3) above). It returns the total number of minutes until the event. For example, if there are 2 days, 3 hours and 4 minutes left, the method returns 3064. (4)(2%) When compiling the two files above, Java compiler laughs(hahaha) with Timer.java:7: cannot find symbol Adding a line around the place marked with //(4) can solve the problem. What is the line? (5)(6%) The hahaha goes away after adding the line in the previous question, but new ones come! What are the minimum changes need to be made in Countdown.java to make all the hahaha go away? (Hint: check access permissions) (6)(12%) Speaking of permissions, what is the tightest permission of each instance variables/methods/constructors of the class Coundown to make all the code above work? Note that there are 12 of them. 2 Java Night Choir (20%) In Java night, all the 200OOP students will sing as a choir (with You-Know-Who as the conductor). If there are six students in the order(1,2,4,3,5,6), we can try to arrange them in four rows, like 1 2 4 3 5 6 More specifically, if there are N students in R rows, the last N%R rows would contain (N/R)+1 students, and the other rows would contain(N/R) students. The students would seat from the most top-left range the students to the choir positions(4% for each method) The quality of your code may be taken into account when grading. You can assume that N > R > 0. ('/' means integer division.) 1 //Choir.java 2 public class Choir{ 3 private int[][] position; 4 private int nStudent; 5 /** arrange the IDs to nRow rows */ 6 public Choir(int[] IDs, int nRow){ 7 // (1) 8 } 9 /** arrange the IDs to 3 rows */ 10 public Choir(int[] IDs){ 11 // (2) 12 } 13 /** re-arrange the internal positions */ 14 public void reshape(int nRow){ 15 // (3) 16 } 17 /** return the ID at position row-num, 18 for instance, position 2-1 of the case above returns 3 19 return -1 if no such position */ 20 public int getID(int row, int num){ 21 // (4) 22 } 23 /** show the choir 24 such that (1, 2, 4, 3, 5, 6) with 4 rows 25 would output as the case above */ 26 public void showChoir(){ 27 // (5) 28 } 29 } 3 Java Night Singer PK (20%) The Java Night is so hot that all the singers are competing for a single five- minute slot of showing off! The organizing team has thus decided the following selection mechanism for picking the one: the first half of the candidates would first form a subgroup, compete among tjemselves and declare their representative; the second half would do the same, Then, the two representatives would have a one-on-one competition to declare the final winner . Given that this seems to be a fair way, the subgroups, sub-subgroups, ... all decide to do tje same. Such a mechanism leads to the following Java code: 1 //PK.java 2 class Singer{ 3 int ID; 4 int score; 5 } 6 public class PK{ 7 static int count; 8 /* (1) */ swap(Singer a, Singer b){ 9 Singer tmp = a; 10 a = b; 11 b = a; 12 } 13 /** after running, arr[left] should contain 14 the best singer between arr[left] and arr[right-1] 15 and will be returned */ 16 private static Singer compete(Singer[] arr, int left, int right){ 17 int len = right-left; 18 if(len > 1){ 19 int middle = left+len/2; 20 Singer first = compete(arr. left, middle); 21 Singer second = compete(arr, middle, right); 22 if(first.score < second.score) 23 swap(arr[left], arr[middle]); 24 count++; 25 26 System.out print(count + ":"); 27 for(int i = 0l i < arr.length; i++) 28 System.out.print("u%d(%d)", arr[i].ID, arr[i].score); 29 System.out.println(); 30 } 31 return arr[left]; 32 } 33 public static Singer compete(Singer[] arr){ 34 count = 0; 35 return compete(arr, 0, arr,length); 36 } 37 } (1)(4%) Fill in the part marked with /* (1) */ with the tightest acess permission modifier. (2)(4%) Tje method swap does not work as expected. How would you change the code from lines 9 to 11 to make the method work? (3)(4%) After correcting swap, what is the output when the public compete is called with an array containingP{(1,80),(2,90)}, where (1,90) means a singer of ID 1 and score 90? (4)(4%) After correcting swap, what is the output when the public compete is called with an array containing{(3,90),(5,80),(6,70),(1,95)}? (5)(4%) After correcting swap, what is the output when the public compete is called with an array containing{(3,90),(5,80),(1,85),(2,60),(4,70)}? 4 Java Night Drinks (30%) What is the best drink for Java night? Java coffee, of course! Now that you are in charge of handling drinks in Java night, you'd better organize all the potential drinks you have. Let;s define the following classes first: 1 public class Drink{ 2 private double amount; 3 public void drink(double sip){ 4 if(amount > sip) amount -= sip; 5 else amount = 0; 6 } 7 public double getAmount(){return amount;} 8 } 9 class Java extends Drink{ 10 couble caffeine; 11 public Java(double a, double e){ 12 amount = a; 13 caffeive = c; 14 } 15 } 16 class Milk extends Drink{} 17 class FatlessMilk extends Milk{} 18 class BlendedJava extends Java{ 19 Milk milk; 20 double ratio; 21 } (1)(2%) At least how many instance variables does class BlendedJava have? (2)(3%) There is one place that may result in compile error (hahaha) in the constructor of class Java. What is it and how could you fix it by changing only one line? (3)(3%) There is one place that may result in compile error (hahaha) in the constructor of class BlendedJava. What is it and how could you fix it? (4)(4%) We want to modify the action of drinking in class BlendedJava as follows. For some sip values, you would drink (ratio * sip) from the Java coffee part and (sip - ratio * sip) from the Milk part. Write down such a drink method for class BlendedJava. 1 public class JavaDeme{ 2 public static void main(String[] argv){ 3 Milk m1 = new Milk(); 4 Milk m2 = new Milk(); 5 Java j1 = new Java(3.0, 2.0); 6 BlendedJava b1 = new BlendedJava(); 7 BlendedJava b2 = new BlendedJava(); 8 b1.milk = m1; 9 j1 = b1; 10 b1.Milk = new FatleddMilk(); 11 Milk[] marr = new Milk[3]; 12 marr[0] = b1.milk; 13 marr[1] = m2; 14 /* CODE */ 15 } 16 } Now, consider another java source file above. Please write down the output when the /* CODE */ part is replaced by the following lines (respectively for each subproblem). If you think there is a compile error, write "compile error" or "hahaha." If you think there is a run-time error(exception), write down "run-time error" or "ohohoh." (5)(2%) System.out.println(m1 instanceof FatleddMilk); (6)(2%) System.out.println(marr instanceof Milk); (7)(2%) System.out.println(marr[1] == marr[0]); (8)(2%) System.out.println(marr[2],getAmount()); (9)(2%) System.out.println(b1 instanceof Object); (10)(2%) System.out.println(j1 instanceof BlendedJava); (11)(2%) System.out.println(b2.milk); (12)(2%) System.out.println(marr[0] instanceof BlendedJava); (13)(2%) System.out.println(j1.caffeive == b1.caffeive); 5 Brainstorming Time (1)(Bonus 5%) On You-Know-Who's cellphone, a program shows the following message: Null Pointer java/lang/NullPointerException What might be the cause of the message? --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.167.72.149
1F:→ andy74139 :已收錄至!! 06/30 10:50







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Tech_Job站內搜尋

TOP