碼雲:https://gitee.com/iseekun99/the_second_process_assessment/tree/master
7-5 jmu-Java-03面向對象基礎-01-構造函數與toString (25 分)
定義一個有關人的Person
類,內含屬性:String name
、int age
、boolean gender
、int id
,所有的變量必須為私有(private
)。注意:屬性順序請嚴格按照上述順序依次出現。
1.編寫無參構造函數:
- 打印"This is constructor"。
- 將name,age,gender,id按照
name,age,gender,id
格式輸出
2.編寫有參構造函數
依次對name,age,gender
賦值。
3.覆蓋toString函數:
按照格式:類名 [name=, age=, gender=, id=]
輸出。建議使用Eclipse自動生成.
4.對每個屬性生成setter/getter方法
5.main方法中
- 首先從屏幕讀取n,代表要創建的對象個數。
- 然后輸入n行name age gender , 調用上面2編寫的有參構造函數新建對象。
- 然后將剛才創建的所有對象
逆序
輸出。 - 接下來使用無參構造函數新建一個Person對象,並直接打印該對象。
輸入樣例:
3
a 11 false
b 12 true
c 10 false
輸出樣例:
Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
程序設計思路:創建一個有關人Person的類,實例化類構造函數,進行調用
知識點:創建類,有參函數與無參函數的構造,調用
1 import java.util.Scanner; 2 class Person{ 3 private String name; 4 private int age; 5 private boolean gender; 6 private int id; 7 public Person(){ 8 this.name = "c"; 9 this.age = 10; 10 this.gender = "false"; 11 this.id = 0; 12 } 13 public void toString(String n,int a,boolean g,int i){ 14 this.name = n; 15 this.age = a; 16 this.gender = g; 17 this.id = i; 18 System.out.println("Person[name="+this.name+",age="+this.age+",gender="+this.gender+",id="+this.id+"]"); 19 } 20 }
以上內容為考試時自己編寫,在與同學總結查看之后得到答案如下:
1 import java.util.Scanner; 2 3 class Person{ 4 private String name = null; 5 private int age = 0; 6 private boolean gender = false; 7 private int id = 0; 8 9 public Person() { 10 System.out.println("This is constructor"); 11 System.out.println(name+","+age+","+gender+","+id); 12 System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]"); 13 } 14 15 public Person(String n, int a, boolean g) { 16 this.name = n; 17 this.age = a; 18 this.gender = g; 19 } 20 21 public String toString() { 22 System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]"); 23 return name; 24 } 25 } 26 27 public class Main { 28 29 public static void main(String[] args) { 30 Scanner reader = new Scanner(System.in); 31 int number = reader.nextInt(); 32 Person[] per = new Person[number]; //初始化對象數組 33 for(int i=0; i<per.length; i++) { //通過循環從鍵盤輸入 34 String name = reader.next(); 35 int age = reader.nextInt(); 36 boolean genter = reader.nextBoolean(); 37 per[i] = new Person(name,age,genter); 38 } 39 for(int x=per.length-1; x>=0;x--){ //通過循環從后往前輸出 40 per[x].toString(); 41 } 42 43 per.toString(); 44 Person s = new Person(); 45 } 46 47 }
運行結果:
7-6 集體評分 (10 分)
程序填空題。請補充以下代碼,完成題目要求。(注意:需要提交完整代碼) 有一個團隊由5個人組成。他們每個人給指導老師一個分數,去掉最高分,去掉最低分,剩下的3個分數的平均分就是該團隊對指導老師的評分。
輸入格式:
在一行中給出5個不超過10的正整數(從小到大排列)。
輸出格式:
輸出集體評分,保留小數點后兩位。
輸入樣例:
1 2 4 6 9
輸出樣例:
4.00
知識點:調用父類有參構造函數
22 class RT extends RR{ 23 RT(int[] grade){ 24 super(grade); 25 } 26 public double mark(){ 27 Arrays.sort(grade); //將數組 升序排序 28 return (double)(grade[1]+grade[2]+grade[3])/3; //求平均值 29 } 30 }
運行結果:
7-7 程序填空題3 (5 分)
參照輸出樣例補全以下程序,使程序輸出結果與輸出樣例一致。
public class Main { public static void main(String[] args) { Son son = new Son(); son.method(); } } class Parent { Parent() { System.out.println("Parent's Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent's Constructor with a boolean parameter"); } public void method() { System.out.println("Parent's method()"); } } class Son extends Parent { //補全本類定義 }
輸出樣例:
Parent's Constructor with a boolean parameter
Son's Constructor without parameter
Son's method()
Parent's method()
程序思路:對父類進行調用,注意順序,在考試時沒有考慮到super()的用法,須寫在子類構造函數的第一句
知識點:參數傳值
class Son extends Parent { void Parent(){ System.out.println("Son's Constructor without parameter"); } public void method() { System.out.println("Son's method()"); }
class Son extends Parent { Son() { super(true); //調用父類有參構造 System.out.println("Son's Constructor without parameter"); } public void method() { System.out.println("Son's method()"); super.method(); }
運行結果:
7-8 求兩點之間距離 (10 分)
定義一個Point類,有兩個數據成員:x和y, 分別代表x坐標和y坐標,並有若干成員函數。 定義一個函數Distance(), 用於求兩點之間的距離。
輸入格式:
輸入有兩行: 第一行是第一個點的x坐標和y坐標; 第二行是第二個點的x坐標和y坐標。
輸出格式:
輸出兩個點之間的距離,保留兩位小數。
輸入樣例:
0 9 3 -4
輸出樣例:
13.34
import java.util.*; import java.math.*; public class Main{ public static void main (String[] args){ Scanner input = new Scanner(System.in); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); System.out.println(String.format("%.2f",Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))); } }
運行結果:
總結:在近幾周的學習中,課上態度沒有端正,導致許多方法定義構造上的模糊,在課下也沒有充分利用時間復習,階段性的考核及時提醒了自己,
在之后的學習中一定要改正,多多思考,注意積累,期待下一次考核時自己可以進步。
學習內容 | 代碼行數 | 博客字數 |
構造方法 | 35 | 100 |
子類與父類 | 30 | 90 |
第二次作業 | 50 | 500 |