Please be kind as I am very new to Java (and programming in general).
I am trying to write a method that will count the occurrences of grades, and I think I have it figured out, but I am losing my mind trying to test it (i TestingPage) I get the error 'char can't be converted to char[]' The method with the grades (getGrades), converted int to char based on the level of grades (marks).
Im sure the solution is simple, but I just can't seem to figure out what I am doing wrong, or what I need to include or take out to test it.
public class ProcessMarks {
Marks testMarks = new Marks();
int[] marks = testMarks.getMarks();
//Method to create a new array of grades based on marks array
public static char getGrades(int marks[]){
int a = 90;
int b = 75;
int c = 60;
int d = 50;
int e = 45;
char[] grades = new char [marks.length];
char gradedMarks = grades[0]
//else is F for fail!
for (int i = 0; i < marks.length; i++){
if (marks[i] >= a)
grades[i] = 'A';
else if (marks[i] >= b && marks[i] < a)
grades[i] = 'B';
else if (marks[i] >= c && marks[i] < b)
grades[i] = 'C';
else if (marks[i] >= d && marks[i] < c)
grades[i] = 'D';
else if (marks[i] >= e && marks[i] < d)
grades[i] = 'B';
else
grades[i] = 'F';
}
for (int n = 0; n < grades.length; n++) {
System.out.print(grades[n] + " ");
if (n % 10 == 9)
System.out.println();
}
return gradedMarks;
}
//Method to establish total occurrences of each grade
public static char gradesDistn (char getGrades[]){
char[] grades = getGrades;
char gradesDistn = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
for (int i = 0; i < grades.length; i++){
if (grades[i] == 'A')
a++;
else if (grades[i] == 'B')
b++;
else if (grades[i] == 'C')
c++;
else if (grades[i] == 'C')
c++;
else if (grades[i] == 'D')
d++;
else if (grades[i] == 'E')
e++;
else
f++;
}
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("E: " + e);
System.out.println("F: " + f);
return gradesDistn;
}
}
public class TestingPage {
public static void main(String[] args) {
int[] marks = Marks.getMarks();
char[] grade = ProcessMarks.getGrades(marks); // char cannot be converted to char[]
int median = ProcessMarks.getMedian(marks);
int min = ProcessMarks.getMin(marks);
int range = ProcessMarks.getRange(marks);
int mean = ProcessMarks.getMean(marks);
int mode = ProcessMarks.getMode(marks);
char grades = ProcessMarks.getGrades(marks);
int distribution = ProcessMarks.gradesDistn(grade);
System.out.println(range);
}
}
In your TestingPage
class void main
method you are doing this:
char[] grade = ProcessMarks.getGrades(marks);
This means you are expecting a char[]
in return but the method getGrades
returns only a char
as below:
public static char getGrades(int marks[]) { }
What data-type a method will return you need to catch the response in the exact same data-type.
If you do the below, your error [ char cannot be converted to char[] ] goes:
char grade = ProcessMarks.getGrades(marks);
But I can see many other flaws as well in your methods.
Example: Like the getGrades() returns gradedMarks which is never populated except the declaration point. It will always return the same value.
In your main method:
char[] grade = ProcessMarks.getGrades(marks);
getGrades()
returns char
, but you assign it to the array of chars char[]
, which is not compatible and cannot even be implicitly casted.
Generally, remember, that type T
and type T[]
are completely different types.
Jandie, as discussed offline - you need to define the type as an array. The above answers should be accepted as they are spot on.
public static char getGrades(int marks[]){
becomes
public static char[] getGrades(int marks[]){
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.