第二次过程性考核
2018-10-14 15:26 wxl! 阅读(1195) 评论(2) 收藏 举报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 |
浙公网安备 33010602011771号