• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructors and calling methods  RSS feed

 
J bech
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,First of all is this how i create a default constructor and a constructor that sets values based on what ive passed into the second student method ??
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 {

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
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear J bech,

There are two problems in your code.
1st Problem: in the StudentEntry class This will not be invoked because you have not declared this method. The compiler is red faced as you have declared a method signature averageGrade(double[] scores) type and you are trying to invoke a method that is not being passed an array of double type.
Second in the code The variable grades cannot be found in this class because if i understand you correctly, you have declared it in the Student class and you want it to be accessible in the StudentEntry Class...
Well my friend, that's not how it works even if the two classes are in the same package.
Then there are two ways to do it
Method 1: If you want the variables of class A to be accessible in Class B then you will need to extend the class A in the class B
Method 2: You declare the grades variable in Student class as public only then it will be against the good OOP practices so it would be better if you use the protected modifier. if you just declare the variable as you have done then it gets the default modifier and that basically means that its accessible to methods within the same class, once outside the class its inaccessible.

And a bit of advice for you, the next time, please use the code tags to post your code.
Cheers.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you have indented your code, I shall try to add the code tags, and if it is indented, it will look a lot better

I think what Ashish Dutt meant to say is that you should declare sum as a local variable in the average method.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code wan't indented, so code tags would not make it look any better. Always post correctly spaced and formatted code. Have a look at these suggestions.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was mistaken about local variables. Sorry. What you have to do is pass the array of marks to that method, and get its return value.
By the way: that method is of general applicability and should be declared in a utility class, with public static modifiers. The reason it can be static is that it is a 1368 in the most dubious classification of methods known to modern science.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!