java编程期末复习(共16页).docx

上传人:飞****2 文档编号:14161860 上传时间:2022-05-03 格式:DOCX 页数:16 大小:29.75KB
返回 下载 相关 举报
java编程期末复习(共16页).docx_第1页
第1页 / 共16页
java编程期末复习(共16页).docx_第2页
第2页 / 共16页
点击查看更多>>
资源描述

《java编程期末复习(共16页).docx》由会员分享,可在线阅读,更多相关《java编程期末复习(共16页).docx(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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 World!,10,10);2.求10个数中的最小值并输出。impo

2、rt 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; i10; i+)numi=scan.nextInt();min=num0;for(int i=1; inumi) min=numi;System.out.println(最小值是+min);3.建立一个学生类,其中成员变量为学号,姓名,及三门课

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=math1;chi=chi1;float sum() return(eng+math+chi);void print() System.out.println(name:

4、+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(lucy,002,70,70,70);if (s1.sum()s2.sum() s1.print();else s2.print();4请编写一个实现如下功能的Application:比较从键盘输入的两个整数是否相等,并根据比较结果显示“相等”或“不相等”。import java.util.Scanner

5、;public class COMPare public static void main(String args )Scanner tr=new Scanner(System.in);int b=tr.nextInt();int a=tr.nextInt();if(a=b) System.out.println(相等);else System.out.println(不相等);5.编写一个Application,利用数组求出”HELLO”,”JAVA”PROGRAM”三个字符串的平均长度。class Average public static void main(String args) S

6、tring 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): import java.util.*;class StudentString num;String name;String sex;boolean cleader;float math;float

7、 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;chn=newChn;eng=newEng;public float sum()return math+chn+eng;public float ave()return sum()/3;public v

8、oid output()System.out.println(num+t+name+t+sex+t+cleader+tt+math+t+chn+t+eng+t+sum()+t+ave();public class 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(请输入

9、第+i+个学生的信息);Scanner reader=new Scanner(System.in);System.out.print(请输入该学生学号:);newNum=reader.nextLine();System.out.print(请输入该学生姓名:);newName=reader.nextLine();System.out.print(请输入该学生性别:);newSex=reader.nextLine();System.out.print(请输入该学生是否为班干部:);newCleader=reader.nextBoolean();System.out.print(请输入该学生数学成

10、绩:);newMath=reader.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=s.length;i+)si-1.out

11、put();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(); System.out.println(str); while(!str.

12、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; hight=h; weight=w; public void show() System.out.p

13、rintln(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(n,a,h,w); hobby=hob; public void display() System.out.println( China hobby= + hobby + ); public class E8_1

14、 public static void main(String args) China cc =new China(wenwen,23,168,104,sing); cc.show(); cc.display(); 9、创建一个图形类,包括计算图形面积的方法。创建两个子类长方形、圆形,分别继承图形类,重写子类中的计算图形面积的方法。写一个测试类,分别创建一个长方形和圆形类的对象,并且分别计算这两个图形的面积./filename:app8_2.java 抽象类的说明abstract class Shape protected String name; public Shape(String xm

15、) name=xm; System.out.print(名称:+name); abstract public double getArea();class Circle extends Shape private final double PI=3.14; private double radius; public Circle(String shapeName,double r) super(shapeName); radius=r; public double getArea() return PI*radius*radius; class Rectangle extends Shape

16、private double width; private double height; public Rectangle(String shapeName,double width,double height) super(shapeName); this.width=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.

17、3); System.out.println(;面积=+rect.getArea(); Shape circle=new Circle(圆,10.2); System.out.println(;面积=+circle.getArea(); 10、编写一个“Student”类,该类拥有属性:校名、学号、性别、出生日期。方法包含构造方法和输出方法。再编写“Student”类的子类:Undergraduate(大学生)。Undergraduate类除拥有父类属性和方法外,还有其自己的属性和方法:附加属性包括系(department)、专业(major);方法包含构造方法和输出方法。class Stud

18、ent String name; int sNum; String sex; String birth; String sname; int Score; public 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

19、.println(学号:+sNum); System.out.println(性别:+sex); 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

20、 Score1,String 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 B=new Under

21、graduate(湖南*学院,男,1988/08/08,许翼,95,信息工程系,计算机网络); B.show1(); 11、按以下要求编写程序(1) 编写Animal接口,接口中声明run() 方法(2) 定义Bird类和Fish类实现Animal接口(3) 编写Bird类和Fish类的测试程序,并调用其中的run()方法Bird类的run()方法输出鸟儿在飞.Fish类的run()方法输出鱼儿在游.解答:public interface Animal void run();class Bird implements Animal public void run() System.out.pr

22、intln(鸟儿在飞.);class Fish implements Animal public void run() System.out.println(鱼儿在游.);public class TestAnimal public static void main(String args) Bird bird = new Bird();bird.run();Fish fish = new Fish();fish.run();12、按以下要求编写程序(1) 创建一个Rectangle类,添加width和height两个成员变量(2) 在Rectangle中添加两种方法分别计算矩形的周长和面积(

23、3) 编程利用Rectangle输出一个矩形的周长和面积public class Rectangle float width, height;public Rectangle(float width, float height) this.width = width;this.height = height;public float getLength()return (this.width + this.height) * 2;public float getArea()return this.width * this.height;public static void main(Strin

24、g args) Rectangle rect = new Rectangle(10, 20);System.out.println(周长是: + rect.getLength();System.out.println(面积是: + rect.getArea();13、由person类派生出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

25、;birth=b1;void display()System.out.println(name: +name+nSex: +sex+nbirthday: +birth);class student extends personprivate 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

26、.out.println(-Display-nThe students );display();System.out.println(Class: +cl+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.printl

27、n(-Display-nThe teachers );display();System.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 I

28、nputStreamReader(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 students name:);/n2=reader.next(); n2=buf.readLine(); System.out.print(Input sex:);s2=reader.next();

29、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 obj1=new student(c,num,to,n2,s2,b2);obj1.displays();break;case 2:System.out.println(-I

30、nput-);System.out.print(Input teachers name:);/n2=reader.next(); n2=buf.readLine(); System.out.print(Input sex:);s2=reader.next();System.out.print(Input birthday:);b2=reader.next();System.out.print(Input department profession:);d=reader.next();System.out.print(Input wage:);w=reader.nextFloat();teach

31、er obj2=new teacher(d,w,n2,s2,b2);obj2.displayt();break;case 3:System.exit(0);default:System.out.println(Error!Please enter again!);14、设计一个窗口,在窗口中摆放两个按钮,若按钮被点击了,就将该按钮上的标记改为“已按过”。/e1.java 简单的事件处理程序(已加入事件处理)import java.awt.*; import javax.swing.*;import java.awt.event.*;public class e1 extends JFrame

32、implements ActionListener static e1 frm=new e1(); static Button b1=new Button(button1); static Button b2=new Button(button2); public static void main(String args) b1.addActionListener(frm); /把监听者frm向事件源b1注册 b2.addActionListener(frm); /把监听者frm向事件源b2注册 frm.setTitle(操作事件); frm.setLayout(new FlowLayout(

33、); frm.setSize(260,170); frm.add(b1); frm.add(b2); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); public void actionPerformed(ActionEvent e) if (e.getSource()=b1) b1.setLabel(已按过); else b2.setLabel(已按过); 15、设计一个窗口,其中包含两个标签(一个用于给出提示信息,另一个用来输出结果)和一个文本框。要求从文本框中获取用户给出的一个整数,并将该

34、数的绝对值在第二个标签上输出。 /e2.java 简单的事件处理程序(已加入事件处理)import java.awt.*; import 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(显示结果); stat

35、ic JTextField t1=new JTextField(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

36、.add(b1); frm.add(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.

37、event.*;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, 20

38、0); frm.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.java 简单的事件处理程序(已加入事件处理)import java.awt.*;

39、;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(

40、10); b=new JButton(复制); f.add(L); f.add(R); f.add(b); b.addActionListener(this); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); public void actionPerformed(ActionEvent e) if(e.getSource()=b) /R.setText(ffffff); R.setText(L.getText(); public static void main(String arg) (new copy().display(); 18.请编写一个应用程序实现如下功能:从键盘输入整型参数x和y,求x和y的平方和并输出。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁