4 Replies - 407 Views - Last Post: 08 October 2012 - 12:33 PM Rate Topic: -----

#1 JArruda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-May 11

Java assignment, create 3 objects to hold & display data

Posted 08 October 2012 - 10:56 AM

Hello everyone, I'm a beginner programmer still learning here.

I have an assignment where I needed to create an Employee class on my own that holds 4 fields. Name, IDnumber, Department, and Position. and then it wants me to write a separate program that creates 3 objects to store specific data, then display this data for each employee on the screen.

For instance, 1 employee will be Susan Meyers. Their IDnumber will be 47899. Their department is Accounting. Their position is Vice President.

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'm having a brain fart right now I think, as I have no idea how I should begin.
I was starting it like this

import java.util.Scanner;

public class EmployeeTest
{
  public static void main(String[] args)
  {
    String testName; //Holds employee names
    Int testIdnumber; //Holds employee idnumbers
    String testDepartment //Holds employee departments
    String testPosition // Holds employee positions


but it's starting to look like it's not going the direction I need. Hope someone can give me some assistance and direction to solve this.

Is This A Good Question/Topic? 0
  • +

Replies To: Java assignment, create 3 objects to hold & display data

#2 JArruda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-May 11

Re: Java assignment, create 3 objects to hold & display data

Posted 08 October 2012 - 11:03 AM

I actually found a better direction and this is what I have now

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?
Was This Post Helpful? 0
  • +
  • -

#3 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Java assignment, create 3 objects to hold & display data

Posted 08 October 2012 - 12:02 PM

You can use an array - check out this introduction.
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,686
  • Joined: 13-March 10

Re: Java assignment, create 3 objects to hold & display data

Posted 08 October 2012 - 12:10 PM

You need to look into creating objects and toString() method.
Was This Post Helpful? 0
  • +
  • -

#5 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Java assignment, create 3 objects to hold & display data

Posted 08 October 2012 - 12:33 PM

Create a for-loop, read in the values and store them in temporary string variables, then create a new employee object, and add the employee to an array. When you've done this for as many times as you want to create new employees, create a new for-loop to print out all the values(using toString() would be the simplest and most logical).

new array
for the length of array
{
   read in values of one emplyee.
   new employee
   array[i] = employee
}

for the length of array
{
   print out array[i].toString()
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1