Hi. I am a very beginner at Java. I am taking an online course on Java and I'm stuck on an assignment. Anyone who is good with Java, can you please take a look at my code? This assignment was due yesterday but I couldn't finish it.
It is very unfinished, but I'm not sure how to continue because I'm still not familiar with the concept of object oriented programming (getters, setters, constructors, etc).
Could anyone please explain more object oriented programming to me? Also what do getters and setters do? And can you give me an example of using a constructor in the employee class? (I think that's what I'm supposed to do)
The assignment instructions are in the comments.

Sorry but the code is VERY incomplete right now. It won't even compile. I am so lost that I have no clue what I am doing anymore. Maybe I should have taken an actual class with a real teacher that could explain it...

Thank you so much. I hope somebody can help me, as I feel extremely hopeless after having spent the past 2 weeks wracking my brain on this simple assignment.

import java.util.Scanner;

/*     Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year.
Use an Employee class, a Name class, an Address class, and a Date class in your solution. Provide appropriate class constructors, getter methods, setter methods, and any other methods you think are necessary.
Your program should prompt the user to enter data for several employees and then display that data. The number of employees to store data for shall be entered from the command line.

(This is the most difficult assignment of the class. I have had only one student correctly implement it on their first try. Do not feel bad if you take multiple attempts.)

Length: No overall restriction. There should be a class and constructor for each of Employee, Name, Address, and Date. Each of those should have a constructor that initializes every instance variable within the class.

"Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year."
This translates, for the Employee class, into

Employee
{
private int number;
private Name name;
private Address address;
private Date hireDate;
}

You will need to have appropriate get/set methods in the Employee class for the data.
You must model the Employee as it is an integral part of the assignment and Object Oriented design.     */


public class project10
{
    public static void main( String[] args )
    {
        Scanner input = new Scanner( System.in );

        int numEmployees;
        System.out.println( "How many employees do you wish to enter?" );

        numEmployees = input.nextInt();

        Employee[] employeeArray = new Employee[numEmployees]; 

        for ( int i = 0; i < numEmployees; i++ ) 
        {
            Employee e1 = new Employee();   //???

            Name first = new Name();
            Name last = new Name();
            System.out.println( "Enter the first name of the employee" );
            e1.setFirstName( input.nextLine() );

            System.out.println( "Enter the last name of the employee" );
            e1.setLastName( input.nextLine() );
        }
    }
}

class Employee      
{
    private int number;
    private Name FirstName;
    private Name LastName;
    private Address address;
    private Date hireDate;

}

class Name
{
    private String FirstName; 
    private String LastName;

    public Name() 
    {
        FirstName = ""; 
        LastName = "";
    }
    public void setFirstName(String firstName) 
    {
        FirstName = firstName;
    }
    public void setLastName(String lastName)
    {
        LastName = lastName;
    }
    public String getFirstName() 
    {
        return FirstName;
    }
    public String getLastName()
    {
        return LastName;
    }
}

class Address
{
}

class Date
{
    private int month; 
    private int day;
    private int year;

    public Date() 
    {
        month = 0; 
        year = 0;
        day = 0;
    }
    public void setDay( int dayOfMonth ) 
    {
        day = dayOfMonth;
    }
    public void setMonth( int monthOfYear )
    {
        month = monthOfYear;
    }
    public void setYear( int whichYear )
    {
        year = whichYear;
    }
    public int getDay() 
    {
        return day;
    }
    public int getMonth() 
    {
        return month;
    }
    public int getYear() 
    {
        return year;
    }
}

Edited 4 Years Ago by klin1344

Lines 57,58 You only need one Name object becuase that contains both first and last name fields
You may also consider some constructors that allow fields to be set, eg
public Name(String first, String last) { ...
because that's simpler to use than creating an empty name then setting both fields.

Rule of thumb on choosing between numeric and string types, in particular the Employee.number which you declared as an int: if you are going to do math on it, then make it a numeric type (int, double), otherwise make it a String. So IMO it should be a String. This is valid for telephone "numbers" too.

This article has been dead for over six months. Start a new discussion instead.