1. 程式人生 > 其它 >this與static關鍵字-計算期末總成績

this與static關鍵字-計算期末總成績

技術標籤:Java基礎入門java

任務描述:
1、學生期末總成績的計算公式為:平時成績(40%)+上機考試(30%)+期末筆試(30%),已知2名同學的成績如表中所示(具體的資料自定),求出他們的期末總成績並輸出。

在這裡插入圖片描述
定義一個學生類,包含私有的成員變數有:姓名、平時成績、上機考試、期末筆試、總成績。
定義一個帶參的構造方法,用於對學生的姓名、平時成績、上機考試、期末筆試賦初值;
定義set和get方法,用於對學生的總成績進行設值和取值;
定義printScore方法,用於計算總成績並按“姓名:總成績”的格式輸出學生的姓名和總成績。
定義一個測試類,在測試類中建立2個學生物件,並分別呼叫printScore方法輸出2名學生的總成績。

執行結果示例:
在這裡插入圖片描述

public class Student {
    private String name;
    private double assignments,lab,written,totalScore;
    public Student(String name,double assignments,double lab,double written,double totalScore) {
        this.name=name;
        this.assignments=assignments;
        this.lab=lab;
        this
.written=written; this.totalScore=totalScore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getAssignments() { return assignments; } public void setAssignments
(int assignments ) { this.assignments = assignments; } public double getLab() { return lab; } public void setLab(int lab ) { this.lab = lab; } public double getWritten() { return written; } public void setWritten(int written ) { this.written = written; } public double getTotalScore() { return totalScore; } public void setTotalScore(double totalScore) { this.totalScore = totalScore; } }
public class StudentTest {
    public static double getTotalScore(double assignments,double lab,double written) {
        double totalScore=assignments*0.4 + lab*0.3+written*0.3;
        return totalScore;
    }
    public static void main(String[] args) {
        System.out.println("姓名\t總成績");
        Student student1=new Student("劉強",85.0,83.0,80.0,getTotalScore(85.0,83.0,80.0));
        Student student2=new Student("楊浩",69.0,75.0,71.0,getTotalScore(69.0,75.0,71.0));
        String name1 = student1.getName();
        double totalScore1 = student1.getTotalScore();
        System.out.println(name1+"\t"+totalScore1);
        String name2 = student2.getName();
        double totalScore2 = student2.getTotalScore();
        System.out.println(name2+"\t"+totalScore2);
        }
    }