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()
    {
        super();
    }

    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.");
            }
           else
            {   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

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

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.

Or the "equals" method of String, in this case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.