考試時間 : 1 小時 30 分座號 : 全五頁第一頁 注意 : 禁止使用電子計算器 不必抄題, 作答時請將試題題號及答案依照順序寫在試卷上, 於本試題上作答者, 不予計分 一 就下列 Java 程式片斷中加入一個 for 迴圈, 使其印出右側結果 (15 分 ) public class AllNumbers number square cube 0 0 0 public static void main( String[] args ) 1 1 1 2 4 8 // print a header for the table 3 9 27 System.out.printf( "%s\t%s\t%s\n", "number", "square", "cube" ); 4 16 64 5 25 125 // 加入一個 for 迴圈 6 36 216 // 7 49 343 // 8 64 512 9 81 729 // end main 10 100 1000 二 承題一, 試於 for 迴圈內加入 if 判斷敘述, 以顯示下列結果 :(10 分 ) number square cube 3 9 27 6 36 216 9 81 729 三 下列程式能將輸入的字串 input= abcdef, 反向列印為 fedcba 試以遞迴的方式撰寫副程式 stringreversehelper, 且其參數宣告必須與 stringreverse 方法內的呼叫一致 (25 分 ) public class Reverse public static void stringreverse( char[] array ) stringreversehelper( array, 0 ); System.out.println(); //stringreversehelper 副程式的放置處 public static void main( String args[] ) String input = "abcdef"; stringreverse( input.tochararray() ); // end main // end class Reverse ( 請接第二頁 )
全五頁第二頁 四 下列網頁按 button 鍵前 後瀏覽器顯示的結果為何?(10 分 ) <!DOCTYPE html> <html> <body> <h2>what Can JavaScript Do?</h2> <p id="demo">javascript can change HTML content.</p> <button type="button" onclick='document.getelementbyid("demo").innerhtml="hello JavaScript!" '>Click Me!</button> </body> </html> 五 根據下列程式回答問題 : Employee, HourlyEmployee, SalariedEmployee 三個類別的關係為何?(5 分 ) 試指出一個建構子, 並說明它的作用 (5 分 ) 試說明 SalariedEmployee 中 super(n) 的作用 (5 分 ) 試指出多型 (polymorphism) 出現的地方 (5 分 ) 執行 PayrollSystemTest 後印出的結果為何?(10 分 ) public abstract class Employee private String name; public Employee( String n ) name = n; public void setname( String str ) name = str; public String getname() return name; return String. format( "%s", getname() ); public abstract double earnings(); ( 請接第三頁 )
public class HourlyEmployee extends Employee private double wage; // wage per hour private double hours; // hours worked for week 全五頁第三頁 public HourlyEmployee( String n, double hourlywage, double hoursworked ) super( n ); setwage( hourlywage ); sethours( hoursworked ); public void setwage( double hourlywage ) if ( hourlywage >= 0.0 ) wage = hourlywage; throw new IllegalArgumentException("Hourly wage must be >= 0.0" ); public double getwage() return wage; public void sethours( double hoursworked ) if ( ( hoursworked >= 0.0 ) && ( hoursworked <= 168.0 ) ) hours = hoursworked; throw new IllegalArgumentException("Hours worked must be >= 0.0 and <= 168.0" ); public double gethours() return hours; public double earnings() if ( gethours() <= 40 ) // no overtime return getwage() * gethours(); return 40 * getwage() + ( gethours() - 40 ) * getwage() * 1.5; return String. format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", super.tostring(), "hourly wage", getwage(),"hours worked", gethours() ); ( 請接第四頁 ) ( 請接第三頁 )
全五頁第四頁 public class SalariedEmployee extends Employee private double weeklysalary; public SalariedEmployee( String n, double salary ) super( n ); setweeklysalary( salary ); public void setweeklysalary( double salary ) if ( salary >= 0.0 ) weeklysalary = salary; throw new IllegalArgumentException( "Weekly salary must be >= 0.0" ); public double getweeklysalary() return weeklysalary; public double earnings() return getweeklysalary(); // end method earnings return String. format( "salaried employee: %s\n%s: $%,.2f", super.tostring(), "weekly salary", getweeklysalary() ); // end method tostring // end class SalariedEmployee public class PayrollSystemTest public static void main( String[] args ) // create subclass objects SalariedEmployee salariedemployee = new SalariedEmployee( "Smith", 800.00 ); HourlyEmployee hourlyemployee = new HourlyEmployee( "Price", 16.75, 40 ); System.out.println( "Employees processed individually:\n" ); System.out.printf( "%s\n%s: $%,.2f\n\n", salariedemployee, "earned", salariedemployee.earnings() ); System.out.printf( "%s\n%s: $%,.2f\n\n",hourlyEmployee, "earned", hourlyemployee.earnings() ); Employee[] employees = new Employee[ 2 ]; employees[ 0 ] = salariedemployee; employees[ 1 ] = hourlyemployee; for ( Employee currentemployee : employees ) System.out.println( currentemployee ); System.out.printf( "earned $%,.2f\n\n", currentemployee.earnings() ); // end for // end main // end class PayrollSystemTest ( 請接第五頁 )
六 說明瀏覽器開啟下列網頁後顯示的訊息與可能的互動 (10 分 ) <!DOCTYPE html> <html> <body> 全五頁第五頁 <p>creating a JavaScript Object.</p> <p id="demo"></p> <p>show something.</p> <script> var person = firstname:"john", lastname:"doe", age:50, eyecolor:"blue"; window.alert( person.firstname + " is " + person.age + " years old."); </script> </body> </html> ( 請接第五頁 )