Use Student Class


Submit solution

Points: 50 (partial)
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem types
Allowed languages
Java 19, Java 8

題目說明

以下為Student類別的程式碼

class Student {

    // Field
    private String name = "Unknown";    // 名字
    private String id = "12156000";     // 學號
    private int totalCredits = 0;       // 累計學分數

    // Constructor
    public Student() {
    }
    public Student(String name, String id) {
        this.name = name;
        this.id = id;
    }

    // Method
    public void addCredits(int credits) {
        this.totalCredits += credits;
    }
    public void printId() {
        System.out.print(name + "'s student ID is " + id);
    }
    public int getCredits() {
        return totalCredits;
    }

}

使用上面定義的類別程式碼,在主程式區段寫出【執行學生資料建置】的程式碼

小提示
import java.util.Scanner;  

// 貼上Student類別的程式碼  

// 主程式區段  
public class MyProgram {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // 宣告name、id

        // 請完成此區段程式碼
        // 必須使用Student類別
        // Student newStudent = new Student(name, id);

        // 累計學分,使用addCredits()

        // 輸出,使用printId()、getCredits()

    }

}

輸入

學生名字 學生學號
已修習學分數(可有多項,當遇到0結束計算)

輸出

{name}'s student ID is {id}, total credits: {totalCredits}

測試資料1 輸入

zz 09156146
2 2 3 1 3 2 2 0 3 1

測試資料1 輸出

zz's student ID is 09156146, total credits: 15
累計學分:2+2+3+1+3+2+2=15

Comments


  • 0
    scu09156146  commented on April 12, 2024, 5:34 p.m.

    題解

    import java.util.Scanner;
    
    // 定義Student類別
    class Student {
        // Field
        private String name = "Unknown"; // 名字
        private String id = "12156000";  // 學號
        private int totalCredits = 0;    // 累計學分數
    
        // Constructor
        public Student() {
        }
    
        public Student(String name, String id) {
            this.name = name;
            this.id = id;
        }
    
        // Method
        public void addCredits(int credits) {
            this.totalCredits += credits;
        }
    
        public void printId() {
            System.out.print(name + "'s student ID is " + id);
        }
    
        public int getCredits() {
            return totalCredits;
        }
    }
    
    // 主程式區段
    public class useStudentClass {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            String name = input.next();
            String id = input.next();
    
            Student newStudent = new Student(name, id); // 建立Student物件
    
            int credit = input.nextInt();
            while (credit != 0) {
                newStudent.addCredits(credit);          // 使用Student類別裡的Method,執行學分累加
                credit = input.nextInt();
            }
            input.close();
    
            newStudent.printId();                       // 使用Student類別裡的Method,印出學生資訊(名字、學號)
            System.out.print(", total credits: " + newStudent.getCredits());    // 使用Student類別裡的Method,取得累計學分
        }
    
    }