Hi every1, i have trouble doing my homework. Could anyone help. the probelm is everytime when i set the degree and it prints out the last degree i entered. when i use the default constructor (withour entering any infomation) to create the UndergradStudent instance, it doesn't allow me to set the degree. :-|

Here are my codes:

public class UndergradStudent extends CollegeStudent
    public UndergradStudent()

    public UndergradStudent(String studentName, String studentId,
                          int graduationYear, String degreeSought)
        super(studentName, studentId, graduationYear, degreeSought); // must be first
    } // end constructor

   public void setDegree(String degreeSought)
    {   if (degree=="BA")
          { degree = degreeSought;
            System.out.println("This studet has assigned to a BA degree.");

         else if (degree == "BS")
               {degree = degreeSought;
                System.out.println("This studet has assigned to a BS degree.");
            {   degree="";
                System.out.println("Undergraduate student could only assign to a BA or BS degree");

     public String getDegree()
        return degree;
    }// end getDegree


If you need to see my superclass code, please check my attachment

Edited 3 Years Ago by pyTony: fixed formatting

I've not really looked at you code, but for string comparisons it would be better to use the string.compare() method instead of the == operator.

This article has been dead for over six months. Start a new discussion instead.