第二次過程性考核——面向對象程序設計


第二次過程性考核

碼雲倉庫鏈接:https://gitee.com/xywymxs/Myself.JAVA/tree/master/

更多代碼片段詳情請見碼雲:https://gitee.com/xywymxs/codes

7-1 學生類-構造函數

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

1.編寫有參構造函數:能對name,sex,age賦值。

2.覆蓋toString函數: 按照格式:類名 [name=, sex=, age=]輸出。使用idea自動生成,然后在修改成該輸出格式。

3.對每個屬性生成setter/getter方法。

4.main方法中:輸入1行name age sex , 調用上面的有參構造函數新建對象。

輸入樣例:

tom 15 male

輸出樣例:

Student [name='tom', sex='male', age=15]

程序設計思路:先引入java的輸入函數,再定義Student類,之后定義Student的構造方法,定義toString方法,並按格式輸出“類名 [name=, sex=, age=]”,最后在Main函數中調用子類。

 涉及知識點:綜合運用類與對象、子類與繼承

代碼:

 1 import java.util.Scanner;
 2 class Student{
 3     private String name;
 4     private String sex;
 5     private int age;
 6     public Student(){
 7         this.name = "aaa";
 8         this.sex = "male";
 9         this.age = 111;
10     }
11     public void toString(String n, int a, String s){
12         this.name = n;
13         this.sex = s;
14         this.age = a;
15         System.out.println("Student [name='"+this.name+"', sex='"+this.sex+"', age="+this.age+"]");
16     }
17 }
18 public class Main{
19     public static void main(String[] args){
20         Scanner reader = new Scanner(System.in);
21         String n = reader.next();
22         int a = reader.nextInt();
23         String s = reader.next();
24         Student ww = new Student();
25         ww.toString(n,a,s);
26     }
27 }

運行結果:

7-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 
18 
19 
20 }

輸入格式:在一行中給出5個不超過1000的正整數。

輸出格式:輸出5個整數的平均值,保留小數點后兩位。

輸入樣例:

1 2 3 4 5

輸出樣例:

3.00

程序設計思路:由題可知,只需補全RR類,需要先定義RR類,之后只需在return傳出返回值的時候編寫平均數式子,這樣輸出的內容即為5個數的平均值。

涉及知識點:參數傳值

代碼:

 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(int a, int b, int c, int d, int e)
18     {
19         return (a+b+c+d+e)/5;
20     }
21 }

運行結果:

7-3 橫平豎直

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

 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 
29    }
30    public char status(int rate){
31 
32 
33    }
34 }

輸入格式:輸入在一行中給出2個絕對值不超過1000的正整數A和B。

輸出格式:在一行中輸出一個字符A或者B

輸入樣例:

50 50

輸出樣例:

A

程序設計思路:定義了重載方法,可知方法名相同,但參數列表中對應的某個參數的類型不同,通過比較,將返回值設為A和B,因為方法為char類型,所以將返回值用單引號引起。

涉及知識點:方法重載,參數傳值

代碼:

 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    private static final char A = 0;
16    private static final char B = 0;
17    int height, width;
18    public Board(int height, int width){
19        this.height = height;
20        this.width = width;
21    }
22    public char getStatus(){
23        if(height<=width){
24           return status(1);
25        }else{
26          return status(1.0);
27        }
28    }
29    public char status(double rate){
30     return 'B';
31 
32 
33    }
34    public char status(int rate){
35     return 'A';
36 
37    }
38 }

運行結果:

7-4 程序改錯題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 }
14 
15 class Dog extends Animal {
16     void shout() {
17         super.shout();
18         System.out.println("wangwang……");
19     }
20 
21     void run() {
22         System.out.println("Dog is running");
23     }
24 }

輸入樣例:

輸出樣例:

animal shout!

wangwang……

Dog is running

程序設計思路:Animal是Dog的上轉型對象,即不能操作子類新增的成員變量,也不能調用子類新增的方法;所以需要將對象的上轉型對象再強制轉換到一個子類對象,這時,該子類對象又具備了子類所有的屬性和功能。

涉及知識點:自類的繼承,對象的上轉型對象

代碼:

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

運行結果:

學習內容 代碼(行) 博客(字)
類與對象、子類與繼承 300 1200

 


免責聲明!

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



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