簡體   English   中英

Java 為什么我需要這種方法以及如何使用它

[英]Java why do I need this method and how do I use it

這是我的Java類的一個作業,問題如下:

修改本章介紹的學生類如下。 每個學生對象還應包含三個測試的分數。 提供基於參數值設置所有實例值的構造函數。 重載構造函數,使得每個測試點最初都被假定為零。 提供一個名為 setTestScore 的方法,它接受兩個參數:測試編號(1 到 3)和分數。 還提供了一個名為 getTestScore 的方法,它接受測試編號並返回適當的分數。 提供一個名為 average 的方法,用於計算並返回該學生的平均考試成績。 修改 toString 方法,使測試分數和平均值包含在學生的描述中。 修改驅動程序類的 main 方法以使用新的 Student 方法。

所以我讓程序運行,我得到了正確的輸出。 我的問題是為什么我需要 getTestScore 方法。 我沒有使用它來獲得正確的輸出。 我的代碼目前的編寫方式是否存在缺陷,還是我只是“需要”它來滿足分配要求?

這是我的代碼:
學生體.java

public class StudentBody 
{
    //-------------------------------------------------------------------
    //  Creates some Address and Student objects and prints them.
    //-------------------------------------------------------------------
    public static void main(String[] args)
    {
        Address school = new Address("800 Lancaster Ave.", "Villanova", "PA", 19085);

        Address jHome = new Address("21 Jump Street", "Blacksburg", "VA", 24551);

        Student john = new Student("John", "Smith", jHome, school);

        Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132);

        Student marsha = new Student("Marsha", "Jones", mHome, school);

        john.setTestScore(1, 93);
        john.setTestScore(2, 86);
        john.setTestScore(3, 77);
        john.average();

        marsha.setTestScore(1, 82);
        marsha.setTestScore(2, 91);
        marsha.setTestScore(3, 97);
        marsha.average();

        System.out.println(john);
        System.out.println();
        System.out.println(marsha);
    }

}

學生.java

    public class Student 
    {
        private String firstName, lastName;
        private Address homeAddress, schoolAddress;
        private int test1, test2, test3;
        private double averageScore;

        //-------------------------------------------------------------------
        // Constructor: Sets up this student with the specified values.
        //-------------------------------------------------------------------
        public Student(String first, String last, Address home, Address school)
        {
            firstName = first;
            lastName = last;
            homeAddress = home;
            schoolAddress = school;
            test1 = 0;
            test2 = 0;
            test3 = 0;
        }

        //-------------------------------------------------------------------
        // Sets the test scores
        //-------------------------------------------------------------------
        public void setTestScore(int testNumber, int testScore)
        {
            switch(testNumber)
            {
                case 1:
                    test1 = testScore;
                    break;

                case 2:
                    test2 = testScore;
                    break;

                case 3:
                    test3 = testScore;
                    break;

                default:
                    return;

            }
        }

        //-------------------------------------------------------------------
        // Gets the test scores
        // Not sure why I should call this method
        //-------------------------------------------------------------------
        public int getTestScore(int testNumber)
        {
            switch(testNumber)
            {
                case 1:
                    return test1;

                case 2:
                    return test2;

                case 3:
                    return test3;

                default:
                    return 0;

            }
        }

        //-------------------------------------------------------------------
        // Averages the test scores
        //-------------------------------------------------------------------
        public void average()
        {
            averageScore = (test1 + test2 + test3)/3;
            return;
        }

        //-------------------------------------------------------------------
        // Returns a string description of this Student object.
        //-------------------------------------------------------------------
        public String toString()
        {
            String result;

            result = firstName + " " + lastName + "\n";
            result += "Home Address:\n" + homeAddress + "\n";
            result += "School Address:\n" + schoolAddress +"\n";
            result += "Test Scores:\n";
            result += "Test #1: " + test1 + "\n";
            result += "Test #2: " + test2 + "\n";
            result += "Test #3: " + test3 + "\n";
            result += "Average Score: " + averageScore ;

            return result;
        }

    }

地址.java

public class Address 
{
    private String streetAddress, city, state;
    private long zipCode;

    //-------------------------------------------------------------------
    // Constructor: Sets up this address with the specified data.
    //-------------------------------------------------------------------
    public Address(String street, String town, String st, long zip)
    {
        streetAddress = street;
        city = town;
        state = st;
        zipCode = zip;
    }

    //-------------------------------------------------------------------
    // Returns a description of this Address object.
    //-------------------------------------------------------------------
    public String toString()
    {
        String result;

        result = streetAddress + "\n";
        result += city + ", " + state + " " + zipCode;

        return result;
    }

}

我的輸出:

John Smith 家庭地址:21 Jump Street Blacksburg, VA 24551 學校地址:800 Lancaster Ave. Villanova, PA 19085 測試分數:測試 #1:93 測試 #2:86 測試 #3:77 平均分:85.0

Marsha Jones 家庭地址:123 Main Street Euclid, OH 44132 學校地址:800 Lancaster Ave. Villanova, PA 19085 測試分數:測試 #1:82 測試 #2:91 測試 #3:97 平均分數:90.0

我很想使用數組而不是為每個測試使用單獨的變量。 你的班級處理過數組了嗎? ArrayList 可能非常有用,尤其是在更接近真實世界場景的情況下,其中測試的數量可能會發生變化。

使用數組時,實際上對於 getScore(int testID) 會有很好的用例。 您可以在循環中使用此方法,該循環遍歷數組中的所有分數以獲取分數以計算平均值。

PS 為什么人們投票否決這個問題,然后不發布他們這樣做的原因? 在我看來,OP 並不是在尋求解決方案或幫助做功課,只是想獲得一些見解。

你是對的,它沒有被使用。 但是您似乎錯過了一個要求:

修改驅動程序類的 main 方法以使用新的 Student 方法。

這意味着,至少對我而言,您添加的方法(包括 getter)應該在main()方法中的某處調用。

這完全是語義上的爭論,並且在任何意義上都無關緊要。

然而......爭論不重要的事物的語義並編寫不必要的代碼來滿足由獨立主管編寫的任意要求對於任何以開發人員的身份進入勞動力市場的人來說都是一個令人驚訝的恰當教訓。 所以,把它當作一個教訓,編寫代碼,然后繼續。

你使用它只是為了滿足任務。 沒有必要成為那里的吸氣劑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM