第七章:课上手写编程题
1、
参考代码
// 计算30的阶乘
public class Factorial {
public static void main(String[] args) {
float fac = 1f;
for (int i = 1; i <= 30; i++)
fac *= i;
System.out.println(fac);
}
}
// 前20个斐波那契数列
public class Fibonacci {
public static void main(String[] args) {
int[] fib = new int[20];
fib[0] = 1;
fib[1] = 1;
for (int i = 2; i < 20; i++)
fib[i] = fib[i-1] + fib[i-2];
for (int i = 0; i < 20; i++)
System.out.print(fib[i] + "\t");
}
}
// 输出数字图形
public class NumberImage {
public static void main(String[] args) {
int n = 1;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= i; j++){
System.out.print(n++ + "\t");
if (j == i)
System.out.println();
}
}
}
2、
参考代码
public class Student{
public String name;
public int age;
public char gender;
public int sno;
public String dno;
}
3、
参考代码
public void displayInfo(){
System.out.println(name + age + gender + sno +dno);
}
4、
参考代码
public int sum(int math, int english, int computer){
return math + english + computer;
}
5、
参考代码
public Student(){}
public Student(String name, int age, char gender, int sno, String dno){
this.name = name;
this.age = age;
this.gender = gender;
this.sno = sno;
this.dno = dno;
}
6、
参考代码
public class STUDENT {
public String name;
public int age;
public char gender;
public String classNum;
public String sno;
STUDENT(){}
STUDENT(String name, int age, char gender, String classNum, String sno){
this.name = name;
this.age = age;
this.gender = gender;
this.classNum = classNum;
this.sno = sno;
}
public void display(){
System.out.println("姓名:" + this.name + "\t" +
"年龄:" + this.age + "\t" +
"性别:" + this.gender + "\t" +
"所在班级:" + this.classNum + "\t" +
"座位号:" + this.sno +"\t");
}
public static void main(String[] args) {
STUDENT s1 = new STUDENT("令狐冲", 23, '男', "01", "01");
s1.display();
STUDENT s2 = new STUDENT("任盈盈", 22, '女', "01", "02");
s2.display();
}
}
7、
参考代码
public class RecursionTest {
public static void main(String[] args) {
RecursionTest rt = new RecursionTest();
int i = 5;
int result = rt.factorial(i);
System.out.println("运算的结果为:" + result);
}
public int factorial(int n){
if (n < 0) return -1;
else if (n <= 1) return 1;
else return factorial(n - 1) * n;
}
}
8、
参考代码
import java.util.Scanner;
public class Calculator {
int count;
void increment(){
count++;
System.out.println(count + "【计数器加1】");
}
void decrement(){
count--;
System.out.println(count + "【计数器减1】");
}
void reset(){
count = 0;
System.out.println(count + "【计数器已归零】");
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
Scanner sin = new Scanner(System.in);
System.out.print("请输入当前值:");
calculator.count = sin.nextInt();
// 归零
calculator.reset();
// 加1
calculator.increment();
calculator.increment();
// 减1
calculator.decrement();
calculator.decrement();
// 归零
calculator.reset();
}
}
9、
参考代码
class Mouse{
public String name;
public boolean isAlive = true;
Mouse(String name){
this.name = name;
}
public void display(){
System.out.println("姓名:" + this.name + "\t状态:" + this.isAlive);
}
}
class Cat{
public String name;
public int record;
Cat(String name){
this.name = name;
}
public void catchMouse(Mouse m){
m.isAlive = false;
this.record++;
}
public void display(){
System.out.println("姓名:" + this.name + "\t抓捕个数:" + this.record);
}
}
public class Test {
public static void main(String[] args) {
Mouse m1 = new Mouse("m1");
Mouse m2 = new Mouse("m2");
Mouse m3 = new Mouse("m3");
Cat c1 = new Cat("c1");
m1.display();
m2.display();
m3.display();
c1.display();
System.out.println();
c1.catchMouse(m1);
m1.display();
m2.display();
m3.display();
c1.display();
}
}
10、
参考代码
public class Employee {
private String name;
private int age;
private double salary;
Employee(String name, int age, double salary){
this.name = name;
this.age = age;
this.salary = salary;
}
Employee(){
this(null, 0, 0);
}
public double setSalsry(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getIncome(){
return salary;
}
}
11、
参考代码
public class Manager {
private String name;
private int age;
private double salary;
private double bonus;
Manager(String name, int age, double salary, double bonus){
this.name = name;
this.age = age;
this.salary = salary;
this.bonus = bonus;
}
Manager(){
this(null, 0, 0, 0);
}
}
12、
参考代码
class InvalidDateException extends Exception {
public InvalidDateException() {
super();
}
public InvalidDateException(String message) {
super(message);
}
}
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(){}
public MyDate(int year, int month, int day) throws InvalidDateException {
if (year < 1 || month < 1 || month > 12 || day < 1 || day > 31) {
throw new InvalidDateException("Invalid date: " + year + "-" + month + "-" + day);
}
this.year = year;
this.month = month;
this.day = day;
}
public void setYear(int year) throws InvalidDateException {
if (year < 1) {
throw new InvalidDateException("Invalid year: " + year);
}
this.year = year;
}
public void setMonth(int month) throws InvalidDateException {
if (month < 1 || month > 12) {
throw new InvalidDateException("Invalid month: " + month);
}
this.month = month;
}
public void setDay(int day) throws InvalidDateException {
if (day < 1 || day > 31) {
throw new InvalidDateException("Invalid day: " + day);
}
this.day = day;
}
public static void main(String[] args) {
try {
MyDate d1 = new MyDate(-1, 13, 32);
MyDate d2 = new MyDate();
d2.setDay(100);
} catch (InvalidDateException e) {
System.out.println("日期输入有误");
}
}
}
13、
参考代码
public class Teacher {
public String tno;
public String ins;
public String sex;
public int age;
public Teacher(String tno, String ins, String sex, int age) {
this.tno = tno;
this.ins = ins;
this.sex = sex;
this.age = age;
}
public String getTno() {
return tno;
}
public String getIns() {
return ins;
}
public String getSex() {
return sex;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return getTno() + ", " + getIns() + ", " + getSex() + ", " + age;
}
public static void main(String[] args) {
Teacher t = new Teacher("12", "computer", "male", 34);
t.setAge(35);
System.out.println(t.toString());
}
}
14、
参考代码
public class Animal{
private String name;
private int legs;
public String getName() {
return name;
}
public int getLegs() {
return legs;
}
public Animal(String name, int legs) {
this.name = name;
this.legs = legs;
}
public Animal(){
this.name = "ABC";
this.legs=4;
}
public void move(){
System.out.println(getName() + " Moving!");
}
public void move(int n){
for (int i = 0; i < n; i++){
System.out.println(getName() + " Moving!");
}
}
public static void main(String[] args) {
Animal a1 = new Animal("BBB", 2);
a1.move();
Animal a2 = new Animal();
a2.move(3);
}
}
15、
参考代码
public class Student {
private String sNO;
private String sName;
private String sSex;
private int sAge;
private int sJava;
public Student(String sNO, String sName, String sSex, int sAge, int sJava) {
this.sNO = sNO;
this.sName = sName;
this.sSex = sSex;
this.sAge = sAge;
this.sJava = sJava;
}
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 void printInfo(){
System.out.println(getNO() + ", " + getName() + ", " + getSex() + ", " + getAge() + ", " + getAge());
}
public static void main(String[] args) {
Student s1 = new Student("1", "小米", "男", 23, 89);
Student s2 = new Student("2", "小李", "男", 22, 69);
s1.printInfo();
s2.printInfo();
System.out.println("平均成绩:" + (s1.getJava() + s2.getJava()) / 2);
}
}
16、
参考代码
public class Circle {
private double Radius;
Circle(){
this.Radius = 10;
}
Circle(double r){
this.Radius = r;
}
public void setRadius(double radius) {
Radius = radius;
}
public double getRadius() {
return Radius;
}
public void show(){
System.out.println("半径:" + getRadius() + "; " + "面积" + 3.14 * getRadius() * getRadius());
}
public static void main(String[] args) {
Circle c1 = new Circle();
c1.show();
Circle c2 = new Circle(20);
c2.show();
}
}
17、
参考代码
package com.exam; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class QQLogin extends JFrame {
// 定义组件
JLabel jl1, jl2;
JTextField jtf1, jtf2;
JButton jb1, jb2;
JPanel jp1, jp2, jp3;
public static void main(String[] args) {
QQLogin qqLogin = new QQLogin();
}
// 构造函数
public QQLogin() {
// 创建组件
jl1 = new JLabel("用户名:");
jl2 = new JLabel("密 码:");
jtf1 = new JTextField(15);
jtf2 = new JTextField(15);
jb1 = new JButton("确定");
jb2 = new JButton("重置");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
// 设置布局管理器
this.setLayout(new GridLayout(3, 1));
// 添加组件
jp1.add(jl1);
jp1.add(jtf1);
jp2.add(jl2);
jp2.add(jtf2);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
// 设置窗体属性
this.setTitle("模拟 QQ 登录界面");
this.setSize(300, 200);
this.setLocation(400, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// 为“确定”按钮添加事件监听
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户名和密码
String username = jtf1.getText().trim();
String password = jtf2.getText().trim();
// 判断用户名和密码是否正确(这里假设用户名和密码都是“123”)
if (username.equals("123") && password.equals("123")) {
JOptionPane.showMessageDialog(null, "用户名和密码输入正确!");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码输入错误!");
}
}
});
// 为“重置”按钮添加事件监听
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 清空用户名和密码输入框
jtf1.setText("");
jtf2.setText("");
}
});
}
}