Results 1 to 5 of 5
  1. #1
    TechJay is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Why cant i call this method ?

    Hello all,First of all is this how i create a default constructor and a constructor that sets values based on params passed in to the student class ??
    Secondly im trying to pass my array double[] grades into my averageGrade() method in the other class from the same package...
    Unfortunately i cannot call my method in StudentEntry... Thanks for the help

    import java.util.*;

    public class Student {

    //privates vars and array
    private String firstName, lastName;
    private int year;
    double[] grades;


    //default
    public Student(){

    firstName = "Jane";
    lastName = "Doe";
    year = 1;
    grades[0] = 15.0;
    grades[1] = 25.0;
    grades[2] = 35.0;
    grades[3] = 45.0;
    grades[4] = 55.0;




    }//end default student


    public Student(String fN,String lN,int yr, double[] scores){

    fN = firstName;
    lN = lastName;
    yr = year;
    scores = grades;

    }

    }//end class






    // Student entry class
    import java.util.*;

    public class StudentEntry extends Student{

    public static void main(String[] args) {

    double total = averageGrade(); //// <--- will not call
    }// end main

    private double averageGrade(double[] scores) {

    double sum = 0;
    for(int i = 0; i < scores.length; i++) {
    sum += grades[i];
    }

    double total = sum / scores.length;
    System.out.println(total);
    return total;

    }// end averageGrade


    }// end class
    Last edited by TechJay; 05-13-2013 at 11:29 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why cant i call this method ?

    The averageGrade( ... ) method takes a double array as a parameter; you're trying to call an averageGrade() method without parameters; it doesn't exist. Even more: that method isn't a static method, i.e. it belong to a StudentEntry object; you don't have such an object.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    TechJay is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: Why cant i call this method ?

    not quite following you, thanks for the reply

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why cant i call this method ?

    Quote Originally Posted by TechJay View Post
    not quite following you, thanks for the reply
    If you don't understand my reply, you don't understand basic concepts sucs as parameters or (non) static methods. Read some turorials on the basics of Java.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Why cant i call this method ?

    parameters are in simple terms the "things" in the brackets. so we can say:

    method() takes No parameters

    method(String x) takes a String as a parameter

    your method:

    averageGrade(double[] array); takes a double array as a parameter so in order to call the method you have to say:

    averageGrade(grades); Grades is your double array, you defined this earlier in your Class

Similar Threads

  1. call a method from another method in same class
    By rockstaedy in forum New To Java
    Replies: 5
    Last Post: 10-03-2012, 02:42 PM
  2. Method call
    By London in forum New To Java
    Replies: 3
    Last Post: 09-15-2011, 09:02 AM
  3. Method call
    By ralf in forum New To Java
    Replies: 51
    Last Post: 07-17-2011, 09:59 PM
  4. How do I call this method
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 06-29-2011, 04:03 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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