第二次过程性考核

码云仓库:https://gitee.com/zhoushuai-9/160121111_weeks_handsome_kao/tree/master
7-5 jmu-Java-03面向对象基础-01-构造函数与toString (25 分)

定义一个有关人的Person类,内含属性:
String nameint ageboolean genderint 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))) ); 
    
  } 
  
}
复制代码
posted @ 2018-10-14 18:26  葫芦娃的老九  阅读(1124)  评论(2编辑  收藏  举报