Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default [SOLVED] Noobie Help! Constructors

    Hey All,

    I'm new to java-forums.org and I am currently studying java. I have an assignment issue, and would appreciate some help or pointers in the right direction. I must admit that I am finding its concepts really difficult, and I have never been naturally good at solving these type of issues.

    I understand the basic concept of constructors, that they are a way of initialising newly created objects, and I can understand simple examples, but when we add extra variables etc I am starting to get very confused. If they were asking for a simple example, e.g setting position or colour for all Frogs I would not have a problem.

    If you need further information please let me know, I'm after pointers or any help you guys / gals can provide.

    All the best - FMJ.

    Below is the question and the code:

    Write code in the FrogCalculator class to modify the signature of the default
    constructor for FrogCalculator such that it takes four arguments of type Frog. The positions of the two Frog instances referenced by the first and second arguments represent the first and second operands, respectively. The positions of the two Frog instances referenced by the third and fourth arguments represent the result of the calculation: the third Frog instance being the ‘tens’ frog and the fourth Frog instance being the ‘units’ frog.
    The constructor should assign the arguments directly to the four corresponding private instance variables and then set the frog referenced by tensFrog to brown and the frog referenced by unitsFrog to yellow.

    The code is:

    public class FrogCalculator
    {
    /* instance variables */

    private Frog operand1Frog;
    private Frog operand2Frog;
    private Frog unitsFrog;
    private Frog tensFrog;


    /**
    * Default constructor for objects of class FrogCalculator
    */
    public FrogCalculator()
    {
    super();
    }


    /* instance methods */

    /**
    * Returns the receiver's operand1Frog
    */
    public Frog getOperand1Frog()
    {
    return operand1Frog;
    }

    /**
    * Sets the receiver's operand1Frog
    */
    public void setOperand1Frog(Frog operand1Frog)
    {
    this.operand1Frog = operand1Frog;
    }

    /**
    * Returns the receiver's operand2Frog
    */
    public Frog getOperand2Frog()
    {
    return operand2Frog;
    }

    /**
    * Sets the receiver's operand2Frog
    */
    public void setOperand2Frog(Frog operand2Frog)
    {
    this.operand2Frog = operand2Frog;
    }

    /**
    * Returns the receiver's unitsFrog
    */
    public Frog getUnitsFrog()
    {
    return unitsFrog;
    }

    /**
    * Sets the receiver's unitsFrog
    */
    public void setUnitsFrog(Frog unitsFrog)
    {
    this.unitsFrog = unitsFrog;
    }

    /**
    * Returns the receiver's tensFrog
    */
    public Frog getTensFrog()
    {
    return tensFrog;
    }

    /**
    * Sets the receiver's tensFrog
    */
    public void setTensFrog(Frog tensFrog)
    {
    this.tensFrog = tensFrog;
    }


    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    heres an example

    Java Code:
    public class Person{
    	//object attributes
    	String name = "";
    	int age = 0;
    	
    	//default constructor
    	public Person(){
    		this.name = "DefualtName";
    		this.age = 10;
    	}
    	
    	//constructor with parameters
    	public Person(String n, int a){
    		this.name = n;
    		this.age = a;
    	}
    }
    whenever you call new Person() without any parameters it will construct the default person with a name of DefaultName and age of 10; However if you call new Person(myName, myAge) you will construct a person object with a name of myName and an age of myAge(assuming myName and myAge have been properly declared as a String and int)
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey xcallmejudasx!

    Thanks for getting back to me so promptly. I understand the concept of your code example, but I am totally lost on trying to use this theory on my question. For example, in this case the object attributes of the frog class are: position and colour.I can understand how to set the position and colour for a frog constructor. But in my head this appears to go much further than the examples in my coursebooks - e.g this appears to be four different frogs, two with different colour requirements.

    I'm lost! I have been working on this for two days with no movement forward in my head.

    Could you show me an example using the parameters in my code? Just something to help the penny drop!!

    Thanks for the support and help, its greatly appreciated.

    All the best - FMJ.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    What crazy book is teaching you Frog arithmetic? Can you show the code for your Frog class?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  5. #5
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    I know! Its ridiculous :-) If you need further information please let me know! I'm sooo lost on this!

    Firstly the question information in greater detail:

    In this question, you will implement a simple Frog Calculator allowing basic integer arithmetic operations selected by the user – addition, subtraction and multiplication – to be performed
    on a pair of integers (between 0 and 9 inclusive), each of which is represented by the position of a frog.Two pairs of frogs will be used: The Frog Calculator will use the positions of the first pair of frogs to designate the two operands (numbers) in an arithmetic operation. These operands can only have single-digit
    values between 0 and 9 inclusive.The Frog Calculator will use the positions of the second pair of frogs, one of which is brown
    and the other yellow, to designate the result of a calculation. The Frog Calculator will use the position of the brown frog to represent any ‘tens’ in a result, and will use the position of the
    yellow frog to represent any ‘units’ in a result. Figure 1 represents multiplying 7 by 8. The result of 7 × 8 is 56, therefore the brown ‘tens’ frog is on stone 5 and the yellow ‘units’ frog is on stone 6.

    The frog class is shown below:

    public class Frog extends OUAnimatedObject
    {
    /* Instance variables */

    private OUColour colour;
    private int position;

    /**
    * Constructor for objects of class Frog which initialises colour to green
    * and position to 1.
    */
    public Frog()
    {
    super();
    this.colour = OUColour.GREEN;
    this.position = 1;
    }

    /* Instance methods */

    /**
    * Returns the position of the receiver.
    */
    public int getPosition()
    {
    return this.position;
    }

    /**
    * Sets the position of the receiver to the value of the argument aPosition.
    */
    public void setPosition (int aPosition)
    {
    this.position = aPosition;
    this.update("position");
    }

    /**
    * Sets the colour of the receiver to the argument's colour.
    */
    public void sameColourAs (Frog aFrog)
    {
    this.setColour(aFrog.getColour());
    }

    /**
    * Returns the colour of the receiver.
    */
    public OUColour getColour()
    {
    return this.colour;
    }

    /**
    * Sets the colour of the receiver to the value of the argument aColour.
    */
    public void setColour(OUColour aColour)
    {
    this.colour = aColour;
    this.update("colour");
    }

    /**
    * Sets the colour of the receiver to brown.
    */
    public void brown()
    {
    this.setColour(OUColour.BROWN);
    }

    /**
    * Sets the colour of the receiver to green.
    */
    public void green()
    {
    this.setColour(OUColour.GREEN);
    }


    /**
    * Causes user interface to emit a sound.
    */
    public void croak()
    {
    this.performAction("croak");
    }

    /**
    * Resets the receiver to its "home" position of 1.
    */
    public void home()
    {
    this.setPosition(1);
    }

    /**
    * Decrements the position of the receiver by 1.
    */
    public void left()
    {
    this.setPosition(this.getPosition()-1);
    }

    /**
    * Increments the position of the receiver by 1.
    */
    public void right()
    {
    this.setPosition(this.getPosition()+1);
    }

    /**
    * Causes a change in an appropriate observing user interface.
    * Icon representing the receiver performs a jump animation
    */
    public void jump()
    {
    this.performAction("jump");
    }

    /**
    * Returns a string representation of the receiver.
    */
    public String toString()
    {
    return "An instance of class "+ this.getClass().getName()
    + ": position " + this.getPosition()
    + ", colour " + this.getColour();
    }
    }

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I'm not exactly sure what you do not understand. You do indeed have four different frogs, each of which supposedly sits on a selected value. My question to you is this: are you having trouble creating the constructor? All you need to do is initialize the Frogs and use the methods provided by the Frog class to set the location/color of the Frogs. BTW, your declarations for the frogs can be
    Java Code:
    private Frog operand1Frog,
                     operand2Frog, 
                     unitsFrog, 
                     tensFrog;
    The super constructor call (super()) in the constructors for your classes is unnecessary: super() is automatically called in any constructor. super only needs to be used if parameters are required for the superconstructor.

    Please try to explain your problem in more detail. Hopefully I cleared some of this up for you.

    Good Luck,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey SB!

    Thanks for the information. I think I have managed to work the code out now.

    I will keep you posted.

    Once again, all help has been gratefully accepted!

    All the best - FMJ.

Similar Threads

  1. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 12:08 PM
  2. Need some help with a task, constructors.
    By JKM in forum New To Java
    Replies: 1
    Last Post: 02-01-2009, 10:09 PM
  3. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM
  4. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:28 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
  •