This Java program is made up of two files.
The file "Entry.java" holds the definition of class Entry
and the file "DirectoryTest.java" holds the main program that uses the Entry class to
define its objects.
"The class "Entry" contains 4 strings as instance variables, a constructor and the
methods to access and modify the instance variables.
-----------------------------------------------------------------------------------------------------------
/**********************Entry.java***************************************/
import java.io.*;
public class Entry{
//instance variables
String name;
String address;
String email;
String phone;
//constructor
public Entry(String Name, String Address, String Email, String Phone)
{
this.name = Name;
this.address = Address;
this.email = Email;
this.phone = Phone;
}
//methods
public void name(String Name){
this.name = Name;
}
public void address(String Address){
this.address = Address;
}
public void email(String Email){
this.email = Email;
}
public void phone(String Phone){
this.phone = Phone;
}
public void Print()
{
if(this.name != null)
System.out.println("\n Name: " + this.name + " \n Address: " + this.address+
" \n email: " + this.email + "\n phone: " + this.phone);
else
System.out.println( "cell empty" );
}
}
--------------------------------------------------------------------------------------------------------------
This is a simple Directory test program that creates an object of class Entry and
initializes it during it's creation. It then calls the print function of class Entry to print
the contents of the object.
-------------------------------------------------------------------------------------------------------------
/**********************DirectoryTest.java***********************************/
import java.io.*;
public class DirectoryTest
{
public static void main(String args[])
{
Entry Directory1= new Entry
("suchita"," 1234 Timothy TX", "suchita@yahoo.com","512 567 8904");
Directory1.Print();
}
}
-----------------------------------------------------------------------------------------------------------
After compiling both the java files and then running the Directory.test, the result:
-----------------------------------------------------------------------------------------------------------
Name: suchita
Address: 1234 Timothy MA
email: suchita@yahoo.com
phone: 512 567 8904
-----------------------------------------------------------------------------------------------------------
Here is a modified version of DirectoryTest.java that creates an array of objects of
class Entry with user input data.
------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.Scanner;
public class DirectoryTest
{
public static void main(String args[])
{
Scanner scan = new Scanner( System.in );
Entry[] NewDirectory = new Entry[3];
String nameIn, addressIn, emailIn, phoneIn;
for (int i=0;i < (NewDirectory.length);i++)
{
System.out.println(" Enter Name: "); nameIn = scan.nextLine();
System.out.println(" Enter Street Address: "); addressIn = scan.nextLine();
System.out.println(" Enter email address: "); emailIn = scan.nextLine();
System.out.println(" Enter phone number: "); phoneIn = scan.nextLine();
Entry temp= new Entry(nameIn, addressIn, emailIn, phoneIn);
NewDirectory[i] = temp;
}
System.out.println("The directory is full.");
for (int i=0;i < (NewDirectory.length);i++)
NewDirectory[i].Print();
}
}
----------------------------------------------------------------------------------------------------------
The file "Entry.java" holds the definition of class Entry
and the file "DirectoryTest.java" holds the main program that uses the Entry class to
define its objects.
"The class "Entry" contains 4 strings as instance variables, a constructor and the
methods to access and modify the instance variables.
-----------------------------------------------------------------------------------------------------------
/**********************Entry.java***************************************/
import java.io.*;
public class Entry{
//instance variables
String name;
String address;
String email;
String phone;
//constructor
public Entry(String Name, String Address, String Email, String Phone)
{
this.name = Name;
this.address = Address;
this.email = Email;
this.phone = Phone;
}
//methods
public void name(String Name){
this.name = Name;
}
public void address(String Address){
this.address = Address;
}
public void email(String Email){
this.email = Email;
}
public void phone(String Phone){
this.phone = Phone;
}
public void Print()
{
if(this.name != null)
System.out.println("\n Name: " + this.name + " \n Address: " + this.address+
" \n email: " + this.email + "\n phone: " + this.phone);
else
System.out.println( "cell empty" );
}
}
--------------------------------------------------------------------------------------------------------------
This is a simple Directory test program that creates an object of class Entry and
initializes it during it's creation. It then calls the print function of class Entry to print
the contents of the object.
-------------------------------------------------------------------------------------------------------------
/**********************DirectoryTest.java***********************************/
import java.io.*;
public class DirectoryTest
{
public static void main(String args[])
{
Entry Directory1= new Entry
("suchita"," 1234 Timothy TX", "suchita@yahoo.com","512 567 8904");
Directory1.Print();
}
}
-----------------------------------------------------------------------------------------------------------
After compiling both the java files and then running the Directory.test, the result:
-----------------------------------------------------------------------------------------------------------
Name: suchita
Address: 1234 Timothy MA
email: suchita@yahoo.com
phone: 512 567 8904
-----------------------------------------------------------------------------------------------------------
Here is a modified version of DirectoryTest.java that creates an array of objects of
class Entry with user input data.
------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.Scanner;
public class DirectoryTest
{
public static void main(String args[])
{
Scanner scan = new Scanner( System.in );
Entry[] NewDirectory = new Entry[3];
String nameIn, addressIn, emailIn, phoneIn;
for (int i=0;i < (NewDirectory.length);i++)
{
System.out.println(" Enter Name: "); nameIn = scan.nextLine();
System.out.println(" Enter Street Address: "); addressIn = scan.nextLine();
System.out.println(" Enter email address: "); emailIn = scan.nextLine();
System.out.println(" Enter phone number: "); phoneIn = scan.nextLine();
Entry temp= new Entry(nameIn, addressIn, emailIn, phoneIn);
NewDirectory[i] = temp;
}
System.out.println("The directory is full.");
for (int i=0;i < (NewDirectory.length);i++)
NewDirectory[i].Print();
}
}
----------------------------------------------------------------------------------------------------------