Java復(fù)習(xí)題(三)閱讀程序題_軟件_第1頁(yè)
Java復(fù)習(xí)題(三)閱讀程序題_軟件_第2頁(yè)
Java復(fù)習(xí)題(三)閱讀程序題_軟件_第3頁(yè)
Java復(fù)習(xí)題(三)閱讀程序題_軟件_第4頁(yè)
Java復(fù)習(xí)題(三)閱讀程序題_軟件_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、JAVA程序設(shè)計(jì)復(fù)習(xí)題之(三)閱讀程序題三、程序閱讀題1閱讀以下程序import java.io.*;public class Reverse2 public static void main(String args )int i,n=10; int a = new int10; try BufferedReader br = new BufferedReader( new InputStreamReader(System.in); ai = Integer.parseInt(br.readLine() ); catch (IOException e) ; for (i= n-1; i >

2、;= 0; i=i-2)System.out.print(ai+" "); System.out.println();請(qǐng)寫(xiě)出該程序的功能:該程序使用字符緩沖輸入流從鍵盤(pán)輸入10個(gè)數(shù),然后倒序并間隔打印出來(lái)。2閱讀以下程序import java.io.* ;public class abc public static void main(String args ) int i, s = 0 ; int a = 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 ; for ( i = 0 ; i< a.length ; i+ )i

3、f (i % 3 = 0) s += ai;System.out.println("s=" + s);請(qǐng)寫(xiě)出該程序的輸出結(jié)果:s=2603、閱讀以下程序:import java.io.*;public class TestRandomAccess public static void main(String args) int data_arr=65,66,56,23,27,1,43,65,4,99; try RandomAccessFile randf=new RandomAccessFile("temp.dat","rw"); f

4、or (int i=0; i<data_arr.length; i+) randf.writelnt(data_arri); randf.writeUTF("Good morning!"); ' for(int i=data_arr.length-l; i>=0; i=i-2) randf.seek(i*4); System,out.print(" "+randf.readInt(); randf.seek(40); System.out.println(randf.readUTF(); randf.close(); catch (I

5、OException e) System.out.println("File access error: "+e); 該程序的輸出結(jié)果是:99 65 1 23 66 Good morning!4、閱讀以下程序并填空。class _ extends Exception String mymsg="我自己定義的異常!" double mynum = 2.0; MyException () super("首字母不能為A! "); MyException (String msg)_ /調(diào)用父類(lèi)構(gòu)造方法,參數(shù)為 msg public void

6、displayme() System.out.println(mymsg); public double mymethod() return Math.sqrt(mynum); class ExceptionTest public static void main(String args) try if ( argsO.charAt(O)= 'A') MyException e = new MyException();System.out.println("kkkk:" + e.mymethod();e.displayme();Systemoutprintl

7、n("*in try*"); _; /拋出異常e else if(argsO.charAt(O)= 'B') throw new MyException ("第一個(gè)字符不應(yīng)是B! "); else System.out.println(args0); catch ( _ ) System.out.println(aaa.getMessage(); aaa.displayme(); System.out.println("" + aaa.mymethod(); catch( _ ) System.out.println(

8、"命令行參數(shù)個(gè)數(shù)錯(cuò)!"); 程序填空:MyException super(msg)throw eMyException aaaArrayIndexOutOfBoundsException5、閱讀以下程序 import java.io.*;public class Test public static void main(String args) SubSubClass m=new SubSubClass(3,6,6);m.show(); class SuperClass int a,b; SuperClass(int x,int y) a=x; b=y; class Sub

9、Class extends SuperClass int c; SubClass(int aa,int bb,int cc) super(aa,bb);c = cc; class SubSubClass extends SubClass int a; SubSubClass(int aa,int bb,int cc) super(aa,bb,cc);a = aa + bb + cc; void show() System.out.println("a="+ a +"nb="+ b +"nc="+ c); 請(qǐng)寫(xiě)出該程序的運(yùn)行結(jié)果:a=6

10、0b=20c=306、閱讀以下程序import java.io.*;public class abc public static void main(String args) String sl = "Hello!"String s2 = new String("World!");System.out.println(sl.concat(s2); 請(qǐng)寫(xiě)出該程序的運(yùn)行結(jié)果:Hello!World!7、閱讀以下程序import java.io.*;public class Class1 public static void main(String args)

11、int i,max,min;int a = 12,67,8,98,23,56,124,55,99,100);max= min= a0;for(i=1; i<a.length; i+)if( ai< min) min = ai;if( ai> max) max = ai;System.out.println( max + " " + min);System.out.println(); 請(qǐng)寫(xiě)出該程序完成的功能:在數(shù)組中查找并輸出最大值和最小值。8、閱讀以下程序import java.awt.*;import java.applet.Applet;public

12、 class DrawMylmage extends Applet Image myImage; /定義一個(gè)圖像類(lèi)Image的對(duì)象myImage public void init()myImage= getImage(getDocumentBase(),"pica.jpg"); public void paint(Graphics g) g.drawImage(myImage,0,0,this); 請(qǐng)寫(xiě)出該程序的功能:在Applet界面中顯示當(dāng)前文件夾下名為“pica.jpg”的圖像。9、閱讀以下程序并填空。import java.awt.*;import java.app

13、let.*;import .*;public class Mypicture _ Applet Image image; public void _() try image=getlmage(new URL(getCodeBase(),'image.gif'); _(MalformedURLException e) public void paint(Graphics g) g.drawlmage(image,0,0,_); public void start() _();程序填空題:extends init catch this repaint10、閱讀以下程序:public

14、 class Sum public static void main( String args) double sum = 0.0 ; for ( int i = 1; i<= 100; i + ) sum += i;, System.out.println( "sum=" + sum ); 該程序完成的功能是:求sum=1+2+3+.+100的和。11、閱讀以下程序:class SuperClass int a,b; SuperClass(int x,int y) a=x; b=y; voidshow() System.out.println("a=&qu

15、ot;+ a + "nb="+ b); class SubClass extends SuperClass int c; SubClass(int aa,int bb,int cc) super(aa,bb);c=cc; voidshow() System.out.println("c="+ c +"na="+ a +"nb="+ b);class SubSubClass extends SubClass int a; SubSubClass(int aa,int bb,int cc) super(aa,bb,cc

16、);a=aa+bb+cc; void show()System.out.println("a="+ a +"nb="+ b +"nc="+ c); class test public static void main(String args) SuperClass p=new SubSubClass(10,20,30);p.show();該程序的輸出結(jié)果是:a=60b=20c=3012、閱讀以下程序:import java.io.*;publiic class Test public static void main(String a

17、rgs) AB s = new AB("Hello!","I love Java.");System.out.println( s.toString() ); class AB String sl; String s2; AB( String strl, String str2 ) sl = str1; s2 = str2; public String toString() return sl + s2; 該程序的輸出結(jié)果是:Hello!I love Java.13、閱讀以下程序,并填空。import _class MyCopy public stati

18、c void main(Stringo args) int ch;FileInputStream fin;_ fout;try fin = new FileInputStream(args0);fout = new FileOutputStream(_);ch = fin.read();while(ch!=-1) _ch = fin.read();fin.close(); fout.close(); catch (_ e1) System.out.println("使用格式錯(cuò)誤!正確格式為:java mycopy源文件名目標(biāo)文件名");System.exit(0); cat

19、ch (FileNotFoundException e3) System.out.println("文件沒(méi)有找到!"); catch (IOException e2) System.out.println("流錯(cuò)誤!"); 程序填空:import java.io.*;FileOutputStreamargs0fout.write(ch);ArrayIndexOutOfBoundsException14、閱讀以下程序import java.io.*;public class Reverse public static void main(String ar

20、gs) int i,n=10;int a = new int10;for(i=0; i<n; i+)try BufferedReader br= new BufferedReader(new InputStreamReader(System.in);ai=Integer.parseInt(br.readLine(); /輸入一個(gè)整數(shù) catch (IOException e) ;for (i = n-1; i >= 0; i-)System.out.print(ai+" ");System.out.println(); 請(qǐng)寫(xiě)出此程序功能:程序運(yùn)行時(shí)從鍵盤(pán)輸入10

21、個(gè)整數(shù),然后倒序輸出。15、閱讀以下程序import java.io.* ;public class Test public static void main(String args ) int i, s = 0;int a = 10, 20, 30, 40, 50, 60, 70, 80, 90;for ( i = 0 ; i < a.length; i+ )if(ai % 3 = 0) s += ai;System.out.println("s=" + s);請(qǐng)寫(xiě)出此程序的輸出結(jié)果:s=18016、閱讀以下程序(提示:注意同步)class One synchron

22、ized void display(int num) System.out.println("two " + num);try Thread.sleep(1000); catch (InterruptedException e) System.out.println(”中斷”);System.out.println(”完成”); class Two implements Runnable int number; One one; Thread t; public Two(One one_num, int n) one = one_num;number = n;t = new

23、 Thread(this);t.start(); public void run()one.display(number);public class Synch public static void main(String args) throws InterruptedException One one = new One();int digit = 100;Two s1 = new Two(one,digit);Two s2 = new Two(one,digit);Two s3 = new Two(one,digit);Two s4 = new Two(one,digit);s1.t.j

24、oin();s2.t.join();s3.t.join();s4.t.join();System.out.println("Synch結(jié)束!"); 此程序的輸出結(jié)果是:two 100完成two 100完成two 100完成two 100完成Synch 結(jié)束!17、閱讀以下程序,并填空。import _ ;class FileTypepublic static void main(String args)_; try FileReader fis = new_(args0);BufferedReader reader = new BufferedReader( );Strin

25、g s;while(s=reader.readLine()!=_) System.out.println(" "+(i+)+":" + s);fis.close();reader.close(); catch (IOException e) System.out.println(e); catch (_ e1) System.out.println(“缺少命令行參數(shù)!”); 程序填空:java.io.*;int i;FileReaderfisnullArrayIndexOutOfBoundsException18、閱讀以下程序:public class

26、Sum public static void main(String args) double sum = 0.0:for (int i=1; i<=100; i+)sum += i*i;System.out.println( "sum="+sum); 該程序的功能是:求出sum的值為1到100的平方和。19、閱讀以下程序:class Example public static void main(String args) int a = new int33;a00=1; a11=1; a22=1;System.out.println("數(shù)組a:"

27、);for (int i=0; i < a.length; i+) for(int j=0; j < ai.length; j+)System.out.print(aij + " ");System.out.println(); 該程序的輸出結(jié)果是:1 0 00 1 00 0 120、閱讀以下程序:public class Test2static boolean foo(char c)System.out.print(c);return true; public static void main(String args)int i = 0:for(foo(

28、9;A'); foo('B') && (i<2); foo('C')i+;foo('D'); 此程序的輸出結(jié)果是:"ABDCBDCB"21、閱讀以下程序,并填空。import _;import java.awt.event.*;public class OvalDrawer extends Frame implements Runnable private Color colors= Color.RED,Color.BLACK,Color.BLUE,Color.GREEN,Color.DARK_G

29、RAY ; private Color color; private int x=l0,y=10,width=10,height=10; public OvalDrawer(String title)super(title);_; /設(shè)置窗口大小為300*300setVisible(true);_; /創(chuàng)建線(xiàn)程并啟動(dòng) public void run() while(true) x = (int)(Math.random0*300);y = (int)(Math.random0*300);width = (int)(Math.random()*100);height = (int)(Math.r

30、andom()*100);color = colors(int)(Math.random()*(colors.length-1);_; /刷新窗口內(nèi)容try Thread.sleep(400); catch(InterruptedException e) throw new RuntimeException(e); public void paint(Graphics g)g.setColor(color);_; /畫(huà)橢圓 public static void main(String args) new OvaIDrawer("hello"); 程序填空:java.awt.

31、*;setSize(300,300);new Thread(this).start();repaint();g.drawOval(x,y,width,height);22、閱讀以下程序:public class Sum public static void main(String args) double sum = 0.0;for (int i=1; i<=100; i+) sum += 1.0 / (double)i;System.out.println("sum="+sum); 此程序完成的功能是:求解sum=1+1/2+1/3+.+1/100的值并打印輸出。2

32、3、閱讀以下程序:import java.awt.*;import java.applet.Applet;public class Applet1 extends Applet public void paint(Graphics g) g.drawLine(30,5,100,45);g.drawRect(30,50,50,20);g.drawOval(30,80,50,40);g.drawString("They are figures! ",30,150); 此程序完成的功能是:在Applet界面中顯示一些由直線(xiàn)、矩形框、橢圓框和文字組成的圖形。24、閱讀以下程序:im

33、port java.io.*;public class Test public static void main(String args) int i;int a=11,22,33,44,55,66,77,88,99;for(i=0; i <= a.length/2; i+)System.out.print(ail + aa.length-i-1 + " ");System.out.println(); 此程序的輸出結(jié)果是:110 110 110 110 11025、閱讀程序并填空import java.awt.*;import java.applet.*;publi

34、c class DrawStringDemo _ Applet private Font afont = _Font("Helvetica",Font.BOLD,18); public void init() _(Color.black); public void paint(Graphics g) g.setColor(Color.green);_(afont);_("This is a test",10,40); 程序填空:extends new setBackground setFont drawString26、閱讀以下程序public clas

35、s Test( public static void main(String args)System.out.printf("sqrt(2.0)=%f", Math.sqrt(2.0); 此程序的輸出是:sqrt(2.0)=1.41421427、閱讀以下程序public class SumTest public static void main(String args) double sum = 0.0;for(int i=1; i <= args.length; i+)sum += Double.parseDouble(argsi);System.out.print

36、ln("sum=" + sum); 此程序完成的功能是:從命令行輸入若干個(gè)數(shù),求這若干個(gè)數(shù)的和并輸出結(jié)果。28、閱讀以下程序import java.util.Scanner;class IfTest public static void main(String args)double y,x;Scanner keyin = new Scanner(System.in);System.out.print(”請(qǐng)輸入x的值:”);x = keyin.nextDouble();if ( x > 0 ) y = 2 * x;else if( x = 0) y = 2 + Mat

37、h.cos(x);else y = x * x + 1; System.out.println("y=" + y); 此程序完成的功能是:從鍵盤(pán)輸入一個(gè)數(shù)x,根據(jù)x的值求出相應(yīng)的y值。 2*x x>0y= 2 + cos(x) x=0 x2+1 x<029、閱讀以下程序,完成填空,使其完整。import _;class Rectangle public static void main(String args) _w,l,S,s; /定義變量Scanner keyin = new Scanner(System.in);System.out.print(”請(qǐng)輸入長(zhǎng)方形的長(zhǎng):”);l = keyin.nextDouble();System.out.print(”請(qǐng)輸入長(zhǎng)方形的寬:”);w = _ ; /輸入寬S = _ ; /計(jì)算面積并保存在S中s = _ ; /計(jì)算周長(zhǎng)并保存在s中System.out.println("此長(zhǎng)方形的面積:"+ S +"n此長(zhǎng)方形的周長(zhǎng):" +s); 程序填空:java.

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論