Hello Deathmonger
Organize your classes into packages. If you add them to the same package then you do not have to import your own classes. See this code:
Name.java
package pack;
import java.io.*;
public class Name {
String first;
String last;
String middle;
public Name() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter first name: ");
first = in.readLine();
System.out.print("Enter last name: ");
last = in.readLine();
System.out.print("Enter middle name: ");
middle = in.readLine();
}
public Name(String firstName, String lastName, String middleName) {
first = firstName;
last = lastName;
middle = middleName;
}
public String firstLast() {
return first + " " + last;
}
public String full() {
return first + " " + middle + " " + last;
}
public String lastFirstMI() {
return last + ", " + first + ", " + middle.substring(0,1) + ".";
}
}
NewNameDriver.java
package pack;
import java.io.*;
public class NewNameDriver {
public static void main(String[] args) throws IOException {
Name testName = new Name();
System.out.println("Name in first-last format is" +
testName.firstLast());
System.out.println("Name in last-first-initial format is" +
testName.lastFirstMI());
}
}
The Java source files need do be in a folder called according to your package. For my code, it would be a folder called "pack". Also, classes that do not have the
public modifier cannot be used by other classes that are not part of the same package.
Please use code tags when posting code. I hope this helped.
