I have a constructor class as so:
public class Student
{
// Declare variables
public int studentID;
public String courseName;
public int courseScore;
public char courseGrade;
// Default constructor
public Student()
{
studentID = 1;
courseName = "CIS 2085";
courseScore = 89;
}
//========================================
// Constructor with parameters
public Student(int id, String course, int score)
{
setStud(id, course, score);
}
//========================================
public String toString()
{
return (studentID + " " + courseName + " " + courseScore);
}
//========================================
public void setStud(int id, String course, int score)
{
studentID = id;
courseName = course;
courseScore = score;
}
//========================================
public int getStudentID()
{
return studentID;
}
//========================================
public String getCourseName()
{
return courseName;
}
//========================================
public int getCourseScore()
{
return courseScore;
}
//========================================
public char getCourseGrade()
{
return courseGrade;
}
//========================================
public static char calculateGrade(int courseScore)
{
if (courseScore >= 94)
courseGrade = 'A';
else if ((courseScore >= 85) && (courseScore <= 93))
courseGrade = 'B';
else if ((courseScore >= 75) && (courseScore <= 84))
courseGrade = 'C';
else if ((courseScore >= 65) && (courseScore <= 74))
courseGrade = 'D';
else
courseGrade = 'F';
return courseGrade;
}
//========================================
}
And I have my implementation class so far, like this:
import java.util.*;
public class StudentImplementation
{
// Sllow 'console' to receive input from the user
static Scanner console = new Scanner(System.in);
// Method main
public static void main(String[] args)
{
// Declare variables
// Object 1: first. Uses the default constructor.
Student first = new Student();
//System.out.println(first);
// Object 2: second.
Student second = new Student(3, "Advanced Java Programming", 83);
//System.out.println(second);
// Use calculateGrade method to receive grade for object 1
char grade = Student.calculateGrade(first.getCourseScore);
System.out.println(grade);
}
}
So I am trying to use my calculateGrade method to output the appropriate grade based on my objects courseScore.
The Student class is in a separate file from my implementation class.
When I run this program I receive the error:
Exception in thread "main" java.lang.NoSuchMethodError: Student.calculateGrade(I)C
at StudentImplementation.main(StudentImplementation.java:27)
Change your calculateGrade method to:
public char calculateGrade() {
courseScore = this.getCourseScore(); // This refers to the student
// from whom you called the
// calculaGrade() method -->
// first.calculateGrade()
if (courseScore >= 94) {
courseGrade = 'A';
} else if ((courseScore >= 85) && (courseScore <= 93)) {
courseGrade = 'B';
} else if ((courseScore >= 75) && (courseScore <= 84)) {
courseGrade = 'C';
} else if ((courseScore >= 65) && (courseScore <= 74)) {
courseGrade = 'D';
} else {
courseGrade = 'F';
}
return courseGrade;
}
Then just use first.calculateGrade(). And print the result:
Student first = new Student();
char firstStudentGrade = first.calculateGrade();
System.out.println(firstStudentGrade);
PS: method names do not start with capital letters ;)
Here's a cleaner version of your Student class that does what you want it to do. I've taken out the unnecessary variables and methods, and cleaned it up a bit.
The reason I did this is because right now, your code contains a lot of things that don't make sense (eg. adding a parameter to a method so you can set the parameters value in the method itself and then return it, using a setter method in your constructor).
public class Student {
public int studentID;
public String courseName;
public int courseScore;
public Student() {
this(1, "CIS 2085", 89);
}
public Student(int id, String course, int score) {
this.studentID = id;
this.courseName = course;
this.courseScore = score;
}
@Override
public String toString() {
return studentID + " " + courseName + " " + courseScore;
}
public int getStudentID() {
return studentID;
}
public String getCourseName() {
return courseName;
}
public int getCourseScore() {
return courseScore;
}
public char getCourseGrade() {
if (courseScore >= 94) {
return 'A';
} else if (courseScore >= 85 && courseScore <= 93) {
return 'B';
} else if (courseScore >= 75 && courseScore <= 84) {
return 'C';
} else if (courseScore >= 65 && courseScore <= 74) {
return 'D';
}
return 'E';
}
}
Here's an example implementation to go along with that:
public class StudentImplementation {
public static void main(String[] args) {
Student first = new Student(); //default constructor
System.out.println(first.getCourseGrade());
Student second = new Student(3, "Advanced JAVA Programming", 83);
System.out.println(second.getCourseGrade());
}
}
Use:
public char CalculateGrade()
{
if (this.courseScore >= 94)
courseGrade = 'A';
else if ((this.courseScore >= 85) && (this.courseScore <= 93))
courseGrade = 'B';
else if ((this.courseScore >= 75) && (this.courseScore <= 84))
courseGrade = 'C';
else if ((this.courseScore >= 65) && (this.courseScore <= 74))
courseGrade = 'D';
else
courseGrade = 'F';
g = courseGrade;
return g;
}
And call it as:
System.out.println(first.CalculateGrade());
After creating your Student
object, simply call your method...
Student first = new Student();
first.CalculateGrade();
And remove the g
parameter from CalculateGrade(char gg)
. You don't need it.
Change the statement
char grade = Student.calculateGrade(first.getCourseScore);
to
char grade = Student.calculateGrade(first.getCourseScore());
You missed the ()
during the method call.
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.