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

提交评论