Results 1 to 6 of 6
Thread: help in setMethod and getMethod
- 09-27-2009, 06:06 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 22
- Rep Power
- 0
help in setMethod and getMethod
here is the Question: Each student object contain scores for 3 tests.
Provide method called setTestScore accecpt 2 parameters:test number(1through 3) and the score.
Also, provide a method called getTestScore accepts the test number and returns the appropriate score.
I don't know how to put the test number; however I have do for the score.
Below is my code.
Java Code:public class Student { private String firstName, lastName; private double test1,test2,test3,average; //----------------------------------------------------------------- // Constructor: Sets up this student with the specified values. //----------------------------------------------------------------- public Student (String first, String last,double test1,double test2,double test3) { firstName = first; lastName = last; this.test1 = test1; this.test2 = test2; this.test3 = test3; } public Student (String first, String last) { firstName = first; lastName = last; } public void setTestScore(double test1,double test2,double test3) { this.test1 = test1; this.test2 = test2; this.test3 = test3; } public double getTestScore1() { return test1; } public double getTestScore2() { return test2; } public double getTestScore3() { return test3; } public void averageMarks() { average = (test1+test2+test3)/3; } //----------------------------------------------------------------- // Returns a string description of this Student object. //----------------------------------------------------------------- public String toString() { String result; result = firstName + " " + lastName + "\n"; result += "Three test result:\n" + test1 + test2+test3 + "\n"; result += "Average marks:\n" +average ; return result; } }Java Code:public class StudentBody { //----------------------------------------------------------------- // Creates some Address and Student objects and prints them. //----------------------------------------------------------------- public static void main (String[] args) { Student john = new Student ("John", "Smith", 12, 13,14); Student marsha = new Student ("Marsha", "Jones"); john.averageMarks(); System.out.println (john); System.out.println (); System.out.println (marsha); } }
- 09-27-2009, 06:20 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
The question says: "Provide method called setTestScore accept 2 parameters: test number(1through 3) and the score."
Your setTestScore() is quite different. Change it so that it has only two parameters the way the question asks.
For example if the question said "provide a method, foo(), taking two parameters: name and age. The method should ..." then I would write something like:
Java Code:SomeClass { private void foo(String name, int age) { // then figure out what goes here } }Last edited by pbrockway2; 09-27-2009 at 06:23 AM. Reason: eg added
- 09-27-2009, 06:42 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 22
- Rep Power
- 0
But this code didnt' not make sense.Java Code:public void setTestScore(double test1,double test2,double test3) { this.test1 = test1; this.test2 = test2; this.test3 = test3; System.out.println("TestNumber1:" +test1); System.out.println("TestNumber2:" +test2); System.out.println("TestNumber3:" +test3); }
And, the getTestScore if I combine all, it got error.why is this so?
Thanks you for the help.Java Code:public double getTestScore() { return test1,test2,test3; }
- 09-27-2009, 07:53 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
But this still has three parameters when the question says that it should have only two.Java Code:public void setTestScore(double test1,double test2,double test3) {
Read the question again. Make sure you understand that the setTestScore() method should have only two parameters, and that you understand what these parameters should be. Then rewrite your method so that it conforms to what the question asked. You can worry about what actually goes into the body of the method once you are clear about what it should do.
- 09-28-2009, 11:14 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 22
- Rep Power
- 0
Java Code:public void setTestScore(int testNumber, int score) { for(int i=0;i<4;i+=) //how should I continue } public double getTestScore() { return test1,test2,test3; }
Could you please help me??
Thanks a lot
- 09-28-2009, 02:34 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks