user2709168
user2709168

Reputation: 117

Class Constructor Objects don't display correctly

You can check my previous questions if you want to know what the purpose of my program is. Here is my almost finished code:

import javax.swing.JOptionPane;

public class AssignmentTen
{
    public static void main (String[] args)
    {
        System.out.println();
        int num = Integer.parseInt(args[0]);
        int eNumber;
        String input2;
        String input3;
        String input4;
        String input5;
        String input6;
        int input7;
        int input8;
        int input9;
        int input10;

        Employee[] employees = new Employee[num];
        for (int i = 0; i < num; i++)
        {
            eNumber = getInt ("Enter Employee Number:");
            input2 = getString ("Enter Employee First Name:");
            input3 = getString ("Enter Employee Last Name:");
            input4 = getString ("Enter Employee Street:");
            input5 = getString ("Enter Employee City:");
            input6 = getString ("Enter Employee State (Initials):");
            input7 = getInt ("Enter Employee Zip Code (5 Digits):");
            input8 = getInt ("Enter Employee Hire Month (MM):");
            input9 = getInt ("Enter Employee Hire Day (DD):");
            input10 = getInt ("Enter Employee Hire Year(YYYY):");

            Name name = new Name(input2, input3);
            Address address = new Address (input4, input5, input6, input7);
            Date hireDate = new Date (input8, input9, input10);
            employees[i] = new Employee (eNumber, name, address, hireDate);

            System.out.println("#" + employees[i].empNumber + "\n" + employees[i].empName + "\n" + employees[i].empAddress + "\nHire Date: " + employees[i].empHireDate + "\n\n");
        }
    }

    public static int getInt(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return Integer.parseInt(str);
    }

    public static String getString(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return str;
    }
}

class Employee
{
    Number empNumber;
    Name empName;
    Address empAddress;
    Date empHireDate;

    public Employee(Number empNumber, Name empName, Address empAddress, Date empHireDate)
    {
        this.empNumber = empNumber;
        this.empName = empName;
        this.empAddress = empAddress;
        this.empHireDate = empHireDate;
    }
}

class Name
{
    String firstName;
    String lastName;

    Name(String first, String last)
    {
        firstName = first;
        lastName = last;
    }
}

class Address
{
    String eStreet;
    String eCity;
    String eState;
    int eZipCode;

    Address(String street, String city, String state, int zipCode)
    {
        eStreet = street;
        eCity = city;
        eState = state;
        eZipCode = zipCode;
    }
}

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

    Date(int eMonth, int eDay, int eYear)
    {
        month = eMonth;
        day = eDay;
        year = eYear;
    }
}

I want my program to display information for an amount of employees specified through the command line (so for example, I type 'java AssignmentTen 3' to record data for three different employee objects). I would like the information to display like this:

[#]EmployeeNumber

FirstName LastName

Street City StateInitials ZipCode

Hire Date: MM DD YYYY

So for example:

#123

John Smith

123 Example Street CA 12345

Hire Date: 09/30/2013

However, what ends up displaying isn't correct at all. The Employee Number displays fine, but then the Name line reads 'Name@[RandomCharacters]', the Address line reads 'Address@[RandomCharacters]', and the Hire Date line reads 'Hire Date: Date@[RandomCharacters]'. How can I make it so it displays the way I want (including the spaces between words and the slashes dividing the month, day, and year)?

Thanks.

Edit: I added the changes the accepted answer suggested, but I'm still running into trouble with the Date line. I used this code:

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

    Date(int eMonth, int eDay, int eYear)
    {
        month = eMonth;
        day = eDay;
        year = eYear;
    }

    public String toString()
    {
        return month + "/" + day + "/" + year;
    }
}

But it still came up with random characters.

Upvotes: 0

Views: 175

Answers (3)

Alex
Alex

Reputation: 271

Your Name, Address, and Date variables that you add to employee are OBJECTS with their own variables. Either override toString() to print out each variable or manually call them via employees[i].name.firstname.

@Override
public String toString(){
    return firstName + lastName;
}

Also you should be using private variables with getter and setter access methods.

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

You need to write a toString method for each of Name, Address and Date. This is what gets called behind the scenes when you try to print any object. Here's what the toString method might look like for the Date class. Have a go at doing the other two for yourself.

public String toString() {
    return month + "/" + day + "/" + year;
}

You might find it useful to read http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() .

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279990

The characters you are seeing are the result of the un-overriden toString() method your class inherits from Object class (since all classes extend from Object). You need to implement your custom toString() method to return the format you want for each of your classes. For example,

class Name
{
    String firstName;
    String lastName;

    Name(String first, String last)
    {
        firstName = first;
        lastName = last;
    }

    public String toString() 
    {
        return "[firstName = " + firstName + ", lastName = " + lastName + "]";
    }
}

Then you can do

Name name = new Name("John", "Doe");
System.out.println(name);

would print

[firstName = John, lastName = Doe]

Upvotes: 1

Related Questions