Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8709607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:24:13+00:00 2026-06-13T04:24:13+00:00

This is Java, using BlueJ. I have four classes called Person, Letter, Address and

  • 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());
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:24:15+00:00Added an answer on June 13, 2026 at 4:24 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this issue: first, I execute a SQL query using Java and then
When I first learned Java, I was using an IDE called BlueJ. It had
This is the code I have in a file called Test2.java in a package
For this i am running BlueJ doing object-oriented java and i have JVM 7.1
I'm using this java script library to show ratings http://www.fyneworks.com/jquery/star-rating/#tab-Overview . I'm using below
Should I implement named parameters in Java using Hash tables? I saw this entry:
I'm trying to bind the dhcpctl library to Java using JNA. This is mi
Below is my Oracle Procedure. When i call this procedure using java, it throws
this is a small program using Java Scanner which reads a file of double
Using Java sockets, I made a simple server. This works because it sends data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.