Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: creating constructor

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default creating constructor

    I have a Machine class but I don't know how to create a constructor which takes owner, price, and belly_star parameters.


    main class

    //create machines:
     
    		Machine star_on = new Machine(fix_it_up, generator.nextInt(10), true);
    		Machine star_off = new Machine(fix_it_up, generator.nextInt(10), false);

    Machine class


    public class Machine 
    {
    	boolean belly_star;  //whether the machine changes the star or not
    	int price;
    	int counter;
    	Chappie owner;
     
     
    	/**
    	 * if belly star is true, and sneetch has no star, put on a star
    	 * 
    	 * @return true if the machine is successful, false otherwise
    	 */
    	boolean run(Sneetch s)
    	{
    		if(s.money<price)
    		{
    			return false;
    		}
     
    		if(s.is_starred!=belly_star)
    		{
    			s.changeStar();
    			s.spendMoney(price);
    			owner.collectMoney(price);
    			return true;
    		}
     
    		return false;
    	}
    	/**
    	 * 
    	 * @param raise
    	 * @return price
    	 */
    	public void priceHike(double raise)
    	{
    		price = price*(1 + raise);
     
    	}
     
     
    }


  2. #2

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: creating constructor

    You code them essentially just like a method; however you have no return type and the "method name" is the class name.
    So in your case it would simply be
    public Machine(//parameters go here) {
         //Other operations in here
    }

    It's really pretty straight forward

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: creating constructor

    I'm not sure which one fix_it_up is, but your constructor should look something like this

    public Machine(Fix_It_Up_Class fix_it_up, int number, boolean b)
    {


    }

    Also, maybe I'm missing something, but I noticed another possible problem.

    price = price*(1 + raise);

    price is going to be a double since you have a doube, raise I think, in there.

    Also, even if price were a double, it would seem you never set it to something other than 0 to start with.

    That would mean that price would be 0.0 (if it was double that is)

    and

    0.0 (1 + raise) will always be equal to 0.0 no matter what raise is.

    s.spendMoney(price);
    owner.collectMoney(price);

    Will be spending 0.0 and your owner won't be getting paid anything either.

    Also long as s.money is non-negative, it's always going to skip that first if statement.

    if(s.is_starred!=belly_star)

    belly_star is never initialized.

    By any chance is belly_star the third parameter to your constructor?

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: creating constructor

    The price would possibly instantiated upon creation of the constructor is what I would guess.

Similar Threads

  1. Creating an array in constructor ... defaults all values?
    By mwebb in forum Object Oriented Programming
    Replies: 2
    Last Post: February 19th, 2012, 03:06 PM
  2. Constructor
    By kbbaloch in forum Object Oriented Programming
    Replies: 6
    Last Post: February 1st, 2012, 10:40 PM
  3. Constructor help
    By sambar89 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 26th, 2011, 10:17 PM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM