java編程期末復習_第1頁
java編程期末復習_第2頁
java編程期末復習_第3頁
java編程期末復習_第4頁
java編程期末復習_第5頁
已閱讀5頁,還剩11頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精選優質文檔-傾情為你奉上編程題:1編寫一個輸出"Hello World!"的程序,用兩種方式實現(Application、Applet)。application程序public class apublic static void main(String args)System.out.println("Hello World!");2、applet程序public class b extends java.applet.Appletpublic paint(java.awt.Graphics g)g.outstring("Hello Worl

2、d!",10,10);2.求10個數中的最小值并輸出。import java.util.Scanner;public class Test public static void main(String args)int num=new int10;int min;Scanner scan=new Scanner(System.in);System.out.print("請輸入10個數字:");for(int i=0; i<10; i+)numi=scan.nextInt();min=num0;for(int i=1; i<10; i+)if(min&g

3、t;numi) min=numi;System.out.println("最小值是"+min);3.建立一個學生類,其中成員變量為學號,姓名,及三門課成績。另外建立一個包含主方法的類,定義2個學生類的對象,求出這2個學生三門課總分的最高分,并將最高分這個學生的信息輸出。class student String name,no; float eng,math,chi; public student(String name1,String no1,float eng1,float math1,float chi1)name=name1;no=no1;eng=eng1;math=

4、math1;chi=chi1;float sum() return(eng+math+chi);void print() System.out.println("name:"+name+"t no:"+no+"t total:"+sum();public class maxpublic static void main(String args)student s1=new student("lily","001",80,90,50);student s2=new student("lu

5、cy","002",70,70,70);if (s1.sum()>s2.sum() s1.print();else s2.print();4請編寫一個實現如下功能的Application:比較從鍵盤輸入的兩個整數是否相等,并根據比較結果顯示“相等”或“不相等”。import java.util.Scanner;public class COMPare  public static void main(String args )   Scanner tr=new Scanner(System.in); 

6、0; int b=tr.nextInt();   int a=tr.nextInt();   if(a=b)     System.out.println("相等");   else     System.out.println("不相等");  5.編寫一個Application,利用數組求出”HELLO”,”JAVA”PROGRAM”三個字符串的平均長度

7、。class Average public static void main(String args) String array = new String3;array0 = "HELLO"array1 = "JAVA"array2 = "PROGRAM"int total = array0.length();total += array1.length();total += array2.length();System.out.println("平均字符串長度為: " + total/3);6、習題(6_13):

8、 import java.util.*;class StudentString num;String name;String sex;boolean cleader;float math;float chn;float eng;public Student(String newNum,String newName,String newSex,boolean newCleader,float newMath,float newChn,float newEng)num=newNum;name=newName;sex=newSex;cleader=newCleader;math=newMath;ch

9、n=newChn;eng=newEng;public float sum()return math+chn+eng;public float ave()return sum()/3;public void output()System.out.println(num+"t"+name+"t"+sex+"t"+cleader+"tt"+math+"t"+chn+"t"+eng+"t"+sum()+"t"+ave();public clas

10、s book6_13_2public static void main(String args)String newNum,newName,newSex;boolean newCleader;float newMath,newChn,newEng;Student s=new Student3;for(int i=1;i<=s.length;i+)System.out.println("請輸入第"+i+"個學生的信息");Scanner reader=new Scanner(System.in);System.out.print("請輸入該

11、學生學號:");newNum=reader.nextLine();System.out.print("請輸入該學生姓名:");newName=reader.nextLine();System.out.print("請輸入該學生性別:");newSex=reader.nextLine();System.out.print("請輸入該學生是否為班干部:");newCleader=reader.nextBoolean();System.out.print("請輸入該學生數學成績:");newMath=reade

12、r.nextFloat();System.out.print("請輸入該學生語文成績:");newChn=reader.nextFloat();System.out.print("請輸入該學生英語成績:");newEng=reader.nextFloat();si-1=new Student(newNum,newName,newSex,newCleader,newMath,newChn,newEng);System.out.println("學號t姓名t性別t班干部否t數學t語文t英語t總分t平均分");for(int i=1;i&l

13、t;=s.length;i+)si-1.output();7、/第5章第11題import java.io.*;public class Exercises5_11 public static void main(String args) throws IOException String str; BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in); System.out.print("請輸入字符串,輸入exit退出:"); do str=buf.readLine(); S

14、ystem.out.println(str); while(!str.equals("exit"); 8、定義一個Person類,該類具有姓名、身高、體重、年齡屬性,能夠對上述相關信息進行輸出display()。由Person類派生出China類,增加愛好屬性。.class Person private String name; private int age; private float hight; private float weight; public Person(String n,int a,float h,float w) name=n; age=a; hig

15、ht=h; weight=w; public void show() System.out.println("Person name ="+name +",age=" + age + ", hight=" + hight + ", weight=" + weight + ""); class China extends Person private String hobby; public China(String n,int a,float h,float w,String hob)super

16、(n,a,h,w); hobby=hob; public void display() System.out.println( "China hobby=" + hobby + ""); public class E8_1 public static void main(String args) China cc =new China("wenwen",23,168,104,"sing"); cc.show(); cc.display(); 9、創建一個圖形類,包括計算圖形面積的方法。創建兩個子類長方形、圓形,分別

17、繼承圖形類,重寫子類中的計算圖形面積的方法。寫一個測試類,分別創建一個長方形和圓形類的對象,并且分別計算這兩個圖形的面積./filename:app8_2.java 抽象類的說明abstract class Shape protected String name; public Shape(String xm) name=xm; System.out.print("名稱:"+name); abstract public double getArea();class Circle extends Shape private final double PI=3.14; priv

18、ate double radius; public Circle(String shapeName,double r) super(shapeName); radius=r; public double getArea() return PI*radius*radius; class Rectangle extends Shape private double width; private double height; public Rectangle(String shapeName,double width,double height) super(shapeName); this.wid

19、th=width; this.height=height; public double getArea() return width*height; public class E8_2 public static void main(String args) Shape rect =new Rectangle("長方形",6.5,10.3); System.out.println(";面積="+rect.getArea(); Shape circle=new Circle("圓",10.2); System.out.println(&

20、quot;;面積="+circle.getArea(); 10、編寫一個“Student”類,該類擁有屬性:校名、學號、性別、出生日期。方法包含構造方法和輸出方法。再編寫“Student”類的子類:Undergraduate(大學生)。Undergraduate類除擁有父類屬性和方法外,還有其自己的屬性和方法:附加屬性包括系(department)、專業(major);方法包含構造方法和輸出方法。class Student String name; int sNum; String sex; String birth; String sname; int Score; public

21、Student(String name1,int sNum1, String sex1,String birth1, String sname1,int Score1) name=name1; sNum=sNum1; sex=sex1; birth=birth1; sname=sname1; Score=Score1; void show() System.out.println("所在學校:"+name); System.out.println("學號:"+sNum); System.out.println("性別:"+sex);

22、System.out.println("生日:"+birth); System.out.println("姓名:"+sname); System.out.println("成績:"+Score); class Undergraduate extends Student String department; String major; public Undergraduate(String name1,int sNum1, String sex1,String birth1, String sname1,int Score1,Strin

23、g department1,String major1) super(name1,sNum1,sex1, birth1, sname1, Score1); department=department1; major=major1; void show1() super.show(); System.out.println("系部:"+department); System.out.println("專業:"+major); public class aa public static void main(String arg) Undergraduate

24、B=new Undergraduate("湖南*學院","男","1988/08/08","許翼",95,"信息工程系","計算機網絡"); B.show1(); 11、按以下要求編寫程序(1) 編寫Animal接口,接口中聲明run() 方法(2) 定義Bird類和Fish類實現Animal接口(3) 編寫Bird類和Fish類的測試程序,并調用其中的run()方法Bird類的run()方法輸出"鳥兒在飛."Fish類的run()方法輸出"魚兒

25、在游."解答:public interface Animal void run();class Bird implements Animal public void run() System.out.println("鳥兒在飛.");class Fish implements Animal public void run() System.out.println("魚兒在游.");public class TestAnimal public static void main(String args) Bird bird = new Bird()

26、;bird.run();Fish fish = new Fish();fish.run();12、按以下要求編寫程序(1) 創建一個Rectangle類,添加width和height兩個成員變量(2) 在Rectangle中添加兩種方法分別計算矩形的周長和面積(3) 編程利用Rectangle輸出一個矩形的周長和面積public class Rectangle float width, height;public Rectangle(float width, float height) this.width = width;this.height = height;public float g

27、etLength()return (this.width + this.height) * 2;public float getArea()return this.width * this.height;public static void main(String args) Rectangle rect = new Rectangle(10, 20);System.out.println("周長是:" + rect.getLength();System.out.println("面積是:" + rect.getArea();13、由person類派生出

28、student類和teacher類import java.io.*;import java.util.*;class personprivate String name,sex,birth;public person(String n1, String s1, String b1)name=n1;sex=s1;birth=b1;void display()System.out.println("name: "+name+"nSex: "+sex+"nbirthday: "+birth);class student extends pe

29、rsonprivate String cl;private int no;private float total; public student(String c,int num,float to,String n2,String s2,String b2) super(n2,s2,b2); cl=c; no=num; total=to;void displays()System.out.println("-Display-nThe student's ");display();System.out.println("Class: "+cl+&q

30、uot;nNo: "+no+"nTotal score: "+total);class teacher extends personprivate String dp;private float wage; public teacher(String d,float w,String n2,String s2,String b2) super(n2,s2,b2); dp=d; wage=w;void displayt()System.out.println("-Display-nThe teacher's ");display();Sy

31、stem.out.println("Department profession:"+dp+"nWage:"+wage);public class sttry public static void main(String args) throws IOException String n2,s2,b2,c,d;int num,ch;float to,w; Scanner reader=new Scanner(System.in); BufferedReader buf; buf=new BufferedReader(new InputStreamReade

32、r(System.in);while(true)System.out.println("-Meun-n1.Input studentn2.Input teachern3.ExitnEnter:");ch=reader.nextInt();switch(ch)case 1:System.out.println("-Input-");System.out.print("Input student's name:");/n2=reader.next(); n2=buf.readLine(); System.out.print(&qu

33、ot;Input sex:");s2=reader.next();System.out.print("Input birthday:");b2=reader.next();System.out.print("Input class:");c=reader.next();System.out.print("Input No:");num=reader.nextInt();System.out.print("Input total score:");to=reader.nextFloat();student

34、obj1=new student(c,num,to,n2,s2,b2);obj1.displays();break;case 2:System.out.println("-Input-");System.out.print("Input teacher's name:");/n2=reader.next(); n2=buf.readLine(); System.out.print("Input sex:");s2=reader.next();System.out.print("Input birthday:"

35、;);b2=reader.next();System.out.print("Input department profession:");d=reader.next();System.out.print("Input wage:");w=reader.nextFloat();teacher obj2=new teacher(d,w,n2,s2,b2);obj2.displayt();break;case 3:System.exit(0);default:System.out.println("Error!Please enter again!&

36、quot;);14、設計一個窗口,在窗口中擺放兩個按鈕,若按鈕被點擊了,就將該按鈕上的標記改為“已按過”。/e1.java 簡單的事件處理程序(已加入事件處理)import java.awt.*; import javax.swing.*;import java.awt.event.*;public class e1 extends JFrame implements ActionListener static e1 frm=new e1(); static Button b1=new Button("button1"); static Button b2=new Butt

37、on("button2"); public static void main(String args) b1.addActionListener(frm); /把監聽者frm向事件源b1注冊 b2.addActionListener(frm); /把監聽者frm向事件源b2注冊 frm.setTitle("操作事件"); frm.setLayout(new FlowLayout(); frm.setSize(260,170); frm.add(b1); frm.add(b2); frm.setDefaultCloseOperation(JFrame.EX

38、IT_ON_CLOSE); frm.setVisible(true); public void actionPerformed(ActionEvent e) if (e.getSource()=b1) b1.setLabel("已按過"); else b2.setLabel("已按過"); 15、設計一個窗口,其中包含兩個標簽(一個用于給出提示信息,另一個用來輸出結果)和一個文本框。要求從文本框中獲取用戶給出的一個整數,并將該數的絕對值在第二個標簽上輸出。 /e2.java 簡單的事件處理程序(已加入事件處理)import java.awt.*; imp

39、ort javax.swing.*;import java.awt.event.*;public class e2 extends JFrame implements ActionListener static e2 frm=new e2(); static JButton b1=new JButton("提交"); static JLabel l1=new JLabel("請輸入一個整數"); static JLabel l2=new JLabel("顯示結果"); static JTextField t1=new JTextFie

40、ld(10); public static void main(String args) b1.addActionListener(frm); /把監聽者frm向事件源b1注冊 / frm.setTitle("操作事件"); frm.setLayout(null); frm.setSize(460,370); l1.setBounds(20,10,100,25); t1.setBounds(140,10,170,25); l2.setBounds(20,30,170,25); b1.setBounds(70,135,150,25); frm.add(b1); frm.add

41、(l1); frm.add(l2); frm.add(t1); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); public void actionPerformed(ActionEvent e) int x=Integer.parseInt(t1.getText(); l2.setText(Math.abs(x)+" "); 16、13-6/第13章第6題import java.awt.*;import javax.swing.*;import java.awt.event

42、.*;import java.awt.Rectangle;public class Exercises13_6 extends JFrame implements ActionListener static Exercises13_6 frm = new Exercises13_6(); static JButton b1 =new JButton(); static int i=0; public static void main(String args) b1.addActionListener(frm); /把監聽者frm向事件源b1注冊 frm.setSize(300, 200); f

43、rm.setTitle("JFrame"); frm.add(b1); b1.setBounds(new Rectangle(71, 40, 147, 49); b1.setText("開始點擊"); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); public void actionPerformed(ActionEvent e) i+; b1.setText("被點擊"+i+"次"); 17、Lab68/copy

44、.java 簡單的事件處理程序(已加入事件處理)import java.awt.*; ;import javax.swing.*;import java.awt.event.*;public class copy implements ActionListener JButton b; JTextField L,R; public void display() JFrame f=new JFrame(); f.setSize(400,150); f.setBackground(Color.lightGray); f.setLayout(new FlowLayout(FlowLayout.LEFT); L=new JTextField(10); R=new JTextField(10); b=new JButton("復制"); f.add(L); f.add(R); f.add(b); b.addActionListener(this); f.setDefaultCloseOperation(JFrame.EXIT_O

溫馨提示

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

評論

0/150

提交評論