第二次过程性考核

 码云考试:https://gitee.com/pxtddd/the_second_process_assessment

 码云练习:https://gitee.com/pxtddd/16012011_pang_xiaotian/tree/master

7-1 学生类-构造函数

 

(1)题目:

 

  定义一个有关学生的Student类,内含类成员变量: String name、String sex、int age,所有的变量必须为私有(private)。

 

(2)程序设计思路:

 

          先定义student类,之后用private将3个变量私有化定义,而且用this.xxx=xxx建立构造函数,最后定义主类Main,在里面对外部私有变量进行调用,最后输入输出

 

 (3)知识点:

 

          综合运用类与对象、子类与继承

 

(4)代码:

 

复制代码
 1 import java.util.Scanner;
 2 class Student{
 3   String name;
 4   String sex;
 5   int age;
 6   private String getName(){
 7       return name;
 8   }
 9   private void setName(String name){
10       this.name=name;
11   }
12   private String getSex(){
13       return name;
14   }
15   private void setSex(String Sex){
16       this.sex=sex;
17   }
18   private int getAge(){
19       return age;
20   }
21   private void setAge(int age){
22       this.age=age;
23   }
24   Student (String name,String sex,int age){
25       this.name=name;
26       this.sex=sex;
27       this.age=age;
28   }
29 }
30 public class Main{
31     public static void main (String[] args){
32         Scanner in = new Scanner(System.in);
33         String n;
34         String s;
35         int a;
36         n=in.next();
37         a=in.nextInt();
38         s=in.next();
39         Student student=new Student(n,s,a);
40         System.out.println("Student [name='"+student.name+"', sex='"+student.sex+"', age="+student.age+"]");
41     }
42 }
复制代码

 

 

 

 (5)运行结果:

 

          

 

7-2 定义类

 

(1)题目:

 

         请补充以下代码,完成输出要求。(注意:需要提交完整代码)

(2)程序设计思路:

 

            直接在RR类中添加,先用double定义a,b,c,d,e,之后定义一个总的x,将abcde的平均值赋值给x,并用return返回并输出

 

(3)知识点:

 

            参数传值

 

(4)代码:

 

       

 

复制代码
 1 import java.util.Scanner;
 2 public class Main {
 3     public static void main(String[] args) {
 4                 Scanner in = new Scanner(System.in);
 5                 int a,b,c,d,e;
 6                 a = in.nextInt();
 7                 b = in.nextInt();
 8                 c = in.nextInt();
 9                 d = in.nextInt();
10                 e = in.nextInt();
11                 RR rr = new RR();
12                 double dd = rr.fun(a,b,c,d,e);
13                 System.out.printf("%.2f",dd);
14     }
15 }
16 class RR{
17     public double fun (double a ,double b ,double c,double d,double e){
18         double x = (a+b+c+d+e)/5;
19         return x;
20     }
21 }
复制代码

 

 

 

 (5)运行结果:

 

          

 

7-3 横平竖直

 

(1)题目:

 

          程序填空题。根据题目要求完善下面的代码。请提交完整代码。 一个木块如果高度比宽度大,我们说它是竖着放的,否则我们说它是平放的。 读入一个木块的高度和宽度。如果它是平放的,则输出A,       否则输出B。

(2)程序设计思路:

 

         分别将字符B和A赋值给char型的x,并return传回

 

 (3)知识点:

 

         方法重载,参数传值

 

(4)代码:

 

 

 

复制代码
 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String[] args){
 4         Scanner in = new Scanner(System.in);
 5         int height, width;
 6         char status;
 7         height = in.nextInt();
 8         width = in.nextInt();
 9         Board board = new Board(height, width);
10         status = board.getStatus();
11         System.out.print(status);
12     }
13 }
14 class Board{
15    int height, width;
16    public Board(int height, int width){
17        this.height = height;
18        this.width = width;
19    }
20    public char getStatus(){
21        if(height<=width){
22           return status(1);
23        }else{
24          return status(1.0);
25        }
26    }
27    public char status(double rate){
28         char x = 'B';
29         return x;
30    }
31    public char status(int rate){
32         char x = 'A';
33         return x;
34    }
35 }
复制代码

 

(5)运行结果:

 

      

 

7-4 程序改错题2

 

(1)题目:程序改错题。以下代码存在错误,请修改后提交。

 

(2)程序设计思路:

 

         类animal中有void shout但没有void run,补充即可

 

(3)知识点:

 

       子类的继承

 

(4)代码:

 

复制代码
 1 public class Main {
 2     public static void main(String[] args) {
 3         Animal animal = new Dog();
 4         animal.shout();
 5         animal.run();
 6     }
 7 }
 8 
 9 class Animal {
10     void shout() {
11         System.out.println("animal shout!");
12     }
13     void run(){
14       
15     }
16 }
17 
18 class Dog extends Animal {
19     void shout() {
20         super.shout();
21         System.out.println("wangwang……");
22     }
23 
24     void run() {
25         System.out.println("Dog is running");
26     }
27 }
复制代码

 

 

 

(5)运行结果:

posted @ 2018-10-13 18:13  pxttttt  阅读(637)  评论(1编辑  收藏  举报