物件導向設計原理期末考(II)A 102/5/28
1. class TLprice 繼承抽象類別 time 且實作介面 rating,用以計算電話通話費。
(a) abstract 類別 time 定義四個成員變數,及三個抽象方法:
• public int hh=0,mm=0,ss=0,seconds=0;
• toTime(String timeString); // 將時間字串轉成整數,分別存入
// hh (時),mm (分),ss(秒) 三個整數變數中
• toSeconds(); // hh*3600+mm*60+ss seconds
• getSeconds(); // 傳回轉成秒數的時間
(b) 介面 rating定義兩個抽象方法:
• callSec(); // 傳回通話秒數 (終話時間 - 始話時間)
• callPrice(char type, int callSec); // 傳回通話費用
(c) class TLprice繼承抽象類別 time 且實作介面 rating,且增加三個方法:
• getCallNo(); // 傳回發話號碼
• getCallType() // 傳回通話類別
• setLprice() // 設定區域通話費(以秒計價)
• setGprice() // 設定長途通話費(以秒計價)
• setMprice() // 設定行動通話費(以秒計價)
每筆通話記錄存放格式說明:
(a) 發話號碼Calling No.:0~7 字元
(b) 通話類別Call Type:8~8 字元 // L:區域通話, G:長途通話, M:行動通話
(c) 始話時間:9~14字元
(d) 終話時間:15~20字元
(a) (b) (c) (d)
25380000 L 110325 111007
2. 印出每筆通話記錄之 Calling No. Call Type Call Sec. Call Price
參考答案
編譯java檔案TLprice之後執行class的 prob10528印出結果
abstract class time{
public int hh=0,mm=0,ss=0,seconds=0;
abstract void toTime(String timeString);
abstract void toSeconds();
abstract int getSeconds();
}
interface rating{
abstract int callSec();
abstract double callPrice(char type, int callSec);
}
public class TLprice extends time implements rating{
String callRec;
public double Lprice=0, Gprice=0, Mprice=0;
TLprice(String ss){
callRec=ss;
}
public void toTime(String tt){
hh=Integer.parseInt(tt.substring(0,2));
mm=Integer.parseInt(tt.substring(2,4));
ss=Integer.parseInt(tt.substring(4));
}
public void toSeconds(){
seconds=hh*3600+mm*60+ss;
}
public int getSeconds(){
return seconds;
}
public String getCallNo(){
return callRec.substring(0,8);
}
public String getCallType(){
return callRec.substring(8);
}
public int callSec(){ // (終話時間 - 始話時間)
int sec1, sec2;
toTime(callRec.substring(9,15)); // 始話時間
toSeconds();
sec1=getSeconds();
toTime(callRec.substring(15)); // 終話時間
toSeconds();
sec2=getSeconds();
return sec2-sec1;
}
void setLprice(double pp){
Lprice=pp;
}
void setGprice(double pp){
Gprice=pp;
}
void setMprice(double pp){
Mprice=pp;
}
public double callPrice(char locate, int p){
switch(locate){
case 'L':
return Lprice*p;
case 'G':
return Gprice*p;
case 'M':
return Mprice*p;
default:
return 0.0;
}
}
}
class prob10528{
public static void main(String[] args){
double upL=0.05, upG=0.1, upM=0.15; // unit price of 3 call type
String[] callRec={"25380000L110325111007","25380011G122503123710","25381100M145912151559",
"25382222M151111151616","25383311L155555161616","25385566G162022163559",
"25386116M163333164316","25387123G170000172310","25388412L145912151559"};
System.out.println(" Calling No.\t Call Type \t Call Sec. \t Call Price");
for(String ss:callRec){
TLprice tl=new TLprice(ss);
tl.setLprice(upL);
tl.setGprice(upG);
tl.setMprice(upM);
System.out.println(" "+tl.getCallNo()+"\t "+tl.getCallType()+" \t"+tl.callSec()+" \t"+
tl.callPrice(ss.charAt(8), tl.callSec()));
}
}
}
物件導向設計原理期末考(II)B 102/5/28
1. 輸入兩班的成績資料檔: clsA.txt & clsB.txt
(a) clsA.txt 的存放格式:學號/性別/姓名/成績1/成績2/成績3/成績4
• 學期成績=成績1*0.15+成績2*0.25+成績3*0.3+成績4*0.3
• 性別M:男生,性別F:女生
(b) clsB.txt 的存放格式:學號(0~7) 性別(8~8)姓名(9~23)
成績1(24~26)成績2(27~29)成績3(30~32)成績4(33~35)
• 學期成績=成績1*0.15+成績2*0.25+成績3*0.3+成績4*0.3
• 性別M:男生,性別F:女生
(c) 先輸入clsA.txt,再輸入clsB.txt
* (可以 Skip step 2) 30%
2. 輸出前,先將「學期成績」及格的學生資料依成績由高至低順序排序。
輸出前,先將「學期成績」不及格的學生資料依成績由高至低順序排序。
3. 將「學期成績」及格的學生資料寫入 pass.txt 檔,
將「學期成績」不及格的學生資料寫入 fail.txt 檔,
輸出的格式為:學號(*) 姓名 成績1 成績2 成績3 成績4 學期成績
男生於學號後加上「*」,女生於學號後加上「 」
prob20528無排序
import java.io.*; class prob20528{ public static void main(String[] args) throws IOException { FileReader fr1=new FileReader("clsA.txt"); // input file1: clsA.txt ==> A班學生成績資料檔 BufferedReader bfr1= new BufferedReader(fr1); FileReader fr2=new FileReader("clsB.txt"); // input file2: clsB.txt ==> B班學生成績資料檔 BufferedReader bfr2= new BufferedReader(fr2); // // 將學號、姓名、成績1(15%), 成績2(25%), 成績3(30%), 成績4(30%)及學期成績輸出 // FileWriter fw1 = new FileWriter("pass.txt"); // output file1:pass.txt 學期成績及格資料檔 BufferedWriter bfw1 = new BufferedWriter(fw1); FileWriter fw2 = new FileWriter("fail.txt"); // output file2:fail.txt 學期成績不及格資料檔 BufferedWriter bfw2 = new BufferedWriter(fw2); // // String name,id,str,gender,sc1,sc2,sc3,outstring=""; int recno=0,isc1,isc2,isc3, isc4, pno=0, fno=0; double fin; char star='*'; while ((str=bfr1.readLine())!=null) // read a record from input file1 { String[] token=str.split("[/]"); id=token[0]; gender=token[1]; name=token[2]; isc1=Integer.parseInt(token[3]); isc2=Integer.parseInt(token[4]); isc3=Integer.parseInt(token[5]); isc4=Integer.parseInt(token[6]); fin=finScore(isc1,isc2,isc3,isc4); if(gender.compareTo("M")==0) star='*'; else star=' '; System.out.println(id+star+" "+name+" "+gender+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); if(fin >= 60){ bfw1.write(id+star+" "+name+" "+gender+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); bfw1.newLine(); }// output to pass.txt else{ bfw2.write(id+star+" "+name+" "+gender+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); bfw2.newLine(); }// output to fail.txt } while ((str=bfr2.readLine())!=null) // read a record from input file2 { id=str.substring(0,8); gender=str.substring(8,9); name=str.substring(9,24); isc1=Integer.parseInt(str.substring(24,27)); isc2=Integer.parseInt(str.substring(27,30)); isc3=Integer.parseInt(str.substring(30,33)); isc4=Integer.parseInt(str.substring(33)); fin=finScore(isc1,isc2,isc3,isc4); if(gender.compareTo("M")==0) star='*'; else star=' '; System.out.println(id+star+" "+name+" "+gender+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); if(fin >= 60){ bfw1.write(id+star+" "+name+" "+gender+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); bfw1.newLine(); }// output to pass.txt else{ bfw2.write(id+star+" "+name+" "+gender+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); bfw2.newLine(); }// output to fail.txt } bfw1.flush(); bfw2.flush(); fr1.close(); fw1.close(); fr2.close(); fw2.close(); } static double finScore(int a, int b, int c, int d){ return (int)((a*0.15+b*0.25+c*0.3+d*0.3)*100)/100.0; } }prob20528sort排序版本
import java.io.*; class score{ String[] data; int no; score(String[] ss, int num){ data=ss; no=num; } void sort(){ String tmp; for(int i=0;i<no-1;i++){ for(int j=i+1;j<no;j++){ if(data[i].compareTo(data[j]) < 0){ tmp=data[i]; data[i]=data[j]; data[j]=tmp; } } } } } class prob20528sort{ public static void main(String[] args) throws IOException { FileReader fr1=new FileReader("clsA.txt"); // input file1: clsA.txt ==> A班學生成績資料檔 BufferedReader bfr1= new BufferedReader(fr1); FileReader fr2=new FileReader("clsB.txt"); // input file2: clsB.txt ==> B班學生成績資料檔 BufferedReader bfr2= new BufferedReader(fr2); // // 將學號、姓名、成績1(15%), 成績2(25%), 成績3(30%), 成績4(30%)及學期成績輸出 // FileWriter fw1 = new FileWriter("pass.txt"); // output file1:pass.txt 學期成績及格資料檔 BufferedWriter bfw1 = new BufferedWriter(fw1); FileWriter fw2 = new FileWriter("fail.txt"); // output file2:fail.txt 學期成績不及格資料檔 BufferedWriter bfw2 = new BufferedWriter(fw2); // // String name,id,str,gender,sc1,sc2,sc3,outstring=""; int recno=0,isc1,isc2,isc3, isc4, pno=0, fno=0; double fin; String[] pass=new String[20]; // for sorting String[] fail=new String[20]; // for sorting char star='*'; while ((str=bfr1.readLine())!=null) // read a record from input file1 { String[] token=str.split("[/]"); id=token[0]; gender=token[1]; name=token[2]; isc1=Integer.parseInt(token[3]); isc2=Integer.parseInt(token[4]); isc3=Integer.parseInt(token[5]); isc4=Integer.parseInt(token[6]); fin=finScore(isc1,isc2,isc3,isc4); if(gender.compareTo("M")==0) star='*'; else star=' '; System.out.println(id+star+" "+gender+" "+name+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); outstring=fin+","+id+star+","+gender+","+name+","+isc1+","+isc2+","+isc3+","+isc4; if(fin >= 60) pass[pno++]=outstring; else fail[fno++]=outstring; } while ((str=bfr2.readLine())!=null) // read a record from input file2 { id=str.substring(0,8); gender=str.substring(8,9); name=str.substring(9,24); isc1=Integer.parseInt(str.substring(24,27)); isc2=Integer.parseInt(str.substring(27,30)); isc3=Integer.parseInt(str.substring(30,33)); isc4=Integer.parseInt(str.substring(33)); fin=finScore(isc1,isc2,isc3,isc4); if(gender.compareTo("M")==0) star='*'; else star=' '; System.out.println(id+star+" "+gender+" "+name+" "+isc1+" "+isc2+" "+isc3+" "+isc4+" "+fin); outstring=fin+","+id+star+","+gender+","+name+","+isc1+","+isc2+","+isc3+","+isc4; if(fin >= 60) pass[pno++]=outstring; else fail[fno++]=outstring; } score score1 =new score(pass, pno); score1.sort(); for(int i=0;i< pno;i++){ String[] tk=pass[i].split("[,]"); bfw1.write(tk[1]+" "+tk[2]+" "+tk[3]+" "+tk[4]+" "+tk[5]+" "+tk[6]+" "+tk[0]); bfw1.newLine(); } score score2=new score(fail, fno); score2.sort(); for(int i=0;i< fno;i++){ String[] tk=fail[i].split("[,]"); bfw2.write(tk[1]+" "+tk[2]+" "+tk[3]+" "+tk[4]+" "+tk[5]+" "+tk[6]+" "+tk[0]); bfw2.newLine(); } bfw1.flush(); bfw2.flush(); fr1.close(); fw1.close(); fr2.close(); fw2.close(); } static double finScore(int a, int b, int c, int d){ return (int)((a*0.15+b*0.25+c*0.3+d*0.3)*100)/100.0; } }
clsA
A9528003/F/Mary Chen/75/82/65/73
A9528005/M/Roney Liu/86/62/75/66
A9528006/M/CoCo Wu/43/62/45/50
A9528011/F/Jeny Kuo/95/88/93/92
A9528012/M/Johny Hu/72/66/65/73
A9528013/F/Colud Lu/53/42/35/47
A9528017/M/Beer Lee/77/66/88/54
A9528025/M/Cool Hung/43/62/45/65
A9528028/F/Fly Wang/93/82/98/85
A9528031/F/Rose Chung/82/76/67/70
clsB
A9528002MBruce Wang 075076065093
A9528008MRoney Chen 086072075057
A9528010FEdwin Liao 082086067050
A9528014FMilly Shai 095085093072
A9528018MAndy Chen 082063065073
A9528022FWoody Lu 063052035037
A9528026MBlue Cheng 071057093057
A9528030MDenny Hung 048063045045
A9528038FCandy Lin 093082088085
A9528042MAdam Wu 082088067074
A9528050FKitty Lee 042056041065
沒有留言:
張貼留言