Passing in a DAUNT Faction Type for Daunt Class

  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Class Type
AI Thread Summary
To pass a DAUNT faction type into the Daunt class, it is suggested to declare a Faction attribute within the Daunt class, initializing it with Faction.DAUNT. A getter method, public Faction getMyenum(), should be implemented to retrieve this value. The return type of the getter must be specified as Faction to avoid confusion. The discussion emphasizes the importance of understanding these programming concepts for better coding practices. Overall, the exchange highlights the connection between enums and class attributes in Java.
Robben
Messages
166
Reaction score
2

Homework Statement



Suppose I have an enum class named Faction and one of the constants is named DAUNT. I created a Daunt class of the enum but how can I pass in a DAUNT faction type in for Daunt?

Homework Equations



None

The Attempt at a Solution



Code:
public enum Faction {
   ALMIGHTY, AMBITION, DAUNT, RESTLESS, CAN;
}

public class Daunt {

public Daunt() {
}

}
 
Physics news on Phys.org
There is no connection between the DAUNT enum and Daunt class other than a similarity in the name

As an example, you could add a
Java:
Class Daunt {
    Faction myenum = Faction.DAUNT;
    ...
}
as an attribute in the Daunt class and a getter to retrieve it.
 
jedishrfu said:
There is no connection between the DAUNT enum and Daunt class other than a similarity in the name

As an example, you could add a
Java:
Class Daunt {
    Faction myenum = Faction.DAUNT;
    ...
}
as an attribute in the Daunt class and a getter to retrieve it.

I see so then in order to retrieve it I will do

Java:
public getMyenum() {
    return myenum;
}
 
jedishrfu said:
Yes that's one way.

What will the value of the return type of myenum be? Will it have no value? For example, I won't have to declare int or String into my getter, i.e. public int getMyenum()?
 
Actually, I figured it out. It will be Faction. Thank you very much! I should have never majored in math and should have majored in CS.
 
Nah math is cool too.

Always appreciate what you learn because it will help you learn more.
 
  • Like
Likes Robben
@Robben, you need to include the return type (Faction) in your definition of getMyenum.

Java:
public Faction getMyenum() {
    return myenum;
}
 
Mark44 said:
@Robben, you need to include the return type (Faction) in your definition of getMyenum.

Java:
public Faction getMyenum() {
    return myenum;
}

Understood. Thank you!
 
Back
Top