Results 1 to 6 of 6
  1. #1
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default 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);
       }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    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 07:23 AM. Reason: eg added

  3. #3
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    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);
    
    
    	}
    But this code didnt' not make sense.
    And, the getTestScore if I combine all, it got error.why is this so?
    Java Code:
    public double getTestScore()
    	{
    		return test1,test2,test3;
    	}
    Thanks you for the help.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Java Code:
    public void setTestScore(double test1,double test2,double test3)
    	{
    But this still has three parameters when the question says that it should have only two.

    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.

  5. #5
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    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

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You find out what the test number is.
    If the test number passed is one, you update the score for test 1 e.t.c

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •