Adventure game constructor method problems Java - java

I'm creating a text adventure game in Java. I've created an exit, room, creature and an item class, and now I'm instantiating and putting values for them in my main class.
I'm getting this problem though: as I'm creating exits for my room, I'm getting a compiler error saying
Constructor Exit in class Exit cannot be applied to given types
This is the line of code I'm getting the compiler error for:
Exit exit1 = new Exit("Exit1", "Exit description", "Exit transition text");
and in my Exit class I have this constructor method:
public void Exit(String exit, String exitDescription, String exitTransition) {
this.Exit(exit, exitDescription, exitTransition);
}
but every time I tell the IDE (Netbeans) to correct the compiler error, it generates this in my Exit class:
Exit(String exit1, String exit_description, String exit_transition_text) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
So my question is, why doesn't my constructor method work? Also, I had help from my professor and I believe he said that I don't want the generated code. Don't remember exactly though.

This is not a constructor:
public void Exit(String exit, String exitDescription, String exitTransition) {
constructors have no return type.
This is a constructor:
public Exit(String exit, String exitDescription, String exitTransition) {
Of course you know that even if this were a valid constructor the code in its body doesn't make much sense as it appears you're trying to have the constructor call itself recursively:
// void removed
public Exit(String exit, String exitDescription, String exitTransition) {
this.Exit(exit, exitDescription, exitTransition); // this calls itself!
}

Related

Eclipse won't recognize constructor

So I have a class with the following constructors:
public TaxiModule(TaxiData taxiData)
{
this(taxiData, VehicleUtils.getDefaultVehicleType());
}
public TaxiModule(TaxiData taxiData, VehicleType vehicleType)
{
this.taxiData = taxiData;
this.vehicleType = vehicleType;
}
When I try to call:
new TaxiModule(taxiData)
Eclipse gives me an error, saying: "The constructor is undefined." When I click on the little light-bulb thingy for the quick fix, it suggests:
Add argument to match constructor TaxiModule(TaxiData, TaxiConfigGroup).
Add argument to match constructor TaxiModule(TaxiData, TaxiConfigGroup, VehicleType).
Clearly from the constructor details my call is valid and the second suggestion does not fit.
Any help would be great.
Thanks!

Passing values from jinternalframe1 to jinternalframe2

I've been reading a lot about constructors in Java as well as searching here in stackoverflow for related questions but I'm still confused on how my program will get a string value from my jinternalframe1 to jinternalframe2.
I have a jinternalframe which calls jinternalframe1. Here's my code.
ForgotPassword fp = new ForgotPassword();
JDesktopPane MainDesk = this.getDesktopPane();
MainDesk.add(fp);
this.dispose();
fp.show();
And here's my jinternalframe1..
public class ForgotPassword extends javax.swing.JInternalFrame {
public ForgotPassword(String acType, String uName) {
initComponents();
acType = AccountType.getSelectedItem() + "";
uName = username.getText();
}
AccountType variable is a jcombobox with three options: Administrator, LevelOne, LevelTwo.
username variable is a jTextField. I also have a jbutton called Next which calls jinternalframe2.
User will need to click Next button, and will check if username exist in the database. (I have already figured out this part) And then hides jinternalframe1 and calls jinternalframe2 if username exists in the database.
Now I am confused with this part.. jinternalframe2. I would like the Account Type and username value from jinternalframe1 to jinternalframe2.. I am trying this out but got no luck..
public class ForgotPassword2 extends ForgotPassword {
public ForgotPassword2(String acType, String uName) {
initComponents();
AccountType.getText() = acType;
username.getText() = uName;
}
You'll notice that the variable AccountType here in jinternalframe2 is a jTextField.
Both AccountType and username jTextField here in jinternalframe2 is not editable (disabled).
Error occurs on this lines:
ForgotPassword fp = new ForgotPassword();
public ForgotPassword2(String acType, String uName)
Error message on both lines
constructor ForgotPassword in class ForgotPassword cannot be applied
to given types; required: String,String found: no arguments
reason: actual and formal argument lists differ in length
Can someone enlighten me on how to use constructors right on my program? I am using netbeans by the way. Thank you in advance!
This has little to do with constructors and more to do with passing information between objects of different classes. For one you don't mis-use of inheritance for this purpose as you appear to be doing. Instead you use composition -- the class that needs information from another class needs a valid reference to the active object of the other class. Then the first class can call methods on the other one.
I think that for your purposes, you will likely be better off getting the user's information in a modal fashion using an internal option pane such as a JOptionPane.showInternalConfirmDialog(...). Whenever you open a modal dialog, the calling code halts at the point where you display the modal dialog. The calling code will then resume once the modal dialog is no longer visible, and at that point you can query the JPanel class that is displayed in your option pane for the data that it holds.
Note as an aside: if you are asking question about code, and you state that your code has an "error", you will want to post the full error message for all to see.
Also, this isn't valid Java:
AccountType.getText() = acType;
as you can't have a method call on the left side of an assignment statement.
What error occurs on those lines?
This is not a valid statement:
public ForgotPassword2(String acType, String uName)
It's not very clear what is your intended designe, but from what you posted I guess you need to create a new instance of ForgotPassword2:
public class ForgotPassword extends javax.swing.JInternalFrame {
String acType;
String uName;
public ForgotPassword(String acType, String uName) {
this.acType = acType;
this.uName = uName;
}
public void next(){
...
ForgotPassword2 fp2 = new ForgotPassword2(this.acType, this.uName);
...
}
}
Also this statement is very suspicious:
AccountType.getText() = acType;
This statement doesn't replace the reference to the String in the AccountType. You will need to call a setter.

How to call an Object's Method from another class without creating a sub-class and/or inheriting class?

I've been learning basic code at a very beginner level. Now I'm finally starting to dabble in actually writing simple programs, and got really stuck.
I'm writing a simple a program that consists of two classes; People,
MainPage.
Once the program runs, the method openApp() is called in main method from (MainPage Class).
public static void main(String[] args) {
openApp();
}
Next, when the openApp() is called, the user has three menus to choose to go to that are selected by entering the corresponding number
i.e. 1 = Newsfeed, 2 = Profile or 3 = Friends.
public class MainPage {
public static void openApp() {
System.out.println("Welcome to App!");
System.out.println();
System.out.println("To Select Option for:");
System.out.println("Newsfeed : 1");
System.out.println("Profile : 2");
System.out.println("Friends : 3");
System.out.println("Enter corresponding number: ");
int optionSelected = input.nextInt();
switch (optionSelected) {
case 1: System.out.println("NewsFeed");
break;
case 2: System.out.println("Profile");
break;
case 3: System.out.println("Friends");
break;
if (optionSelected == 3) {
people.friend();// Is it possible to write: friend() from "People" Class without extending to People Class
}
}
}
If User selects "friends" then the program calls a method from
People Class called friend(People name) in MainPage class that prints out people object's friends.
My Attempt:
if (optionSelected == 3) {
people.friend();
}
The Error I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
people cannot be resolved
Problem is I don't want to extend People Class in MainPage and inherit all it's methods, yet I still want to call an Object method from People Class to print people object's friends.
Note: just in case anyone would want to look at the friend(People people) method that is located in the People class:
public void friend(People people) {
System.out.println(people.friend);
Excellent question format.
You can declare an Object of type People, and use that.
Example
public class MainPage
{
People people = new People();
// .. Some code.
if(optionSelected == 3) {
people.friend();
}
}
Explanation
Your friend method is an instance method. This means that in order to access it, you need to have an instance of the object created. This is done with the new keyword. Secondly, unless People is some form of utility class, then your friend method should probably read more like:
public void friend()
{
System.out.println(this.friend);
}
And for the sake of good code design, remember that your MainPage class is outputting to the user, so you should return the value rather than print it. Secondly, you should conform to good naming standards, and in Java we use the get prefix when getting a class member.
public void getFriend()
{
return this.friend;
}
and in the MainPage class, you should print this.
if(optionSelected == 3)
{
System.out.println(people.getFriend());
}

Trying to make a parent class

I have 2 classes, cPuzzlePieces and cShapes. cPuzzlePieces extends cShapes.
Right now I get 2 errors on cPuzzlePeaces.
The first error is on the first line of the def and says:
implacet super constructor cShapes is undefined for default constructor.
The second error on the first line of the construter says
constructor call must be the first staemnt
and it is on the first staement.
Here is my code:
public class cPuzzlePieces extends cShapes{ // first error message is here
int mAmountOfShapes;
Context InContext;
void cPuzzlePieces(Context MyContext) throws IOException
{
super( MyContext); // SECOND ERROR MESSAGE IS HERE
InContext=MyContext;
}
}
public class cShapes
{
cShape[] MyShapes;
public int mAmountOfShapes=0;
boolean AnimationRunning=false;
cShapes(Context InContext) throws IOException
{
}
...
...
}
This
void cPuzzlePieces(Context MyContext) throws IOException
is a method, not a constructor.
Remove the void keyword. Add an appropriate access modifier (if need be). Also check for the IOException. Currently, nothing is throwing it.
Related
"Constructor call must be the first statement in a constructor" issue in Java
Java naming conventions state that class names should start with an uppercase alphanumeric character. Please follow that.

Constructor being called again?

I have this constructor;
public UmlDiagramEntity(ReportElement reportElement, int pageIndex, Controller controller) {
super(reportElement.getX1(), reportElement.getY1(), reportElement.getX2(), reportElement.getY2());
setLayout(null);
this.pageIndex = pageIndex;
this.controller = controller;
reportElements = reportElement.getInternalReportElements();
components = new ArrayList<AbstractEntity>();
changedComponentIndex = -1;
PageListener p = new PageListener();
this.addMouseMotionListener(p);
this.addMouseListener(p);
setPage();
}
And I have an update method in the same class;
#Override
public void update(ReportElement reportElement) {
if (changedComponentIndex == -1) {
super.update(reportElement);
} else {
reportElements = reportElement.getInternalReportElements();
if (components.size() == reportElements.size()) {
if (!isCommitted) {
if (reportElement.getType() == ReportElementType.UmlRelation) {
if (checkInvolvementAndSet(changedComponentIndex)) {
anchorEntity(changedComponentIndex);
} else {
resistChanges(changedComponentIndex);
}
return;
}
}
..................goes on
When I follow the flow from the debugger, I see that when update is called, somewhere in the method, the program goes into the constructor and executes it all over again (super, pageIndex, etc.). Why does it go to the constructor :D I didn't tell it to go there.
I can make a deeper analysis and see where it goes to the constructor if you want. By the way, changedComponentIndex is a static variable.
I would find it far more probable that you are seeing it construct two different objects. You'd have to provide more information like a stack trace; here you haven't even shown the constructor being invoked!
The behaviour you describe is pretty much impossible. Either your code is different from what you've shown or you're not debugging the code you think you're debugging. Without complete code that we can run, that's all we can say.
Are you sure that update is not called indirectly from within the constructor, which would result in a breakpoint in update getting triggered.
Try setting a breakpoint at the start of the constructor and at the end, then one in update. When you hit the first constructor breakpoint, hit 'continue' and see which breakpoint gets triggered next.
Is this multi-threaded? Is it possible that the constructor for a different instance created on another thread is being called?

Categories