Reputation: 87
I'm trying to finish my project by calling the methods I've created in my class. I'm getting an error "constructor in class cannot be applied to given types.
Here is my main
public static void main(String[] args) {
double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades cls = new HomeworkGrades();
and this is my class
public class HomeworkGrades {
private double[] grades;
double [] HWgrades = { 1, 2, 3, 4, 5, 6, 7, 8};
public HomeworkGrades(double [] gradesEarned)
{
grades= gradesEarned;
for (int i =0; i<gradesEarned.length;i++)
HWgrades =gradesEarned;
}
public double calcAverage()
{
int sum =0;
for(int i =0; i<HWgrades.length; i++)
{
sum = (int) (sum + HWgrades[i]);
}
double average = sum / (double)HWgrades.length;
return average;
}
public static double calcLowestGrade(double[]a, double total)
{
double temp;
double size;
double array[] = { 1, 2, 3, 4, 5, 6, 7, 8};
size = array.length;
for (int i =0; i <size; i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("Lowest grade is " + array[0]);
return array[0];
}
}
Upvotes: 4
Views: 49
Reputation: 858
You forgot to set the parameter for your new HomeworkGrades()
-instance.
Therefore
double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades cls = new HomeworkGrades();
should be
double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades cls = new HomeworkGrades(grades);
Besides... You are doing some crazy stuff while initializing HomeworkGrades...
This will work just fine:
double[] HWgrades;
public HomeworkGrades(double [] gradesEarned) {
HWgrades =gradesEarned;
}
You aren't even using private double[] grades;
and you don't have to set your array array.length times in order to get the array at the end. Just do it once.
EDIT In order to print your Average or whatever you want from the instance just get the double and print it in System.out.println()
public static void main(String[] args) {
double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90};
HomeworkGrades cls = new HomeworkGrades(grades);
System.out.println("Average: " + cls.calcAverage());
}
EDIT2 Your function to get the lowest grade is strange... Just use this istead:
public double calcLowestGrade() {
double lowest = Double.MAX_VALUE;
for (double d : HWgrades) {
if (d < lowest) {
lowest = d;
}
}
return lowest;
}
One mistake was that you made the function static and therefore it couldn't get the HWgrades array. Furthermore you don't have to create a new array because you already gave the object the array in the beginning.
Upvotes: 1
Reputation: 311526
The constructor you shared takes a double[]
. It seems like you forgot to pass the grades
array you created:
double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades cls = new HomeworkGrades(grades);
Upvotes: 2