程序设计题
1. 编一个Student类,类体包括:
(1)表示学生号的String类型的成员变量sNo,访问权限是private;
(2)表示学生姓名的String类型的成员变量sName,访问权限是private;
(3)表示学生年龄的int类型的成员变量sAge,访问权限是private;
(4)一个带3个参数的构造方法,给3个成员变量赋初值,访问权限是public;
(5)给这3个成员变量分别编写getXxx、setXxx方法,访问权限都是public(注意:这里要求编写6个方法)。
public class Student {
private String sNo;
private String sName;
private int sAge;
public Student(String a, String b, int c) {
this.sNo = a;
this.sName = b;
this.sAge = c;
}
public void setSNo(String a) {
this.sNo = a;
}
public void setsSName(String a) {
this.sName = a;
}
public void setSAge(int a) {
this.sAge = a;
}
public String getSNo() {
return sNo;
}
public String getSName() {
return sName;
}
public int getSAge() {
return sAge;
}
public static void main(String args[]) {
Student s = new Student("201702506", "lk", 19);
System.out.println(s.getSNo());
System.out.println(s.getSName());
System.out.println(s.getSAge());
}
}
2.按以下要求编写程序。
(1)创建一个矩形类Rectangle,添加width和height两个double的私有成员变量。
(2)在Rectangle中添加两种方法分别计算矩形的周长和面积。
(3)编程利用Rectangle输出一个矩形的周长和面积。
public class Rectangle {// 创建一个Rectangle类
private double width, length;// 定义width和height两个成员变量。
double area, zhou;
Rectangle(double w, double len) { // 分别对两个变量初始化不同的值
width = w;
length = len;
}
public double zhouRectangle() {
zhou = (width + length) * 2;
return zhou;
}
public double areaRectangle() {
area = width * length;
return area;
}
public static void main(String[] args) { // 声明对象和创建对象
double c, s;
Rectangle Rectangle1 = new Rectangle(10, 20);
c = Rectangle1.zhouRectangle();
s = Rectangle1.areaRectangle();
System.out.println("周长是:" + c);
System.out.println("面积是:" + s);
}
}
3.按以下要求编写程序
(1)编写Animal接口,接口中声明run()方法。
(2)定义Bird类和Fish类实现Animal接口。
(3)编写Bird类和Fish类的测试程序,并调用其中的run()方法。
interface Animal {// 声明接口
abstract void run();// 定义抽象方法
}
class Bird implements Animal {// 实现接口
public void run() {
System.out.println("测试run方法成功");
}
}
class Fish implements Animal {// 实现接口
public void run() {
System.out.println("测试run方法成功");
}
}
public class Animaljiekou {
public static void main(String[] args) {
Animal p1;// 声明接口变量
p1 = new Bird();// 实现类对象赋值接口变量
p1.run();// 接口回调
p1 = new Fish();// 实现类对象赋值接口变量
p1.run();// 接口回调
}
}
4.编写一个程序使之从键盘读入10个整数存入整型数组a中,并输出这10个整数的最大值和最小值。
package a1;
import java.util.Scanner;
public class Test43 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] score = new int[10];
System.out.print("请输入10个数:");
for (int i = 0; i < score.length; i++) {
score[i] = input.nextInt();
}
int max = score[0];
int min = score[0];
for (int i = 0; i < score.length; i++) {
if (max < score[i]) {
max = score[i];
}
if (min > score[i]) {
min = score[i];
}
}
System.out.println("最大值 " + max + ",最小值 " + min);
}
}
5. 定义一个“点”(PointDemo)类用来表示三维空间中的点,类体的成员变量x,y,z分别表示三维空间的坐标。类体中具有如下成员方法的定义:
1)构造方法PointDemo()可以生成具有特定坐标的点对象。
2)setX(), setY(), setZ()为可以设置三个坐标的方法。
3.)getDistance()为可以计算该点距离原点距离平方的方法。
class PointDemo{
private int x, y, z;
public PointDemo(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setZ(int z) {
this.z = z;
}
public double getDistance() {
return (x * x + y * y + z * z);
}
public static void main(String args[]) {
PointDemo x1 = new PointDemo(2, 2, 2);
System.out.println(x1.getDistance());
x1.setX(1);
x1.setY(1);
x1.setZ(1);
System.out.println(x1.getDistance());
}
}
6. 编写一个Java应用程序Test类,实现成员方法max(a,b)的重载。具体要求如下:
a) 编写void max(int a,int b)成员方法,对两个整数进行大小的比较,输出打印较大的那个整数。
b) 编写void max(float a,float b)成员方法,对两个float数进行大小的比较,输出打印较大的那个float数。
c) 编写void max(double a,double b)成员方法,对两个double数进行大小的比较,输出打印较大的那个double数。
package a1;
import java.util.Scanner;
public class Test {
static int a1, b1, max1;
static float a2, b2, max2;
static double a3, b3, max3;
public static void max(int a, int b) {
if (a >= b)
max1 = a;
else
max1 = b;
System.out.println("最大值:" + max1);
}
public static void max(float a, float b) {
if (a >= b)
max2 = a;
else
max2 = b;
System.out.println("最大值:" + max2);
}
public static void max(double a, double b) {
if (a >= b)
max3 = a;
else
max3 = b;
System.out.println("最大值:" + max3);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入两个整数:");
a1 = scanner.nextInt();
b1 = scanner.nextInt();
max(a1, b1);
System.out.println("请输入两个 float 型实数:");
a2 = scanner.nextFloat();
b2 = scanner.nextFloat();
max(a2, b2);
System.out.println("请输入两个 double 型实数:");
a3 = scanner.nextDouble();
b3 = scanner.nextDouble();
max(a3, b3);
}
}
7. 编写一个矩形类对数学中的矩形进行抽象描述,并具有方法可以计算矩形的周长和面积,具体分为以下三个子任务完成。
(1)创建Rectangle类,添加属性width、height,并定义构造方法Rectangle(double _width, double _height);
(2)在Rectangle类中添加两种方法computCircum()和computArea()分别计算矩形的周长和面积;
(3)编程利用Rectangle对象输出一个30*40的矩形的周长和面积。
package 考试;
public class Rectangle {// 创建一个Rectangle类
double width, length;// 定义width和height两个成员变量。
double area, circum;
Rectangle(double width, double heght) { // 分别对两个变量初始化不同的值
this.width = width;
this.length =heght;
}
public double computCircum() {
circum = (width + length) * 2;
return circum;
}
public double computArea() {
area = width * length;
return area;
}
public static void main(String[] args) { // 声明对象和创建对象
double c, s;
Rectangle Rectangle1 = new Rectangle(30, 40);
c = Rectangle1.computCircum();
s = Rectangle1.computArea();
System.out.println("周长是:" + c);
System.out.println("面积是:" + s);
}
}
8. 写出一个类People,并由该类做基类派生出子类Employee和Teacher。其中People 类具有name、age两个保护成员变量,分别为String类型、整型,且具有公有的getAge成员函数,用于返回age变量的值。Employee类具有保护成员变量empno为String类型,Teacher类有teano为String类型和zc成员变量为String类型。
class People{
protected String name;
protected int age;
public int getAge()
{
return age;
}
}
class Employee extends People{
protected String empno;
}
class Teacher extends People{
String teano;
String zc;
}
9. 使用FileReader类实现读取E://word.txt文件中的数据,并将其在控制台上输出。
import java.io.File;
import java.io.FileReader;
public class FileReaderDemo {
public static void main(String[] args) {
File file = new File("E://word.txt"); // 创建文件对象
try {
FileReader fReader = new FileReader(file); // 创建FileReader实例
int length;
while ((length = fReader.read()) != -1) { // 循环读取指定文件内容
System.out.print((char) length);
}
fReader.close(); // 关闭流
} catch (Exception e) {
e.printStackTrace();
}
}
}
10. 编写一个应用程序,如下图所示。
package 考试;
import java.awt.FlowLayout;
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.JTextField;
public class JiaDemo{
private JFrame jf;
private JLabel plus;
private JButton equal;
private JTextField num1, num2, num3;
public JiaDemo() {
jf = new JFrame();
jf.setLayout(new FlowLayout());
num1 = new JTextField(10);
num2 = new JTextField(10);
num3 = new JTextField(15);
plus = new JLabel("+");
equal = new JButton("=");
jf.add(num1);
jf.add(plus);
jf.add(num2);
jf.add(equal);
jf.add(num3);
jf.pack();//依据你放置的组件设定窗口的大小 使之能容纳你放置的所有组件
jf.setVisible(true);
equal.addActionListener(new monitor());
}
private class monitor implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText("" + (n1+n2));
}
}
public static void main(String[] args) {
new JiaDemo();
}
}
11. 使用Swing组件编写一个主类为TestGUI的程序实现如下功能:给出一个窗口,带有一个文本框和一个按钮,文本框输入用,用于输入字符。点击按钮后统计字符个数,并输出显示“你输入字符的个数为: ”。
import java.awt.*;
import javax.swing.*;
public class TestGUI extends JFrame {
public TestGUI() {
this.setSize(400, 300);
this.setLocationRelativeTo(null);//使窗口显示在屏幕中央
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JButton button1 = new JButton("计算");
JTextField text = new JTextField(20);
panel.add(text);
panel.add(button1);
this.add(panel, BorderLayout.NORTH);
this.setVisible(true);
button1.addActionListener(e -> {
int cnt = text.getText().length();
String msg = "你输入字符的个数为:" + cnt;
JOptionPane.showMessageDialog(this, msg);
});
}
public static void main(String[] args) {
new TestGUI();
}
}
12. 创建类Person,其中存储的成员数据为:age(int),sex(boolean),weight(int),至少有一个构造函数可以初始化这三个属性值,同时提供获取这三个属性值的public方。(答案可以删除设置方法)
方法一:package 考试;
public class Person {
int age;
boolean sex;
int weight;
public Person(int age, boolean sex, int weight) {
this.age = age;
this.sex = sex;
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public static void main(String[] args) {
Person p1 = new Person(20, false, 50);
System.out.println(p1.getAge());
System.out.println(p1.isSex());
System.out.println(p1.getWeight());
p1.setAge(22);
p1.setSex(true);
p1.setWeight(60);
System.out.println(p1.getAge());
System.out.println(p1.isSex());
System.out.println(p1.getWeight());
}
}
方法二:public class Person {
private int age;
private boolean sex;
private int weight;
public Person(int age, boolean sex, int weight) {
this.age = age;
this.sex = sex;
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
public class TestPerson {
public static void main(String[] args) {
Person p1 = new Person(21, false, 80);
System.out.println(p1.getAge());
System.out.println(p1.isSex());
System.out.println(p1.getWeight());
p1.setAge(22);
p1.setSex(true);
p1.setWeight(60);
System.out.println(p1.getAge());
System.out.println(p1.isSex());
System.out.println(p1.getWeight());
}
}
13.设计线程类WorkerThread,其构造函数接受一个message字符串作为参数,把该字符串打印到console上,同时,在WorkerThread的main函数中启动该线程。
package 考试;
public class WorkerThread extends Thread {
public WorkerThread(String message) {
System.out.println(message);
}
public static void main(String[] args) {
new WorkerThread("考试必胜").start();
}
}
14. 编写一个程序,用来计算1到100间所有整数的和是多少?
public class Sum {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("1到一100间所有整数的和是:" + sum);
}
}
15. 题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
package a1;
import java.util.Scanner;
public class Prog5{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个学生成绩(0~100):");
int n = scanner.nextInt();
grade(n);
}
//成绩等级计算
private static void grade(int n){
if(n>100 || n<0)
System.out.println("输入无效");
else{
String str = (n>=90)?"分,属于A等":((n>60)?"分,属于B等":"分,属于C等");
System.out.println(n+str);
}
}
}
16. 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
package 考试;
import java.util.*;
public class lianxi06 {
public static void main(String[] args) {
int m, n, x;
Scanner s = new Scanner(System.in);
System.out.print("键入两个正整数m和n:");
m = s.nextInt();
n = s.nextInt();
deff cd = new deff();
x = cd.deff(m, n);
int y = m * n / x;
System.out.println("最大公约数:" + x);
System.out.println("最小公倍数:" + y);
}
}
class deff {
public int deff(int x, int y) {
int t;
if (x < y) {
t = x;
x = y;
y = t;
}
while (y != 0) {
if (x == y)
return 1;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
17. 编程求1+2!+3!+。。。。。。+20!的和。
package 考试;
public class Prog21{
public static void main(String[] args){
long sum = 0;
for(int i=0;i<20;i++) {
sum += factorial(i+1);
}
System.out.println(sum);
}
//阶乘
private static long factorial(int n){
int mult = 1;
for(int i=1;i<n+1;i++)
mult *= i;
return mult;
}
}
18. 使用FileInputStream类和FileOutputStream类,编写一个程序,顺序读取text1.txt的文件里的内容,并将内容拷贝到text2.txt文件。
package a1;
import java.io.*;
public class FileCopyDemo {
public static void main(String[] args) {
File source = new File("e:/text1.txt");// 创建文件,作为要复制的文件
File target = new File("e:/text2.txt");// 创建文件,作为复制后的文件
FileInputStream fis = null;// 创建文件输入流对象
FileOutputStream fos = null;// 创建文件输出流对象
try {
fis = new FileInputStream(source);// 实例化文件输入流对象
fos = new FileOutputStream(target);// 实例化文件输出流对象
byte[] bytes = new byte[1024];// 创建字节数组
int length;
while ((length = fis.read(bytes)) != -1) {
fos.write(bytes, 0, length);// 将源文件内容写入新文件
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();// 释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();// 释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
19. 从键盘读入5个整数,并对它们进行排序,按由小到大的顺序从控制台输出。
方法一:package a1;
//选择排序
import java.util.Scanner;
public class Paixu2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int temp;
int[] a = new int[5] ;
System.out.println("请输入5个整数:");
for (int i = 0; i < 5; i++) {
a[i] = input.nextInt();
}
// 排序
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
// 输出结果
for (int i = 0; i < 5; i++) {
System.out.print(a[i] + " ");
}
}
}
方法二:
//冒泡排序
package a1;
import java.util.Scanner;
public class Bubble {
public static void main(String[] args) {
int[] arr=new int[5];
Scanner sc=new Scanner(System.in);
System.out.println("请输入5个整数:");
for(int i=0;i<5;i++)
{
arr[i]=sc.nextInt(); //输入5个数字存放在数组中;
}
bubbleSort(arr); //调用方法
System.out.println("冒泡排序后:");
for (int e : arr) {
System.out.print(e + " ");
}
}
//冒泡法排序方法:
public static void bubbleSort(int[] arr)
{
for(int x=0;x<arr.length-1;x++)//控制次数,循环长度-1次
{
for(int y=0;y<arr.length-1-x;y++)
{
if(arr[y]>arr[y+1])
{
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
}
20. 从键盘读入一个字符串,试从控制台输出读入的字符串,并且要求输出读入字符串的长度,以及字符串的大写字符形式。
package a1;
import java.util.Scanner;
public class Daxie{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = scanner.nextLine();
System.out.println("该字符串的长度为:" + str.length());
System.out.println("该字符串的大写字符形式为:" + str.toUpperCase());
}
}
21. 接口应用程序测试,具体要求:
(1) Biology生物接口中定义了breathe()抽象方法(用输出语句模拟即可);
(2)Animal动物接口继承了Biology接口,增加eat()和sleep()两个抽象方法;
(3) Human人类接口继承了Animal接口,增加think()和learn()两个抽象方法
(4) 定一个普通人类Person实现Human接口,并进行测试。
public interface Biology {
public void breathe();
}
public interface Animal extends Biology {
public void eat();
public void sleep();
}
public interface Human extends Animal{
public void think();
public void learn();
}
public class Person implements Human {
public void eat() {
System.out.println("我正在吃!");
}
public void sleep() {
System.out.println("我正在睡!");
}
public void breathe() {
System.out.println("我正在呼吸!");
}
public void think() {
System.out.println("我正在思考!");
}
public void learn() {
System.out.println("我正在学习!");
}
public static void main(String[] args) {
Person person = new Person();
person.eat();
person.sleep();
person.breathe();
person.think();
person.learn();
}
}
22.编程实现两个线程共同完成1+2+…10,一个线程计算完1+2+3+4+5后启动另一个线程完成剩下的计算。
public class Example{
public static void main(String args[]) {
ComputerSum sum = new ComputerSum();
sum.computer1.start();
}
}
class ComputerSum implements Runnable {
Thread computer1, computer2;
int i = 1, sum = 0;
ComputerSum() {
computer1 = new Thread(this);
computer2 = new Thread(this);
}
public void run() {
while (i <= 10) {
sum = sum + i;
System.out.println(sum);
i++;
if (i == 6 && Thread.currentThread() == computer1) {
System.out.println(computer1.getName() + "完成任务了!");
computer2.start();
return;
}
}
}
}
23.编写类PrimeNumber判断一个整数是否为素数。具体要求:素数只能被1和它本身整除且大于1的自然数。注意1不是素数。从键盘上输入一个整数n,判断n是否是素数。
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
int i;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int n = scanner.nextInt();
for (i = 2; i < n; i++) {
if (n % i == 0)
break;
}
if (n == i)
System.out.println(n + "是素数");
else
System.out.println(n + "不是素数");
}
}