geekhack

geekhack Community => Off Topic => Topic started by: SSIPAK on Fri, 16 May 2014, 22:15:50

Title: Is this right? Java programming question.
Post by: SSIPAK on Fri, 16 May 2014, 22:15:50
Thanks everyone!
Title: Re: Is this right? Java programming question.
Post by: molt on Fri, 16 May 2014, 22:43:32

public String getCourseName(){
return courseGrade;
}

public String getCoursegrade(){
return courseGrade;
}

Dont know exactly what errors you are talking about, but the getters are returning the same variable. The CourseGrade are returned instead of the CourseName in getCourseName()

The equals method does not have to take two parameters. In this case you can compare the current object with an other object.

        public boolean equals(CourseGrade other)
        {
            return courseGrade.Equals(other.getCoursegrade());
        }
Title: Re: Is this right? Java programming question.
Post by: phoenix1234 on Fri, 16 May 2014, 22:52:43
So this is my assignment for my Introduction to Java class.

Write a class encapsulating the concept of a course grade, assuming a course grade has the following attributes: a course name and a letter grade. Include a constructor, the accessors and mutators, and methods toString and equals. Write a client class to test all the methods in your class.

And this is what I wrote so far and its giving me an error when I run it... Did I even write this correctly?
Thanks in advance!

It seems you missed the public class.
This question should be asked on stackoverflow instead.  :thumb:
Anyway, this below code should work.

Code: [Select]

public class CourseGrade {
private String courseName;
private String courseGrade;

public CourseGrade(String courseName, String courseGrade){
this.courseName = courseName;
this.courseGrade = courseGrade;
}

public String getCourseName(){
return courseGrade;
}

public String getCoursegrade(){
return courseGrade;
}

public void setCourseName(String newName){
courseName = newName;
}

public void setCourseGrade(String newGrade){
courseGrade = newGrade;
}

public String toString(){
return courseName + " : " + courseGrade;
}


public static boolean equals(CourseGrade a, CourseGrade b){

return a.getCoursegrade().equalsIgnoreCase(b.getCoursegrade());

}

public static void main (String[] args){
    CourseGrade a = new CourseGrade("a", "x");
    CourseGrade b = new CourseGrade("b", "y");
    System.out.println(a.equals(b));   
}


}