7-5 jmu-Java-03面向對象基礎-01-構造函數與toString (3 分)
定義一個有關人的Person
類,內含屬性: String name
、int age
、boolean gender
、int id
,所有的變量必須為私有(private
)。 注意:屬性順序請嚴格按照上述順序依次出現。i
運用知識點:
數組聲明 Person[] person
對象數組初始化 personS=new Person[n]
逆序輸出
for (i=n-1;i>=0;i--){
System.out.println(personS[i].toString());
}
無參構造
Person personNone=new Person();
還有基本的類和方法。
代碼如下:
import java.util.Scanner; class Person{ private String name; private int age; private boolean gender; private int id; Person(){ System.out.println("This is constructor"); System.out.printf("%s,%d,%b,%d\n",name,age,gender,id); } public Person(String a,int b,boolean c){ name=a; age=b; gender=c; } public String toString(){ String className=this.getClass().getName(); return (className+" [name="+name+", age="+age+", gender="+gender+", id="+id+"]"); } } public class Main{ public static void main(String[] args){ Scanner read=new Scanner(System.in); int n=read.nextInt(); int i; Person[] personS; personS=new Person[n]; read.nextLine(); for (i=0;i<n;i++){ String readLine=read.nextLine(); String data[]=readLine.split(" "); personS[i]=new Person(data[0],Integer.valueOf(data[1]),Boolean.parseBoolean(data[2])); } for (i=n-1;i>=0;i--){ System.out.println(personS[i].toString()); } Person person1=new Person(); System.out.println(person1.toString()); }
7-6 集體評分
程序填空題。請補充以下代碼,完成題目要求。(注意:需要提交完整代碼) 有一個團隊由5個人組成。他們每個人給指導老師一個分數,去掉最高分,去掉最低分,剩下的3個分數的平均分就是該團隊對指導老師的評分
運用知識點:
簡單的類和方法。
代碼如下:
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
運用知識點:
簡單類和方法
構造函數
主函數調用
son里調用parent里有參構造函數 super(true)
代碼如下:
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 { Son(){ super(true); System.out.println("Son's Constructor without parameter"); } public void method() { System.out.println("Son's method()"); super.method(); } }
運行結果:
7-8 求兩點之間距離
定義一個Point類,有兩個數據成員:x和y, 分別代表x坐標和y坐標,並有若干成員函數。 定義一個函數Distance(), 用於求兩點之間的距離。
運用的知識點:
引入java.util.*和java.main。*
建立類並定義屬性,計算量點間距離
代碼如下:
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)))); } }
運行結果:
碼雲鏈接::https://gitee.com/wxl19981225/16012101
學習內容 | 代碼行數 | 博客字數 |
構造方法與對象 | 45 | |
子類與繼承,重載 | 35 | |
第二次過程性考核 | 130 | 800 |