aspose file tools*
The moose likes Beginning Java and the fly likes Passing Values To A Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Values To A Constructor" Watch "Passing Values To A Constructor" New topic
Author

Passing Values To A Constructor

Brandon Golway
Greenhorn

Joined: Oct 19, 2010
Posts: 23
In my Java college class we have to combine our game classes that we created for our mid-term with other class members classes to make a full game. I successfully merged two out of the three characters/objects but I'm having problems with the third since it uses random values. The superclass Person contains all the shared functions between the people Farmer, Blacksmith, King, Queen and Soldier. The first two weren't written by me and have additional functions, King and Queen just contain a constructor that sets variables since there will only be one king and one queen. I set up Soldier to generate random values for some of the properties such as Height, Weight, Strength, and Age since you will usually have multiple soldiers. The problem that I'm having it that my partner set up his Person class using functions that I'm not familiar with such as super(), so I can't figure it out. Essentially what I need to do is figure out how to pass the values that are generated in the Soldier class to the Person class so that each time a Soldier gets created it's stats will be filled in by the randomly generated values.

tl;dr I need to know how to pass randomly generated values from Soldier() to Person() so that each time a soldier gets created its stats are filled in by the random values.

Soldier.java
Main.java
Person.java
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12

Essentially what I need to do is figure out how to pass the values that are generated in the Soldier class to the Person class


You have the wrong idea here. Since Soldier is extending Person it will inherit the variables from Person. You do not need to redefine these in Soldier...
Brandon Golway
Greenhorn

Joined: Oct 19, 2010
Posts: 23
Yea I was thinking about that, after looking at it some more I figured it was easier to not extend it and just copied the methods from person to the Soldier class. The way my partner did it doesn't really make sense to me since you'll most likely have more than one Blacksmith and one Farmer, and every time a new object is created from one of them all the values have to be defined by hand, where as mine are created and set automatically.
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12

I figured it was easier to not extend it and just copied the methods from person to the Soldier class


The downside of doing this is that you will end up duplicating a lot of your Person code inside Soldier. Like your attack and setRank methods. Keep the extends relationship between them. Just don't redefine the variables again in Soldier. Since they are already defined in Person and are not private, they are available to any class which extends Person (Soldier in this case).

I'm just getting impression you are not getting how classes can be extended. I will see if I can dig out a good tutorial for you or even just create an example myself. Sorry, don't have time at the moment
Brandon Golway
Greenhorn

Joined: Oct 19, 2010
Posts: 23
I do understand the basics of extending classes since I had a problem with this earlier and some of the members explained it to me, the problem that I had was I was redefining the values when all I needed was a constructor. How can I pass the random values that are created in Soldier to the superclass Person? I tried it a few different ways before and it wouldn't accept them.

I removed the variable declarations in Solder but these are the errors that I'm getting now
when I create the constructor in Soldier and use super() it tells me that the variables that are contained in super() Cannot be refered to an instance field height while explicitly invoking a constructor
when I comment out super() it tells me Implicit super constructor Person() is undefined. Must explicitly invoke another constructor
Brandon Golway
Greenhorn

Joined: Oct 19, 2010
Posts: 23
I found one way to do it by passing default values to the Person constructor from the Soldier constructor while also calling my functions in the Soldier class to override the default values. It's probably the wrong way to do it, but it does what I need lol
Imran Perwez
Greenhorn

Joined: Sep 19, 2010
Posts: 12

I found one way to do it by passing default values to the Person constructor from the Soldier constructor while also calling my functions in the Soldier class to override the default values.


Good stuff. Sounds fine
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Your problem here is that since your superclass (Person) does not explicitly contain a public no-argument constructor, none of it's subclasses may have one either. See, in Java, if you do not explicitly declare any constructors, the compiler will automatically insert a default no-argument constructor for you. However, once you declare a constructor, with or without arguments, the compiler no longer does this because it assumes that since you are providing an explicitly stated constructor, maybe you don't mean to let this object be instantiated with no arguments. Therefore, if you want to still have a no-arg constructor along with other constructors you will have to explicitly add it. What does this mean for subclasses? It means that as soon as you add even one constructor that takes arguments to a superclass, its subclasses no longer have a default (compiler inserted) no-arg constructor they can call. In your case, if you don't want to add an explicit no-arg constructor to the Person class, then I would suggest adding a factory method to your soldier class. Take these steps:

1) Override the constructor from Person in your Soldier class. In this just make a call to super(arg, arg, arg....) passing all the arguments...You're just delegating object construction to the superclass constructor here.

2) Add a method 'public static Soldier makeSoldier()' to your soldier class. In the makeSoldier method generate your random values and then call 'return new Soldier(yourRandomVal1, yourRandomVal2, etc....);

This will allow the best of both worlds...You can create a Soldier using explicit values by directly invoking its constructor or somebody who wants a random Soldier can just call:

Soldier randomGuy = Soldier.makeSoldier();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Values To A Constructor