Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Can someone help me assign these values to the variables on the constructor of my game logic?please?

i've been trying so hard this past week to build my first java project but i cant assign values to my constructor variables in my game logic class, I have 3 classes, the main one, game logic, and prompter, i have understood that with the prompter i ask for values but i don't know how to assign them to my jar logic constructor variables

public class JarLogic{
  private int mAnswer;
  private int mMisses;
  private String mItem;
  public JarLogic(){
  mAnswer=answer; //the actual amount of items in the jar
  mMisses=0; //this is where i want to store the amount of missed answers
  mItem = ""; /*this is the name of the item that will be stored in the jar( which i don't know how to assign,
same with the above ones)*/
}
public String getItemType(){
return mItem;
}
//as you can see i cannot use getItemType() because i dont know how to pass the value from the prompter to the JarLogic constructor
}
public class Prompter{
public Prompter(){
  private JarLogic mJarLogic;
  public Prompter(JarLogic jarLogic){
  }
  Scanner scanInt= new Scanner(System.in);
  Scanner scanString= new Scanner(System.in);

  public int AskMaxAmount(){
  System.out.printf("please enter the maximum amount of %s that are going to be stored in the jar ",mGame.getItemType);
   int amount =scanInt.nextInt();
  return amount;
}

  public String AskItemType(){
  System.out.println("please enter what is the jar is going  to be filled with:  ");
String item=scanString.nextLine();  
return item;
  }

}

please help me, i know it is incomplete but i just wanted to show you guys where i got stuck, sorry for my english please help! in conclusion, how do i store a value in the prompter class, and pass it down to the JarLogic class to store it in the constructor?

.

1 Answer

Hi Lucas,

I've had a quick look at your JarLogic class. I can see that you declare three member variables and that you have a constructer and getter defined. In order for your constructer to set the member variables within the class they need to first be passed into the class when it is instantiated.

I can't see how you are calling it in the code you've supplied, only that you pass the jarLogic object into the constructor of the Prompter class.

In order for the member variables within the JarLogic constructer to be set, you have to first pass them into the object when it is being created. This would likely look something like this in your main class file:

JarLogic jarLogic = new JarLogic("answer", "item");

Then you would need to modify your JarLogic.class file to look something like this:

public class JarLogic {
    private int mAnswer;
    private int mMisses;
    private String mItem;

    public JarLogic(String answer, String item) {
        mAnswer = answer;
        mMisses = 0;
        mItem = item; 
    }

    public String getItemType(){
        return mItem;
    }
}

You can see the strings being passed into the constructer, then being assigned to your declared member variables.

Without seeing all of your code I can't be completely sure in my response, but I hope this helps.