java常见编程题

1.已知有一个父类 Person,包含两个属性 name 和 age,还有带两个参数的构造方法 Person()和一个打印信息的info()方法。

public class Person{
protected String name;
protected int age;
public Person(String name, int age){
this.name = name;
this.age = age;}
public String info(){
return “name=+name+,age=+age;
}}

定义一个新的类Student,继承 Person 类,要求:
(1)增加 String 类型的属性“school”;
(2)编写 Student 类的构造方法 Student(String name, int age, String school),并调用父类构造方法;
(3)重写Person 类的 info()方法。

public class Student extends Person {
    private String school;
    public Student(String name, int age, String school) {
        super(name, age);
        this.school = school;
    }
    public String info() {
        return "name=" + name + ", age=" + age + ", school=" + school;
    }
}

2.题目已给定Employee类代码,请按如下要求编程。

  1. 定义津贴接口(IAllowance),只包含抽象方法allowance( ),该方法无返回值。
    2)定义子类教师类(Teacher),继承自Employee类,实现IAllowance接口。要求提供对应属性的getters和setters方法。
    成员变量:
    工号(id) 私有String类型
    职称(title) 私有String类型
    月工资(salary) 私有double类型
    3)教师类Teacher编写构造方法,要求调用Employee类的构造方法,为5个成员初始化,并实现接口类方法allowance( ),显示津贴项目和金额,如“差旅补:660/月”。
    题目提供代码如下:
class  Employee {  	
	//声明成员变量
	String name;	 
	int age;
	
	public Employee(String name, int age ){   //定义带两个参数的构造方法
		this.name = name;
		this.age = age;
	}
}

题解


interface IAllowance {
    void allowance();
}

class Teacher extends Employee implements IAllowance {
    private String id;
    private String title;
    private double salary;

    public Teacher(String name, int age, String id, String title, double salary) {
        super(name, age);
        this.id = id;
        this.title = title;
        this.salary = salary;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public void allowance() {
        System.out.println("差旅补:660/月");
    }
}

3.编写程序实现以下要求:
(1)定义一个动物类接口 Animal,该接口中只有一个抽象方法 shout()。
(2)定义一个子类Dog 实现Animal 并重写 shout()方法,输出“汪汪”叫声。
(3)定义一个子类Cat 实现 Animal 并重写 shout()方法,输出“喵喵”叫声。
(4)定义一个测试类 test,使用多态的方式创建Dog 类和Cat 类对象,并分别调用 shout()
方法。

interface Animal {
    void shout();
}
class Dog implements Animal {
    public void shout() {
        System.out.println("汪汪");
    }
}
class Cat implements Animal {
    public void shout() {
        System.out.println("喵喵");
    }
}
public class Test {
    public static void main(String[] args) {
        Animal dog = new Dog();
        dog.shout(); // 输出:汪汪
        Animal cat = new Cat();
        cat.shout(); // 输出:喵喵
    }
}

4.编写一个程序,使之从键盘读入 10 个整数存入整数数组a 中,并输出这 10 个数。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = new int[10];  // 创建一个长度为10的整数数组
        for (int i = 0; i < 10; i++) {
            a[i] = scanner.nextInt();  // 从键盘读入整数并存入数组中
        }
        for (int i = 0; i < 10; i++) {
            System.out.print(a[i] + " ");  // 输出数组中的整数
        }
    }
}

5.编写一个Java应用程序,在键盘上输入一个正整数n,计算并输出n!的值。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(); // 从键盘读入一个正整数n
        long factorial = 1; // 初始化阶乘结果为1
        // 计算阶乘
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        System.out.println(n + "的阶乘值为:" + factorial);
    }
}

6.本题已提供Shape抽象类和Showable接口代码,请按如下要求编写程序。
1)创建一个矩形(Rectangle)类,并且该类继承Shape,实现Showable接口。Rectangle类新增成员长度(length)和宽度(width),均为double类型,实现抽象类和接口中的相关方法。
2)实现Rectangle类带有三个参数的构造方法,要求调用父类构造方法。
3)重写Object的toString()方法,返回字符串格式为:Rectangle[ 长度=XXX,宽度=XXX,颜色=XXX] ,其中XXX表示对应属性的值。
题目提供代码如下:

abstract class Shape{
	protected String color;
	Shape(String color){
		this.color = color;
	}
	abstract double area();  // 求面积
	abstract double allEdg(); //求周长
}
interface Showable{
	abstract void showInfo();  //显示Shape类的两个抽象方法返回值
}
class Rectangle extends Shape implements Showable {
    private double length;
    private double width;

    Rectangle(double length, double width, String color) {
        super(color);
        this.length = length;
        this.width = width;
    }
    double area() {
        return length * width;
    }
    double allEdge() {
        return 2 * (length + width);
    }

    public void showInfo() {
        System.out.println("矩形的面积为:" + area());
        System.out.println("矩形的周长为:" + allEdge());
    }

    public String toString() {
        return "Rectangle[ 长度=" + length + ",宽度=" + width + ",颜色=" + color + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(5.0, 3.0, "红色");
        System.out.println(rectangle);
        rectangle.showInfo();
    }
}

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值