0

This is Java, using BlueJ.

I have four classes called Person, Letter, Address and PhoneNumber. In each, I override the toString() method to return a concatenated string of the values I want from the class. When calling the Letter toString(), it is returning null on all values.

The idea is to use the hard coded information, pass it into the appropriate class, and return it in a standard letter format.

Am I headed in the right direction for printing out the information hard coded, or should I go a different route? This is a homework problem, but I feel I have hit a brick wall.

Here are the classes:

public class Person
{
private static String aPerson;
private String first;
private String middle;
private String last;    
private Address address;
private PhoneNumber phone;

public String getFirst()
{
    return this.first;
}

public void setFirst(String FirstName)
{
    this.first = FirstName;
}

    public String getMiddle()
{
    return this.middle;
}

public void setMiddle(String MiddleName)
{
    this.middle = MiddleName;
}

    public String getLast()
{
    return this.last;
}

public void setLast(String LastName)
{
    this.last = LastName;
}

public Address getMyAddress()
{
    return this.address;
}
public void setMyAddress(Address Address)
{
    this.address = Address;
}

public PhoneNumber getMyPhoneNum()
{
    return this.phone;
}

public void setMyPhoneNum(PhoneNumber Number)
{
    this.phone = Number;
}

public Person()
{
    aPerson = getFirst() + getMiddle() + getLast() + getMyAddress() + 
    getMyPhoneNum();
}

public String toString()
{
    return aPerson;
}
}

PhoneNumber:

public class PhoneNumber
{
private String number;
private int areaCode = 0;
private int phonePrefix = 0;
private int phoneLineNum = 0;
private int phoneExtension = 0;

public String getNumber()
{
    return number;
}

public void setNumber(String Number)
{
    number = Number;
}

public int getAreaCode()
{
    return areaCode;
}

public void setAreaCode(int AreaCode)
{
    areaCode = AreaCode;
}

public int getPrefix()
{
    return phonePrefix;
}

public void setPrefix(int Prefix)
{
    phonePrefix = Prefix;
}

public int getPhoneLineNumber()
{
    return phoneLineNum;
}

public void setLineNum(int PhoneNumber)
{
    phoneLineNum = PhoneNumber;
}

public int getExtension()
{
    return phoneExtension;
}

public void setExtension(int Extension)
{
    phoneExtension = Extension;
}
}

Address:

public class Address
{
private String state;
private String anAddress;
private String address;
private String city;
private int zip = 0;

public String getState()
{
   return state;
}

public void setState(String State)
{
   state = State;
}

   public String getAddress()
{
   return address;
}

   public void setAddress(String Address)
{
   address = Address;
}

   public String getCity()
{
   return city;
}

   public void setCity(String City)
{
   city = City;
}

   public int getZip()
{
   return zip;
}

   public void setZip(int Zip)
{
   zip = Zip;
}

public Address()
{
   anAddress = getState() + getAddress() + getCity() + getZip();
}

public String toString()
{
   return this.anAddress;
}
}

Letter:

public class Letter
{
private Person to;
private Person from;
private String body;
private String finishedLetter;

public Person getTo()
{
    return to;
}

public void setTo(Person newValue)
{
    to = newValue;
}

public Person getFrom()
{
    return from;
}

public void setFrom(Person newValue)
{
    from = newValue;
}

public String getBody()
{
    return body;
}

public void setBody(String newValue)
{
    body = newValue;
}

public Letter()
{
    finishedLetter = getTo() + " \n" + getFrom() + " \n" + getBody();
}

public String toString()
{
    return finishedLetter;
}
}

And main:

public class MainClass
{
public static void main(String args[])
{
    PhoneNumber phone1 = new PhoneNumber();
    phone1.setAreaCode(417);
    phone1.setPrefix(447);
    phone1.setLineNum(7533);
    phone1.setExtension(0);

    PhoneNumber phone2 = new PhoneNumber();
    phone2.setAreaCode(210);
    phone2.setPrefix(336);
    phone2.setLineNum(4343);
    phone2.setExtension(9850);

    Address address1 = new Address();

    address1.setState("MO");
    address1.setAddress("1001 East Chestnut Expressway");
    address1.setCity("Springfield");
    address1.setZip(65807);

    Address address2 = new Address();

    address2.setState("TX");
    address2.setAddress("4800 Calhoun Road");
    address2.setCity("Houston");
    address2.setZip(77004);

    Person person1 = new Person();

    person1.setFirst("Shane");
    person1.setMiddle("Carroll");
    person1.setLast("May");
    person1.setMyAddress(address1);
    person1.setMyPhoneNum(phone1);

    Person person2 = new Person();

    person2.setFirst("Ted");
    person2.setMiddle("Anthony");
    person2.setLast("Nugent");
    person2.setMyAddress(address2);
    person2.setMyPhoneNum(phone2);

    Letter aLetter = new Letter();

    aLetter.setTo(person2);
    aLetter.setFrom(person1);
    aLetter.setBody("This is the body");

    System.out.println(aLetter.toString());
}
}
Joshua Duncan
  • 13
  • 1
  • 3
  • 2
    You're asking for free advice and as such should put in the effort to make it as easy as possible for the volunteers to help you. Please post your *pertinent* code here. Don't force us to dig through the internet or through a large code base to find it. Thanks in advance for your cooperation in this. – Hovercraft Full Of Eels Oct 19 '12 at 02:39
  • Have you at least debugged your code? By the way, This is StackOverflow. – Luiggi Mendoza Oct 19 '12 at 02:40
  • 1
    In the meantime, I've suggested an edit and moved your code from pastebin to here directly. Whenever possible, it is always preferable to offer your problematic code snippets here. If you feel that the snippet is too long to post here... that means that it probably is, and minimize the amount of code you need to show by focusing your problem. – Anthony Neace Oct 19 '12 at 02:41
  • 2
    @HyperAnthony the problem with OP code is that showing the whole problem won't solve his/her problem. OP must provide a [SSCCE](http://sscce.org) and at least show some respect to him/herself and make a debug to know what's happening. – Luiggi Mendoza Oct 19 '12 at 02:42
  • @LuiggiMendoza I understand that (and edited to make that point while you were commenting), but my suggestion was more to make OP familiar with getting code up on here in the first place. I think you and Hovercraft Full of Eels have the other point covered. :) – Anthony Neace Oct 19 '12 at 02:43
  • Where is Joshua in all of this discussion? You're being kind of quiet. – Hovercraft Full Of Eels Oct 19 '12 at 02:48
  • 1
    Again, you shouldn't post all of your code as most of it is not relevant to the problem at hand, and many will comment tl;dr (too long; didn't read). Instead do some debugging to isolate the problem and either post pertinent code, or has been suggested, an [sscce](http://sscce.org). – Hovercraft Full Of Eels Oct 19 '12 at 02:49
  • 2
    And I was quite because I was trying the solution. Sorry for all the code. I will debug more next time and post properly. – Joshua Duncan Oct 19 '12 at 03:00

2 Answers2

6

Your Letter constructor is calling methods such as getTo() and getFrom() before those fields have been filled. Don't do this since your finishedLetter String will never be correctly "finished". i.e.,

public Letter()
{
    finishedLetter = getTo() + " \n" + getFrom() + " \n" + getBody();
}

will always result in null + "\n" + null + "\n" + null

Perhaps that sort of code should be in the toString() method instead.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

When your letter is constructed using new Letter(), it initializes its instance field finishedLetter with several null values. Because to, from, and body haven't yet been set with their corresponding setters, their getters return null, resulting in finishedLetter being equal to "null \nnull \nnull".

To fix this, I one approach is to define the finishedLetter in the toString() method itself. This will both fix the issue and take a more object-oriented approach to the program design.

// remove constructor (if you wish) and finishedLetter field

public String toString() {
    return getTo() + " \n" + getFrom() + " \n" + getBody();
}

An even better approach is to require to, from, and body, as parameters in the Letter constructor.

// remove finishedLetter field

public Letter(Person to, Person from, String body) {
    this.to = to;
    this.from = from;
    this.body = body;
}

public String toString() {
    return getTo() + " \n" + getFrom() + " \n" + getBody();
}
FThompson
  • 28,352
  • 13
  • 60
  • 93