简体   繁体   中英

Java why do I need this method and how do I use it

This is for an assignment for my Java class, the problem is as follows:

Modify the student class presented in this chapter as follows. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each test sore is assumed to be initially zero. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called getTestScore that accepts the test number and returns the appropriate score. Provide a method called average that computes and returns the average test score for this student. Modify the toString method such that the test scores and average are included in the description of the student. Modify the driver class main method to exercise the new Student methods.

So I got the program to run and I got the correct output. My question is why do I need the method getTestScore. I didn't use it to get the correct output. Is there a flaw with the way my code is currently written or do I just "need" it to satisfy the assignment?

Here is my code:
StudentBody.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);
    }

}

Student.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;
        }

    }

Address.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;
    }

}

My Output:

John Smith Home Address: 21 Jump Street Blacksburg, VA 24551 School Address: 800 Lancaster Ave. Villanova, PA 19085 Test Scores: Test #1: 93 Test #2: 86 Test #3: 77 Average Score: 85.0

Marsha Jones Home Address: 123 Main Street Euclid, OH 44132 School Address: 800 Lancaster Ave. Villanova, PA 19085 Test Scores: Test #1: 82 Test #2: 91 Test #3: 97 Average Score: 90.0

I'd be tempted to use arrays instead of having separate variables for each test. Has your class dealt with arrays yet? An ArrayList could be very useful, especially in something closer to a real world scenario where the number of tests can change.

When using an array, you actually would have a good use case for a getScore(int testID). You could use this method in a loop that iterates through all the scores in the array to fetch the scores for computing the average.

PS Why are people voting this question down, and then not posting their reason why they are doing so? It seems to me the OP is not asking for a solution or help on doing their homework, just trying to gain some insights.

You're correct, it's not being used. But you seem to have missed a requirement:

Modify the driver class main method to exercise the new Student methods.

This implies, at least to me, that the methods you added (including the getter) should be invoked somewhere in the main() method.

It's entirely an argument in semantics and is, in every sense of the word, not important.

However ... Arguing the semantics of unimportant things and writing unnecessary code to fulfill arbitrary requirements written by detached supervisors is a surprisingly apt lesson for anybody entering the workforce as a developer. So, consider it a lesson, write the code, and move on.

You use it just to satisfy the assignment. There is no need to be the getter there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM