Java 練習(創建類, 設計類Circle計算圓的面積, 對象數組)


按要求創建一個Person類的對象

要求:
(1)創建Person類的對象,設置該對象的name,age和sex屬性,調用study方法,輸出字符串"studying",調用showAge()方法顯示age值,調用addAge()方法給對象的age屬性值圳加2歲。
(2)創建第二個對象,執行上述操作,體會同一個類的不同對象之間的關系。
Person1.java

public class Person1 {
	String name;
	int age;
	/**
	 * sex:1 表明是男性 
	 * sex:0 表明是女性
	 */
	int sex;
	
	public void study() {
		System.out.println("studying");
	}
	
	public void showAge() {
		System.out.println("age:" + age);
	}
	
	public int addAge(int i) {
		age += i;
		return age;
	}
}

Person1Test.java

public class Person1Test {
	public static void main(String[] args) {
		Person1 p1 = new Person1();
		
		p1.name = "Tom";
		p1.age = 18;
		p1.sex = 1;
		
		p1.study();
		p1.showAge();
		
		int newAge = p1.addAge(2);
		System.out.println(p1.name + "的新年齡為: " + newAge );
		
		System.out.println(p1.age);
		
		//*********************************
		Person1 p2 = new Person1();
		p2.showAge();
		p2.addAge(10);
		p2.showAge();
		
		p1.showAge();
	}

}

運行結果:

設計類Circle計算圓的面積

public class CirecleTest {
	public static void main(String[] args) {
		Cirecle c1 = new Cirecle();
		
		c1.radius = 2.1;
		
		//方式一
//		double area = c1.findArea();
//		System.out.println(area);
		
		//方式二
		c1.findArea();
	}
	

}


class Cirecle{
	//屬性
	double radius;
	
	//求圓的面積
	//方式一:
//	public double findArea() {
//		double area = Math.PI * radius * radius ;
//		return area;
//	}
	
	//方式二:
	public void findArea() {
		double area = Math.PI * radius * radius;
		System.out.println("面積為: " + area);
	}
}

運行結果:

對象數組

定義類Student,包含三個屬性:學號number(int),年級state(int),成績score(int)。
創建 20個學生對象,學號為1到20,年級和成績都由隨機數確定。
問題—:打印出3年級(state值為3)的學生信息。
問題二:使用冒泡排序按學生成績排序,並遍歷所有學生信息

public class Student1Test {
	public static void main(String[] args) {
		//聲明 Student 類型的數組
				Student1[] stus = new Student1[20];
				
				for(int i = 0; i < stus.length; i++) {
					//給元素數組賦值
					stus[i] = new Student1();
					//給Student對象的屬性賦值
					stus[i].number = (i + 1);
					//年級 [1,6]
					stus[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
					//成績 [0,100]
					stus[i].score = (int)(Math.random() * (100 - 0  + 1));
				}
				
				Student1Test s1 = new Student1Test();
				s1.print(stus);
				
				System.out.println("====================================================");
				
				s1.searchState(stus, 3);
				
				System.out.println("====================================================");
				s1.sort(stus);
				s1.print(stus);
				
					
	}
	
	public void print(Student1[] stus) {
		for(int i = 0; i < stus.length; i++) {
			System.out.println(stus[i].info());
		}
	}
	
	public void searchState(Student1[] stus, int state) {
		for(int i = 0; i< stus.length; i++) {
			if(stus[i].state == state) {
				System.out.println(stus[i].info());
			}
		}
	}
	
	public void sort(Student1[] stus) {
		for(int i = 0; i < stus.length -1; i++) {
			for(int j = 0; j < stus.length - 1 - i; j++ ) {
				if(stus[j].score > stus[j + 1].score) {
					Student1 temp = stus[j];
					stus[j] = stus[j + 1];
					stus[j + 1] = temp;
				}
			}
		}
	}

}


class Student1{
	int number;  //學號
	int state;   //年級
	int score;   //成績
	
	//顯示學生信息的方法
	public String info() {
		return "學號: " + number + ", 年級: " + state + ", 成績: " + score;
	}
}

運行結果:

學號: 1, 年級: 4, 成績: 37
學號: 2, 年級: 5, 成績: 60
學號: 3, 年級: 5, 成績: 48
學號: 4, 年級: 6, 成績: 89
學號: 5, 年級: 1, 成績: 22
學號: 6, 年級: 3, 成績: 54
學號: 7, 年級: 2, 成績: 36
學號: 8, 年級: 5, 成績: 62
學號: 9, 年級: 4, 成績: 16
學號: 10, 年級: 5, 成績: 35
學號: 11, 年級: 6, 成績: 97
學號: 12, 年級: 1, 成績: 51
學號: 13, 年級: 2, 成績: 9
學號: 14, 年級: 5, 成績: 50
學號: 15, 年級: 3, 成績: 74
學號: 16, 年級: 3, 成績: 69
學號: 17, 年級: 4, 成績: 69
學號: 18, 年級: 6, 成績: 36
學號: 19, 年級: 6, 成績: 93
學號: 20, 年級: 1, 成績: 81
====================================================
學號: 6, 年級: 3, 成績: 54
學號: 15, 年級: 3, 成績: 74
學號: 16, 年級: 3, 成績: 69
====================================================
學號: 13, 年級: 2, 成績: 9
學號: 9, 年級: 4, 成績: 16
學號: 5, 年級: 1, 成績: 22
學號: 10, 年級: 5, 成績: 35
學號: 7, 年級: 2, 成績: 36
學號: 18, 年級: 6, 成績: 36
學號: 1, 年級: 4, 成績: 37
學號: 3, 年級: 5, 成績: 48
學號: 14, 年級: 5, 成績: 50
學號: 12, 年級: 1, 成績: 51
學號: 6, 年級: 3, 成績: 54
學號: 2, 年級: 5, 成績: 60
學號: 8, 年級: 5, 成績: 62
學號: 16, 年級: 3, 成績: 69
學號: 17, 年級: 4, 成績: 69
學號: 15, 年級: 3, 成績: 74
學號: 20, 年級: 1, 成績: 81
學號: 4, 年級: 6, 成績: 89
學號: 19, 年級: 6, 成績: 93
學號: 11, 年級: 6, 成績: 97


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM