Java习题——面向对象综合练习题

1.编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。然后对Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩。最后在Main函数中对student类的功能进行验证。

package cn.edu.nefu.exercise1;
/*1.编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。
然后对Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩。
最后在Main函数中对student类的功能进行验证。*/
public class Person {
	String Name;
	int Age;
	String Sex;
	public Person(String name, int age, String sex) {
		super();
		Name = name;
		Age = age;
		Sex = sex;
	}
}
package cn.edu.nefu.exercise1;

import java.util.Scanner;

//然后对Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩。
public class Student extends Person {

	int[] scores = new int[5];
	public Student(String name, int age, String sex) {
		super(name, age, sex);
		scores();
		
	}
	//存入成绩
	public void scores(){
		for(int i = 0;i
package cn.edu.nefu.exercise1;

public class Test {
	public static void main(String[] args) {
		Student s = new Student("李一",19,"男");
		s.aveScores();
	}
}

3..(1)定义一个游戏中Hero 英雄的类,在该类中定义英雄的名字, 生命值和等级3 个属性,定义一个构造函数完成对生命值和等级的初始化,分别赋初值为100,1。同时实现名字的输入和英雄信息的输出。

(2)在上一题的基础上,为英雄再定义拥有一个参数的构造方法,传入一个英雄类型的值,如果为1,则为普通英雄,生命值为100,如果该值为2,则为高级英雄,生命值初始化为200。

(3)在上两英雄类型的基础上,为英雄添加一个基本战斗的方法, 该方法拥有一个英雄类型的参数,当传入另一个英雄时,能降低对方100 点血。 再增加一个绝招的重载方法,加入一个战斗类型参数,通过输入不同绝招参数,降低对方不同的血量。

package cn.edu.nefu.exercise2;

import java.util.Scanner;

public class Hero {
//	3..(1)定义一个游戏中Hero 英雄的类,在该类中定义英雄的名字, 生命值和等级3 个属性,
//	 定义一个构造函数完成对生命值和等级的初始化,分别赋初值为100,1。同时实现名字的输入和英雄信息的输出。
	String name;
	int vitality;//生命值
	int grade; 
	
	//名字输入
	public void nameInput(){
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入英雄名字:");
		name = sc.nextLine();
	}
	//英雄信息输出
	public void showInfo(){
		System.out.println("姓名:"+name+"\n生命值:"+vitality+"\n等级:"+grade);
		System.out.println();
	}
	
	public Hero(){
		super();
		this.vitality = 100;
		this.grade = 1;
		nameInput();
		showInfo();
	}
	
	
//	(2)在上一题的基础上,为英雄再定义拥有一个参数的构造方法,传入一个英雄类型的值,
//	 如果为1,则为普通英雄,生命值为100,如果该值为2,则为高级英雄,生命值初始化为200。
	public Hero(int type){
		nameInput();
		if(type==1){
			this.vitality = 100;
			System.out.println("普通英雄创建成功;");
		}else if(type == 2){
			this.vitality = 200;
			System.out.println("高级英雄创建成功;");
		}
		this.grade = 1;
		showInfo();
	}
//	(3)在上两英雄类型的基础上,为英雄添加一个基本战斗的方法, 该方法拥有一个英雄类型的参数,
//	当传入另一个英雄时,能降低对方100 点血。 再增加一个绝招的重载方法,加入一个战斗类型参数,
//	通过输入不同绝招参数,降低对方不同的血量。
	public void combat(Hero hero){
		hero.vitality -= 100;
		System.out.println(hero.name+"受到攻击,生命值减100;");
		hero.showInfo();
	}
	
	public void combat(Hero hero,int type){
		if(type == 1){
			hero.vitality -= 50;
			System.out.println(hero.name+"受到轻度攻击,生命值减50");
		}else if(type == 2){
			hero.vitality -= 80;
			System.out.println(hero.name+"受到中度攻击,生命值减80");
		}else{
			hero.vitality -= 100;
			System.out.println(hero.name+"受到重度攻击,生命值减100");
		}
		hero.showInfo();
	}


}
package cn.edu.nefu.exercise2;

public class Test {
	public static void main(String[] args) {
		Hero h = new Hero();
		Hero h1 = new Hero(2);
		h.combat(h1);
		h.combat(h1, 2);
	}
}

4.、设计一个BankAccount类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等。BankAccount类包括,账号(BankAccountId)、开户日期Date(日期),Rest(余额)。另有一个构造函数和三个成员函数Bankin()(处理存入账),Bankout()处理取出账)和和一个负责生成账号的自动增长的函数。

package cn.edu.nefu.exercise3;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BankAccount {
//	4.、设计一个BankAccount类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等。
//	BankAccount类包括,账号(BankAccountId)、开户日期Date(日期),Rest(余额)。
	int BankAccountId;
	String Date;
	double Rest;
	
	private static int count = 0; //判断创建了几个账户
	
	//获取日期
	public void date(){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		Date date = cal.getTime();
		this.Date = df.format(date);
		System.out.println(this.Date+"成功创建账户"+this.BankAccountId+"余额为"+this.Rest+"元");
	}
	
//	另有一个构造函数和三个成员函数Bankin()(处理存入账),Bankout()处理取出账)和和一个负责生成账号的自动增长的函数。
	public BankAccount() {
		super();
		count++;
		id();
		date();
	}
	
	//生成账号
	private void id(){
		this.BankAccountId =2019000+count;
	}
	
	public void Bankin(double money){
		this.Rest += money;
		System.out.println("账户"+this.BankAccountId+"存入"+money+"元,余额为"+this.Rest+"元");
		
	}
	
	public void Bankout(double money){
		this.Rest -= money;
		if(this.Rest
package cn.edu.nefu.exercise3;

public class Test {
	public static void main(String[] args) {
		BankAccount ba = new BankAccount();
		BankAccount ba1 = new BankAccount();
		BankAccount ba2 = new BankAccount();
		BankAccount ba3 = new BankAccount();
		BankAccount ba4 = new BankAccount();
		ba.Bankin(300000);
		ba.Bankout(5000);
		ba1.Bankout(400);
	}

}

 

5、 编写一个程序,已有若干学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。
思路:
    设计一个学生类Stud,除了包括no(学号)、name(姓名)、和deg(成绩)数据成员外。有两个静态变量sum和num,分别存放总分和人数,另有一个构造函数、一个普通成员函数disp()和一个静态成员函数avg(),它用于计算平均分。

/*编写一个程序,已有若干学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。
        思路:
            设计一个学生类Stud,除了包括no(学号)、name(姓名)、和deg(成绩)数据成员外。有两个静态变量
        sum和num,分别存放总分和人数,另有一个构造函数、一个普通成员函数disp()和一个静态成员函数avg(),
        它用于计算平均分。*/
package cn.edu.nefu.oop.exercise5;

public class Stud {
    int no;
    String name;
    double deg;
    static double sum = 0;
    static int num = 0;

    public Stud(int no, String name, double deg) {
        this.no = no;
        this.name = name;
        this.deg = deg;
        disp();
        sum += deg;
        num++;

    }

    public void disp(){
        System.out.println("学号:"+this.no+"\t姓名:"+this.name+"\t成绩:"+this.deg);
    }

    public static void avg(){
        System.out.println("平均分:"+sum/num);
    }
}

 

package cn.edu.nefu.oop.exercise5;

public class Test {
    public static void main(String[] args) {
        Stud s = new Stud(2016001,"李一",89);
        Stud s1 = new Stud(2016002,"石英",90);
        Stud s2 = new Stud(2016003,"汪建熙",80);
        Stud.avg();
    }
}

6.编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。
思路:
    设计一个学生类Stud,除了包括no(学号)、name(姓名)和deg(成绩)数据成员外,有两个静态变量sum和num,分别存放总分和人数,另有成员函数disp(),用于输出数据成员的值,另有一个静态成员函数avg(),它用于计算平均分。在main()函数中定义了一个对象数组用于存储输入的学生数据。

/*编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。
        思路:
            设计一个学生类Stud,除了包括no(学号)、name(姓名)和deg(成绩)数据成员外,有两个静态变量
        sum和num,分别存放总分和人数,另有成员函数disp(),用于输出数据成员的值,另有一个静态成员函数
        avg(),它用于计算平均分。在main()函数中定义了一个对象数组用于存储输入的学生数据。*/
package cn.edu.nefu.oop.exercise6;

public class Stud {
    int no;
    String name;
    double deg;
    static double sum = 0;
    static int num = 0;

    public Stud(int no, String name, double deg) {
        this.no = no;
        this.name = name;
        this.deg = deg;
        disp();
        sum += deg;
        num++;
    }

    public void disp(){
        System.out.println("学号:"+this.no+"\t姓名:"+this.name+"\t成绩:"+this.deg);
    }

    public static void avg(){
        System.out.println("平均分:"+sum/num);
    }

    public static void main(String[] args) {
        Stud [] studs = new Stud[5];
        studs[0] = new Stud(2016001,"lily",89);
        studs[1] = new Stud(2016002,"linlin",90);
        studs[2] = new Stud(2016003,"sun",78);
        studs[3] = new Stud(2016004,"jams",67);
        studs[4] = new Stud(2016005,"dandan",95);
        Stud.avg();
    }
}

7.编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求只输出成绩在80~89分的学生数据。

思路:
    设计一个学生类Stud,包括no(学号)、name(姓名)和deg(成绩)数据成员,和成员函数 disp(),用于只输出成绩在80~89分数段的学生数据。在main()函数中定义了一个学生对象数组,用于存储输入的学生数据。

/*编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求只输出成绩在80~89分的学生数据。
        思路:
            设计一个学生类Stud,包括no(学号)、name(姓名)和deg(成绩)数据成员,和成员函数 disp(),
        用于只输出成绩在80~89分数段的学生数据。在main()函数中定义了一个学生对象数组,用于存储输入的
        学生数据。*/
package cn.edu.nefu.oop.exercise7;

public class Stud {
    int no;
    String name;
    double deg;

    public Stud(int no, String name, double deg) {
        this.no = no;
        this.name = name;
        this.deg = deg;
        disp();
    }

    public  void disp(){
        if(this.deg>=80&&this.deg<=89){
            System.out.println("学号:"+this.no+"  姓名:"+this.name+"  成绩:"+this.deg);
        }
    }

    public static void main(String[] args) {
        Stud[] studs = new Stud[5];
        studs[0] = new Stud(2016001,"lily",89);
        studs[1] = new Stud(2016002,"linlin",80);
        studs[2] = new Stud(2016003,"sun",78);
        studs[3] = new Stud(2016004,"jams",87);
        studs[4] = new Stud(2016005,"dandan",95);
    }
}

8.编写一个程序,统计学生成绩,其功能包括输入学生的姓名和成绩,按成绩从高到低排列打印输出,对前%70的学生定为合格(PASS),而后30%的学生定为不合格(FAIL)
思路:
    设计一个类student,包含学生的姓名和成绩等数据。

    设计一个类Compute,这个类中包含了多个学生的信息,方法有 sort()、disp(),,它们分别用于按成绩排序和输出数据。

/*8.编写一个程序,统计学生成绩,其功能包括输入学生的姓名和成绩,按成绩从高到低排列打印输出,
对前%70的学生定为合格(PASS),而后30%的学生定为不合格(FAIL)
        思路:
            设计一个类student,包含学生的姓名和成绩等数据。
            设计一个类Compute,这个类中包含了多个学生的信息,方法有 sort()、disp(),
        它们分别用于按成绩排序和输出数据。*/
package cn.edu.nefu.oop.exercise8;

public class Student {
    String name;
    double deg;

    public Student(String name, double deg) {
        this.name = name;
        this.deg = deg;
    }

}
package cn.edu.nefu.oop.exercise8;

public class Compute {
    public void sort(Student [] students){
        for(int i = 0;i < students.length;i++){
            for(int j = i;j < students.length;j++){
                if(students[i].deg < students[j].deg){
                    Student temp = students[i];
                    students[i] = students[j];
                    students[j] = temp;
                }
            }
        }
    }

    public void disp(Student[] students){
        sort(students);
        for(int i = 0;i < students.length;i++){
            if(i <= students.length*0.7-1){
                System.out.println("姓名:"+students[i].name+"  成绩:"+students[i].deg+"  PASS");
            }else {
                System.out.println("姓名:"+students[i].name+"  成绩:"+students[i].deg+"  FAIL");
            }
        }
    }
}
package cn.edu.nefu.oop.exercise8;

public class Test {
    public static void main(String[] args) {
        Student[] s = new Student[5];
        s[0] = new Student("李明",89);
        s[1] = new Student("林逸",78);
        s[2] = new Student("汪洋",36);
        s[3] = new Student("田凯",79);
        s[4] = new Student("孙玉",65);

        Compute compute = new Compute();
        compute.disp(s);
    }
}

9.设计一个词典类Dic,每个单词包括英文单词及对应的中文含义,并有一个英汉翻译成员函数,通过查词典的方式将一段英语翻译成对应的汉语。
思路:    字典项类DicItem包括EngLish(英语单词)、Chinese(对应中文含义)数据成员,字典类包括一个字典项类的列表,包含Add()(添加单词)和trans(英汉翻译)成员函数。

/*9.设计一个词典类Dic,每个单词包括英文单词及对应的中文含义,并有一个英汉翻译成员函数,
通过查词典的方式将一段英语翻译成对应的汉语。
        思路:    字典项类DicItem包括EngLish(英语单词)、Chinese(对应中文含义)数据成员,
        字典类包括一个字典项类的列表,包含Add()(添加单词)和trans(英汉翻译)成员函数。*/
package cn.edu.nefu.oop.exercise9;

public class DicItem {
    String English;
    String Chinese;

    public String getEnglish() {
        return English;
    }

    public void setEnglish(String english) {
        English = english;
    }

    public String getChinese() {
        return Chinese;
    }

    public void setChinese(String chinese) {
        Chinese = chinese;
    }
}
package cn.edu.nefu.oop.exercise9;

public class Dic {
    DicItem [] dic = new DicItem[100];//字典列表
    static int index = 0;
    //添加单词
    public void Add(String English,String Chinese){
        DicItem word = new DicItem();
        word.setEnglish(English);
        word.setChinese(Chinese);
        dic[index] = word;
        index++;
    }

    //英汉翻译
    public void trans(String word){
        for (int i=0;i
package cn.edu.nefu.oop.exercise9;

public class Test {
    public static void main(String[] args) {
        Dic dic = new Dic();
        dic.Add("my","我的");
        dic.Add("English","英语");
        dic.Add("computer","计算机");
        dic.trans("英语");
        dic.trans("my");
        dic.trans("computer");
    }
}

10.自已封装一个动态数组类,可以根据用户传递的数据,动态的对数组的长度进行扩展;

类名是:MyArray

方法有:

void add(int value);  //追加一个值

vold remove(int index);   //根据索引,删除一个值

void add(int position,int value); //在指定位置插入一个数值

void set(int position,int value); //修改某个位置的数值

int get(int index); //根据索引,获得元数的值

int size();  //获得动态数组中元素的个数;

//10.自已封装一个动态数组类,可以根据用户传递的数据,动态的对数组的长度进行扩展;
package cn.edu.nefu.oop.exercise10;

import java.util.Arrays;

public class MyArray {
    int [] array = new int[0];

    //    void add(int value);  //追加一个值
    public void add(int value){
        extend();
        array[array.length-1] = value;
    }

    //扩展数组
    private void extend(){
        int[] newArray = new int[array.length+1];
        for (int i = 0;i < array.length;i++){
            newArray[i] = array[i];
        }
        array = newArray;
    }

//    void remove(int index);   //根据索引,删除一个值
    public void remove(int index){
        for (int i = index;i < array.length-1;i++){
            array[i] = array[i+1];
        }
        int[] newArray = new int[array.length-1];
        for (int i = 0;i < newArray.length;i++){
            newArray[i] = array[i];
        }
        array = newArray;
    }

//    void add(int position,int value); //在指定位置插入一个数值
    public void add(int position,int value){
        extend();
        for (int i = array.length-1; i > position;i--){
            array[i] = array[i-1];
        }
        array[position] = value;
    }

//    void set(int position,int value); //修改某个位置的数值
    public void set(int position,int value){
        array[position] = value;
    }

//    int get(int index); //根据索引,获得元数的值
    public int get(int index){
        return array[index];
    }

//    int size();  //获得动态数组中元素的个数;
    public int size(){
        return array.length;
    }

    //打印数组
    public void print(){
        System.out.println(Arrays.toString(array));
    }
}
package cn.edu.nefu.oop.exercise10;

public class Test {
    public static void main(String[] args) {
        MyArray arr = new MyArray();
        arr.print();
        arr.add(5);
        arr.print();
        arr.add(90);
        arr.add(23);
        arr.add(45);
        arr.add(87);
        arr.print();
        arr.add(2,100);
        arr.print();
        arr.remove(1);
        arr.print();
        arr.set(3,600);
        arr.print();
        System.out.println(arr.get(3));
        System.out.println(arr.size());
    }


}

 

 

 

 

你可能感兴趣的:(Java习题——面向对象综合练习题)