第二次過程性考核


碼雲倉庫:https://gitee.com/yyh1234/16012013_in_yi_hong__kaoheer/tree/master

7-1 學生類-構造函數

(1)題目:

  定義一個有關學生的Student類,內含類成員變量: String name、String sex、int age,所有的變量必須為私有(private)。

(2)代碼:

 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 }

 (3)程序設計思路:

          先定義student類,之后用private將3個變量私有化定義,而且用this.xxx=xxx建立構造函數,最后定義主類Main,在里面對外部私有變量進行調用,最后輸入輸出

 (4)知識點:

          綜合運用類與對象、子類與繼承

 (5)運行結果:

          

7-2 定義類

(1)題目:

         請補充以下代碼,完成輸出要求。(注意:需要提交完整代碼)

(2)代碼:

       

 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 }

 (3)程序設計思路:

            直接在RR類中添加,先用double定義a,b,c,d,e,之后定義一個總的x,將abcde的平均值賦值給x,並用return返回並輸出

 (4)知識點:

            參數傳值

 (5)運行結果:

          

7-3 橫平豎直

(1)題目:

          程序填空題。根據題目要求完善下面的代碼。請提交完整代碼。 一個木塊如果高度比寬度大,我們說它是豎着放的,否則我們說它是平放的。 讀入一個木塊的高度和寬度。如果它是平放的,則輸出A,       否則輸出B。

(2)代碼:

 

 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 }

 (3)程序設計思路:

         分別將字符B和A賦值給char型的x,並return傳回

 (4)知識點:

         方法重載,參數傳值

 (5)運行結果:

      

7-4 程序改錯題2

(1)題目:程序改錯題。以下代碼存在錯誤,請修改后提交。

(2)代碼:

 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 }

 (3)程序設計思路:

         類animal中有void shout但沒有void run,補充即可

 (4)知識點:

       子類的繼承

(5)運行結果:

內容 代碼行數 博客
類與對象 ,子類與繼承 125 320


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM