abstract class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugard
    New Member
    • Aug 2007
    • 50

    abstract class

    I am doing an assignment and following is part of the question:

    A program is to be devised for a university to work out whether or not, a student or a professor, is outstanding. The criterion for a professor to be outstanding is to have over 120 publications. The criterion for a student to be outstanding is to have a Grade Point Average of 3.3. For a professor, the three class data embers would be name, number of publications and the method is Outstanding().

    I have done the following code

    public abstract class SchoolMember {

    //These are the attributes for a SchoolMember class

    String name;
    String surname;
    String address;
    String contactNumber;

    public abstract boolean outstanding();

    public class Professor extends SchoolMember {

    Integer numberOfPublica tions;

    public boolean outstanding() {

    if (numberOfPublic ations > 120)
    return true;

    }

    }
    I am being given an error in this line of code
    public boolean outstanding()

    Can please some1 help me to solve this problem?

    Thank you very much
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    [code=java]
    //Integer numberOfPublica tions; .............. it's wrong here.
    int numberOfPublica tions; //Try this.
    [/code]

    Kind regards,
    Dmjpro.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by sugard
      I am doing an assignment and following is part of the question:

      A program is to be devised for a university to work out whether or not, a student or a professor, is outstanding. The criterion for a professor to be outstanding is to have over 120 publications. The criterion for a student to be outstanding is to have a Grade Point Average of 3.3. For a professor, the three class data embers would be name, number of publications and the method is Outstanding().

      I have done the following code






      I am being given an error in this line of code
      public boolean outstanding()

      Can please some1 help me to solve this problem?

      Thank you very much
      Your method specifies that it returns a boolean but if you look at it, there is a possibility that it might not return anything at all.
      Declare a boolen at the start of your method and initialize it, set its value appropriately in the if or if else part and return the boolean at the end of the method.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dmjpro
        [code=java]
        //Integer numberOfPublica tions; .............. it's wrong here.
        int numberOfPublica tions; //Try this.
        [/code]

        Kind regards,
        Dmjpro.
        That should not be a problem on 1.5 and higher.

        Comment

        • sugard
          New Member
          • Aug 2007
          • 50

          #5
          yes something is wrong with the return of the boolean

          I edited the code to this way:

          public class Professor extends SchoolMember {

          Integer numberOfPublica tions;
          Boolean isOutStanding;

          public boolean outstanding() {

          if (numberOfPublic ations > 120);

          }
          return
          isOutStanding = true;

          }
          but still I am having an error now in the part of the return

          Comment

          • sugard
            New Member
            • Aug 2007
            • 50

            #6
            ok i fixed it. I placed the return statement inside the breaces and it compiled fine. Thanks

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by sugard
              ok i fixed it. I placed the return statement inside the breaces and it compiled fine. Thanks
              I thought your specs said the method should be called isOutstanding so it really should be looking like this

              [CODE=java]public boolean isOutstanding() {
              boolean outstanding = false;
              if(numberOfPubl ications > 120) {
              outstanding = true;
              }
              return outstanding;
              }[/CODE]

              Comment

              • sugard
                New Member
                • Aug 2007
                • 50

                #8
                I made this as a launcher to test if it works correctly.


                public class Launcher {


                public static void main(String[] args) {

                Professor prof = new Professor();
                prof.outStandin g(140); {
                System.out.prin tln("Outstandin ");
                }

                }

                }
                I am havin an error in this line of code Professor prof = new Professor();

                Can someone pls tell me whats wrong with that ?

                THanks

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by sugard
                  I made this as a launcher to test if it works correctly.




                  I am havin an error in this line of code Professor prof = new Professor();

                  Can someone pls tell me whats wrong with that ?

                  THanks
                  Don't just say you have an error. Read the error message and see what it's telling you.

                  P.S You have gone through a tutorial on inheritance and constructors right?

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Originally posted by sugard
                    yes something is wrong with the return of the boolean

                    I edited the code to this way:

                    [CODE=java]
                    public class Professor extends SchoolMember {

                    Integer numberOfPublica tions;
                    Boolean isOutStanding;

                    public boolean outstanding() {

                    if (numberOfPublic ations > 120);
                    return
                    isOutStanding = true;
                    }
                    }
                    [/CODE]

                    but still I am having an error now in the part of the return
                    That code won't consider the if-part. If you use
                    [CODE=java]
                    if(something);
                    [/CODE]it is equivalent to
                    [CODE=java]
                    if(something)
                    // do absolutely nothing
                    ;
                    [/CODE]as just ; is an empty command.

                    What you want is, to answer with "true", if the condition is fulfilled and with false if it isn't. The code posted by r035198x solves that problem.

                    Considering your Launcher
                    [CODE=java]
                    public class Launcher {

                    public static void main(String[] args) {

                    Professor prof = new Professor();
                    prof.outStandin g(140); {
                    System.out.prin tln("Outstandin ");
                    }
                    }
                    }
                    [/CODE]you seem to have misunderstood something - you want this:
                    [CODE=java]
                    public class Launcher {

                    public static void main(String[] args) {

                    Professor prof = new Professor();
                    if(prof.outStan ding(140)) {
                    System.out.prin tln("Outstandin g");
                    }
                    }
                    }
                    [/CODE]That code of yours however shouldn't give you an error at the line
                    [CODE=java]Professor prof = new Professor();[/CODE]What is the exact error?

                    Greetings,
                    Nepomuk

                    PS.: For posting code, better not use QUOTE tags but CODE tags. It looks much better and makes it easier to quote you.

                    Comment

                    • sugard
                      New Member
                      • Aug 2007
                      • 50

                      #11
                      My code is edited to this way
                      Code:
                      public abstract class SchoolMember {
                      	
                      	//These are the attributes for a SchoolMember class
                      	
                      	String name;
                      	String surname;
                      	String address;
                      	String contactNumber;
                      	
                      	public abstract boolean outstanding();
                      }

                      Code:
                      public class Professor extends SchoolMember {
                      	
                      	Integer numberOfPublications;
                      	Boolean isOutStanding;
                      	
                      	public boolean outstanding() {
                      		
                      		if (numberOfPublications > 120)
                      		{
                      			return
                      			isOutStanding = true;
                      		}
                      
                      	}
                      	
                      }

                      Code:
                      public class Launcher {
                      
                      
                      	public static void main(String[] args) {
                      
                      		Professor prof = new Professor();
                      		if (prof.outStanding(140)) {
                      			System.out.println("Outstanding");
                      		}
                      
                      	}
                      
                      }
                      I am having an error in this line public boolean outstanding() of the Professor's class and an error in this line Professor prof = new Professor(); in the launcher.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by sugard
                        My code is edited to this way
                        Code:
                        public abstract class SchoolMember {
                        	
                        	//These are the attributes for a SchoolMember class
                        	
                        	String name;
                        	String surname;
                        	String address;
                        	String contactNumber;
                        	
                        	public abstract boolean outstanding();
                        }

                        Code:
                        public class Professor extends SchoolMember {
                        	
                        	Integer numberOfPublications;
                        	Boolean isOutStanding;
                        	
                        	public boolean outstanding() {
                        		
                        		if (numberOfPublications > 120)
                        		{
                        			return
                        			isOutStanding = true;
                        		}
                        
                        	}
                        	
                        }

                        Code:
                        public class Launcher {
                        
                        
                        	public static void main(String[] args) {
                        
                        		Professor prof = new Professor();
                        		if (prof.outStanding(140)) {
                        			System.out.println("Outstanding");
                        		}
                        
                        	}
                        
                        }
                        I am having an error in this line public boolean outstanding() of the Professor's class and an error in this line Professor prof = new Professor(); in the launcher.
                        Have you read any of the replies that I posted?

                        Comment

                        • Nepomuk
                          Recognized Expert Specialist
                          • Aug 2007
                          • 3111

                          #13
                          Originally posted by r035198x
                          Have you read any of the replies that I posted?
                          Doesn't seem so, does it?

                          Comment

                          • dmjpro
                            Top Contributor
                            • Jan 2007
                            • 2476

                            #14
                            Originally posted by r035198x
                            That should not be a problem on 1.5 and higher.
                            Yes that's right.

                            Kind regards,
                            Dmjpro.

                            Comment

                            • sugard
                              New Member
                              • Aug 2007
                              • 50

                              #15
                              Yes I did and I altered the code that I sent previously to what I understood from what you said.

                              Comment

                              Working...