Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with program...

 
Andrew Hartman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can someone help me out here please - I'm working through an example in a book, and have the following game board playing problem with no solution.
I need to create a class called 'Counter', with attributes to represent its colour (intialised as white) and the number square it is on (intialised as 0).
It needs methods to set and return its colour.
Also it should have a method to move to a given postion on the board using a integer parameter and another method to returns its current position.
Any help with this would be great. I've had a few goes at it, but to be honest I don't think I'm doing much right
Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12184
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post what code you've got so far (and please, use the code tags - it will preserve the formatting). This doesn't sound too bad, but people here are generally more willing to help you fix what you've done than to write it for you. Plus, you'll learn more!!!
(as sure as i say that, somebody will post the complete solution!!! )
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure you get help here. Just post some code, ask a question.
 
Andrew Hartman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sorry should of realised posting code would be more useful. Here's what I've got so far:

So now my problems are intialising these variables:
colour --> white
sqaure_number --> 0
and,
Also it should have a method to move to a given postion on the board using an integer parameter and another method to returns its current position.
Maybe, public void move()
I'm not really sure.
Thanks for your help guys
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So now my problems are intialising these variables:
colour --> white
sqaure_number --> 0

You can write these just behind the field declarations, like
int a = 0;
The initialisation with white doesn't seem to make much sense, though - it will allways be replaced in the constructor.

Maybe, public void move()

Yeah, that's a starting point. Another possible name would be moveTo(). Beside the naming, there has to be the parameter inside of the paranthese. And a methodbody wich assigns the value of the parameter to square_number, but you knew that.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12184
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will allways be replaced in the constructor.

As it's written, yes, but you could create a couple more constructors... one that takes no parameters, one that takes only an int, and one that takes both. at the very least, i'd create the constructer that takes no arguments:

then, if you get the declarations right (per Tobias' suggestion), you could just call

and you'd have your color of white and position of 0.
let us know when you get this, and we'll move on to your next question!!!
 
Andrew Hartman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, think I'm getting a better hang of this. Got this so far:

but I'm still unsure where to intialise my variables? Do i intialise in the constructor or at the top? Am I structuring my code logically?
Many Thanks,
Andy
 
fred rosenberger
lowercase baba
Bartender
Posts: 12184
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can intialize your variables in either place. i'd probably initialize them where you declare them:
private String colour = "White";
private int square_number = 0;
You currently have a constructor that takes a string for the color. So, as it sits, the ONLY way to make your object it to tell it what the color should be. That kind of defeats the purpose of having a default. You need to create the constructor that takes no arguments (ALWAYS reccomended when you write any constructors yourself). Then you'll be able to create an object with your default values. And, you could put the initialization in there...
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I thought it should move to a given point?

but I'm still unsure where to intialise my variables? Do i intialise in the constructor or at the top? Am I structuring my code logically?

In Java it is usualy done "at the top" (filde declaration), while in C++ you have to do it in the constructor.
The structure can be improve by grouping the two get methods together; get and set methods are usualy at the end of a class.
 
Andrew Hartman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just quickly whipped up this, and my problems I think relate to what you've been saying:

Error:
TestCounter.java:5: Counter(java.lang.String) in Counter cannot be applied to ()
Counter mycounter = new Counter();
^ pointing to new
How can I first look at the default value? Then presumably I can change the colour using the constructor.
you could create a couple more constructors... one that takes no parameters, one that takes only an int, and one that takes both. at the very least, i'd create the constructer that takes no arguments:

How can I create one that takes both?
Thanks for your help
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I create one that takes both?
You can't create one constructor that takes a variable number of arguments, if that's what you're asking. (Well, to be honest, you can in Java 1.5).
You perhaps want to just add a new constructor to your Counter class - one that doesn't take any arguments. Or, you may prefer to use the constructor that is already available - the one that takes a String argument.
I'm not sure if this helps clarify things for you. How's it going?
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can I first look at the default value? Then presumably I can change the colour using the constructor.

You can't. To change the color, write a setColor() method.

How can I create one that takes both?

Give it two parameters, seperate them with a comma:
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[i]You can't create one constructor that takes a variable number of arguments, if that's what you're asking. (Well, to be honest, you can in Java 1.5).[/]
That's new to me, how does it work? Like printf() in C?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12184
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have as many constructors in a class as you want, as long as they all have a different signiature (or, in English, a different list of input parameters). So, i could have

note that i have 3 constructors, all that take different parameters.
[ April 06, 2004: Message edited by: fred rosenberger ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12184
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from here...

Varargs
The varargs functionality allows multiple arguments to be passed as parameters to methods. It requires the simple ... notation for the method that accepts the argument list and is used to implement the flexible number of arguments required for printf.
void argtest(Object ... args) {
for (int i=0;i <args.length; i++) {
}
}

argtest("test", "data");

 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks fred!
 
Andrew Hartman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Through a lil experimental work, I've managed to sort things out and learned a few neat principles along the way!
Just want to say a big thankyou for helping me out, I love this place!
Being a complete beginner to programming I would normally feel a bit intimidated asking questions, with so many experts on here.
But the cool thing about this place is, your all real friendly and happy to help with problems which at sometimes must seem really trivial.
Anyways, thanks again... this will definitely be my 1st point of call for expert help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic