版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
基本操作題1無題目:importjava.applet.*;//引入Applet類importjava.awt.Graphics;//引入Graphics類
//*********Found********publicclassJava_1extendsApplet{//*********Found********publicvoidpaint(Graphicsg){g.drawString("歡迎你參加Java考試!",25,25);}}基本操作無題目2.publicclassJava_1{//*********Found**********publicstaticvoidmain(_________________args[]){newSimpleThread("第1").start();newSimpleThread("第2").start();}}//*********Found**********classSimpleThreadextends________________{publicSimpleThread(Stringstr){super(str);}publicvoidrun(){for(inti=0;i<5;i++){//*********Found**********System.out.println(i+""+______________());try{sleep((int)(2*100));}catch(InterruptedExceptione){}}System.out.println("運行!"+getName());}}基本操作題3.importjavax.swing.JOptionPane;publicclassJava_1{publicstaticvoidmain(Stringargs[]){intx,result;StringxVal;//*********Found**********xVal=JOptionPane.showMessageDialog("輸入第1個整數(shù):");//*********Found**********x=Integer.parseInt(xVal);result=x*x;//*********Found**********JOptionPane.showMessageDialog(null,"該數(shù)的平方是"+result);System.exit(0);}}基本操作4/Java_1.java,字符串類的startsWith()和endsWith()方法importjavax.swing.*;publicclassJava_1{publicstaticvoidmain(Stringargs[]){Stringstrings[]={"started","starting","ended","ending"};Stringoutput="";//測試startsWith()方法
for(inti=0;i<strings.length;i++)if(strings[i].startsWith("st"))output+="\""+strings[i]+"\"開始于\"st\"\n";output+="\n";//從字符串第2個字符開始
for(inti=0;i<strings.length;i++)if(strings[i].startsWith("art",2))output+="\""+strings[i]+"\"開始于\"art\"第2個字符位置\n";代碼未完,見下一頁output+="\n";//測試endsWith()方法
for(inti=0;i<strings.length;i++)//*********Found********if(strings[i].endsWith("____________"))output+="\""+strings[i]+"\"結(jié)束于\"ed\"\n";JOptionPane.showMessageDialog(null,output,"顯示字符串類的比較",//*********Found********JOptionPane._________________________);System.exit(0);}}基本操作5:對Java-1.java文件進行完善并調(diào)試,使程序的輸出結(jié)果如下: 字符串“現(xiàn)在學習如何訪問一個字符串” 字符串長度為:13
其中第7個字符是:問 從字節(jié)數(shù)組的第7個到12獲取字符是:問一個字符publicclassJava_1{publicstaticvoidmain(Stringargs[]){Stringstring="現(xiàn)在學習如何訪問一個字符串";System.out.println("字符串\""+string+"\"");//*********Found**********System.out.println("字符串長度:"+_________________________);System.out.println("其中第7個字符是:"+string.charAt(7));charsub[]=newchar[20];System.out.print("從字節(jié)數(shù)組的第7到12獲取字符是:");string.getChars(7,12,sub,0);System.out.println(sub);}}基本操作無題目6:importjava.io.*;publicclassJava_1{//*********Found**********publicstaticvoidmain(String[]args)____________Exception{InputStreamReaderir;BufferedReaderin;//*********Found**********ir=newInputStreamReader(____________);in=newBufferedReader(ir);System.out.println("輸入年份是:");Strings=in.readLine();//*********Found**********intyear=Integer.__________________(s);if(year%4==0&&year%100!=0||year%400==0){System.out.println(""+year+"年是閏年.");}else{System.out.println(""+year+"年不是閏年.");}}}基本操作無題目7://*********Found********import__________________;publicclassJava_1{publicstaticvoidmain(Stringargs[]){Strings1="hashcode",s2="HashCode";Stringoutput="\""+s1+"\"的Hash碼是"+s1.hashCode()+"\n\""+s2+"\"的Hash碼是"+s2.hashCode();JOptionPane.showMessageDialog(null,output,"顯示字符串類的hash碼",//*********Found********JOptionPane.______________________________);System.exit(0);}}基本操作8:importjavax.swing.JOptionPane;publicclassJava_1{publicstaticvoidmain(Stringargs[]){Strings1,s2,s3,s4,output;s1=newString("hello");s2=newString("goodbye");s3=newString("HappyBirthday");s4=newString("happybirthday");output="s1="+s1+"\ns2="+s2+"\ns3="+s3+"\ns4="+s4+"\n\n";//測試字符串相等
(s1.equals("hello"))//*********Found**********output=output+"s1equals\"hello\"\n";elseoutput=output+"s1doesnotequal\"hello\"\n";//用==測試相等
if(s1=="hello")output+="s1equals\"hello\"\n";elseoutput+="s1doesnotequal\"hello\"\n";//忽略字符格式測試相等
if(s3.equalsIgnoreCase(s4))output+="s3equalss4\n";elseoutput+="s3doesnotequals4\n";代碼未完,見下一頁//內(nèi)容比較
output+="\pareTo(s2)is"+pareTo(s2)+"\pareTo(s1)is"+pareTo(s1)+"\pareTo(s1)is"+pareTo(s1)+"\pareTo(s4)is"+pareTo(s4)+"\pareTo(s3)is"+pareTo(s3)+"\n\n";//測試包含字符格式的域匹配
if(s3.regionMatches(0,s4,0,5))output+="First5charactersofs3ands4match\n";elseoutput+="First5charactersofs3ands4donotmatch\n";//忽略字符格式的域匹配
if(s3.regionMatches(true,0,s4,0,5))output+="First5charactersofs3ands4match";elseoutput+="First5charactersofs3ands4donotmatch";//*********Found**********JOptionPane.(null,output,"字符串構(gòu)造方法示例",JOptionPane.INFORMATION_MESSAGE);System.exit(0);}}基本操作9:publicclassJava_1{publicstaticvoidmain(Stringargs[]){intx=4;//*********Found**********System.out.println("x的值是:"+(x_____________4?9.0:99.99));}}基本操作無題目10:基本操作無題目11:簡單應用無題目1:importjava.io.*;publicclassJava_2{publicstaticvoidmain(String[]args)throwsIOException{FileinputFile;FileoutputFile;FileInputStreamin;FileOutputStreamout;intc;
inputFile=newFile("source.txt");outputFile=newFile("dest.txt");in=newFileInputStream(inputFile);//*********Found********out=newFileOutputStream(outputFile);while((c=in.read())!=-1)//*********Found********breakin;in.close();out.close();}}簡單應用無題目2:importjava.text.*;publicclassJava_2{publicstaticvoidmain(String[]args){Person[]people=newPerson[2];people[0]=newWorker("老張",30000);people[1]=newStudent("小王","計算機科學");for(inti=0;i<people.length;i++){Personp=people[i];//*********Found**********System.out.println(____________________()+","+p.getDescription());}}}//*********Found**********________________classPerson{publicPerson(Stringn){name=n;}//*********Found**********public___________________StringgetDescription();publicStringgetName(){returnname;}privateStringname;}代碼未完,見下一頁//*********Found**********classWorker_________________Person{publicWorker(Stringn,doubles){super(n);salary=s;}publicStringgetDescription(){NumberFormatformatter=NumberFormat.getCurrencyInstance();return"工人,年薪是"+formatter.format(salary)+"。";}privatedoublesalary;}//*********Found**********classStudent_________________Person{publicStudent(Stringn,Stringm){super(n);major=m;}publicStringgetDescription(){return"學生,專業(yè)是"+major+"。";}privateStringmajor;}簡單應用3:importjavax.swing.*;publicclassJava_2{publicstaticvoidmain(Stringargs[]){StringBufferbuf=newStringBuffer("你好!祝你成功!");Stringoutput="buf="+buf.toString()+"\nCharacterat0:"+buf.charAt(0)+"\nCharacterat4:"+buf.charAt(4);//*********Found**********charcharArray[]=newchar[buf.length()];//*********Found**********buf.____________________(0,buf.length(),charArray,0);output+="\n\n在字符串緩存中的字符是:";//*********Found**********for(inti=0;i<charArray.length();++i)output+=charArray[i];buf.setCharAt(0,'您');buf.setCharAt(6,'材');output+="\n\nbuf="+buf.toString();//*********Found**********buf.______________________;output+="\n\nbuf="+buf.toString();JOptionPane.showMessageDialog(null,output,"字符串緩存的字符相關方法示范",JOptionPane.INFORMATION_MESSAGE);System.exit(0);}}簡單應用4:publicclassJava_2{publicstaticvoidmain(Stringargs[]){inti=0;//*********Found********String_____________={"HelloWorld!","Hello!","HELLOWORLD!!"};while(i<4){try{System.out.println(greetings[i]);}//*********Found********_________________(ArrayIndexOutOfBoundsExceptione){//*********Found********System.out.println("Catch"+_____________.getMessage());System.out.println("Endingtheprint.");}finally{System.out.println("---------------------");}//*********Found********_______________;}}}簡單應用5:importjava.io.*;publicclassJava_2{publicstaticvoidmain(String[]args){bytebuf[]=newbyte[5];intlen=0,c1=0,c2=0;//*********Found**********______________________{//*********Found**********FileInputStreamin=_________________________("test.txt");while((len=in.read(buf,0,5))>0){for(inti=0;i<len;i++)if(buf[i]>='0'&&buf[i]<='9'){c1++;}elseif((buf[i]>='a'&&buf[i]<='z')||buf[i]>='A'&&buf[i]<='Z')c2++;if(len<5)break;}//*********Found**********in.______________();}catch(Exceptione){}System.out.println("數(shù)字數(shù)是"+c1+",字母數(shù)是"+c2);}}簡單應用無題目6:importjava.io.File;publicclassJava_2{publicstaticvoidmain(Strings[]){//GettingtheCurrentWorkingDirectoryStringcurDir=System.getProperty("user.dir");System.out.println("當前的工作目錄是:"+curDir);
//*********Found**********Fileff=newFile(____________________);String[]files=ff.list();for(inti=0;i<files.length;i++){Stringss=curDir+"\\"+files[i];traverse(0,ss); }}/**遞歸地遍歷目錄樹*@paramlevel目錄的層次*@params當前目錄路徑名*/代碼未完,見下一頁publicstaticvoidtraverse(intlevel,Strings){Filef=newFile(s);for(inti=0;i<level;i++)System.out.print("");if(f.isFile()){System.out.println(f.getName());}elseif(f.isDirectory()){//*********Found**********System.out.println("<"+f._____________________+">");String[]files=f.list();level++;//*********Found**********for(inti=0;i<files.length;________________________){Stringss=s+"\\"+files[i];//*********Found**********traverse(_____________________,ss);}}else{System.out.println("ERROR!");}}}簡單應用無題目7:
//*********Found********publicclassJava_2____________FrameimplementsActionListener{publicstaticvoidmain(Stringargs[]){Java_2fr=newJava_2("Hello!");fr.setLayout(newFlowLayout());fr.setSize(200,100);fr.setBackground(Color.blue);Buttonb=newButton("確定");//*********Found********b._____________________(newJava_2());fr.add(b);fr.setVisible(true);}publicvoidactionPerformed(ActionEvente){System.exit(0);}publicJava_2(Stringstr){super(str);}publicJava_2(){}}簡單應用8:publicclassJava_2{publicstaticvoidmain(Stringargs[]){try{Sleepa=newSleep();Threadt=newThread(a);//*********Found**********t.start();t.join();intj=a.i;System.out.println("j="+j+",a.i="+a.i);}catch(Exceptione){}}}//*********Found**********classSleepimplementsRunnable{inti;publicvoidrun(){try{//*********Found**********Thread.sleep(50);i=10;}catch(InterruptedExceptione){}}}簡單應用9:importjava.awt.*;importjavax.swing.*;//*********Found********publicclassJava_2extends________________{publicvoidinit(){ContainercontentPane=getContentPane();JLabellabel=newJLabel("Java的誕生是對傳統(tǒng)計算模式的挑戰(zhàn)!",SwingConstants.CENTER);contentPane.add(label);}}簡單應用無題目10:簡單應用無題目11:代碼未完,見下一頁綜合運用無題目1.importjavax.swing.JOptionPane;publicclassJava_3{publicstaticvoidmain(Stringargs[]){StringfirstNumber,//用戶輸入第1個字符串
secondNumber,//用戶輸入第2個字符串
result;//astringcontainingtheoutputintnumber1,//比較的第1個數(shù)
number2;//比較的第2個數(shù)
//讀用戶輸入的第1個字符串readfirstnumberfromuserasastringfirstNumber=JOptionPane.showInputDialog("Enterfirstinteger:");//讀用戶輸入的第2個字符串readsecondnumberfromuserasastringsecondNumber=JOptionPane.showInputDialog("Entersecondinteger:");//將字符串類型轉(zhuǎn)換成整數(shù)類型
number1=Integer.parseInt(firstNumber);number2=Integer.parseInt(secondNumber);result="";
代碼未完,見下一頁//*********Found********if(number1==number2)//本行有錯需修改
result=number1+"=="+number2;if(number1!=number2)result=number1+"!="+number2;if(number1<number2)result=result+"\n"+number1+"<"+number2;if(number1>number2)result=result+"\n"+number1+">"+number2;if(number1<=number2)result=result+"\n"+number1+"<="+number2;//*********Found********if(number1>=number2)//本行有錯需修改
result=result+"\n"+number1+">="+number2;//顯示結(jié)果
//*********Found********JOptionPane.showMessageDialog(null,result,"ComparisonResults",//*********Found********Font.INFORMATION_MESSAGE);//程序正常退出
//*********Found********System.exit(0);}}/*JOptionPane類的常用靜態(tài)方法如下:
showInputDialog()showConfirmDialog()showMessageDialog()showOptionDialog()*/綜合運用無題目2.importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;publicclassJava_3{publicstaticvoidmain(String[]args){JFrameframe=newImageViewerFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}classImageViewerFrameextendsJFrame{publicImageViewerFrame(){setTitle("ImageViewer");setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);label=newJLabel();ContainercontentPane=getContentPane();contentPane.add(label,BorderLayout.CENTER);chooser=newJFileChooser();chooser.setCurrentDirectory(newFile("."));JMenuBarmenuBar=newJMenuBar();setJMenuBar(menuBar);JMenumenu=newJMenu("File");menuBar.add(menu);JMenuItemopenItem=newJMenuItem("Open");
代碼未完,見下一頁//*********Found**********_______________.add(openItem);//*********Found**********openItem.___________________________(newActionListener(){publicvoidactionPerformed(ActionEventevt){intr=chooser.showOpenDialog(null);if(r==JFileChooser.APPROVE_OPTION){//*********Found**********Stringname=__________________.getSelectedFile().getPath();label.setIcon(newImageIcon(name));}}});labelT=newJLabel("紅軍不怕遠征難");labelT.setFont(newFont("隸書",Font.PLAIN,DEFAULT_SIZE));contentPane.add(labelT,BorderLayout.NORTH);faceCombo=newJComboBox();faceCombo.setEditable(true);faceCombo.addItem("隸書");faceCombo.addItem("華文新魏");faceCombo.addItem("華文行楷");faceCombo.addItem("華文隸書");
代碼未完,見下一頁//*********Found**********faceCombo.________________________(newActionListener(){publicvoidactionPerformed(ActionEventevent){//*********Found**********labelT.setFont(newFont((String)____________________.getSelectedItem(),Font.PLAIN,DEFAULT_SIZE));}});JPanelcomboPanel=newJPanel();comboPanel.add(faceCombo);contentPane.add(comboPanel,BorderLayout.SOUTH);}privateJLabellabel;privateJLabellabelT;privateJFileChooserchooser;privateJComboBoxfaceCombo;privatestaticfinalintDEFAULT_SIZE=24;publicstaticfinalintDEFAULT_WIDTH=570;publicstaticfinalintDEFAULT_HEIGHT=400;}綜合運用3.//Java_3.java:二分法檢索算法importjava.util.*;publicclassJava_3{privateStringcolors[]={“紅”,“白”,“藍“,"黑","黃","紫","茶色","粉紅"};privateArrayListaList;//ArrayList引用
publicJava_3(){aList=newArrayList(Arrays.asList(colors));Collections.sort(aList);//ArrayList分類
System.out.println("對ArrayList分類:"+aList);}publicvoidprintSearchResults(){printSearchResultsHelper(colors[3]);//首項
printSearchResultsHelper(colors[0]);//中間項
printSearchResultsHelper(colors[7]);//最后項
printSearchResultsHelper("綠");//最低
printSearchResultsHelper("灰");//不存在
//*********Found********printSearchResultsHelper("____________");//不存在
}privatevoidprintSearchResultsHelper(Stringkey){intresult=0;System.out.println("\n檢索關鍵字:"+key);result=Collections.binarySearch(aList,key);System.out.println((result>=0?"找到下標"+result:"沒有找到("+result+")"));}publicstaticvoidmain(Stringargs[]){//*********Found********newJava_3().____________________________;}}綜合運用4//程序的功能是,判斷用戶以命令行方式鍵盤輸入的路徑類名是否在Java類庫中。publicclassJava_3{publicstaticvoidmain(String[]args){StringclassName;if(args.length>0)className=args[0];elseclassName="java.lang.Name";//*********Found********____________//*********Found********{Classc=Class.forName(_______________);System.out.println(className+"是Java類庫中已有的類。");}//*********Found********catch(ClassNotFound_______________e){System.out.println(className+"不在Java類庫中。");}}}綜合運用5importjava.io.*;publicclassJava_3{publicstaticintdata[]={32,18,41,23,2,56,36,67,59,20};publicstaticvoidmain(Stringargs[]){inti;//*********Found**********intindex=data.__________________;System.out.println("排序前:");for(i=0;i<index;i++)System.out.print(""+data[i]+"");System.out.println();//*********Found**********BubbleSort(_________________);System.out.println("排序后:");for(i=0;i<index;i++)System.out.print(""+data[i]+"");System.out.println();}//冒泡法排序
publicstaticvoidBubbleSort(intindex){inti,j;inttemp;for(j=1;j<index;j++){for(i=index-1;i>=j;i--){ if(data[i]<data[i-1]){//比較相鄰的兩個數(shù)
temp=data[i];data[i]=data[i-1];//*********Found**********data[i-1]=__________________;}}}}}綜合運用無題目6importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;//*********Found**********publicclassJava_3extends__________________implementsActionListener{privateJFrameframe;privateJTextFieldname;privateJPasswordFieldpass;privateJLabelnameLabel;privateJLabelpassLabel;privateJPaneltextPanel;privateJPanellabelPanel;privateJButtonbutton;privateJTextAreatextArea;
publicvoidinitGUI(){frame=newJFrame("FramewithDialog");//*********Found**********frame.________________(this);button=newJButton("JDialog");//*********Found**********button._______________(this);textArea=newJTextArea("",3,10);
frame.getContentPane().add(textArea,BorderLayout.CENTER);frame.getContentPane().add(button,BorderLayout.NORTH);
frame.setSize(400,300);frame.setVisible(true); }代碼未完,見下一頁publicvoidactionPerformed(ActionEvente){finalJDialogdia=newJDialog(frame,"logininformation");JButtonok=newJButton("ok");ok.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){textArea.setText("");textArea.append("name:"+name.getText()+"\r\n");textArea.append("passWord:"+newString(pass.getPassword())+"\r\n");//*********Found**********dia.setVisible(________________);//隱藏對話框
}}); name=newJTextField("",10);pass=newJPasswordField("",10); pass.setEchoChar('*');textPanel=newJPanel();textPanel.setLayout(newGridLayout(2,1,10,10));textPanel.add(name);textPanel.add(pass);
nameLabel=newJLabel("name");passLabel=newJLabel("passWord");labelPanel=newJPanel();labelPanel.setLayout(newGridLayout(2,1,20,20));labelPanel.add(nameLabel);labelPanel.add(passLabel);
代碼未完,見下一頁dia.getContentPane().add(labelPanel,BorderLayout.WEST);dia.getContentPane().add(textPanel,BorderLayout.CENTER); dia.getContentPane().add(ok,BorderLayout.SOUTH);dia.setSize(200,130);dia.setVisible(true);}
publicvoidwindowClosing(WindowEventevent){frame.setVisible(false);System.exit(0);}
publicstaticvoidmain(Stringargs[]){Java_3example=newJava_3();//*********Found**********example.____________;}}綜合運用7importjava.text.*;publicclassJava_3{publicstaticvoidmain(String[]args){Personp=newStudent("王甜甜","計算機科學");System.out.println(p.getName()+","+p.getDescription());}}abstractclassPerson{publicPerson(Stringn){name=n;}//*********Found********public_______________StringgetDescription();publicStringgetName(){returnname;}privateStringname;}//*********Found********classStudent___________Person{publicStudent(Stringn,Stringm){super(n);major=m;}//*****
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 煤礦機電設備管理系統(tǒng)技術方案
- 績效發(fā)展咨詢服務
- 展會服務合同范本在線看
- 拼花地板購銷合同樣本
- 個人工作承諾
- 社區(qū)安寧餐飲業(yè)靜音承諾
- 馬戲團表演安全保障服務協(xié)議
- 終止協(xié)議合同的操作
- 版評審表采購合同
- 機電工程招標文件解讀與指導
- 黑龍江省龍東地區(qū)2025屆英語九上期末監(jiān)測模擬試題含解析
- 2024年人教版小學三年級科學(上冊)期末試卷及答案
- 公共廣播系統(tǒng)施工與方案
- 2024年個人信用報告(個人簡版)樣本(帶水印-可編輯)
- 硒鼓回收處理方案
- 書法創(chuàng)作與欣賞智慧樹知到期末考試答案章節(jié)答案2024年華僑大學
- 經(jīng)典導讀與欣賞-知到答案、智慧樹答案
- 悉尼歌劇院-建筑技術分析
- 肺結(jié)核病防治知識宣傳培訓
- 三切口食管癌手術步驟
- 食品安全與衛(wèi)生智慧樹知到期末考試答案2024年
評論
0/150
提交評論