




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 Java程序设计总复习题1、 编写一个Java程序,用if-else语句判断某年份是否为闰年。(分支)/ Programme Name LeapYear.javapublic class LeapYearpublic static void main(String args)int year=2010; if(args.length!=0) year=Integer.parseInt(args0);if(year%4=0 & year%100!=0)|(year%400=0) System.out.println(year+ 年是闰年。);else System.out.println(ye
2、ar+ 年不是闰年。); /if-else语句2、 编写一个Java程序在屏幕上输出1!+2!+3!+10!的和。(循环)/ programme name ForTest.javapublic class ForTest public static void main( String args ) int i,j,mul,sum=0;for(i=1;i=10;i+) mul=1;for(j=1,j=i;j+) mul=mul*j; sum=sum+mul;System.out.println(“1!+2!+3!+10!= ”+sum);3、 依次输入10个学生成绩,判断学生(优秀、良好、中等、
3、及格、不及格)并计算人数(switch)4、 使用冒泡排序(数组)public class BubbleSort public static void main(String args) int array=63,4,24,1,3,5;BubbleSort sorter=new BubbleSort();sorter.sort(array);/冒泡排序public void sort(int array)for(int i=1;iarray.length;i+)for(int j=0;jarrayj+1)int temp=arrayj;arrayj=arrayj+1;arrayj+1=temp
4、; showArray(array);/遍历数组,并输出数组的元素。public void showArray(int array)for(int i=0;iarray.length;i+) System.out.print(arrayi+t);System.out.println();5、 实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同 (字符串)import java.util.Scanner;public class Register String name;String password;String newPassword;/public void
5、 nameExe()Scanner input=new Scanner(System.in);System.out.println(请输入用户名,密码和验证密码); System.out.print(用户名:); name=input.next(); System.out.print(密码:); password=input.next(); System.out.print(验证密码:); newPassword=input.next();while(name.length()3|(password.equals(newPassword)=false)|(password.length()6)
6、if(name.length()3)System.out.println(用户名不能小于3);if(password.equals(newPassword)=false)|password.length()0&age12) ticketPrice=20; else if(age20) ticketPrice=40; else if(age50) ticketPrice=80; else ticketPrice=35; System.out.println(门票价格:+ticketPrice); /import java.util.Scanner;public class Test public
7、 static void main(String args) Scanner input=new Scanner(System.in);Tourist t1=new Tourist();System.out.print(请输入年龄:);t1.setAge(input.nextInt();t1.ticket(); 7、 (1)编写一个圆类Circle,该类拥有:一个成员变量Radius(私有,浮点型); / 存放圆的半径; 两个构造方法 Circle( ) / 将半径设为0 Circle(double r ) /创建Circle对象时将半径初始化为r 三个成员方法 double getArea(
8、 ) /获取圆的面积 double getPerimeter( ) /获取圆的周长 void show( ) /将圆的半径、周长、面积输出到屏幕(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:一个成员变量double hight(私有,浮点型); / 圆柱体的高; 构造方法 Cylinder (double r, double h ) /创建Circle对象时将半径初始化为r 成员方法 double getVolume( ) /获取圆柱体的体积 void showVolume( ) /将圆柱体的体积输出到屏幕编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高
9、,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。/Programme Name TestCylinder.java class Circle /定义父类-园类private double radius; /成员变量-园半径Circle() /构造方法radius=0.0;Circle(double r) /构造方法 radius=r; double getPerimeter() /成员方法-求园周长return 2*Math.PI*radius;double getArea() /成员方法-求园面积return Math.PI*radius*radius;void disp() /成员方
10、法-显示园半径、周长、面积System.out.println(圆半径=+radius);System.out.println(圆周长=+getPerimeter();System.out.println(圆面积=+getArea();class Cylinder extends Circle /定义子类-圆柱类private double hight; /成员变量-园柱高Cylinder(double r,double h) /构造方法 super(r);hight=h;public double getVol() /成员方法-求园柱体积return getArea()*hight;publ
11、ic void dispVol() /成员方法-显示园柱体积System.out.println(圆柱体积=+getVol();public class TestCylinder /定义主类public static void main(String args) /主程入口 Circle Ci=new Circle(10.0); / 生成园类实例 Ci.disp(); / 调用园类的方法 Cylinder Cyl=new Cylinder(5.0,10.0); /生成圆柱类实例 Cyl.disp(); /调用父类方法 Cyl.dispVol(); /调用子类方法 8、 编写一个Java应用程序
12、,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。(异常处理)/programme name Strinput.javaimport java.io.*;public class Strinput public static void main(String args) String s1=null,s2=null,ss,si,sf; int i1,i2; float f1,f2; BufferedReader strin=new BufferedReader(new Input
13、StreamReader(System.in); trySystem.out.print (输入第一个字符串: ); s1= strin.readLine(); System.out.print (输入第二个字符串: ); s2= strin.readLine(); catch(Exception e) System.out.println(e.getMessage(); i1 = Integer.parseInt(s1); i2 = Integer.parseInt(s2); f1 = Float.parseFloat(s1); f2 = Float.parseFloat(s2); ss =
14、 strAdd(s1,s2); si = strAdd(i1,i2); sf = strAdd(f1,f2); System.out.println (输入的二个字符串相加结果为:+ss ); System.out.println (输入字符串转换为整数相加结果为:+si ); System.out.println (输入字符串转换为浮点数相加结果为:+sf ); static String strAdd(String str1,String str2) return str1+str2;static String strAdd(int int1,int int2) return String
15、.valueOf(int1+int2);static String strAdd(float flt1,float flt2) return String.valueOf (flt1+flt2); 9、 应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:E:/myjava/Hello.java)/ Programme Name FISDemo.javaimport java.io.*; public class FISDemo public static void main(String args) byte b
16、uf=new byte2056; try FileInputStream fileIn=new FileInputStream(e:/myjava/Hello.java); int bytes=fileIn.read(buf,0,2056); String str=new String(buf,0,bytes); System.out.println(str);catch(Exception e) e.printStackTrace( );10、 编写一个Java程序将当100,101,102,103,104,105个数以数组的形式写入到Dest.txt文件中,并以相反的顺序读出显示在屏幕上。
17、(文件)import java.io.*; public class IODemo public static void main( String args ) int data = 100,101,102,103,104,105; int t=new int200; try / File file=new File(dest.txt); DataOutputStream out = new DataOutputStream (new FileOutputStream(dest.txt); for(int i=0;idata.length;i+) out.writeInt(datai); ou
18、t.close(); DataInputStream in = new DataInputStream (new FileInputStream(dest.txt); /先读出来再倒序输出 for(int i=0;i= 0;i-) System.out.print( +ti); /* for(int i= data.length-1;i= 0;i-) t=in.readInt(datai); System.out.print( +t); */ System.out.println( ); in.close(); catch(IOException e) System.out.println(e
19、.getMessage(); 11、 编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。/ programme name TestThread; / 声明一个子线程类Threaddemo; class ThreadDemo extends Thread public ThreadDemo(String str) super(str); public void run() for(int i=0;i20;i+)System.out.print(“ ”+this.getName();Try Sleep(300);catch(InterruptedExcep
20、tion e) System.out.println(e.getMessage(); Return;System.out.println(“ /end”);public class TestThread public static void main( String args ) ThreadDemo thread1=new ThreadDemo(“T1”); ThreadDemo thread2=new ThreadDemo(“T2”); ThreadDemo thread3=new ThreadDemo(“T3”); thread1.start(); thread2.start(); th
21、read3.start(); 10. 编写程序,在屏幕上显示带标题的窗口,并添加一个按钮。当用户单击按钮时,结束程序。(窗体编程)/ Programme Name ButtonEventDemo.javaimport javax.swing.*;import java.awt.event.*;public class ButtonEventDemo extends JPanel implements ActionListenerprotected JButton b1;/声明一个按钮对象public ButtonEventDemo() /构造方法ImageIcon ButtonIcon = n
22、ew ImageIcon(images/green.png); /创建按钮的图标对象 b1 = new JButton(退出按钮, ButtonIcon); /生成按钮对象b1.setMnemonic(KeyEvent.VK_E); /设置b1的助记符是Alt+E b1.setToolTipText(这是退出按钮。);/ 设置按钮提示条 this.add(b1); /往面板对象中加载按钮 b1.addActionListener(this); /本类对象注册为按钮的事件监听器 public void actionPerformed(ActionEvent e)/按钮事件响应方法System.e
23、xit(0); /按b1则退出主程序 private static void createGUI() /创建窗体 JFrame.setDefaultLookAndFeelDecorated(true); /设置java隐含观感 JFrame frame = new JFrame(按钮测试); /生成应用程序主窗体 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /设置关闭时隐含操作 ButtonEventDemo CPane = new ButtonEventDemo(); /生成主类对象-面板 CPane.setOpaque(tru
24、e); /面板要求不透明 frame.setContentPane(CPane); /设置主类对象为主窗体的内容面板 frame.pack(); /主窗体紧缩显示 frame.setVisible(true); /设置主窗体可见 public static void main(String args) /将createGUI()列入线程 javax.swing.SwingUtilities.invokeLater(new Runnable() public void run() createGUI(); ); 12、定义一个表示学生信息的类Student,要求如下:(有关类的编程)(1)类St
25、udent的成员变量:sNO表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。(2)类Student带参数的构造方法: 在构造方法中通过形参完成对成员变量的赋值操作。(3)类Student的方法成员:getNo():获得学号; getName():获得姓名; getSex():获得性别;getAge()获得年龄;getJava():获得Java 课程成绩(4) 根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。/Pragramme name Student;public class Student String sNO,sName,sSex;int sAge,sJava;public Student(String XH,String XM,String XB,int NL,int
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 后勤购买服务合同范本
- 英语教师课题申报书范例
- 企业重组并购合同范本
- 公司转账合同范本
- 厂房土建包工合同范本
- 会务搭建合同范本
- 修井合同范本
- 合同范本 光伏
- 合同范本背面填写
- 业务开发合同范本
- 2025年海域使用权租赁合同
- 四年级希望杯历年数学竞赛试题与答案1-13届+奥数分类专项练习集等
- 《走近世界民间美术》 课件 2024-2025学年人美版(2024)初中美术七年级下册
- (2025春)人教版三年级数学下册全册教案
- 河南2025年02月郑州市公安机关公开招考1200名警务辅助人员笔试历年典型考题(历年真题考点)解题思路附带答案详解
- 儿童保健手册 (一)
- (正式版)JBT 14449-2024 起重机械焊接工艺评定
- 河北单招考试三类职业适应性测试考试题与答案
- 实木家具工艺标准(全流程)
- 《风电调度运行管理规范》
- 日本文学史试卷
评论
0/150
提交评论