Java有关类的练习题

  1. 建立一个人类(Person)和学生类(Student),功能要求如下。
    (1)Person类中包含4个私有型的数据成员name,addr,sex,age,分别为字符串型,字符串型,字符型及整型,表示姓名,地址,性别和年龄。一个4参数构造方法,一个2参数的构造方法,一个无参数的构造方法,一个输出方法显示4种属性
    (2)Student类继承Person类,并增加成员math,english存放数学成绩和英语成绩。一个6参数构造方法,一个2参数的构造方法,一个无参数的构造方法

Person类

class Person{
	private String name;
	private String addr;
	private char sex;
	private int age;
	
	Person(String name,String addr,char sex,int age) {
		this.name = name;
		this.addr = addr;
		this.sex = sex;
		this.age = age;
	}
	Person(String name,String addr){ 
		this.name = name;
		this.addr = addr;
	}
	Person(){}
	
	public void print() {
		System.out.println("姓名:"+this.name);
		System.out.println("地址:"+this.addr);
		System.out.println("性别:"+this.sex);
		System.out.println("年龄:"+this.age);
	}
}

Student类

class Student extends Person{
	int math;
	int english;
	Student(String name,String addr,char sex,int age,int math,int english){
		super(name,addr,sex,age);
		this.math = math;
		this.english = english;
	}
	Student(int math,int english){
		this.math = math;
		this.english = english;
	}
	Student(){}
	@Override 
	public void print() {
		super.print();
		System.out.println("数学成绩:"+math);
		System.out.println("英语成绩:"+english);
	}
}

测试一下

		Student stu = new Student("张三","安徽",'男',20,99,87);
		stu.print();

Java有关类的练习题_第1张图片

2.定义员工类,具有姓名,年龄,性别属性,并具有构造方法和显示数据方法。定义管理层类,继承员工类,并自己的属性职务和年薪。定义职员类,继承员工类,并有自己的属性所属部门和月薪。
1)员工类

class staff{
	private String name;
	private int age;
	private char sex;
	
	public staff(String name,int age,char sex){
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public String getName() {
		return this.name;
	}
	public int getAge() {
		return this.age
	}
	public char getSex() {
		return this.sex;
	}
	public void tell() {
		System.out.println("姓名:"+this.name);
		System.out.println("年龄:"+this.age);
		System.out.println("性别:"+this.sex);
	}
}

2)管理层类

class administration extends staff{
	private String post;
	private double salary;
	public administration(String name,int age,char sex,String post,double salary) {
		super(name, age, sex);
		this.post = post;
		this.salary = salary;
	}
	public String getPost() {
		return this.getPost();
	}
	public double getSalary() {
		return this.salary;
	}
	@Override
	public void tell() {
		super.tell();
		System.out.println("职位:"+this.post);
		System.out.println("年薪:"+this.salary);
	}
}

3)职员类

class staffMember extends staff{
	private String department;
	private double salary;
	public staffMember(String name,int age,char sex,String department,double salary) {
		super(name,age,sex);
		this.department = department;
		this.salary = salary;
	}
	public String getDepartment() {
		return this.department;
	}
	public double getSalary() {
		return this.salary;
	}
	@Override
	public void tell() {
		super.tell();
		System.out.println("部门:"+this.getDepartment());
		System.out.println("月薪:"+this.getSalary());
	}
}

3.编写程序,统计出字符串want you to know one thing中字母n和字母o的出现次数

		String str = "want you to know one thing";
		char [] arr = str.toCharArray();
		int oNum = 0;
		int nNum = 0;
		for(char element:arr) {
			if(element=='o')
				oNum++;
			else if(element=='n')
				nNum++;
		}
		System.out.println("字母n出现的次数:"+nNum);
		System.out.println("字母o出现的次数:"+oNum);

4.建立一个可以实现整型数组的操作类(Array),类中允许操作的数组大小有外部动态指定,同时在Array类里需要提供有数组的以下处理:进行数据的增加(如果数据满了则无法增加),可以实现数组的容量扩充,取得数组的全部内容,完成之后再次基础上派生两个子类。
数组排序类
数组反转类

你可能感兴趣的:(Java)