Results 1 to 9 of 9
  1. #1
    hss02stir is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Question Trying to make this code work...

    Please could someone tell me how to make this code work (what changes to make) and why?
    Java Code:
    public interface ChristmasPresents {
    
    	public void whoNeedsWhat();
    }
    public class People {
    
    	public enum P {Mum,Dad,Jason,Manny}
    	
    	public static P m = P.Mum;
    	public static P d = P.Dad;
    	public static P j = P.Jason;
    	public static P b = P.Manny;
    }
    public class Presents {
    	
    	public String presentOne = "cooking utensils.";
    	public String presentTwo = "science books.";
    	public String presentThree = "a computer.";
    	public String presentFour = "a Hazel.";
    	public String fillIn = " needs ";
    		
    	public String whoNeedsWhat(){
    		return (m) + (fillIn) + (presentOne);
    	}
    }
    Last edited by JosAH; 11-12-2012 at 01:25 PM. Reason: added [code] ... [/code] tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Trying to make this code work...

    What errors are you getting?
    What lines are they on?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    hss02stir is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Trying to make this code work...

    Quote Originally Posted by Tolls View Post
    What errors are you getting?
    What lines are they on?
    Thanks for your response. In the original code, it wasn't recognizing m (line 23) as a variable. I've changed it now (please see below). Is it okay to have enums as strings? I guess it doesn't recognize the enums as strings. What should I do to get a result of "Mum needs cooking utensils."?

    public interface ChristmasPresents {
    public String whoNeedsWhat(People p);
    }
    public class People {

    public enum P {Mum,Dad,Jason,Manny}

    public static P m = P.Mum;
    public static P d = P.Dad;
    public static P j = P.Jason;
    public static P b = P.Manny;
    }
    public class Presents implements ChristmasPresents{

    public String presentOne = "cooking utensils.";
    public String presentTwo = "science books.";
    public String presentThree = "a computer.";
    public String presentFour = "a Hazel.";
    public String fillIn = " needs ";

    public String whoNeedsWhat(People p){

    return p.m + fillIn + presentOne;
    }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Trying to make this code work...

    Please use [code] tags [/code].
    Jos did it for you on your first post.
    Unformatted code is hard to read.

    I'm not sure what you are trying to achieve there.
    Those 'statics' are a bit pointless, as they simply represent the values help in the enum. So you may as well use the enum surely?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    hss02stir is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Trying to make this code work...

    Oh sorry, will do. I'm just trying to practice writing correct code. Please could you show me how you would write this with just the enum without the 'statics'?

    Java Code:
    public interface ChristmasPresents {
    public String whoNeedsWhat(People p);
    }
    public class People {
    
    public enum P {Mum,Dad,Jason,Manny}
    
    public static P m = P.Mum;
    public static P d = P.Dad;
    public static P j = P.Jason;
    public static P b = P.Manny;
    }
    public class Presents implements ChristmasPresents{
    
    public String presentOne = "cooking utensils.";
    public String presentTwo = "science books.";
    public String presentThree = "a computer.";
    public String presentFour = "a Hazel.";
    public String fillIn = " needs ";
    
    public String whoNeedsWhat(People p){
    
    return p.m + fillIn + presentOne;
    }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Trying to make this code work...

    I don't know what it is you are trying to achieve though.
    Not that I would rewrite it anyway...

    You need to explain what it is you expect the code to do, as it is not obvious from this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    hss02stir is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Trying to make this code work...

    Sorry I'm totally new at this. I would like the code to print or return sentences: "Mum needs cooking utensils", "Dad needs science books" etc.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Trying to make this code work...

    I would have a Person class, with a 'name' and 'present' attributes.
    But that really depends on where you are in your learning.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    hss02stir is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

Similar Threads

  1. How do I make my buttons work in the GUI of this code?
    By Interframe in forum AWT / Swing
    Replies: 3
    Last Post: 06-18-2011, 11:12 AM
  2. Allow YOUR work to make a DIFFERENCE!!
    By erictgroup in forum Jobs Offered
    Replies: 0
    Last Post: 06-01-2011, 11:19 PM
  3. Make it work !
    By PhQ in forum New To Java
    Replies: 6
    Last Post: 09-20-2010, 08:22 AM
  4. How do i make this work What am i doing Wrong.
    By Ramaan in forum New To Java
    Replies: 2
    Last Post: 03-01-2010, 11:36 PM
  5. Can't make JTable work -- please help!!
    By cagalli83 in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 09:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •