Java程序设计总复习题
- 编写一个Java程序,用if-else语句判断某年份是否为闰年。(分支)
// Programme Name LeapYear.java
public class LeapYear{
public static void main(String args[]){
int year=2010;
if(args.length!=0)
year=Integer.parseInt(args[0]);
if((year%4==0 && year%100!=0)||(year%400==0))
System.out.println(year+" 年是闰年。");
else
System.out.println(year+" 年不是闰年。");
}
}//if-else语句
- 编写一个Java程序在屏幕上输出1!+2!+3!+……+10!的和。(循环)
// programme name ForTest.java
public 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);
}
}
- 依次输入10个学生成绩,判断学生(优秀、良好、中等、及格、不及格)并计算人数(switch)
- 使用冒泡排序(数组)
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;i<array.length;i++)
for(int j=0;j<array.length-1;j++){
if(array[j]>array[j+1]){
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
showArray(array);
}
//遍历数组,并输出数组的元素。
public void showArray(int[] array){
for(int i=0;i<array.length;i++){
System.out.print(array[i]+"\t");
}
System.out.println();
}
}
- 实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同 (字符串)
import java.util.Scanner;
public class Register {
String name;
String password;
String newPassword;
///
public void 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)){
if(name.length()<3){
System.out.println("用户名不能小于3");
}
if((password.equals(newPassword)==false)||password.length()<6){
System.out.println("两次输入密码不一样或密码不能小于6位");
}
System.out.println("\n"+"请重新输入");
System.out.print("用户名:");
name=input.next();
System.out.print("密码:");
password=input.next();
System.out.print("验证密码:");
newPassword=input.next();
}
System.out.println("注册成功!");
}
}
public class Verify {
public static void main(String[] args) {
Register m1=new Register();
m1.nameExe();
}
}
- 一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购买的门票价格并输出,然后写出测试类测试该类(类的基本实现)
public class Tourist {
int age;
int ticketPrice;
public void setAge(int age){
this.age=age;
}
public void ticket(){
if(age>0&&age<12)
ticketPrice=20;
else if(age<20)
ticketPrice=40;
else if(age<50)
ticketPrice=80;
else
ticketPrice=35;
System.out.println("门票价格:"+ticketPrice);
}
}/
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Tourist t1=new Tourist();
System.out.print("请输入年龄:");
t1.setAge(input.nextInt());
t1.ticket();
}
}
- (1)编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型); // 存放圆的半径;
②两个构造方法
Circle( ) // 将半径设为0
Circle(double r ) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //获取圆的面积
double getPerimeter( ) //获取圆的周长
void show( ) //将圆的半径、周长、面积输出到屏幕
(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:
①一个成员变量
double hight(私有,浮点型); // 圆柱体的高;
②构造方法
Cylinder (double r, double h ) //创建Circle对象时将半径初始化为r
③ 成员方法
double getVolume( ) //获取圆柱体的体积
void showVolume( ) //将圆柱体的体积输出到屏幕
编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。
//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() { //成员方法--显示园半径、周长、面积
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;
}
public 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(); //调用子类方法
}
}
- 编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。(异常处理)
//programme name Strinput.java
import 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 InputStreamReader(System.in));
try{System.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 = 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.valueOf(int1+int2);
}
static String strAdd(float flt1,float flt2) {
return String.valueOf (flt1+flt2);
}
}
- 应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:E:/myjava/Hello.java)
// Programme Name FISDemo.java
import java.io.*;
public class FISDemo {
public static void main(String args[]) {
byte[] buf=new byte[2056];
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( );
}
}
- 编写一个Java程序将当100,101,102,103,104,105个数以数组的形式写入到Dest.txt文件中,并以相反的顺序读出显示在屏幕上。(文件)
import java.io.*;
public class IODemo {
public static void main( String args[] ) {
int data[] = {100,101,102,103,104,105};
int[] t=new int[200];
try{
// File file=new File("dest.txt");
DataOutputStream out = new DataOutputStream (new FileOutputStream("dest.txt"));
for(int i=0;i<data.length;i++)
out.writeInt(data[i]);
out.close();
DataInputStream in = new DataInputStream (new FileInputStream("dest.txt"));
//先读出来再倒序输出
for(int i=0;i<data.length;i++) {
t[i]=in.readInt();
}
for(int i= data.length-1;i>= 0;i--) {
System.out.print(" "+t[i]);
}
/* for(int i= data.length-1;i>= 0;i--) {
t=in.readInt(data[i]);
System.out.print(" "+t);
}*/
System.out.println( );
in.close();
}catch(IOException e)
{
System.out.println(e.getMessage());}
}
}
- 编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。
// programme name TestThread;
// 声明一个子线程类Threaddemo;
class ThreadDemo extends Thread {
public ThreadDemo(String str) {
super(str);
}
public void run() {
for(int i=0;i<20;i++){
System.out.print(“ ”+this.getName());
Try {
Sleep(300);
}catch(InterruptedException 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();
thread3.start();
}
}
10. 编写程序,在屏幕上显示带标题的窗口,并添加一个按钮。当用户单击按钮时,结束程序。(窗体编程)
// Programme Name ButtonEventDemo.java
import javax.swing.*;
import java.awt.event.*;
public class ButtonEventDemo extends JPanel implements ActionListener{
protected JButton b1; //声明一个按钮对象
public ButtonEventDemo() { //构造方法
ImageIcon ButtonIcon = new 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.exit(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(true); //面板要求不透明
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)类Student的成员变量:
sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
- 根据类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 XF) {
super();
sNO=XH;
sName=XM;
sSex=XB;
sAge=NL;
sJava=XF;
}
public String getNO() {
return sNO;
}
public String getName() {
return sName;
}
public String getSex() {
return sSex;
}
public int getAge() {
return sAge;
}
public int getJava() {
return sJava;
}
public static void main(String[] args) {
Student[] st=new Student[5];
st[0]=new Student("09zc01","张三","男",19,94);
st[1]=new Student("09zc02","李四","男",20,85);
st[2]=new Student("09zc03","王五","女",18,96);
st[3]=new Student("09zc04","赵六","男",17,90);
st[4]=new Student("09zc05","杨七","女",21,88);
int max=0,min=100,sum=0;
System.out.println(" 学生信息:");
for (int i=0;i<st.length;i++) {
if (st[i].sJava < min)
min=st[i].sJava;
if (st[i].sJava > max)
max=st[i].sJava;
sum=sum+st[i].sJava;
System.out.println("学生编号:"+st[i].getNO()+", 姓名:"+st[i].getName()+", 性别:"+st[i].getSex()+", 年龄:"+st[i].getAge()+", Java课学分:"+st[i].getJava());
}
System.out.println();
System.out.println(" 共有学生:"+st.length+", 平均成绩:"+sum/st.length);
System.out.println(" 最小学分:"+min+", 最大学分:"+max);
}
}