碼雲倉庫:https://gitee.com/zhoushuai-9/160121111_weeks_handsome_kao/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]
import java.util.Scanner;
class Person{ //定義Person類
private String name = null;
private int age = 0;
private boolean gender = false;
private int id = 0;
public Person() {
System.out.println("This is constructor");
System.out.println(name+","+age+","+gender+","+id);
System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]");
}
public Person(String n, int a, boolean g) { //編寫構造參數,並給予賦值
this.name = n;
this.age = a;
this.gender = g;
}
public String toString() {
System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]");
return name;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner reader = new Scanner(System.in);
int number = reader.nextInt();
Person[] per = new Person[number]; //初始化
for(int i=0; i<per.length; i++) { //循環輸入
String name = reader.next();
int age = reader.nextInt();
boolean genter = reader.nextBoolean();
per[i] = new Person(name,age,genter);
}
for(int x=per.length-1; x>=0;x--){
per[x].toString();
}
per.toString();
@SuppressWarnings("unused")
Person s = new Person();
}
}
7-6 集體評分 (10 分)
程序填空題。請補充以下代碼,完成題目要求。(注意:需要提交完整代碼) 有一個團隊由5個人組成。他們每個人給指導老師一個分數,去掉最高分,去掉最低分,剩下的3個分數的平均分就是該團隊對指導老師的評分。
輸入格式:
在一行中給出5個不超過10的正整數(從小到大排列)。
輸出格式:
輸出集體評分,保留小數點后兩位。
輸入樣例:
1 2 4 6 9
輸出樣例:
4.00
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner w = new Scanner(System.in);
int a, b, c, d, e;
a = w.nextInt();
b = w.nextInt();
c = w.nextInt();
d = w.nextInt();
e = w.nextInt();
RR rr = new RR();
double dd = rr.fun(a, b, c, d, e);
System.out.printf("%.2f", dd);
w.close();
}
}
class RR {
double z;
public double fun(int a, int b, int c, int d, int e) {
z = (a + b + c + d + e) / 5;
return z;
}
}
7-7 程序填空題3 (5 分)
參照輸出樣例補全以下程序,使程序輸出結果與輸出樣例一致。
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 {
Son(){
super(true);//調用父類
System.out.println("Son's Constructor without parameter");
}
public void method() {
System.out.println("Son's method()");
super.method();//調用父類method方法
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Son son = new Son();
son.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))) );
}
}

