Java Assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javalearner
    New Member
    • Nov 2007
    • 6

    #1

    Java Assignment

    Good Morning Guys! -- i printed the 65 page tutorial at work EXCELLENT HELP! ...

    I have to write an Employee.class which grabs a (string first name, string last name, string salary) also is salary is below 0.0 - set it to = 0...

    is this correct?


    "//Lab4 - Employee.java
    //Create a Java Class for storing employee information

    public class employee
    {
    private String firstname; //saves first name
    private String lastname; //saves lastname for input
    private double salary; //saves salary for input

    public employee( String firstname, String lastname, double salary) //begin constructor
    {
    firstname = firstname;
    lastname = lastname;
    salary = salary;
    }

    public String getfirstname()
    {
    return firstname;
    }

    public void setfirstname(St ring firstname)
    {
    firstname = firstname; //is this neccessary? =)
    }
    public String getlastname()
    {
    return lastname;

    }
    public void setlastname(Str ing lastname)
    {
    lastname = lastname;
    }
    public double getsalary()
    {
    return salary;
    }
    public void setsalary(doubl e salary)
    {
    if(salary < 0)
    {
    salary = 0;
    }
    }
    }"
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by javalearner
    public void setfirstname(St ring firstname)
    {
    firstname = firstname; //is this neccessary? =)
    }
    That is not necessary; it's wrong: it sets the parameter 'firstname' to itself which
    is a useless operation; change the assignment to:
    [code=java]
    this.firstname= firstname;
    [/code]

    You'll see the same problem over and over again in your code.

    kind regards,

    Jos

    Comment

    • javalearner
      New Member
      • Nov 2007
      • 6

      #3
      Originally posted by JosAH
      That is not necessary; it's wrong: it sets the parameter 'firstname' to itself which
      is a useless operation; change the assignment to:
      [code=java]
      this.firstname= firstname;
      [/code]

      You'll see the same problem over and over again in your code.

      kind regards,

      Jos
      Do'h! - no wonder i was recieving errors :)
      alright, so if i use 'this.lastname = lastname ; that's correct?

      what does 'this' mean by definition when used in java?

      Comment

      • javalearner
        New Member
        • Nov 2007
        • 6

        #4
        Originally posted by javalearner
        Do'h! - no wonder i was recieving errors :)
        alright, so if i use 'this.lastname = lastname ; that's correct?

        what does 'this' mean by definition when used in java?

        btw : when i compile and then execute - i am recieving ::
        "java.lang.NoCl assDefFoundErro r: Lab4/employee (wrong name: employee)
        at java.lang.Class Loader.defineCl ass1(Native Method)
        at java.lang.Class Loader.defineCl ass(ClassLoader .java:620)
        at java.security.S ecureClassLoade r.defineClass(S ecureClassLoade r.java:124)
        at java.net.URLCla ssLoader.define Class(URLClassL oader.java:260)
        at java.net.URLCla ssLoader.access $100(URLClassLo ader.java:56)
        at java.net.URLCla ssLoader$1.run( URLClassLoader. java:195)
        at java.security.A ccessController .doPrivileged(N ative Method)
        at java.net.URLCla ssLoader.findCl ass(URLClassLoa der.java:188)
        at java.lang.Class Loader.loadClas s(ClassLoader.j ava:306)
        at sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:268 )
        at java.lang.Class Loader.loadClas s(ClassLoader.j ava:251)
        at java.lang.Class Loader.loadClas sInternal(Class Loader.java:319 )
        Exception in thread "main" "


        is that just due to the fact its a class with no action?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by javalearner
          btw : when i compile and then execute - i am recieving ::
          "java.lang.NoCl assDefFoundErro r: Lab4/employee (wrong name: employee)
          I assume your class compiled fine then. Your class is named 'employee', not
          'Lab4/employee'. Go to the directory where your employee class file is stored
          and type 'java -cp . employee'.

          The '-cp .' flag tells the jvm to search for .class files in the current directory.
          Don't think you're ready now because that class doesn't contain a method named
          public static void main(String[] args) so it still won't run but you'll get another
          error message.

          kind regards,

          Jos

          Comment

          • javalearner
            New Member
            • Nov 2007
            • 6

            #6
            Originally posted by JosAH
            I assume your class compiled fine then. Your class is named 'employee', not
            'Lab4/employee'. Go to the directory where your employee class file is stored
            and type 'java -cp . employee'.

            The '-cp .' flag tells the jvm to search for .class files in the current directory.
            Don't think you're ready now because that class doesn't contain a method named
            public static void main(String[] args) so it still won't run but you'll get another
            error message.

            kind regards,

            Jos
            ohh, i know, thats the next step i believe....i need to create an app with a method and all that stuff, called employeetest.ja va -- that pulls FROM that java class...


            Desktop\Lab4>ja va -cp . employee
            Exception in thread "main" java.lang.NoSuc hMethodError: main

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by javalearner
              ohh, i know, thats the next step i believe....i need to create an app with a method and all that stuff, called employeetest.ja va -- that pulls FROM that java class...


              Desktop\Lab4>ja va -cp . employee
              Exception in thread "main" java.lang.NoSuc hMethodError: main
              Yep, as I wrote: there's no main method in that class. The jvm complains about it.

              kind regards,

              Jos

              Comment

              • javalearner
                New Member
                • Nov 2007
                • 6

                #8
                Originally posted by JosAH
                Yep, as I wrote: there's no main method in that class. The jvm complains about it.

                kind regards,

                Jos

                alright maybe i am going about this incorrectly??
                //Lab4 - Employee.java
                //Create a Java Class for storing employee information

                public class employee
                {
                private String firstname; //saves first name
                private String lastname; //saves lastname for input
                private double salary; //saves salary for input

                public employee( String firstname, String lastname, double salary) //begin constructor
                {
                firstname = firstname;
                lastname = lastname;
                salary = salary;
                }

                public String getfirstname()
                {
                return firstname;
                }

                public void setfirstname(St ring firstname)
                {
                this.firstname = firstname; //is this neccessary? =)
                }
                public String getlastname()
                {
                return lastname;

                }
                public void setlastname(Str ing lastname)
                {
                this.lastname = lastname;
                }
                public double getsalary()
                {
                return salary;
                }
                public void setsalary(doubl e salary)
                {
                if(salary < 0)
                {
                salary = 0;
                }
                }
                }

                that is how employee.java / employee.class ends.

                //Lab 4 - 3.14
                //Bring in employee.class and configure to run as an app

                public class employeetest
                {
                public static void main(String args[])
                {
                employee employee1 = new employee("John" , "Smith", 50400);
                employee employee2 = new employee("Jack" , "Russell", 100000);

                System.out.prin tln( "Salary:");

                }
                }

                i'm stuck here, because i am trying to bring in employee class info here...
                am i doing something wrong ?

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by javalearner
                  alright maybe i am going about this incorrectly??
                  //Lab4 - Employee.java
                  //Create a Java Class for storing employee information

                  public class employee
                  {
                  private String firstname; //saves first name
                  private String lastname; //saves lastname for input
                  private double salary; //saves salary for input

                  public employee( String firstname, String lastname, double salary) //begin constructor
                  {
                  firstname = firstname;
                  lastname = lastname;
                  salary = salary;
                  }

                  public String getfirstname()
                  {
                  return firstname;
                  }

                  public void setfirstname(St ring firstname)
                  {
                  this.firstname = firstname; //is this neccessary? =)
                  }
                  public String getlastname()
                  {
                  return lastname;

                  }
                  public void setlastname(Str ing lastname)
                  {
                  this.lastname = lastname;
                  }
                  public double getsalary()
                  {
                  return salary;
                  }
                  public void setsalary(doubl e salary)
                  {
                  if(salary < 0)
                  {
                  salary = 0;
                  }
                  }
                  }

                  that is how employee.java / employee.class ends.

                  //Lab 4 - 3.14
                  //Bring in employee.class and configure to run as an app

                  public class employeetest
                  {
                  public static void main(String args[])
                  {
                  employee employee1 = new employee("John" , "Smith", 50400);
                  employee employee2 = new employee("Jack" , "Russell", 100000);

                  System.out.prin tln( "Salary:");

                  }
                  }

                  i'm stuck here, because i am trying to bring in employee class info here...
                  am i doing something wrong ?
                  Note that 'javac' (your Java compiler) uses the same flag '-cp .' that tells it where
                  it can find compiled .class files. It needs those .class files because it needs to
                  know what that 'employee' class is all about when it has to compile your employeetest
                  class.

                  kind regards,

                  Jos

                  Comment

                  • javalearner
                    New Member
                    • Nov 2007
                    • 6

                    #10
                    Originally posted by JosAH
                    Note that 'javac' (your Java compiler) uses the same flag '-cp .' that tells it where
                    it can find compiled .class files. It needs those .class files because it needs to
                    know what that 'employee' class is all about when it has to compile your employeetest
                    class.

                    kind regards,

                    Jos
                    that definately makes sense - could you let me know what i need to do for that? I thought i was doing good. lol

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by javalearner
                      that definately makes sense - could you let me know what i need to do for that? I thought i was doing good. lol
                      Just type 'javac' on your command line and the compiler tells you everything you
                      wanted to know but were too afraid to ask ;-)

                      kind regards,

                      Jos

                      Comment

                      Working...