您的位置:首页 > 编程语言 > Java开发

Java程序设计实用教程(第四版,叶贺亚)考试复习题-纯属个人思想

2015-06-29 16:35 579 查看
这是我大二考试的复习资料,当时我在老师那里高培训,面对这种题目我笑了,,,不为别的就是觉得自己很简单就可以写出来,于是我就把代码部分发到博客,其他的在我的下载频道,地址点击打开链接。。

93、简答:关键字this 与 super 的用法
this
1)指当前对象
2)调用当前对象的成员
3)调用本类重载的构造方法
super([参数列表])
1)构造父类
2)调用父类成员
94、编程:判断某年是否为闰年
import java.util.Scanner;
public class JudgeLeepYear {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("是否是闰年:" + isLeepYear(sc.nextInt()));
	}
	public static boolean isLeepYear(int year) {
		return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
	}
}
95、编程:用java.lang.Math类生成10个0-99之间的随机整数,求最大最小值。
public class GenerateRandom {
	public static void main(String[] args) {
		rand(10);
	}
	public static void rand(int number){
		for (int i = 0; i < number; i++) {
			System.out.print((int)(Math.random()*100)+" ");
		}
	}
}
96、编程: 求解1+1/2!+1/3!+``````
import java.util.Scanner;
public class Factories {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int fact = 1;
		double sum = 0;
		for (int i = 1; i <= n; i++) {
			fact *= i;
			sum += 1.0 / fact;
		}
		System.out.println(sum);
	}
}
97、编程:写一个矩形类(类名为:Rect),包含:①两个protected整型属性,分别代表:宽、高;②两个构造方法:一个带两个参数,一个不带参数;③两个方法:求面积area()、求周长perimeter() 
public class Rect {
	protected int width, height;
	public Rect(int width, int height) {
		this.width = width;
		this.height = height;
	}
	public Rect() {
		this(0, 0);
	}
	public int area() {
		return width * height;
	}
	public int perimeter() {
		return (width + height) * 2;
	}
}
98、编程:写一个复数类(类名为:Complex),包含:①两个protected整型属性,分别代表:实数部分、虚数部分;②两个构造方法:一个带两个参数,一个不带参数;③两个方法:求实数加法的complexAdd(Complex a)方法,能把当前复数对象的实部、虚部组合成a+bi的字符串形式的String toString( )方法;④在main( )方法中对该复数类进行如下功能验证:调用complexAdd( )方法把1+2i 和3+4i 相加产生一个新的复数4+6i,调用toString( )方法把新复数转换成字符串“4+6i”,然后用System.out.println( )方法输出该字符串。
public class Complex {
	protected int real, imaging;
	public Complex() {
		this(0, 0);
	}
	public Complex(int real, int imaging) {
		this.real = real;
		this.imaging = imaging;
	}
	// 原地修改
	public void complexAdd(Complex a) {
		this.real += a.real;
		this.imaging += a.imaging;
	}
	public String toString() {
		return real + "+" + imaging + "i";
	}
	public static void main(String[] args) {
		Complex c1 = new Complex(1, 2);
		Complex c2 = new Complex(3, 4);
		c1.complexAdd(c2);
		System.out.println(c1.toString());// 输出4+6i
	}
}
99、编程:①创建一个自定义的框架Frame对象作为程序的主窗口;②设置窗口标题;③窗口位置(200,200)、窗口大小(240,150);④设置流布局管理;⑤添加标签、文本行及按钮组件;⑥事件响应:点击“Ok”按钮,能实现把第一个文本行中的信息显示在第二个文本行。
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame extends Frame {
	private static final long serialVersionUID = 1L;
	private Button btn = null;
	private TextField text1 = null, text2 = null;
	public MyFrame() {
		super("学生很懒!");
		this.setLocation(200, 200);
		this.setSize(240, 150);
		this.setLayout(new FlowLayout());
		this.add(new Label("文本一"));
		this.add(text1 = new TextField(20));
		this.add(new Label("文本二"));
		this.add(text2 = new TextField(20));
		this.add(btn = new Button("OK"));
		btn.addActionListener(new ActionListener() {
			// 这个地方简单点想
			public void actionPerformed(ActionEvent e) {
				String t1 = text1.getText();
				text2.setText(t1);
			}
		});
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new MyFrame();
	}
}
100、编程:实现在TextArea中输出最后一次鼠标单击的x,y坐标和连续单击的次数。
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TextAreaSystem extends Frame implements WindowListener,
		MouseListener {
	private static final long serialVersionUID = 1L;
	private TextArea ta = null;
	private int x = 0, y = 0;
	private int click = 0, clicks = 0;// 点击1次和点击两次
	public TextAreaSystem() {
		this.setBounds(100, 200, 300, 300);
		// new对象
		ta = new TextArea();
		// 添加组件
		this.add(ta);
		// this.addMouseListener(this);// 这个可以不加
		ta.addMouseListener(this);
		this.addWindowListener(this);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new TextAreaSystem();
	}
	public void windowActivated(WindowEvent arg0) {// 没用到
	}
	public void windowClosed(WindowEvent arg0) {// 没用到
	}
	public void windowClosing(WindowEvent arg0) {
		System.out.println("x:" + x + " ,y:" + y);
		System.exit(0);
	}
	public void windowDeactivated(WindowEvent arg0) {// 没用到
	}
	public void windowDeiconified(WindowEvent arg0) {// 没用到
	}
	public void windowIconified(WindowEvent arg0) {// 没用到
	}
	public void windowOpened(WindowEvent arg0) {// 没用到
	}
	public void mouseClicked(MouseEvent arg0) {// 目标
		// 获得鼠标点击的位置,当然他只能获得注册次监听器组件范围内的点
		// 注意:TextArea可以和Frame抢焦点,如果是this注册监听,那么x和y的值就是0,不会改变
		x = arg0.getX();
		y = arg0.getY();
		if (arg0.getClickCount() > 1) {
			clicks++;// 连续单机两次
		} else {
			click++;// 单击一次
		}
		// 显示信息
		ta.setText("最后一次单击位置: X-" + x + " , Y-" + y);
		ta.append("\n单击次数为:" + click);
		ta.append("\n双击次数为:" + clicks);
	}
	public void mouseEntered(MouseEvent arg0) {// 没用到
	}
	public void mouseExited(MouseEvent arg0) {// 没用到
	}
	public void mousePressed(MouseEvent arg0) {// 没用到
	}
	public void mouseReleased(MouseEvent arg0) {
	}
}
101、编程:以输出奇数/偶数序列线程为例,分别采用两种创建线程的方式编程实现:①继承线程Thread类;②实现Runnable接口
public class Test_Run {
	public Test_Run() {
		Thread o = new Odd();
		Thread e = new Thread(new Even());
		o.start();
		e.start();
	}
	public static void main(String[] args) {
		new Test_Run();
	}
	// 奇数类
	private class Odd extends Thread {
		public void run() {
			for (int i = 100; i < 200; i+=2) {
				System.out.print(i+" ");
			}
			System.out.println("\n奇数输出完毕");
		}
	}
	// 偶数类
	private class Even implements Runnable {
		public void run() {
			for (int i = 201; i < 300; i+=2) {
				System.out.print(i+" ");
			}
			System.out.println("\n偶数输出完毕");
		}
	}
}
102、编程:由键盘输入一个整数,求出该数所有的因子,如输入12,则输出12的所有因子为1、2、3、4、6、12
import java.util.Scanner;
public class Divisor {// Divisor因子
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int factor = sc.nextInt();// factor因子
		for (int i = 1; i <= factor; i++) {
			if (factor % i == 0) {
				System.out.print(i + " ");
			}
		}
	}
}
103、编程:写一个程序,将[1,1000]范围内能同时被3、5整除的数输出。
public class DividedExactlyByThreeAndFive {
	public static void main(String[] args) {
		int count = 0;// 这个只是为了输出好看
		for (int i = 1; i <= 1000; i++) {
			if (i % 3 == 0 || i % 5 == 0) {
				count++;
				System.out.print(i + " ");
				if (count % 5 == 0) {
					System.out.println();
				}
			}
		}
	}
}
104、编程:写一个字符界面的应用程序,从键盘接收用户输入的5个整数,输出其中的最大、最小值。
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class UserMaxMin extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JButton btnVerify = null;// 确认按钮
	// textInput表示输入文本框ta表示输出最大最小文本框
	private JTextField textInput = null;
	private JTextArea ta = null;
	public UserMaxMin() {
		super("输出最大最小值");
		this.setBounds(200, 100, 325, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setBackground(Color.cyan);
		this.setLayout(null);// 绝对定位
		addModule();
		this.setVisible(true);
	}
	private void addModule() {
		// new 对象
		JLabel helpJLabel = new JLabel("提示:每个整数之间用英文状态下的“,”隔开。");
		JLabel inJLabel = new JLabel("请输入:");
		textInput = new JTextField(20);
		ta = new JTextArea();
		btnVerify = new JButton("确认");
		// 给组件绝对定位
		helpJLabel.setBounds(20, 5, 300, 40);
		inJLabel.setBounds(20, 35, 100, 40);
		textInput.setBounds(70, 45, 220, 20);
		ta.setBounds(20, 80, 220, 100);
		btnVerify.setBounds(100, 200, 60, 40);
		// ta设置属性
		ta.setEditable(false);
		ta.setText("最大值\t最小值");
		ta.append("\n0\t0");
		ta.append("\n这个输入一定要按要求,\n不然后果自负。");
		// btnVerify加监听
		btnVerify.addActionListener(this);
		// this添加组件
		this.getContentPane().add(helpJLabel);
		this.getContentPane().add(inJLabel);
		this.getContentPane().add(textInput);
		this.getContentPane().add(ta);
		this.getContentPane().add(btnVerify);
	}
	public void actionPerformed(ActionEvent arg0) {
		String s = textInput.getText();// 这个地方有bug
		String[] strs = s.split(",");
		if (strs.length != 5) {
			JOptionPane.showConfirmDialog(this, "输入非法");
			return;
		}
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < strs.length; i++) {
			int temp = Integer.parseInt(strs[i]);
			if (max < temp) {
				max = temp;
			}
			if (min > temp) {
				min = temp;
			}
		}
		ta.setEditable(false);
		ta.setText("最大值\t最小值");
		ta.append("\n" + max + "\t" + min);
		ta.append("\n这个输入一定要按要求,\n不然后果自负。");
	}

	public static void main(String[] args) {
		new UserMaxMin();
	}
}
105、编程:用字符输入输出流,将一个文本文件从一个文件夹拷贝到另一个文件夹。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFileChar {
	public static void main(String[] args) {
		// 文件内容为”恭喜你成功拷贝文件!!!“;这里不考虑放在同一个地方的情况。
		copyFileFromByChar("d:/ex/a/", "fileStream.txt", "d:/ex/b/");// 给文件路径名
	}
	public static void copyFileFromByChar(String dirOld, String fileName,
			String dirNew) {
		FileReader fr = null;
		FileWriter fw = null;
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			fr = new FileReader(dirOld + fileName);
			fw = new FileWriter(dirNew + fileName);
			br = new BufferedReader(fr);
			bw = new BufferedWriter(fw);
			while(true){
				String aline = null;
				try {
					aline = br.readLine();
					bw.write(aline);
					bw.newLine();
					bw.flush();
				} catch (Exception e) {
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {// 真正做项目的关流的方法
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
106、编程:从键盘接收用户输入的一个姓名,输出“姓名 Welcome you!”
import java.util.Scanner;
public class Welcom {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		System.out.println(s+" Welcome you!");
	}
}
107、编程:从键盘接收用户输入的10个整数,然后以从大到小的顺序输出这些整数。
import java.util.Scanner;
public class TenInt {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] mat = new int[10];
		for (int i = 0; i < mat.length; i++) {
			mat[i] = sc.nextInt();
		}
		for (int i = 0; i < mat.length-1; i++) {
			for (int j = i+1; j < mat.length; j++) {
				if(mat[i]<mat[j]){
					int temp = mat[i];
					mat[i] = mat[j];
					mat[j] = temp;
				}
			}
		}
		for (int i = 0; i < mat.length; i++) {
			System.out.print(mat[i]+" ");
		}
		System.out.println();
	}
}
109、编写程序输出九九乘法表。
public class MultiplicationTable {// MultiplicationTable九九乘法表
	public static void main(String[] args) {
		lowerTriangular();
		turnLlowerTriangular();
		System.out.println("-------------------------------------------");
		turnUpperTriangular();
		upperTriangular();
	}
	public static void upperTriangular() {// 上三角
		for (int i = 1; i <= 9; i++) {// 行
			for (int j = i; j <= 9; j++) {// 列
				System.out.print(i + "*" + j + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
	public static void turnUpperTriangular() {// 上三角
		for (int i = 9; i >= 1; i--) {// 行
			for (int j = i; j <= 9; j++) {// 列
				System.out.print(i + "*" + j + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
	public static void lowerTriangular() {// 下三角
		for (int i = 1; i <= 9; i++) {// 列
			for (int j = 1; j <= i; j++) {// 行
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
	public static void turnLlowerTriangular() {// 下三角
		for (int i = 9; i >= 1; i--) {// 列
			for (int j = 1; j <= i; j++) {// 行
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
}
110、能判断一个数是否为素数。
import java.util.Scanner;
public class Primer {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(isPrime(sc.nextInt()));
	}
	public static boolean isPrime(int prime) {
		if (prime == 2 || prime == 3 || prime == 5) {
			return true;
		}
		for (int i = 2; i * i <= prime; i++) {// 例如25=5*5,然而5是他的因子。
			if (prime % i == 0) {
				return false;
			}
		}
		return true;
	}
}
111、编程:用字节输入输出流,将一个文本文件从一个文件夹拷贝到另一个文件夹。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
	public static void main(String[] args) {
		// 文件内容为”恭喜你成功拷贝文件!!!“;这里不考虑放在同一个地方的情况。
		copyFileFrom("d:/ex/a/", "text.java", "d:/ex/b/");// 给文件路径名
	}
	public static void copyFileFrom(String dirOld, String fileName,
			String dirNew) {
		FileInputStream fin = null;
		FileOutputStream fout = null;
		try {
			fin = new FileInputStream(dirOld + fileName);
			byte[] buffer = new byte[512];
			fout = new FileOutputStream(dirNew + fileName);
			// 这种方法才是完全拷贝,不多不少。这种字节流只有一个缺点;那就是速度慢,优点是正确性。
			int count = 0;
			while ((count = fin.read(buffer)) > 0) {
				fout.write(buffer, 0, count);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {// 真正做项目的关流的方法
			if (fin != null) {
				try {
					fin.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fout != null) {
				try {
					fout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: