#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0

    Review of my code- Need help pls!!


    Hello all,

    I need help to figure out how to complete the rest of the code.

    Question

    Write a class called FullName that represents a personís full name. It must have separate fields for title (e.g., Mr., Mrs., Ms.), first name, middle name, and last name. Override the toString() method to return a nicely formatted name. Create as many methods as you think necessary.

    My code

    Code:
    public class FullName {
    	
    	public static String Title;
    	public static String FN;
    	public static String MN;
    	public static String LN;
    	
    	public FullName(String Title, String FN, String MN, String LN){
    		this.Title=Title;
    		this.FN=FN;
    		this.MN=MN;
    		this.LN=LN;	
    		
    		System.out.println("Full name: "+this);
    }
    		
    	
    	public String toString(){
    		return (Title+""+FN+" "+MN+" "+LN);
    	}
    
    
    
    }
    Part B

    Code:
    public class MailingAddress {
    	
    	public static String SA;
    	public static String City;
    	public static String PV;
    	public static String PC;
    	
    	public MailingAddress(String SA, String City, String PV, String PC){
    		this.SA=SA;
    		this.City=City;
    		this.PV=PV;
    		this.PC=PC;	
    		
    		System.out.println(this);
    		
    }
    	
    	public String toString(){
    		return(SA+""+City+" "+PV+" "+PC);
    	}
    
    }
    Part C

    Code:
    public class ShippingLabel {
    
    
    	public static void main(String[]args){
    	
    	FullName name=new FullName("Mr.","George", "G", "Georgian");
    	MailingAddress shipFrom= new MailingAddress("1 Toronto Drive","Toronto","Ontario","M2K 2K2");
    	MailingAddress shipTo= new MailingAddress("1 London Drive","London","Ontario","K1K 1K1");
    	ShippingLabel label = new ShippingLabel();
    	System.out.println(label);
    
    }
    
    	
    	
    }

    I can't seem to figure out how to fully complete the code for part C and I don't know if I am missing anything in part B. Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Part A and B look fine.

    Your shipping label class doesn't contain anything to print. If you want it to print a full label containing a person's full name, from address and to address, then you will need to put references to those objects in the shipping label class. Then you will need to write a toString() method in the shipping label class that will print the information you want when you call System.out.println(label).

    I would give you an example, but that would be basically handing you a solution. You should be able to do it based on what I described above.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    Thanks a lot Cameron. I spent good couple of hours and now I came up with the following code(it's close but not perfect). It prints the result two times.

    Code:
    public String toString(){
    		return(shipFrom+""+shipTo);
    	}
    
    	
    	public static void main(String[]args){
    		
    	
    	FullName name=new FullName("Mr.","George", "G", "Georgian");
    	MailingAddress shipFrom= new MailingAddress("1 Toronto Drive","Toronto","Ontario","M2K 2K2");
    	MailingAddress shipTo= new MailingAddress("1 London Drive","London","Ontario","K1K 1K1");
    	ShippingLabel label = new ShippingLabel("Ship from:"+shipFrom,"Ship to:"+shipTo);
    	System.out.println(label);
    
    }
    
    	
    	
    }

    Outcome:

    Full name: Mr.George G Georgian
    1 Toronto DriveToronto Ontario M2K 2K2
    1 London DriveLondon Ontario K1K 1K1
    Ship from:1 London DriveLondon Ontario K1K 1K1Ship to:1 London DriveLondon Ontario K1K 1K1
    Ship from:1 London DriveLondon Ontario K1K 1K1Ship to:1 London DriveLondon Ontario K1K 1K1
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Check your class A and B for print statements that shouldn't be there.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    Done. Awesome the code looks a lot better so does the outcome. I get the same address for both "from" and "to". Anything I can do to fix it?


    outcome:

    Full name: Mr.George G Georgian
    Ship from:1 London Drive London Ontario K1K 1K1 Ship to:1 London Drive London Ontario K1K 1K1
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Do you have a constructor for your shipping label class?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    Yes, I do. Find the code below:

    Code:
    public ShippingLabel(String shipFrom, String shipTo){
    		
    		this.shipFrom=shipFrom;
    		this.shipTo=shipTo;
    		
    		
    	}
  14. #8
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,739
    Rep Power
    1959
    Try look in your constructor for ShippingLabel (you didn't post that).

    [EDIT]
    The constructor looks fine.
  16. #9
  17. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,739
    Rep Power
    1959
    look in you Class A and Class B.
    You made all the variables static, which means all classes you create share and edit the same variables.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Can you post the class in its entirety or edit your first post with the updated class information. It's hard to debug something when I can't see the full picture

    Edit: Nevermind. Mr. Fujin found the problem. Kudos!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    YOU GUYS ARE AWESOME . Not only you helped me to add to my knowledge today, but you also walked me through to debug my code. I removed the static and the outcome turn how to be perfect.

    "Full name: Mr.George G Georgian
    Ship from:1 Toronto Drive Toronto Ontario M2K 2K2 Ship to:1 London Drive London Ontario K1K 1K1
    "

    I have been taking courses through school to learn Java, however the material they teach is useless. Without material online, I could not learn a thing. Do you have any tips for me or sources that I can learn more about Java programming?
  22. #12
  23. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,739
    Rep Power
    1959
    Originally Posted by javastudent100
    Do you have any tips for me or sources that I can learn more about Java programming?
    If you look at the top of the Java forum, you will find several "sticky" posts with information and links to external resources/guides.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    Thanks again.
    Cheers!

IMN logo majestic logo threadwatch logo seochat tools logo