Heres my program outline and the code i have written so far im having trouble with the toString im reall not sure about them and the constructors in the AddressBookEntry i have not started the drivers yet i just didnt want to leave out any information
4. Constructor Class - Address
a. String variables for street, city, state, and zip code.
b. Constructor to accept street, city, state, zip code and set variables
c. Methods to get each variable.
d. Method to return a string for Address object.
5. Constructor Class - AddressBookEntry
a. Instance variable for name
b. Instance variable for address, using the Address Class.
c. Static variable to calculate the number of entries created.
d. Constructors: (overload the constructor)
i. Constructor to accept a name and Address object
ii. Constructor to accept a name, street, city, state, and zip code.
e. Methods:
i. Methods that set the address (overload the method) - set the address by accepting an Address object - set the address by accepting variables for street, city, state, and zip.
ii. Method that gets the address, using the Address class
iii. Method to return a string for AddressBookEntry object.
iv. Static method that returns the number of entries created.
6. Driver classes - Code 2 driver classes to create the address book
a. User enters name, street, city, state, and zip code.
b. First driver will create address book, using name, address, street, city, zip
c. Second driver will create address book, using name and Address class
d. The application will then display the address book entry and total number of addresses.
e. The user can then continue with the application or exit.
f. Include your full name in the title bar for all JOption windows
=============================== Address - Constructor Class code ================================
package program4;
/**
*
* Java Programming I
* Java Program #4
* Due Date: May 7,2004
* Constructor Class Defines an Object
*/
publicclass Address {
private String street;
private String city;
private String state;
private String zip;
public Address(String addressStreet, String addressCity, String addressState, String addressZip) {
street = addressStreet;
city = addressCity;
state = addressState;
zip = addressZip;
}
public String getStreet(){
return street;
}
public String getCity(){
return city;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
public String toString() {
return street +
city +
state +
zip;
}
}
===================== This is my code so far for AddressBookEntry Constructor class. This is where i have gotten so far im not at all sure about the toString or the Constructors to accept Address object ========================================================
package program4;
/*
*
* Java Programming I
* Java Program #4
* Due Date: May 7, 2004
* Constructor Class Defines an Object
*/
publicclass AddBookEntry {
private String name;
private Address addy;
privatestaticint numEntries = 0;
public AddBookEntry(String name, Address addy) {
name = " ";
setAddy();
}
public AddBookEntry(String name, String street, String city,String state, String zip) {
}
publicvoid setAddress(int numEntries, String street, String city, String state, String zip) {
numEntries = numEntries++;
addy = new Address(street,city,state,zip);
}
publicvoid setAddress(Address addressAdd, int numEntries) {
numEntries = numEntries++;
addy = addressAdd;
}
public Address getAddress() {
return addy;
}
public String toString() {
return name;
}
publicint getNumEntries() {
return numEntries;
}
}
a. Your Address class looks good to me except for the toString() method b. The toString() method looks OK, except that you don't want your data to all run together. Something like this might be better.
public String toString() {
return "Street: " + street +
"City: " + city +
"State: " + state +
"Zip: " + zip;
}
The other class, however, needs help in several areas. Here is what I would do. Note the comments.
publicclass AddBookEntry {
private String name;
private Address addy;
privatestaticint numEntries = 0;
public AddBookEntry(String nm, Address ad) {//Note name changes!!!!!
name = nm;//not a good idea to have instance variable and parameter with the same name.
setAddy(ad);//setAddy needs a parameter!
}
public AddBookEntry(String name, String street, String city,String state, String zip) {
this(name, new Address(street, city, state, zip);//sends data to the other constructor.
}
publicvoid setAddress(int numEntries, String street, String city, String state, String zip) {
numEntries++; //this is the same as what you had, but shorter
addy = new Address(street,city,state,zip);
/*
OR DO THIS INSTEAD OF THE PREVIOUS TWO LINES
this.setAddress(new Address(street,city,state,zip));//sends data to other overloaded method.
*/
}
publicvoid setAddress(Address addressAdd) {//did not need other parameter
numEntries++;
addy = addressAdd;
}
public Address getAddress() {
return addy;
}
public String toString() {
return "Name: " + name + addy.toString();//address is part of this object
}
publicstaticint getNumEntries() {//static variable should have static method. Stated in your directions.
return numEntries;
}
I think both of my constructor classes are complete im just posting them to be sure here is my first driver class im having trouble with the coding for the output window
Address - Constructor class 1 =============================
package program4;
/**
*
* Java Programming I
* Java Program #4
* Due Date: May 7,2004
* Address - Constructor Class Defines an Object
*/
publicclass Address {
private String street;
private String city;
private String state;
private String zip;
public Address(String addressStreet, String addressCity, String addressState, String addressZip) {
street = addressStreet;
city = addressCity;
state = addressState;
zip = addressZip;
}
public String getStreet(){
return street;
}
public String getCity(){
return city;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
public String toString() {
return "Address: \n" +
street + "\n" +
city + "," + state + "\n" + "\t\t\t" + zip;
}
}
[java]
AddBookEntry Constructor class 2
================================
[java]
package program4;
/*
*
* Java Programming I
* Java Program #4
* Due Date: May 7, 2004
* AddBookEntry - Constructor Class Defines an Object, Overloaded Constructor and
* Overloaded Method, Calculates Number of Entries
*/
publicclass AddBookEntry {
private String name;
private Address addy;
privatestaticint numEntries = 0;
public AddBookEntry(String addressName, Address addressAdd) {
name = addressName;
setAddress(addressAdd);
}
public AddBookEntry(String street, String city, String state, String zip, String name) {
this(name, new Address(street, city, state, zip));
}
publicvoid setAddress(int numEntries, String street, String city, String state, String zip) {
this.setAddress(new Address(street,city,state,zip));
}
publicvoid setAddress(Address addressAdd) {
numEntries++;
addy = addressAdd;
}
public Address getAddress() {
return addy;
}
public String getName() {
return name;
}
public String toString() {
String create = "Name: " + name + addy.toString() +
"Number of Entries:" + "\t" + numEntries + "\n";
return create;
}
publicstaticint getNumEntries() {
return numEntries;
}
}
Driver class one AddApp1 ==========================
package program4;
import javax.swing.*;
/**
*
* Java Programming I
* Java Program #4
* AddApp1 - Driver class creates the address book entry
* using name and address object
*/
publicclass AddApp1 {
publicstaticvoid main(String[] args) {
try {
String choice = " ";
while(!(choice.equalsIgnoreCase("x"))) {
String name = JOptionPane.showInputDialog
( "Enter a Name: ");
String street = JOptionPane.showInputDialog
( "Enter Street Address: ");
String city = JOptionPane.showInputDialog
( "Enter City: ");
String state = JOptionPane.showInputDialog
( "Enter State: ");
String zip = JOptionPane.showInputDialog
( "Enter Zip Code: ");
Address create = new Address(street,city,state,zip);
String message = create + "Press Enter to continue or 'x' to exit:";
choice = JOptionPane.showInputDialog(null, message, "Benjamin Shively",JOptionPane.PLAIN_MESSAGE);
}
}
catch (NullPointerException e) {
System.exit(0);
}
System.exit(0);
}
}
I cant seem to get the name output this driver should create the address book using the name and address object
i have tried a few differet steps but all the out put i get is the main address
>I cant seem to get the name output this driver should create the address book using the name and address object That is because you have created an Address object instead of an AddBookEntry object. The Address object doesn't store the name, but the AddBookEntry object does.
> I made some of the changes you recommended im not sure on the setAddy i changed it to setAddress That is a much better name. Names should be as self-explanatory as possible, but setAddy left the reader wondering "what is an Addy?" The name setAddress makes it obvious what the method is doing.
While we are on the topic of names, if I were you, I would consider renaming the AddBookEntry class. A method can have a verb like Add in the name, because a method does something. Think of your other method names - setThis(), getThat() - all contain verbs. But a class defines what an object is, so its name should be a noun. If Add is supposed to be short for Address, I'd go with the longer version. It would be more self-explanatory.
As long as I'm on the topic of names, I'm sure that you are getting the name "Constructor class" from your instructor. You can't change the terminology that he/she uses, but for your own education, let me suggest what is IMHO, a better way to describe that type of class.
What you are calling Constructor classes, I would call "object" classes since they define objects. Technically, ALL classes have constructors. If you don't write one, the compiler creates one for you. It will even create one for your driver class, even though that class doesn't really need one. However, the driver class does NOT define an object, so calling it a driver is more appropriate.
Don't try to get your teacher to change. But if you keep that in the back of your mind, Java might make more sense.
I took somemore of your suggestion and i finished my Address book i think i made this program harder then it was, but then again alot of what the teachers says is confusing then how you explin things makes a bit better sense
Thx a ton for all the help
Actually i have one more program to write for my final lol
if theres anyway i can repay ya let me know if your familiar with IRC i have a small server dont wanna spam the board just email me if ya want to stop by sometime
>if theres anyway i can repay ya let me know I'm glad I could help. Don't worry about "repayment." I've believed in the "Pay It Forward" approach since long before the movie of that name popularized the expression. Other's helped me (and still do). Do the same when you are able.
i think i made this program harder then it was Actually, I liked the general design of your program. Many of the programs that I see here try to put everything in the main method of the driver class. Creating an Address class and an AddressBookEntry class makes sense from an OOP point of view.
The next logical step would be to create an AddressBook class that can organize and store AddressBookEntry objects. Perhaps that would be followed by an AddressBookGUI class that can take input and display everything visually. Of course, those probably involve concepts that you haven't studied yet.
I also appreciate the fact that you've clearly done a lot of work on your own before asking for help. More than a few people have simply posted their homework assignment like they'd post a want-ad. I don't need any more homework than I already have from my job, but I don't mind helping someone who is clearly trying to help themself.
how you explin things makes a bit better sense I have the advantage of seeing where you are having problems and what you did not understand. I can focus my explanations only on those areas.
Actually i have one more program to write for my final lol If you run into trouble, post.