第二次過程性考核


碼雲倉庫的地址:https://gitee.com/wslgx/projects

7-1 學生類-構造函數 (10 分)

定義一個有關學生的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]

 1 import java.util.Scanner;
 2 class Student{
 3   private String name;
 4   private String sex;
 5   private int age;
 6   public String getName(){
 7       return name;
 8   }
 9   public void setName(String name){
10       this.name=name;
11   }
12   public String getSex(){
13       return name;
14   }
15   public void setSex(String Sex){
16       this.sex=sex;
17   }
18   public int getAge(){
19       return age;
20   }
21   public 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   public void toString(String n, int a, String s) {   
30       this.name = n;
31       this.sex = s;
32       this.age = a;
33       System.out.println("Student [name='" + this.name + "', sex='" + this.sex + "', age=" + this.age + "]");
34   }
35 }
36 public class Main{
37     public static void main (String[] args){
38         Scanner reader = new Scanner(System.in);
39         String n;
40         String s;
41         int a;
42         n=reader.next();
43         a=reader.nextInt();
44         s=reader.next();
45         Student ww = new Student(n,s,a);
46         ww.toString(n,a,s);
47         reader.close();
48     }
49 }
View Code

程序設計思路:定義學生類,對name,sex,age賦值,定義構造方法,生成setter/gette方法,覆蓋實現toString函數以便打印輸入該類對象時輸出類名 [name=, sex=, age=]。main方法中調用有參構造函數新建對象。

使用到的知識點:創建類和對象,構造方法,定義方法,調用有參函數等。

 

7-2 定義類 (5 分)

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

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                int a,b,c,d,e;
                a = in.nextInt();
                b = in.nextInt();
                c = in.nextInt();
                d = in.nextInt();
                e = in.nextInt();
                RR rr = new RR();
                double dd = rr.fun(a,b,c,d,e);
                System.out.printf("%.2f",dd);
    }
}
class RR{



}

輸入格式:

在一行中給出5個不超過1000的正整數。

輸出格式:

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

輸入樣例:

1 2 3 4 5

輸出樣例:

3
 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                 return (a+b+c+d+e)/5;
19     }
20 }
View Code

程序設計思路:定義RR類然后在return傳出返回值的時候編寫平均數式子輸出5個數的平均值。

使用到的知識點:參數傳值,類的定義。

 

7-3 橫平豎直 (5 分)

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

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int height, width;
        char status;
        height = in.nextInt();
        width = in.nextInt();
        Board board = new Board(height, width);
        status = board.getStatus();
        System.out.print(status);
    }
}
class Board{
   int height, width;
   public Board(int height, int width){
       this.height = height;
       this.width = width;
   }
   public char getStatus(){
       if(height<=width){
          return status(1);
       }else{
         return status(1.0);
       }
   }
   public char status(double rate){

 

} public char status(int rate){

} }

 

輸入格式:

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

輸出格式:

在一行中輸出一個字符A或者B。

輸入樣例:

50 50

輸出樣例:

A
 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        return 'B';
29    }
30    public char status(int rate){
31        return 'A';
32    }
33 }
View Code

程序設計思路:根據題意可知:如果高度大於寬度,則輸出A,否則輸出B。

使用到的知識點:方法重載 ,參數傳值。

 

7-4 程序改錯題2 (5 分)

程序改錯題。以下代碼存在錯誤,請修改后提交。

public class Main {
	public static void main(String[] args) {
		Animal animal = new Dog();
		animal.shout();
		animal.run();
	}
}

class Animal {
	void shout() {
		System.out.println("animal shout!");
	}
}

class Dog extends Animal {
	void shout() {
		super.shout();
		System.out.println("wangwang……");
	}

	void run() {
		System.out.println("Dog is running");
	}
}

輸入格式:

輸出格式:

輸入樣例:

輸出樣例:

animal shout!
wangwang……
Dog is running
 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 }
View Code

 程序設計思路:類animal中有void shout但沒有void run,補充即可。

使用到的知識點:子類的繼承性,對象的上轉型對象。

學習內容 代碼 博客
類與對象,子類和繼承 339 600

 


免責聲明!

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



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