jeudi 15 mai 2014

Java - la classe de constructeurs ne peuvent pas être appliquées - Stack Overflow


Could someone please explain to me what is wrong with my constructor method? I have tried a million different things but nothing seems to work. I am supposed to create a class (ScanArray) that contains a constructor and two methods that find the max and min values of an array created in the main class. Here is what I have so far.


import java.util.Scanner;

public class Assign7_Polk {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] midTerm1 = new int[8];
int[] midTerm2 = new int[8];
int[] finalExam = new int[8];
int[] grades = new int[8];

for (int i = 0; i < midTerm1.length; i++) {
System.out.println("Enter the 8 Mid Term 1 grades: ");
midTerm1[i] = input.nextInt();
}
for (int i = 0; i < midTerm2.length; i++) {
System.out.println("Enter the 8 Mid Term 2 grades: ");
midTerm2[i] = input.nextInt();
}
for (int i = 0; i < finalExam.length; i++) {
System.out.println("Please enter 8 Final Exam grades: ");
finalExam[i] = input.nextInt();
}
for (int i = 0; i < grades.length; i++) {
grades[i] = (midTerm1[i] + midTerm2[i] + finalExam[i]);
}
}
}



class ScanArray {

int Max = 0;

public ScanArray(int grades) {}

int FindMax(int[] grades) {
int Max = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] > Max) {
Max = grades[i];
}
}
return Max;
}

int FindMin(int[] grades) {
int Min = grades[0];
for (int i = 1; i > grades.length; i++) {
if (grades[i] < Min) {
Min = grades[i];
}
}
return Min;

ScanArray findarray = new ScanArray();

int highest = findarray.FindMax();
int lowest = findarray.FindMin();

System.out.println("The highest grade is " + highest);
System.out.println("The lowest grade is " + lowest);
}
}



Add default constructor to your class


public ScanArray(){


}

Since you have defined a constructor for ScanArray class with parameters, Compiler will not auto-generate default no-args constructor. Please add it manually


Other way is just change the way you construct the ScanArray instance like new ScanArray(5);




Remove the argument from the constructor, just have


public ScanArray(){}



Just delete your constructor public ScanArray(int grades) - it does nothing, the compiler will add the default parameter-less constructor for you automatically.




    public ScanArray()
{
}

use above or remove the constructor


int highest = findarray.FindMax();//pass integer array
int lowest = findarray.FindMin();//pass integer array

check this out still you need to pass integer array to get highest and lowest value.




When you define a constructor other than the default no-argument constructor, Java doesn't add the default constructor automatically to your class. So in this case because you defined the constuctor with arguments:


public ScanArray(int grades)
{
}

You must add explicitly the no-argument constructor if you are ever going to use it in your code:


public ScanArray()
{
}


Could someone please explain to me what is wrong with my constructor method? I have tried a million different things but nothing seems to work. I am supposed to create a class (ScanArray) that contains a constructor and two methods that find the max and min values of an array created in the main class. Here is what I have so far.


import java.util.Scanner;

public class Assign7_Polk {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] midTerm1 = new int[8];
int[] midTerm2 = new int[8];
int[] finalExam = new int[8];
int[] grades = new int[8];

for (int i = 0; i < midTerm1.length; i++) {
System.out.println("Enter the 8 Mid Term 1 grades: ");
midTerm1[i] = input.nextInt();
}
for (int i = 0; i < midTerm2.length; i++) {
System.out.println("Enter the 8 Mid Term 2 grades: ");
midTerm2[i] = input.nextInt();
}
for (int i = 0; i < finalExam.length; i++) {
System.out.println("Please enter 8 Final Exam grades: ");
finalExam[i] = input.nextInt();
}
for (int i = 0; i < grades.length; i++) {
grades[i] = (midTerm1[i] + midTerm2[i] + finalExam[i]);
}
}
}



class ScanArray {

int Max = 0;

public ScanArray(int grades) {}

int FindMax(int[] grades) {
int Max = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] > Max) {
Max = grades[i];
}
}
return Max;
}

int FindMin(int[] grades) {
int Min = grades[0];
for (int i = 1; i > grades.length; i++) {
if (grades[i] < Min) {
Min = grades[i];
}
}
return Min;

ScanArray findarray = new ScanArray();

int highest = findarray.FindMax();
int lowest = findarray.FindMin();

System.out.println("The highest grade is " + highest);
System.out.println("The lowest grade is " + lowest);
}
}


Add default constructor to your class


public ScanArray(){


}

Since you have defined a constructor for ScanArray class with parameters, Compiler will not auto-generate default no-args constructor. Please add it manually


Other way is just change the way you construct the ScanArray instance like new ScanArray(5);



Remove the argument from the constructor, just have


public ScanArray(){}


Just delete your constructor public ScanArray(int grades) - it does nothing, the compiler will add the default parameter-less constructor for you automatically.



    public ScanArray()
{
}

use above or remove the constructor


int highest = findarray.FindMax();//pass integer array
int lowest = findarray.FindMin();//pass integer array

check this out still you need to pass integer array to get highest and lowest value.



When you define a constructor other than the default no-argument constructor, Java doesn't add the default constructor automatically to your class. So in this case because you defined the constuctor with arguments:


public ScanArray(int grades)
{
}

You must add explicitly the no-argument constructor if you are ever going to use it in your code:


public ScanArray()
{
}

0 commentaires:

Enregistrer un commentaire