Results 1 to 12 of 12

Thread: Constructors

  1. #1
    MainStream is offline Member
    Join Date
    Jun 2014
    Posts
    28
    Rep Power
    0

    Unhappy Constructors

    Straight to the point

    Java Code:
    ublic class Puppy{
       
       int puppyAge;
    
       public Puppy(String name){
          // This constructor has one parameter, name.
          System.out.println("Passed Name is :" + name ); 
       }
       public void setAge( int age ){
           puppyAge = age;
       }
    
       public int getAge( ){
           System.out.println("Puppy's age is :" + puppyAge ); 
           return puppyAge;
       }
       public static void main(String []args){
          /* Object creation */
          Puppy myPuppy = new Puppy( "tommy", "gerald", "antoine" );
    
          /* Call class method to set puppy's age */
          myPuppy.setAge( 2 );
    
          /* Call another class method to get puppy's age */
          myPuppy.getAge( );
    
          /* You can access instance variable as follows as well */
          System.out.println("Variable Value :" + myPuppy.puppyAge ); 
       }
    }
    This code has 3 names in it.

    My code is

    Java Code:
    import java.io.*;
    
    public class Puppy 
    {
    	int puppyAge;
    	String puppyName;
    	
    	public Puppy()
    	{
    	}
    	
    	public void setName(String name)
    	{
    		puppyName = name;
    	}
    	
    	public String getName()
    	{
    		System.out.println("Puppy's Name is: " + puppyName);
    		return puppyName;
    	}
    	
    	public void setAge(int age)
    	{
    		puppyAge = age;
    	}
    	
    	public int getAge()
    	{
    		System.out.print("Puppy's age is : " + puppyAge);
    		return puppyAge;
    	}
    public static void main(String []args)throws IOException
    	{
    		BufferedReader CSIT = new BufferedReader(new InputStreamReader(System.in));
    	      Puppy myPuppy = new Puppy();
    
    	      System.out.println("Input Name of Puppy: ");
    	      myPuppy.setName(CSIT.readLine());
    
    	      System.out.println("Input Age of Puppy: ");
    	      myPuppy.setAge(Integer.parseInt(CSIT.readLine())); 
    	   }
    How do I put 3 values of the each variable without replacing the last inputted one?

    Like when I input "Tommy" and input another name "Gerald", "Tommy" won't be replaced by "Gerald" when I input again.
    Last edited by MainStream; 10-02-2014 at 05:33 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Constructors

    Straight to the point: you can't ask questions about using the Puppy class without showing the Puppy class.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Constructors

    I really don't know what you're asking. Are you trying to create 3 puppies with different names? Are you trying to create a single Puppy with 3 names? Something else entirely?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  4. #4
    MainStream is offline Member
    Join Date
    Jun 2014
    Posts
    28
    Rep Power
    0

    Default Re: Constructors

    Edited my post :) Sorry about that.

    @Kevin yes I want 3 puppies with 3 different names.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructors

    Quote Originally Posted by MainStream View Post
    How do I put 3 values of the each variable without replacing the last inputted one?
    You don't! That would be a Puppies class and not a Puppy class. Best to have an array
    or list of the Pupply class where each instance of Puppy is a single puppy. If you want to
    have a Puppies class, then maintain an internal array or list or map and have the setter append
    the data.

    However, there appears to be a flaw in your class design anyway. You apparently prompt for an
    age for a given puppy name when setting them. But your constructor includes no such age value.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    MainStream is offline Member
    Join Date
    Jun 2014
    Posts
    28
    Rep Power
    0

    Default Re: Constructors

    Quote Originally Posted by jim829 View Post
    You don't! That would be a Puppies class and not a Puppy class. Best to have an array
    or list of the Pupply class where each instance of Puppy is a single puppy. If you want to
    have a Puppies class, then maintain an internal array or list or map and have the setter append
    the data.

    However, there appears to be a flaw in your class design anyway. You apparently prompt for an
    age for a given puppy name when setting them. But your constructor includes no such age value.

    Regards,
    Jim
    I'm currently lost with codes that have predefined values and turning them into a code that needs the values inputted. :/ Can you give me a sample code of a code that has a predefined code and turned it into one that can input values? I've searched everywhere on the web and not a single code was converted into inputs.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructors

    Quote Originally Posted by MainStream View Post
    I'm currently lost with codes that have predefined values and turning them into a code that needs the values inputted. :/ Can you give me a sample code of a code that has a predefined code and turned it into one that can input values?
    Sorry but I don't understand what you are asking? What do you mean "predefined values" and " turning them into code?"

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    MainStream is offline Member
    Join Date
    Jun 2014
    Posts
    28
    Rep Power
    0

    Default Re: Constructors

    Quote Originally Posted by jim829 View Post
    Sorry but I don't understand what you are asking? What do you mean "predefined values" and " turning them into code?"

    Regards,
    Jim
    Java Code:
    Puppy myPuppy = new Puppy( "tommy", "gerald", "antoine" );
    This object has 3 predefined names.

    Which I need a code to make those name inputted instead of predefined, sorry if my english is bad :(

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Constructors

    If you want three different puppies, you have to create 3 different instances of the Puppy class. You create an instance of a class by calling the new keyword. Each Puppy has a single name, which you pass into the constructor.

    Hint: how many times do you think you should call the constructor using the new keyword? How many arguments should the constructor take?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructors

    I thought you were doing just that with your readLine() statement and using your setter. I explained how you
    might accomplish this for more than one name. Are you familiar with arrays, lists, and or maps? You would
    also need to use a for loop for multiple prompts.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    MainStream is offline Member
    Join Date
    Jun 2014
    Posts
    28
    Rep Power
    0

    Default Re: Constructors

    Ok thx guys :) Arrays or new instances it is.

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Constructors

    Just to be complete, are there always going to be exactly 3 puppies, or can there potentially be more?

    If exactly 3 an array will indeed work just fine. But if the number is "any" (can be 1, can be 20 puppies), you might want to start with an array first for learning purposes and then look into replacing the array with an ArrayList which does not have one specific size.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Constructors
    By zhider in forum New To Java
    Replies: 5
    Last Post: 11-13-2012, 03:43 AM
  2. Help with constructors
    By philip1597 in forum New To Java
    Replies: 7
    Last Post: 08-31-2012, 01:56 PM
  3. constructors
    By droidus in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 08:14 PM
  4. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

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