Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with beginner assignment

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with beginner assignment

    I've had to write a class on my own, which I have, and then I need to apply it to another program that creates three objects that holds specific data. The class seems right to me, it holds spots for an Employee name, Employee Idnumber, the department and position of the employee as well.

    The second half of this question asks me to plug in specific information into three objects in a separate program. For instance, 1 employee will be Susan Meyers. Their IDnumber will be 47899. Their department is Accounting. Their position is Vice President.

    Having a brain fart right now on how to go about this. Any help would be appreciated, specifically an example on how to accomplish the above one so I can finish the others confidently.

    Below is the class I wrote first.



    public class Employee
    {
    //Fields
    private String name; //Employees name
    private int idnumber; //Employees ID number
    private String department; //Department the employee works at
    private String position; //Employees position
     
    //Constructor
    public Employee(String nam, int idnum, String dep, String pos)
    {
    name = nam;
    idnumber = idnum;
    department = dep;
    position = pos;
    }
     
    //The setname that accepts argument for employees name
    public void setName(String nam)
    {
    name = nam;
    }
     
    //The setIdnumber accepts argument for employees id number
    public void setIdnumber(int idnum)
    {
    idnumber = idnum;
    }
     
    //The setDepartment accepts argument for employees department
    public void setDepartment(String dep)
    {
    department = dep;
    }
     
    //The setPosition accepts argument for employees position
    public void setPosition(String pos)
    {
    position = pos;
    }
     
    //The getName method returns the name of the employee
    public String getName()
    {
    return name;
    }
     
    //The getIdnumber method returns the employees ID number
    public int getIdnumber()
    {
    return idnumber;
    }
     
    //The getDepartment method returns the employees department name
    public String getDepartment()
    {
    return department;
    }
     
    //The getPosition method returns the employees position
    public String getPosition()
    {
    return position;
    }
    }

    I started coding and this is what direction I was heading

    import java.util.Scanner;
     
    public class EmployeeTest
    {
      public static void main(String[] args)
      {
        String testName1; //Holds employee names
        int testIdnumber1; //Holds employee idnumbers
        String testDepartment1; //Holds employee departments
        String testPosition1; // Holds employee positions
     
          //Creates a Scanner object for keyboard input
          Scanner keyword = new Scanner(System.in);
     
          testName1 = ("Susan Meyers");
          System.out.println(testName1);
     
      }
    }

    I was going to do this for each one, but it doesn't seem very efficient? Is there an easier or cleaner way to do this?
    Last edited by Jarruda; October 8th, 2012 at 01:03 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with beginner assignment

    The Employee class seems usable. Looks like that portion of the assignment is going well so far.

    The part about utilizing the class from a program looks like the place you are stuck. To me you are asking for someone to write the code to do one of the employees, so you can copy-paste to complete the others.
    Great thinking. You broke the big problem down into smaller parts that you can more easily understand. If you knew how to do one of them, you could do the others.

    Your problem is no longer setting up three employees, but setting up just one. Break this new, smaller problem down into even smaller steps and see if that is something you can write code for. If you have problems breaking down steps, post more questions.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with beginner assignment

    This is what I have so far.

    public class EmployeeTest
    {
      public static void main(String[] args)
      {
        String testName1; //Holds employee names
        int testIdnumber1; //Holds employee idnumbers
        String testDepartment1; //Holds employee departments
        String testPosition1; // Holds employee positions
     
          //Creates a Scanner object for keyboard input
          Scanner keyword = new Scanner(System.in);
     
          testName1 = ("Susan Meyers");
          System.out.println(testName1);
     
          testIdnumber1 = 47899;
          System.out.println(testIdnumber1);
     
          testDepartment1 = ("Accounting");
          System.out.println(testDepartment1);
     
          testPosition1 = ("Vice President");
          System.out.println(testPosition1);
     
      }
    }

    It seems to get the job done, but now I'm wondering why I even bothered writing the class to begin with if I'm not using it. How can I incorporate the code I typed in the Employee class into the EmployeeTest program?

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with beginner assignment

    I think I got it!

    public class EmployeeTest
    {
      public static void main(String[] args)
      {
        Employee a = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");
        System.out.println("Name: " + a.getName());
        System.out.println("ID: " + a.getIdnumber());
        System.out.println("Department: " + a.getDepartment());
        System.out.println("Position: " + a.getPosition());
     
        Employee b = new Employee("Mark Jones", 39119, "IT", "Programmer");
        System.out.println("\nName: " + b.getName());
        System.out.println("ID: " + b.getIdnumber());
        System.out.println("Department: " + b.getDepartment());
        System.out.println("Position: " + b.getPosition());
     
        Employee c = new Employee("Joy Rogers", 81774, "Manufacturing", "Engineer");
         System.out.println("\nName: " + c.getName());
        System.out.println("ID: " + c.getIdnumber());
        System.out.println("Department: " + c.getDepartment());
        System.out.println("Position: " + c.getPosition());
     
     
     
      }
    }

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with beginner assignment

    Looks like the code is taking shape nicely.

    Make sure it is all formatted well, javadoc in place, etc.

    I don't know if the (school)class has gone over it yet, but the Employee class may benefit from a toString method.

Similar Threads

  1. another assignment from beginner programmer trouble
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2011, 12:13 PM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. Homework assignment (beginner)
    By z3ohyr84 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 30th, 2011, 01:32 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  5. Beginner needs help with simple java assignment.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 06:53 PM