Author Topic: Is this right? Java programming question.  (Read 3351 times)

0 Members and 1 Guest are viewing this topic.

Offline SSIPAK

  • * Maker
  • Thread Starter
  • Posts: 449
  • ★ Bootlegcaps ★
Is this right? Java programming question.
« on: Fri, 16 May 2014, 22:15:50 »
Thanks everyone!
« Last Edit: Fri, 16 May 2014, 23:09:39 by SSIPAK »

Offline molt

  • Posts: 5
Re: Is this right? Java programming question.
« Reply #1 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());
        }
« Last Edit: Fri, 16 May 2014, 22:51:36 by molt »
Filco MJ2 Browns

Offline phoenix1234

  • Posts: 584
  • Location: Saigon - Vietnam
Re: Is this right? Java programming question.
« Reply #2 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));   
}


}

I like linear switches