2018/12/11

2018/12/11

1.設計一個類Student,該類包括姓名、學號和成績。設計一個方法,按照成績從高到低的順序輸出姓名、學號和成績信息。

public class Student {
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getID() {
		return ID;
	}
	public void setID(int nomber) {
		this.ID = ID;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	private String name;
	private int ID;
	private int score;
	
	Student(){
		
	}
	public Student(String name, int ID, int score) {
		this.name = name;
		this.ID = ID;
		this.score = score;
	}
	void show(){
		System.out.println("id:"+ID+"姓名:"+name+"成績:"+score);
	}
	public static void sort(Student [] stu){
		Student s;
		for(int i =0;i<stu.length-1;i++){
			for(int j =0;j<stu.length-i-1;i++){
				if(stu[i].getScore()<stu[i+1].getScore()){
					s=stu[i] ;
					stu[i]=stu[i+1];
					stu[i+1] = s;
				}
			}
		}
	}
}

主函數

	Student s = new Student();
		Student [] stu = new Student[3];
		stu[0] = new Student("zhang",1,73);
		stu[1] = new Student("li",2,95);
		stu[2] = new Student("zhang",3,84);
		stu[2].sort(stu);
		for(Student stus:stu){
			stus.show();
		}

2定義一個汽車類Vehicle,要求如下:[選做題]
2.1屬性包括:汽車品牌brand(String類型)、顏色color(String類型)和速度speed(double類型),並且所有屬性爲私有。
2.2至少提供一個有參的構造方法(要求品牌和顏色可以初始化爲任意值,但速度的初始值必須爲0)。
2.3爲私有屬性提供訪問器方法。注意:汽車品牌一旦初始化之後不能修改。
2.4定義一個一般方法run(),用打印語句描述汽車奔跑的功能
2.5定義測試類VehicleTest,在其main方法中創建一個品牌爲“benz”、顏色爲“black”的汽車。

	private String brand;
	private String color;
	private double speed;
	public String getBrand() {
		return brand;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public double getSpeed() {
		return speed;
	}
	public void setSpeed(double speed) {
		this.speed = speed;
	}
	public Vehicle(String brand, String color) {
		this.brand = brand;
		this.color = color;
	}
	void run(double speed){
		this.speed = speed;
		System.out.println(color+"色的"+brand+"速度是"+speed);
	}

主函數

Scanner s = new Scanner(System.in);
		Vehicle v = new Vehicle("黑","benz");
		v.run(100);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章