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

Help with program...

Andrew Hartman
Greenhorn

Joined: Mar 18, 2004
Posts: 16
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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!!! )


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
I'm sure you get help here. Just post some code, ask a question.
Andrew Hartman
Greenhorn

Joined: Mar 18, 2004
Posts: 16
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

Joined: Apr 06, 2004
Posts: 55

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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Mar 18, 2004
Posts: 16
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Apr 06, 2004
Posts: 55

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

Joined: Mar 18, 2004
Posts: 16
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

Joined: Dec 10, 2001
Posts: 7023
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55

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

Joined: Apr 06, 2004
Posts: 55
[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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Apr 06, 2004
Posts: 55
Thanks fred!
Andrew Hartman
Greenhorn

Joined: Mar 18, 2004
Posts: 16
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with program...