0

I'll try to explain this as clearly as I can. I am trying to narrow down my code for an assignment by turning three of the classes used into a package that the program will access through an import statement. Here is my original 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;
    }

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

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;
    }

    public String toString()
    {
        return eStreet + " " + eCity + " " + eState + " " + eZipCode;
    }
}

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;
    }
}

I need to take the Name class, the Address class, and the Date class and put them into a package called util. First of all, I organize all of my java programs in a folder called My Java Programs (C:\MyJavaPrograms) as I was instructed to do so. To create what I think is supposed to be the package, I copied each class and put them into their own individual file, I put the statement 'package.util' at the top, and then I named them Name.java, Address.java, and Date.java and I put them in a folder called util, which is located in C:\MyJavaPrograms.

Afterwards, I put the statement 'import util.*;' at the top of my original code and put it in C:\MyJavaPrograms. For one reason or another, the file won't compile. I've already done a lot of googling for my problem, including reading up on this, which doesn't help me, at least to the extent that I understand what is wrong.

I suspect that there is a problem with my classpath, although I do not have any idea what I can do to fix it. If it helps with figuring out the problem, I do know that I can't use the javac command in the command prompt without going to this thread and typing in one of the responses.

Ideally, I would want to compile my new assignment and have it do exactly what the code I posted at the top of this question would do now, except it does it while utilizing a pacakage with the extra classes. I would really appreciate it if someone could send me in the right direction.

Community
  • 1
  • 1
user2709168
  • 117
  • 1
  • 14
  • What is the compilation error that you are getting? – Jason Oct 04 '13 at 03:54
  • I'm guessing the compile error might have been from failing to put the source files into the right package subfolders. See my response below. If that's not the case, plesae post the exact error message. – paulsm4 Oct 04 '13 at 04:07
  • @Jason 'error: package does not exist' then it displays my statement 'import util.*;' – user2709168 Oct 04 '13 at 16:33

2 Answers2

0
  1. Check your package definition (the first line in each .java file). It should be 'package util;' not 'package.util'.
  2. Are the classes in your util package marked as public?
Jason
  • 11,744
  • 3
  • 42
  • 46
  • I might have mistyped in my post, but I did put 'package util;' and my classes are marked as public. – user2709168 Oct 04 '13 at 16:35
  • So, your main class is in c:\MyJavaPrograms and your util classes are in c:\MyJavaPrograms\util and you are running javac from c:\MyJavaPrograms? – Jason Oct 10 '13 at 03:47
0

1) Breaking classes into separate files, and organizing the files into a package, is a Good Thing.

2) Suppose you wanted to break your classes into "Assignment10.java" and "Util.java". They could both be in package "com.myclass". You'd do the following:

a) Create the files Assignment10.java and Util.java

b) Put "package com.myclass;" at the top of both

c) Create a new directory "com\", and subdirectory "myclass\"

d) Copy both *.java files into com\myclass

e) Compile and run from your root directory

3) If you were using an IDE (like Eclipse or Netbeans), it will automatically create the directories for you

Here are a couple of good tutorials:

----------------------------- ADDENDUM -----------------------------

To compile and run a Java program with packages from the command line:

1) Create package folder:

mkdir myclass\util

2) Create .java files

cd myclass\util
notepad AssignmentTen.java =>

package myclass.util;

public class AssignmentTen {

  public static void main (String[] args) {
    System.out.println ("In main...");
    Name name = new Name("Jack", "Torrance");
    System.out.println ("Exiting main.");
  }

}

notepad Name.java =>

package myclass.util;

public class Name {

  public Name (String first, String last) {
    System.out.println ("My name is " + first + " " + last + "...");
  }

}

3) Compile

javac *.java
dir =>

10/04/2013  10:38 AM               589 AssignmentTen.class
10/04/2013  10:36 AM               248 AssignmentTen.java
10/04/2013  10:38 AM               593 Name.class
10/04/2013  10:35 AM               177 Name.java

4) Execute

cd ..\..
java myclass.util.AssignmentTen =>

In main...
My name is Jack Torrance...
Exiting main.
Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190